Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the T template type at runtime passing a variable

Tags:

java

templates

Is there way to pass a Class variable when instanciating a new template class named TemplateClass or is it absolutely a non-sens ?

Example of something i would like to do but that do not compile:

final List<TemplateClass> fields = new ArrayList<>();

public void loadDataFields() {
    for(Field x : ClassA.class.getDeclaredFields()) {
        fields.add(new TemplateClass<x.getClass()>());
    }
}

Edit

Here is the TemplateClass :

public class TemplateClass<T extends Comparable> {
    int count;
    T min;
    T max; 
}
like image 692
user7364588 Avatar asked Jan 27 '23 22:01

user7364588


1 Answers

In short - No.

There is no way it instantiates a template with dynamic class in runtime. That is because templates are evaluated at build time.

like image 182
Roee Gavirel Avatar answered Feb 05 '23 06:02

Roee Gavirel