Is it possible to generate something like the following--
public interface Reference{
// Returns a reference of the provided type (in theory)
public <E> E getReference(int num);
// Set the reference of the type
public <E> void setReference(int num, E ref);
}
public class ClassFullOfReferenceVariables implements Reference{
int intRef_1 = 1, intRef_2 = 2;
ClassFullOfReferenceVariables cforv;
public static void main(String[] args){
ClassFullOfReferenceVariables myClass = new ClassFullOfReferenceVariables();
myClass.<Integer>setReference(0, new Integer(10));
myClass.<Integer>setReference(1, new Integer(15));
myClass.<ClassFullOfReferenceVariables>setReference(0, myClass);
System.out.println(myClass.intRef_1);
System.out.println(myClass.intRef_2);
System.out.println(myClass.cforv);
System.out.println(myClass);
System.out.println(myClass.cforv.intRef_1);
System.out.println(myClass.cforv.intRef_2);
}
public <E> void setReference(int num, E ref){
if(ref instanceof Integer){
switch(num){
case 0:
intRef_1 = (Integer)ref; // slightly amazed this is legal
break;
case 1:
intRef_2 = (Integer)ref;
break;
default:
intRef_1 = (Integer)ref;
break;
}
}
else if(ref instanceof ClassFullOfReferenceVariables)
cforv = (ClassFullOfReferenceVariables)ref;
else System.out.println("Unsupported reference type");
}
public <E> E getReference(int num){
E e = null;
Integer i1 = intRef_1;
/*
if(i1 instanceof E){ // obviously wont work due to type-erasure
}*/
return e;
}
}
--and be capable of returning a valid reference with the getReference(int num) method, or do I need to provide a dummy-variable and a warning to the user, such as--
//Warning! E must be a valid type
public <E> E getReference(int num, E dummy)