Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

java - How to get class of generic type when there is no parameter of it?

I just learned about this fine looking syntax

Collections.<String>emptyList()

to get an empty List with elements which are supposedly of type String. Java's source looks like this:

public static final List EMPTY_LIST = new EmptyList<Object>();
:
public static final <T> List<T> emptyList() {
  return (List<T>) EMPTY_LIST;
}

Now if I code a method in that way where the generic type does not appear in the parameter list, is there any way how I can access the actual class that becomes T?

I'm saying, up to now my approach to code the same thing would have been

private <T> T get(String key, Class<T> clazz) {
  // here I can do whatever I want with clazz, e.g.:
  return clazz.cast(value);
}

If I removed the clazz-parameter I wouldn't be able to do the cast(). Obviously I could do

  return (T) value;

but that gives me the usual warning Type safety: Unchecked cast from Object to T. Ok, @SuppressWarnings("unchecked") helps here, but actually I want to do something with the intended return type of the method. If I add a local variable

T retValue;

I'd have to initialise it with something, null doesn't help. After I assign it like

@SuppressWarnings("unchecked")
T retValue = (T) value;

I could do, e.g.

retValue.getClass().getName()

but if the cast fails I end up with no information about T again.

Since Java (or at least my Java 6) does not have the generic info any more during runtime, I currently can't think of a way to do this. Is there a way? Or do I have to stick with my "old" approach here?

Please note that the example I lined out is very simple and doesn't make much sense. I want to do more complicated stuff here, but that's out of the scope.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you want the generic type at runtime you need to either have it as a field or create a sub-class of a type for a specific combination of types.

e.g.

List<String> list = new ArrayList<String>() {}; // creates a generic sub-type
final Class type = (Class) ((ParameterizedType) list.getClass()
                            .getGenericSuperclass()).getActualTypeArguments()[0];
System.out.println(type);

prints

class java.lang.String

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...