Reputation: 1703
Im using the following code to find class members in reflection that are primitive and some object ,my question is there is a way to identify if field is type primitive ,object ,class reference because i want to call to specific method according to the type. for example if field is primitive call to handlePrimitive if field type other type reference (in the example below SalesOrderItemPK primaryKey; )call to method handleClassReferance etc
just of understanding i need to get the class and invistigate it and create data according to the member type...
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 to specific method that can handle fields according to the 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 for example should look like this and I need to know for primaryKey key to create an object.
@Entity
public class SalesOrderItem
{
@EmbeddedId
SalesOrderItemPK primaryKey;
private String ProductId;
private String Notes;
Upvotes: 4
Views: 15746
Reputation: 597076
If you don't call .toString()
, but instead cast Type
to Class
, you get .isPrimitive()
Upvotes: 17