Can I discover a Java class' declared inner cl

2020-01-31 02:38发布

In Java, is there any way to use the JDK libraries to discover the private classes implemented within another class? Or do I need so use something like asm?

3条回答
beautiful°
3楼-- · 2020-01-31 03:13
package com.test;

public class A {

    public String str;

    public class B {
        private int i;
    }
}
package com.test;

import junit.framework.TestCase;

public class ReflectAB extends TestCase {
    public void testAccessToOuterClass() throws Exception {
           final A a = new A();
           final A.B b = a.new B();
           final Class[] parent = A.class.getClasses();
           assertEquals("com.test.A$B", parent[0].getName());
           assertEquals("i" , parent[0].getDeclaredFields()[0].getName());
           assertEquals("int",parent[0].getDeclaredFields()[0].getType().getName());
           //assertSame(a, a2);
        }

}
查看更多
我只想做你的唯一
4楼-- · 2020-01-31 03:21

I think this is what you're after: Class.getClasses().

查看更多
登录 后发表回答