1

Main.Java : Main Class which creating the object of Test which of generic type Test.Java: Its Generic class which is creating object of Wrapper class of generic type Wrapper.java: Its a Generic Class of Type We are passing String,Long as type for creating Test.java object . And passing long as a type for creating the object of Wrapper class. Main.Java:

    public class MainClass {
   Test<String,Long> test;

   MainClass()
   {
       test = new Test<String,Long>(){};   
   }
   public static void main(String[] arr)
   {
       MainClass m = new MainClass();
   }
  }

Test.Java

     public class Test<T,U> 
    {
     Wrapper<U> mywrapper;
    Test()
    {
      Type type = this.getClass().getGenericSuperclass();
      System.out.println("TYPE: " + type.toString());

      Type type1 = ((ParameterizedType)this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
      System.out.println("TYPE1: " + type1.getClass().getName());


      Type[] types = ((ParameterizedType)type).getActualTypeArguments();

      for (int i = 0; i < types.length; i++) {
      System.out.println("TYPES[" + i + "]: " + types[i].toString());
      }
      mywrapper = new Wrapper<U>(){};
  }
}

Wrapper.Java

 public class Wrapper<U> {
  Wrapper()
  {
      Type type = this.getClass().getGenericSuperclass();
      System.out.println("TYPE: " + type.toString());

      Type type1 = ((ParameterizedType)this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
      System.out.println("TYPE1: " + type1.getClass().getName());


      Type[] types = ((ParameterizedType)type).getActualTypeArguments();

      for (int i = 0; i < types.length; i++) {
      System.out.println("TYPES[" + i + "]: " + types[i].toString());
      }
  }
}

And after executing the program we are getting output as
TYPE: Test TYPE1: java.lang.Class TYPES[0]: class java.lang.String TYPES[1]: class java.lang.Long TYPE: Wrapper *TYPE1: sun.reflect.generics.reflectiveObjects.TypeVariableImpl TYPES[0]: U*

So from the output you can see for the Wrapper class we are getting Type as U instead of Long as you can see in Test.Java.

4 Answers 4

2

Hope the following program will clarify your doubt.

public class VMGeneric<A>
{
    private A objVariable;

    public VMGeneric(A localVariable)
    {
        objVariable = localVariable;
    }

    public String getNameOfGenericType()
    {
        return objVariable.getClass().getCanonicalName();
    }

    public static void main(String[] args)
    {
        VMGeneric<String> o1 = new VMGeneric<>("");
        VMGeneric<Object> o2 = new VMGeneric<>(new Object());

        System.out.println(o1.getNameOfGenericType());
        System.out.println(o2.getNameOfGenericType());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

So from the output you can see for the Wrapper class we are getting Type as U instead of Long as you can see in Test.Java.

Right. Class metadata reflects what is written at compile time. So in order to get the type it must be hard-coded at compile-time.

Comments

0

Is it possible to get the type of the Parameter in Wrapper.java . Actually my doubt is why its working in first level indirection and not working for Wrapper.java. On problem I found in my code is that mywrapper = new Wrapper(){}; will create an anonymous class of type U and thats the reason its printing U as datatype. But if I remove the {} then I am getting classclassException as Wrapper.java

 java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType

Comments

0

FYI: In Wrapper, U is not a parameter. Instead You are saying Your class can accept any type of object instead of U. No one will know until you pass the real object. Again U is not a parameter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.