0

I'm creating a Class object for class Box which has a type parameter. And in the place where I get Class objects it throws an warning that class type is raw - (Class objCB = b.getClass()).

My doubt is while creating a Class object for any class with type parameter why should generics come into the picture. And how to resolve the warning. Below is my code

//Box Class
public class Box <T extends Comparable<T>>{

public T objA;
public T objB;      

public void set(T t1, T t2)
{
    this.objA = t1;
    this.objB = t2;
}

public int compare()
{       
return this.objA.compareTo(this.objB);      
}   

}

// Refelection class
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;

public class Test6 {

public static void main(String[] args) throws ClassNotFoundException {

//using getClass
Box <Integer> b = new Box <Integer>();
Class objCB = b.getClass();    //warning thrown here
Method[] methods = objCB.getMethods();  
for (Method m : methods)
{
    System.out.println(m.getName());
}

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

//using class
Class objC2 = Box.class;   //warning thrown here
Field[] fields = objC2.getFields();
for (Field f : fields)
{   
    System.out.println(f.getName());
}

System.out.println("==================");
//using forName()   
Class objC3 = Class.forName("Box");  //warning thrown here
Type[] types = objC3.getTypeParameters();
for (Type t : types)
{
    System.out.println(t.toString());
}

System.out.println("==================");
//using Type()  
Class objI = Integer.TYPE;  //warning thrown here
System.out.println(objI.getName());
}
}
user1925406
  • 713
  • 3
  • 15
  • 33
  • 3
    Class `Class` has itself a type parameter. You get a warning because you are using `Class` without a type parameter. [Don't use raw types](http://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it). Use `Class>` or at least `Class>` instead of the raw type `Class`. – Jesper Jul 24 '15 at 10:50
  • > works but using > throws a compile error Type mismatch: cannot convert from Class to Class> – user1925406 Jul 24 '15 at 10:58
  • 1
    To understand some of the problem here you need to know [what type erasure is](http://stackoverflow.com/questions/313584/what-is-the-concept-of-erasure-in-generics-in-java). – Raedwald Jul 24 '15 at 12:20

1 Answers1

1

The Class class has a generic parameter which in general should point to the type the class represents. It's used to safely instantiate the object using newInstance() or getConstructor(...).newInstance() or call some other methods like cast() or getEnumConstants(). For example, you can use without a type cast:

Integer i = Integer.class.getConstructor(int.class).newInstance(0);

In many cases it's difficult to preserve exact type argument. For example, Object.getClass() method has return type Class<?>. On the other hand, when you are not going to instantiate the class, having Class<?> is fine. So replace your raw Class with Class<?>.

If you later want to change it to some specific class in safe manner, you may use asSubclass:

Class<?> someClass = i.getClass();
// type check is done here: CCE will be thrown if someClass is not Integer.class
Class<? extends Integer> c = someClass.asSubclass(Integer.class);
// No type check is necessary here.
Integer ii = c.getConstructor(int.class).newInstance(0);

In any case using raw types is bad and may lead to serious problems, that's why the warning is issued. Avoid raw types.

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334