Check reflection if type is primitive or type is an object

Im using the following code to search for class members in reflection that are primitive and some objects, my question is is there a way to determine if a field is a type primitive, an object, a class reference, because I want to call a specific method according with type. for example, if the field is a primitive call to handlePrimitive, if the field type is used in another type (in the example below, SalesOrderItemPK primaryKey;) calls the handleClassReferance method, etc.

just for understanding, I need to get a class and look at it and create data according to the type of member ...

for (Object clsObj : listClsObj) { Field[] declaredFields = clsObj.getClass().getDeclaredFields(); numOfEntries = 1; do { Object newInstance = clsObj.getClass().newInstance(); for (Field field : declaredFields) { // Get member name & types Class<?> fieldType = field.getType(); Type genericType = field.getGenericType(); String fieldTypeName = fieldType.getName(); String memberName = field.getName(); if (genericType instanceof ParameterizedType) { ParameterizedType pt = (ParameterizedType) genericType; for (Type typeOfReferance : pt.getActualTypeArguments()) { String classTypeName = typeOfReferance.toString(); String[] parts = classTypeName.split(" "); memberReferance = parts[1]; 

here I want to call a specific method that can process fields according to data types

 public static SwitchInputType<?> switchInput(final String typeName, final String memberName, final int cnt) { if (typeName.equals("java.lang.String")) { return new SwitchInputType<String>(new String(memberName + " " + cnt)); } else if (typeName.equals("char")) { return new SwitchInputType<Character>(new Character('a')); 

the class should look like this, and I need to know the primaryKey key to create the object.

 @Entity public class SalesOrderItem { @EmbeddedId SalesOrderItemPK primaryKey; private String ProductId; private String Notes; 
+4
source share
1 answer

If you do not call .toString() , but instead discard Type in Class , you get .isPrimitive()

+15
source

All Articles