?
Solved

Identify the type

Posted on 2012-04-12
1
Medium Priority
?
230 Views
Last Modified: 2012-08-13
I have written a method as follows,

public static <T> List<T> add(List<T> list, T t) {
    if(list == null) return null;
    if(t == null) return list;

    List<T> outputList = new ArrayList<T>(list);
    outputList.add(t); // append item into list

    return outputList;
}

Open in new window


I want to understand the Class for it's input parameters. I am using the following code for this purpose,

   
private static Class<?>[] getParameterTypes(Method method) {
		Type[] t = method.getGenericParameterTypes();
		List<Class<?>> paramTypeList = new ArrayList<Class<?>>();
		
		for(Type arg: t) {
			paramTypeList.add(getClass(arg));
		}
				
		java.util.Iterator<Class<?>> iterator = paramTypeList.iterator();
		Class<?>[] paramTypeArray = new Class<?>[paramTypeList.size()];
		int count = 0;
		
		while(iterator.hasNext()) {
			paramTypeArray[count] = iterator.next();
			count++;
		}
		
		return paramTypeArray;
	}
        
private static <T> Class<?> getClass(Type type) {
		if (type instanceof Class<?>) {
			return (Class<?>) type;
		} else if (type instanceof ParameterizedType) {
			return getClass(((ParameterizedType) type).getRawType());
		} else if (type instanceof GenericArrayType) {
			Type componentType = ((GenericArrayType) type).getGenericComponentType();
			Class<?> componentClass = getClass(componentType);
			if (componentClass != null) {
				return Array.newInstance(componentClass, 0).getClass();
			} else {
				return null;
			}
		} else {
			return null;
		}
	}

Open in new window


The problem is that for the argument T, the getClass method returns null and my business logic fails. Can anybody suggest a way to identify the class of T.

Thanks,
Vijay Bhore
0
Comment
Question by:vijaybhorems
1 Comment
 
LVL 14

Accepted Solution

by:
CPColin earned 1000 total points
ID: 37840405
You probably can't get the type from the List, because generic types are erased at runtime (search for a phrase like "java type erasure" for more details). However, since you're passing an object of type T to that add() method, you could just call getClass() on that object.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month16 days, 18 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question