33import java .lang .annotation .Annotation ;
44import java .lang .reflect .Field ;
55import java .lang .reflect .Method ;
6+ import java .util .Collection ;
67import java .util .Map ;
78
89import org .apache .commons .lang3 .ClassUtils ;
10+ import org .slf4j .Logger ;
11+ import org .slf4j .LoggerFactory ;
912
1013/**
1114 * Provides type operations, mainly checks and casts for Java Primitives, to be used in the templates
1215 *
1316 */
14- public class JavaUtil extends CommonUtil {
17+ public class JavaUtil {
18+
19+ /**
20+ * Logger for this class
21+ */
22+ private static final Logger LOG = LoggerFactory .getLogger (JavaUtil .class );
1523
1624 /**
1725 * The constructor.
@@ -54,7 +62,7 @@ public String boxJavaPrimitives(Class<?> pojoClass, String fieldName) throws NoS
5462
5563 if (pojoClass == null ) {
5664 throw new IllegalAccessError (
57- "Class object is null. Cannot generate template as it might obviously depend on reflection." );
65+ "Class object is null. Cannot generate template as it might obviously depend on reflection." );
5866 }
5967
6068 if (equalsJavaPrimitive (pojoClass , fieldName )) {
@@ -114,7 +122,7 @@ public boolean equalsJavaPrimitiveOrWrapper(String simpleType) {
114122 * @throws SecurityException if the field cannot be accessed.
115123 */
116124 public boolean equalsJavaPrimitive (Class <?> pojoClass , String fieldName )
117- throws NoSuchFieldException , SecurityException {
125+ throws NoSuchFieldException , SecurityException {
118126
119127 if (pojoClass == null ) {
120128 return false ;
@@ -161,10 +169,10 @@ public boolean equalsJavaPrimitiveIncludingArrays(String simpleType) {
161169 * @throws SecurityException if the field cannot be accessed.
162170 */
163171 public boolean equalsJavaPrimitiveIncludingArrays (Class <?> pojoClass , String fieldName )
164- throws NoSuchFieldException , SecurityException {
172+ throws NoSuchFieldException , SecurityException {
165173
166174 return equalsJavaPrimitive (pojoClass , fieldName ) || (pojoClass .getDeclaredField (fieldName ).getType ().isArray ()
167- && pojoClass .getDeclaredField (fieldName ).getType ().getComponentType ().isPrimitive ());
175+ && pojoClass .getDeclaredField (fieldName ).getType ().getComponentType ().isPrimitive ());
168176 }
169177
170178 /**
@@ -198,7 +206,7 @@ public String castJavaPrimitives(String simpleType, String varName) throws Class
198206 * @throws SecurityException if the field cannot be accessed.
199207 */
200208 public String castJavaPrimitives (Class <?> pojoClass , String fieldName )
201- throws NoSuchFieldException , SecurityException {
209+ throws NoSuchFieldException , SecurityException {
202210
203211 if (equalsJavaPrimitive (pojoClass , fieldName )) {
204212 return String .format ("((%1$s)%2$s)" , boxJavaPrimitives (pojoClass , fieldName ), fieldName );
@@ -207,6 +215,31 @@ public String castJavaPrimitives(Class<?> pojoClass, String fieldName)
207215 }
208216 }
209217
218+ /**
219+ * @param pojoClass {@link Class} the class object of the pojo
220+ * @param fieldName {@link String} the name of the field
221+ * @return true if the field is an instance of java.utils.Collections
222+ * @throws NoSuchFieldException indicating something awefully wrong in the used model
223+ * @throws SecurityException if the field cannot be accessed.
224+ */
225+ public boolean isCollection (Class <?> pojoClass , String fieldName ) throws NoSuchFieldException , SecurityException {
226+
227+ if (pojoClass == null ) {
228+ return false ;
229+ }
230+
231+ Field field = pojoClass .getDeclaredField (fieldName );
232+ if (field == null ) {
233+ field = pojoClass .getField (fieldName );
234+ }
235+ if (field == null ) {
236+ return false ;
237+ } else {
238+ return Collection .class .isAssignableFrom (field .getType ());
239+ }
240+
241+ }
242+
210243 /**
211244 * Returns the Ext Type to a given java type
212245 *
@@ -301,7 +334,7 @@ public String getReturnType(Class<?> pojoClass, String methodName) throws NoSuch
301334
302335 if (pojoClass == null ) {
303336 throw new IllegalAccessError (
304- "Class object is null. Cannot generate template as it might obviously depend on reflection." );
337+ "Class object is null. Cannot generate template as it might obviously depend on reflection." );
305338 }
306339 String s = "-" ;
307340 Method method = findMethod (pojoClass , methodName );
@@ -324,11 +357,11 @@ public String getReturnType(Class<?> pojoClass, String methodName) throws NoSuch
324357 */
325358 @ SuppressWarnings ("unchecked" )
326359 public String getReturnTypeOfMethodAnnotatedWith (Class <?> pojoClass , String annotatedClassName )
327- throws ClassNotFoundException {
360+ throws ClassNotFoundException {
328361
329362 if (pojoClass == null ) {
330363 throw new IllegalAccessError (
331- "Class object is null. Cannot generate template as it might obviously depend on reflection." );
364+ "Class object is null. Cannot generate template as it might obviously depend on reflection." );
332365 }
333366
334367 Method [] methods = pojoClass .getDeclaredMethods ();
@@ -381,7 +414,7 @@ private Method findMethod(Class<?> pojoClass, String methodName) {
381414
382415 if (pojoClass == null ) {
383416 throw new IllegalAccessError (
384- "Class object is null. Cannot generate template as it might obviously depend on reflection." );
417+ "Class object is null. Cannot generate template as it might obviously depend on reflection." );
385418 }
386419 for (Method m : pojoClass .getMethods ()) {
387420 if (m .getName ().equals (methodName )) {
@@ -391,6 +424,22 @@ private Method findMethod(Class<?> pojoClass, String methodName) {
391424 return null ;
392425 }
393426
427+ /**
428+ * Checks whether the class given by the full qualified name is an enum
429+ *
430+ * @param className full qualified class name
431+ * @return <code>true</code> if the class is an enum, <code>false</code> otherwise
432+ */
433+ public boolean isEnum (String className ) {
434+
435+ try {
436+ return ClassUtils .getClass (className ).isEnum ();
437+ } catch (ClassNotFoundException e ) {
438+ LOG .warn ("{}: Could not find {}" , e .getMessage (), className );
439+ return false ;
440+ }
441+ }
442+
394443 /**
395444 * Returns the first enum value of an enum class
396445 *
0 commit comments