Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果对象类是使用java反射的字段上的数组,则无法获取包和类名_Java_Reflection - Fatal编程技术网

如果对象类是使用java反射的字段上的数组,则无法获取包和类名

如果对象类是使用java反射的字段上的数组,则无法获取包和类名,java,reflection,Java,Reflection,我有一些问题 class example{ Text text=new Text(); int a; JLabel[] label=new JLabel[3]; Panel[][][] panel=new Panel[5][5][5]; } 因此,我尝试了反思: Class cls=Class.forName("example"); Field[]f=cls.getDeclaredFields(); for(field : f){ field.

我有一些问题

class example{
     Text text=new Text();
     int a;
     JLabel[] label=new JLabel[3];
     Panel[][][] panel=new Panel[5][5][5];
}
因此,我尝试了反思:

Class cls=Class.forName("example");
Field[]f=cls.getDeclaredFields();
for(field : f){
     field.getType().getPackage();
     field.getType().getName();
}

package的结果为null,类[[[Ljava.awt.Panel

的结果数组是一种特殊类型。它不属于任何包

尝试(未经测试):

Class cls=Class.forName(“示例”);
字段[]f=cls.getDeclaredFields();
用于(字段:f){
Class type=field.getType()
int numArrayDimensions=0;
while(type.isArray()){
type=type.getComponentType();
NumarayDimensions++;
}
如果(!type.isPrimitive()){
Package pkg=type.getPackage();
}
String className=getFormattedClassName(类型,numArrayDimensions);
}
私有字符串getFormattedClassName(类类型,int numArrayDimensions){
StringBuilder sb=新的StringBuilder(type.getName());
IntStream.range(0,numArrayDimensions.foreach(dimension->sb.append(“[]”));
使某人返回字符串();
}

另一方面,原语是一种语言功能。表示它们的类对象不属于任何包。

请尝试使用以下代码获取包名

        Package p=cls.getPackage();
        System.out.println(p.getName());
我已经试过使用示例程序

        Class cls=Countcharacters.class;            
        Package p=cls.getPackage();
        System.out.println(p.getName());

根据您的评论,我假设您想要实现的是以类似于类中声明的方式打印字段类型

        Package p=cls.getPackage();
        System.out.println(p.getName());
class Example {
    int a;
    int[][] f;
    JLabel[][] label = new JLabel[3][];
    String s;
}
你想得到什么

int
int[][]
javax.swing.JLabel[][]
java.lang.String
然后在表示声明字段的实例上使用
Field.getType().getTypeName()

  • field.getType()
    将返回表示字段中可以存储的类型的类
  • getTypeName()
    以易于阅读的形式返回名称,因此对于字段
    int[]]f;
    您将获得
    int[][]
    而不是
    [[I
演示:

类示例{
INTA;
int[]f;
JLabel[][]标签=新的JLabel[3][];
字符串s;
}
课堂演示{
公共静态void main(字符串[]ar){
Class cls=Example.Class;
Field[]fields=cls.getDeclaredFields();
用于(字段f:字段){
System.out.println(f.getType().getTypeName());
}
}
}

您想用包名实现什么,在本例中是javax.swing?列出从我的代码中加载的包名。@新手,预期结果是什么?OP询问数组类型。如果我使用基本数据类型和多个数组,则是错误的。@新手,我已更新了答案。请查看Javadoc when您正在寻找拟合方法:@newbie您可以更新您的问题并显示预期结果吗?@newbie?我拒绝了编辑。如何
String type2=field.getType().getCanonicalName();
帮助您获取软件包?解析?我的代码中有错误吗?@新手请不要参考外部站点的代码。只需告诉什么不起作用或者为什么您认为有更好的方法。+1表示Java SE 8中引入的getTypeName。我不知道此方法。如果OP不需要软件包和名称,则不需要单独使用这是一个更干净的解决方案方法注释中提到的OP提供了相同的输出。因此,仍然不清楚OP要求的是什么。
getCanonicalName
非常相似。目前,我能找到的
getTypeName
之间输出的主要区别是,何时我们将打印非静态内部类的类型,而
getCanonicalName
将打印为
外部类$Inner
但是
getTypeName
作为
Outer.Inner
更接近我们用代码编写它的方式。但是我同意,我们仍然不知道OP是否真的想要实现,这就是为什么我在开始回答时说它是基于OP注释的(这也不是很清楚)。
class Example {
    int a;
    int[][] f;
    JLabel[][] label = new JLabel[3][];
    String s;
}

class Demo {

    public static void main(String[] ar) {
        Class<?> cls = Example.class;
        Field[] fields = cls.getDeclaredFields();
        for (Field f : fields) {
            System.out.println(f.getType().getTypeName());
        }

    }
}