Solved

Identify the type

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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…

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now