POJO instantiator Utility

Just a random post for everyone.

If you don't know what Plain Old Java Object (POJO) is, take a look at Wikipedia's explanation thereof.

I have written a simple POJO instantiator. This instantiator is not fully perfect but it does what it was designed thus far.
My criteria I have choose of what constitutes a POJO is simple:
  1. A class must have a no constructor at all, or a public zero-argument constructor.
  2. The instance variables must have a respective getter and setter method (of which, both must be public).
What this utility class doesn't do is the following:
  1. It never instantiates a final instance or a static final instance.
  2. serialVersionUID is ignored.
  3. Array instance variables are not instantiated.
  4. Any instance variable which has the same type as the class it is declared is not instantiated. The reason is that the utility class will go into an infinite recursive loop and a StackOverflowError is thrown.
OK, enough of the pleasantries, let's get to the code, shall we? And here it is.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
 
import org.apache.log4j.Logger;
 
/**
 * @author The Elite Gentleman
 * @since 17 August 2011
 *
 */
public class PojoInstantiatorUtil {
     
    private static final Logger logger = Logger.getLogger(PojoInstantiatorUtil.class);
    private static List<String> ignoreFieldList = new ArrayList<String>();
     
    static {
        ignoreFieldList.add("serialVersionUID");
    }
 
    /**
     * Hidden, for a good cause.
     */
    private PojoInstantiatorUtil() {
        // TODO: NOOP
    }
     
    /**
     * Creates a class and instantiate all properties that needs instantiating.
     * Class must have a default/public zero-based constructor.
     *
     * @param clazz
     * @return
     * @throws Exception
     */
    public static <T> T newInstance(Class<T> clazz) throws Exception {
         
        //Cannot instantiate interfaces and abstract classes.
        if (Modifier.isAbstract(clazz.getModifiers()) || clazz.isInterface()) {
            String message = "Cannot create a new instance of class '" + clazz.getName() +"'. Class is either abstract or interface.";
            logger.error(message);
            throw new Exception(message);
        }
         
        T object = clazz.newInstance();
        instantiateInstances(object);
        return object;
         
    }
 
    public static <T> void instantiateInstances(T object) throws Exception {
         //We use reflection to see if we can instantiate this.
         if (object == null) {
             throw new NullPointerException("object is null.");
         }
         
         Class<?> objectClass = object.getClass();
         logger.info("instantiateInstances(" + objectClass.getName() + ")");
                  
         while (Object.class != objectClass && !isClassTypeJavaImplemented(objectClass)) {
         
             Field[] fields = objectClass.getDeclaredFields();
             if (fields != null && fields.length > 0) {
                 for (Field field : fields) {
                     
                     if (ignoreFieldList.contains(field.getName())) {
                         //We can safely ignore it and move on...
                         logger.info(objectClass.getName() + ": Field name '" + field.getName() + "' is on our ignore list. Ignoring...");
                         continue;
                     }
                     
                     Class<?> fieldType = field.getType();
                     if (isTypesIdentical(objectClass, fieldType)) {
                         logger.warn("WARNING: This class '" + objectClass.getName() + "' and instance variable '" + field.getName() + "' of class '" + fieldType.getName() + "' are of the same class name. Redundancy will occur, therefore skipping instantiation.");
                         continue;
                     }
                     
                    //Final fields will be overlooked
                     if (Modifier.isFinal(fieldType.getModifiers()) || Modifier.isStatic(fieldType.getModifiers())) {
                         Object v = field.get(object);
                         if (v != null) {
                             logger.info(objectClass.getName() + ": Field '" + field.getName() + "' is a final. Overlooking...");
                         }
                     }
                     
                     if (isTypeInterface(fieldType)) {
                         logger.info(objectClass.getName() + ": Field '" + field.getName() + "' of type '" + fieldType.getName() + "' is an interface. Nulling it is safer.");
                         continue;
                     }
                     
                     //Get it's declared method.
                     String fieldName = field.getName();
                     Method declaredMethod = objectClass.getMethod("set" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1), fieldType);
                     Object value = null;
                     
                     if (!fieldType.isArray()) {
                         if (String.class == fieldType) {
                             value = "";
                         } else if (isTypePrimitive(fieldType)){
                            value = defaultPrimitiveValue(fieldType);
                         } else if (isTypeBoxable(fieldType)) {
                             value = defaultBoxableValue(fieldType);
                         } else {
                             try {
                                 logger.info("Going to instantiate field '" + field.getName() + " of type '" + fieldType.getName() + "'.");
                                value = newInstance(fieldType);
                            } catch (Exception e) {
                                // TODO Auto-generated catch block
                                logger.info(objectClass.getName() + ": Field '" + field.getName() + "' of type '" + fieldType.getName() + "' threw the following exception. We're, therefore, nulling the field.", e);
                                value = null;
                            }
                         }
                     }
                     
                     if (value != null) {
                         declaredMethod.invoke(object, value);
                     }
                 }
             }
         
             //Instantiate our parents....
             objectClass = objectClass.getSuperclass();
         }   
    }
     
    private static boolean isClassTypeJavaImplemented(Class<?> clazz) {
        //Ignore Sun's implementations
        return (clazz.getName().startsWith("java.") || clazz.getName().startsWith("javax.") || clazz.getName().startsWith("sun."));
    }
     
    /**
     * Check if the provided class type (or component type, if array) is an interface.
     *
     * @param type
     * @return
     */
    private static boolean isTypeInterface(Class<?> type) {
        Class<?> clazz = type;
        if (clazz.isArray()) {
            clazz = getArrayComponentType(type);
        }
         
        return clazz.isInterface();
    }
     
    /**
     * Determines whether parent class and any of its instance variable are of the same type (including arrays).
     * To prevent {@link StackOverflowError} from occurring.
     *
     * @param parent
     * @param other
     * @return
     */
    private static boolean isTypesIdentical(Class<?> parent, Class<?> other) {
        if (parent == other) {
            return true;
        }
         
        if (other.isArray() && !parent.isArray()) {
            Class<?> componentType = getArrayComponentType(other);
            return (parent == componentType);
        }
         
        if (parent.isArray() && !other.isArray()) {
            Class<?> componentType = getArrayComponentType(parent);
            return (other == componentType);
        }
         
        return false;
    }
 
    /**
     * Determines whether the type specified is a primitive type.
     *
     * @param type
     * @return true, if it is, false otherwise.
     */
    private static boolean isTypePrimitive(Class<?> type) {
        return (char.class == type || byte.class == type || int.class == type || short.class == type
                || long.class == type || float.class == type
                || double.class == type || boolean.class == type);
    }
 
    /**
     * Determine whether the type is boxable (using autoboxing/unboxing).
     *
     * @param type
     * @return
     */
    private static boolean isTypeBoxable(Class<?> type) {
        return (Character.class == type
                || Byte.class == type || Integer.class == type || Short.class == type
                || Long.class == type || Float.class == type
                || Double.class == type || Boolean.class == type);
    }
     
    /**
     * Returns a default value based on it's boxable type;
     * @param type
     * @return
     */
    private static Object defaultBoxableValue(Class<?> type) {
        if (Byte.class == type) {
            return Byte.valueOf((byte)0);
        }
         
        if (Character.class == type) {
            return Character.valueOf((char) 0);
        }
         
        if (Integer.class == type) {
            return Integer.valueOf(0);
        }
         
        if (Short.class == type) {
            return Short.valueOf((short)0);
        }
         
        if (Long.class == type) {
            return Long.valueOf(0);
        }
         
        if (Float.class == type) {
            return Float.valueOf(0f);
        }
         
        if (Double.class == type) {
            return Double.valueOf(0d);
        }
         
        if (Boolean.class == type) {
            return Boolean.FALSE;
        }
         
        return null;
    }
     
    /**
     * Returns a default value based on it's boxable type;
     * @param type
     * @return
     */
    private static Object defaultPrimitiveValue(Class<?> type) {
        if (byte.class == type) {
            return (byte)0;
        }
         
        if (char.class == type) {
            return (char) 0;
        }
         
        if (short.class == type) {
            return (short)0;
        }
         
        if (long.class == type) {
            return (long)0;
        }
         
        if (float.class == type) {
            return 0f;
        }
         
        if (double.class == type) {
            return 0d;
        }
         
        if (boolean.class == type) {
            return false;
        }
         
        //We assume it's an int
        return 0;
    }
     
    /**
     * <b>For arrays only:</b>: This method returns the dimension length of an array.
     * @param arrayClass
     * @return
     */
    private static int getArrayDimensionLength(Class<?> arrayClass) {
        if (!arrayClass.isArray()) {
            throw new IllegalArgumentException("Class '" + arrayClass.getName() + "' is not an array.");
        }
         
        int count = 0;
        Class<?> componentType = arrayClass;
        while (componentType.isArray()) {
            componentType = arrayClass.getComponentType();
            count++;
        }
         
        return count;
    }
     
    /**
     * <b>For arrays only:</b> This method returns the component type of the array.
     * @param arrayClass
     * @return
     */
    private static Class<?> getArrayComponentType(Class<?> arrayClass) {
        if (!arrayClass.isArray()) {
            throw new IllegalArgumentException("Class '" + arrayClass.getName() + "' is not an array.");
        }
         
        Class<?> componentType = arrayClass;
        while (arrayClass.isArray()) {
            componentType = arrayClass.getComponentType();
        }
         
        return componentType;
    }
}


If you have found any issues or have further things to see from this utility, please feel free to comment below or give a shout out! :-)


Happy coding! :-)

Comments