Solved

Identify the type

Posted on 2012-04-12
1
226 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

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 …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

718 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