getEnclosingClass and enums

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Fixing "typos" in enums... 18
Enums: Properties vs. Methods 33
generics & enums 1
Internationalization and enums 8
masks and enums 21
enums and scope 5
Understanding Inheritance and Polymorphism 1
enums and modifiers 5

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top