O
ogmios01
Having a weird problem in Java SE 5 (1.5).
First my original problem: when using enums (a new language feature)
when you do a getClass().getName() on them you see that the actual
class gets a $1, $2, ... appended to it. For my application I need to
get the basetype of such an enum, and this is possible with the method
getClass().getEnclosingClass().getName().
Problem 1 solved.
Now this is in a general method in an application/library which can
also run on pre SE 5 versions of Java so I thought to be smart, and use
reflection for the getEnclosingClass() (this method is on Class but
it's not available on pre SE 5). So I use the following code:
String methodName = "getEnclosingClass";
Method method = null;
Class result = null;
try {
method = classParam.getMethod(methodName);
result = (Class)method.invoke(classParam);
}
catch ( Exception ex ) {
...
}
return result;
Works beautifully for all kind of classes... except when classParam is
an enum... then I get a NoSuchMethodFound exception... go figure.
Does anyone have an explanation for this?
Regards,
Ogmios
First my original problem: when using enums (a new language feature)
when you do a getClass().getName() on them you see that the actual
class gets a $1, $2, ... appended to it. For my application I need to
get the basetype of such an enum, and this is possible with the method
getClass().getEnclosingClass().getName().
Problem 1 solved.
Now this is in a general method in an application/library which can
also run on pre SE 5 versions of Java so I thought to be smart, and use
reflection for the getEnclosingClass() (this method is on Class but
it's not available on pre SE 5). So I use the following code:
String methodName = "getEnclosingClass";
Method method = null;
Class result = null;
try {
method = classParam.getMethod(methodName);
result = (Class)method.invoke(classParam);
}
catch ( Exception ex ) {
...
}
return result;
Works beautifully for all kind of classes... except when classParam is
an enum... then I get a NoSuchMethodFound exception... go figure.
Does anyone have an explanation for this?
Regards,
Ogmios