0
        class A<B> {
            ...
        }
    
    Class<A<B>> a = Class.forName(A.class.getTypeName()) throws error 

 a.getDeclaredConstructor(String.class).newInstance("hello world")

what should I do to make an Instance for this class?

I want to know how I can make class Name with generic type this example does not work

other example

public class SimpleTest {

    interface TakePhoto<B> {
        TakePhoto<B> clickShutter();

        B done();
    }

    public @interface Specific {
        String value();
    }

    @Specific("GALAXY-S5")
    static class GalaxyS5TakePhoto<B> implements TakePhoto<B>{
        
        B backPage;

        GalaxyS5TakePhoto(B backPage){
            this.backPage = backPage;
        }

        @Override
        public TakePhoto<B> clickShutter() {
            //...
            return this;
        }

        @Override
        public B done() {
            return backPage;
        }
    }

    @Test
    public void Test() {
        // if a Device is Galaxy S5, I want to make GalaxyS5TakePhoto 
        // if the device is other I will have other class models and make its instance 
        // I have package of these classes and want to make right instance. 
        
        // first collect specific classes which have TakePhoto
        // then find and filter specific model
        // then create new Instance 
    }
}

I just want to make class with Generic type

first collect specific classes which have TakePhoto then find and filter specific model then create new Instance

1 Answers1

0

You cannot because generic information is removed at runtime, there is only class A there.

However, you can create a seperate class like

public class M extends A<B> {
}

Class M will hold the generic information.

Now here is a full demonstration on how to extract generic information from a Class object:

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;

public class GenericInfoInClass {

    // class which contains generic information
    public static class StringList
            extends ArrayList<String>
            implements Comparable<StringList>
    {

        @Override
        public int compareTo(StringList o) {
            return 0;
        }
    }

    public static void main(String[] args) {
        Type[] genericInterfaces = StringList.class.getGenericInterfaces();
        for (Type genericInterface : genericInterfaces) {
            System.out.println(genericInterface);
            System.out.println("[Generic type arguments:]");

            ParameterizedType parameterizedType = (ParameterizedType) genericInterface;
            Type[] genericTypes = parameterizedType.getActualTypeArguments();
            for (Type genericType : genericTypes) {
                System.out.println("  -- " + genericType);
            }
        }

        System.out.println("----------------------------");

        Type genericSuperclass = StringList.class.getGenericSuperclass();
        System.out.println(genericSuperclass);
        System.out.println("[Generic type arguments:]");

        ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
        Type[] genericTypes = parameterizedType.getActualTypeArguments();
        for (Type genericType : genericTypes) {
            System.out.println("  -- " + genericType);
        }
    }
}
JSPDeveloper01
  • 638
  • 1
  • 8
  • 20