John Jerrby
John Jerrby

Reputation: 1703

using constants or enum for primitive types

I wonder what the preferred way to use if I want to ask for primitive type name . Do i need to create an enum class or use already existing one (i was search this without success)

What is your suggestion in this case ?

Here is my code :

     else if (typeName.equals("char")) {
                return new SwitchInputType<Character>(new Character('z'));
            } else if (typeName.equals("decimal")
                    || (typeName.equals("java.math.BigDecimal"))) { 
...

Upvotes: 0

Views: 2266

Answers (2)

Vitaly
Vitaly

Reputation: 2662

I do not like code with many if-else. So, here is my solution of the same problem.

Enum for types:

public enum Types {
    BYTE,
    BOOLEAN,
    SHORT,
    CHAR,
    INT,
    FLOAT,
    LONG,
    DOUBLE,
    OBJECT;

    private static final String ALL_TYPES_STRING = Arrays.toString(Types.values());

    public static Types getType(Class<?> clazz) {
        String className = clazz.getSimpleName().toUpperCase();
        if (ALL_TYPES_STRING.contains(className)) {
            return Types.valueOf(className);
        } else {
            return Types.OBJECT;
        }
    }
}

Method of ReflectionHelper:

 public static void setFieldValue(Object object,
                                     String fieldName,
                                     String value) {
        try {
            Field field = object.getClass().getDeclaredField(fieldName);
            field.setAccessible(true);

            Types types = Types.getType(field.getType());
            switch (types) {
                case BYTE:
                    field.set(object, Byte.valueOf(value));
                    break;
                case BOOLEAN:
                    field.set(object, Boolean.valueOf(value));
                    break;
                case SHORT:
                    field.set(object, Short.valueOf(value));
                    break;
                case CHAR:
                    field.set(object, value.charAt(0));
                    break;
                case INT:
                    field.set(object, Integer.decode(value));
                    break;
                case FLOAT:
                    field.set(object, Float.valueOf(value));
                    break;
                case LONG:
                    field.set(object, Long.valueOf(value));
                    break;
                case DOUBLE:
                    field.set(object, Double.valueOf(value));
                    break;
                case OBJECT:
                default:
                    field.set(object, value);
            }

            field.setAccessible(false);
        } catch (SecurityException | NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

Upvotes: 0

Iswanto San
Iswanto San

Reputation: 18569

You can cast the variable to Object and then get the Class for that object like this :

else if (((Object) typeName).getClass() == Character.class) {  
          return new SwitchInputType<Character>(new Character('z'));
}   
else if (typeName != null && ((Object) typeName).getClass() == BigDecimal.class) { 

}

Upvotes: 1

Related Questions