Solved

Identify the type

Posted on 2012-04-12
1
220 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 500 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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:

765 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