How to get the actual type arguments to an indirectly implemented generic interface?

The short answer is NO. I agree that it is a pity... :( The reason is that Java drops type parameters at the compile stage. They do not exist in byte code.

They are used by compiler only To solve your problem you have to add yet another "regular" parameter of type Class and pass it to the constructor when you are creating an instance of Base: class Base implements Awesome { private E type; public Base(E type) { this. Type = type; } }.

The short answer is NO. I agree that it is a pity... :( The reason is that Java drops type parameters at the compile stage. They do not exist in byte code.

They are used by compiler only. To solve your problem you have to add yet another "regular" parameter of type Class and pass it to the constructor when you are creating an instance of Base: class Base implements Awesome { private E type; public Base(E type) { this. Type = type; } }.

1 The type information is in the byte code. So you're not correct. You can get the type information in this position.

Google for super type tokens to get an example. – Thomas Jung Dec 23 '10 at 9:03 1 @Thomas - Static type information is stored, dynamic info is not. – OrangeDog Dec 23 '10 at 12:00.

Edit: You may just want to look into using: code.google.com/p/gentyref/ If you can guarantee that all implementations of Awesome will not have type arguments, the following code should get you started 1: static void investigate(Object o) { final Class c = o.getClass(); System.out. Println("\n" + c.getName() + " implements: "); investigate(c, (Type)null); } static void investigate(Type t, Type...typeArgs) { if(t == null) return; if(t instanceof Class) { investigate((Class)t, typeArgs); } else if(t instanceof ParameterizedType) { investigate((ParameterizedType)t, typeArgs); } } static void investigate(Class c, Type...typeArgs) { investigate(c. GetGenericSuperclass(), typeArgs); for(Type I : c.

GetGenericInterfaces()) { investigate(i, typeArgs); } } static void investigate(ParameterizedType p, Type...typeArgs) { final Class c = (Class)p.getRawType(); final StringBuilder be = new StringBuilder(c.getName()); b. Append(' 0) { int I = 0, nextTypeArg = 0; for(Type local : localArgs) { if(local instanceof ParameterizedType) { ParameterizedType localP = (ParameterizedType) local; b. Append(localP.getRawType()).

Append(''); } else if(local instanceof TypeVariable) { // reify local type arg to instantiated one. LocalArgsnextTypeArg = typeArgsnextTypeArg; b. Append(localArgsnextTypeArg); nextTypeArg++; } else { b.

Append(local.toString()); } b. Append(", "); i++; } if(typeArgs. Length > 0) { b.

Delete(b.length() - 2, b.length()); } b. Append('>'); } else { String args = Arrays. ToString(localArgs); b.

Append(args. Substring(1, args.length()-1)). Append('>'); } System.out.

Println(b); investigate(c, localArgs); } If, however, instantiations of Awesome or Base will be made, that type information will be lost due to erasure. This can be worked around, as a convention, with something like this: Awesome awesome = new Base() {}; Notice the {}, this creates a new anonymous class that implements (or here extends) Base. This class will have its type parameters available for reflection.

If you're afraid enforcing this convention will be an issue, you can hide the constructors & only expose factory methods: class Base implements Awesome { public static Base newNumberInstance() { return new Base () {}; } protected Base() {} } As the above code has not been completely tested, you may want to do that. The point here is that you can find the actual type parameters given your requirements are stringent enough. Whether or not that applies to your situation is up to you to determine.1 It will print out all of the interfaces a class implements & not just the type parameters of Awesome.

This can be changed, but I figured I'd go for more general & let you work out the specifics. For example, you'll want to test these to see what I mean: investigate(new ArrayList()); investigate(new ArrayList() {}); // new anonymous ArrayList class investigate(""); investigate(new Awesome () {}); // new anonymous implementation of Awesome.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions