Solved

Identify the type

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

708 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

12 Experts available now in Live!

Get 1:1 Help Now