8
class test {
    public static void main(String[] args) {
         new test();
    }
    void method() {
         class inside {
              int a;
              void methodinside() {}
         }
    }
}

I was declaring class using reflection like:

Class c = Class.forName("test");
Class[] cls = c.getDeclaredClasses();
for(Class cl : cls)
     System.out.println(cl.getName());

but, my program cannot find class inside.

Razib
  • 10,965
  • 11
  • 53
  • 80
newbie
  • 929
  • 17
  • 35
  • 1
    Interesting, since we can do the reverse with `getEnclosingClass`. http://ideone.com/nA3Zua – Radiodef Apr 16 '15 at 03:09
  • how to access in main, while class inside in another method or another class ??? – newbie Apr 16 '15 at 03:19
  • If you want to access the class from `main` then you have to declare it somewhere where `main` can see it. The point of these local classes is to not be visible from outside the declaring method. – Thilo Apr 16 '15 at 03:34
  • any idea or other way beside reflection ??? – newbie Apr 16 '15 at 03:37
  • This could be an X-Y-problem. What are you really trying to do? Are you developing some kind of IDE tooling? – Thilo Apr 16 '15 at 04:49
  • yes, i want developing some code to some diagram like classes diagram and others. this is only my problem, i had tool like class visualizer also cannot loading local class too. – newbie Apr 16 '15 at 04:58

4 Answers4

2

I don't think there is a method to find local classes (classes defined inside a method).

You could try to enumerate all classes in the package and then use getEnclosingClass (the opposite of what you are looking for) to filter them.

Community
  • 1
  • 1
Thilo
  • 257,207
  • 101
  • 511
  • 656
2

This is a way to get Inner class in a method.

public class Main {
    public static void main(String[] args) throws ClassNotFoundException {
        Class<?> clazz = Class.forName("Outer$1InnerMethodClass");
        System.out.println(clazz.getName());

        clazz = Class.forName("Outer$2InnerMethodClass");
        System.out.println(clazz.getName());

        clazz = Class.forName("Outer$2InnerMethodClass$1InnerMethodInnetMethod");
        System.out.println(clazz.getName());
    }

}

class Outer{
    void outerMethod1() {
        class InnerMethodClass {
             int a;
             void innerMethod() {}
        }
   }

    void outerMethod2() {
        class InnerMethodClass {
             int a;
             void innerMethod() {
                 class InnerMethodInnetMethod{

                 }
             }
        }
   }
}
Paulo
  • 2,956
  • 3
  • 20
  • 30
  • 1
    This can't be safely generalized though. The exact name of a local class is not mandated by the specification, only its format. – Radiodef Apr 16 '15 at 04:27
2

In Java, there aren't general methods for enumerating classes by design. This is because classes could be loaded from anywhere, or dynamically created. Storing class files in a JAR or directory is just one of any possible class loaders.

So, while it might be more reasonable to assume that enclosed classes are a known, enumerable set, it doesn't make sense to provide a method that works only in that special case.

If you assume the common case of a URLClassLoader, you can treat the underlying URLs as file systems and enumerate their contents. This might be appropriate if you are creating a tool to do something with existing third-party code, and it has even been adopted for many Java EE standards (mistakenly, I feel).

If you are creating a plug-in mechanism, providing metadata in a well-known location is more efficient, reliable, and works with any valid class loader.

erickson
  • 265,237
  • 58
  • 395
  • 493
1

The getDeclaredClasses() method returns (from the javadoc)-

an array of Class objects reflecting all the classes and interfaces declared as members of the class represented by this Class object. This includes public, protected, default (package) access, and private classes and interfaces declared by the class, but excludes inherited classes and interfaces.

So it seems it doesn't return any inner class you have declared. That's why you get nothing in the array of Class cls.

Razib
  • 10,965
  • 11
  • 53
  • 80