Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the `.class` attribute from a generic type parameter?

Tags:

java

generics

The accepted answer to this question describes how to create an instance of T in the Generic<T> class. This involves passing in a Class<T> parameter to the Generic constructor and callin the newInstance method from that.

A new instance of Generic<Bar> is then created, and the parameter Bar.class is passed in.

What do you do if the generic type parameter for the new Generic class is not some known class like Bar but is itself a generic type parameter? Suppose I had some other class Skeet<J> and I wanted to create a new instance of Generic<J> from inside that class. Then, if I try to pass in J.class I get the following compiler error:

cannot select from a type variable. 

Is there any way around this?

The specific bit of code triggering the error for me is:

public class InputField<W extends Component & WidgetInterface>                                                  extends InputFieldArray<W> {   public InputField(String labelText)   {     super(new String[] {labelText}, W.class);   }   /* ... */ }  public class InputFieldArray<W extends Component & WidgetInterface>                                                                  extends JPanel {    /* ... */   public InputFieldArray(String[] labelText, Class<W> clazz)                           throws InstantiationException, IllegalAccessException   {     /* ... */      for (int i = 0 ; i < labelText.length ; i++) {       newLabel = new JLabel(labelText[i]);       newWidget = clazz.newInstance();       /* ... */     }     /* ... */   }   /* ... */ } 

The error happens, because I can't write W.class. Is there some other way of passing in the same information?

like image 582
John Gowers Avatar asked Aug 15 '13 14:08

John Gowers


People also ask

How do you indicate that a class has a generic type parameter?

A generic type is declared by specifying a type parameter in an angle brackets after a type name, e.g. TypeName<T> where T is a type parameter.

Can attributes be generic?

Attributes can be applied to generic types in the same way as non-generic types.

Can We pass multiple types of parameters in a generic class?

We can also pass multiple Type parameters in Generic classes. We can also write generic functions that can be called with different types of arguments based on the type of arguments passed to the generic method. The compiler handles each method.

What is a generic class in Java?

Generics in Java. Generics mean parameterized types. The idea is to allow type (Integer, String, … etc, and user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types. An entity such as class, interface, or method that operates on a ...

How do I get generic arguments of a class?

The GetGenericArgument()method has to be set on the Base Type of your instance (whose class is a generic class myClass<T>). Otherwise, it returns a type[0]. Example: Myclass<T> instance = new Myclass<T>(); Type[] listTypes = typeof(instance).BaseType.GetGenericArguments(); Share Follow edited Jun 21 at 19:56 Peter Mortensen

Can you use typeof () with a generic parameter?

You can't use typeof() with a generic parameter, though. – Reynevan Mar 8 '19 at 17:52 7 @Reynevan Of course you can use typeof()with a generic parameter.


1 Answers

Using .class on a type parameter isn't allowed - because of type erasure, W will have been erased to Component at runtime. InputField will need to also take a Class<W> from the caller, like InputFieldArray:

public InputField(String labelText, Class<W> clazz) {     super(new String[] {labelText}, clazz); } 
like image 187
Paul Bellora Avatar answered Oct 13 '22 06:10

Paul Bellora