I'm going to say that you cannot do this simply based on the name.
You can try to split by . and $ , but this code example shows that it is not always obvious where the simple name begins:
class Ideone { private static class Bar {}; public static void main (String[] args) throws java.lang.Exception { class Foo$o { class Bar$bar {} }; class Foo$o$Bar { class Bar$bar {} }; class Foo$o$Bar$Bar$bar {} print(Ideone.class); print(Bar.class); print(Foo$o.class); print(Foo$o.Bar$bar.class); print(Foo$o$Bar.Bar$bar.class); print(Foo$o$Bar$Bar$bar.class); } private static void print(Class<?> clazz) { System.out.printf("fqn=%s, sn=%s%n", clazz.getName(), clazz.getSimpleName()); } }
Output:
fqn=Ideone, sn=Ideone fqn=Ideone$Bar, sn=Bar fqn=Ideone$1Foo$o, sn=Foo$o fqn=Ideone$1Foo$o$Bar$bar, sn=Bar$bar fqn=Ideone$1Foo$o$Bar$Bar$bar, sn=Bar$bar fqn=Ideone$2Foo$o$Bar$Bar$bar, sn=Foo$o$Bar$Bar$bar
Ideone demo
i.e. if you said "name bit after the final $ or . ", you are mistaken.
The only convincing way to do this is to load the class, possibly without initializing it:
Class<?> clazz = Class.forName(className, false, someClassLoadeR);
Andy turner
source share