从反射获取字段的类名

发布于 2024-12-27 16:37:36 字数 1257 浏览 1 评论 0原文

我在这件事上兜圈子。

我有一个类 Person,例如

public class Person {
String name = "";
}

现在,我想内省这个类实例 &弄清楚类的名称声明为什么。

所以, name = String 或 java.lang.String

这是我的代码:

“this”是 Person 的一个实例。

try {
    String className = this.getClass().getName();
        Class cls = Class.forName(className);
        Field fieldlist[] = cls.getDeclaredFields();
        for (int i = 0; i < fieldlist.length; i++) {
           Field fld = fieldlist[i];
           int mod = fld.getModifiers();
           System.out.println("1. " + fld.toGenericString());
           System.out.println("2. " + fld.getName());
           System.out.println("3. " + fld.getGenericType() + "]");



           Object oj = fld.getType();

           // Says that 4: class java.lang.String

           System.out.println("4: " + oj.toString());
           Class c1 = oj.getClass();

           // Should throw Exception
           String stype = c1.getDeclaringClass().toString();
           System.out.println("5. " + stype);


        }
      }
      catch (Throwable e) {
         System.err.println(e);
      }

我设法到达这样的部分:

class java.lang.String

但我需要它是“java.lang.String”

有什么想法吗?

Im going round in circles on this.

I have a class Person, eg

public class Person {
String name = "";
}

Now, I would like to introspect this class instance & figure out what Class is name declared as.

So, name = String or java.lang.String

This is my Code:

'this' is an instance of Person.

try {
    String className = this.getClass().getName();
        Class cls = Class.forName(className);
        Field fieldlist[] = cls.getDeclaredFields();
        for (int i = 0; i < fieldlist.length; i++) {
           Field fld = fieldlist[i];
           int mod = fld.getModifiers();
           System.out.println("1. " + fld.toGenericString());
           System.out.println("2. " + fld.getName());
           System.out.println("3. " + fld.getGenericType() + "]");



           Object oj = fld.getType();

           // Says that 4: class java.lang.String

           System.out.println("4: " + oj.toString());
           Class c1 = oj.getClass();

           // Should throw Exception
           String stype = c1.getDeclaringClass().toString();
           System.out.println("5. " + stype);


        }
      }
      catch (Throwable e) {
         System.err.println(e);
      }

I managed to get to a part that states:

class java.lang.String

but I need it to be "java.lang.String"

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青衫儰鉨ミ守葔 2025-01-03 16:37:36

尝试.. getType() 然后 getName()

 fld.getType().getName()

编辑:(Aften Green Days 的评论) -- 请注意
在大多数情况下,fld.getType().getCanonicalName() 将给出相同的输出。使用内部类时,输出会有所不同。这是来自搜索的链接。根据您需要对类名执行的操作,您可以选择 getName() 或 getCanonicalName() 之一

Try.. getType() and then getName()

 fld.getType().getName()

Edit:(Aften Green Days' comment) -- Note that
fld.getType().getCanonicalName() will give same output in most cases. The output is different when innerclasses are used. Here is link came from search. Depending what you need to do with classname you may choose one of getName() or getCanonicalName()

哭泣的笑容 2025-01-03 16:37:36
System.out.println("3. " + fld.getType().getCanonicalName()); 

结果是:

3. java.lang.String
System.out.println("3. " + fld.getType().getCanonicalName()); 

results in:

3. java.lang.String
无戏配角 2025-01-03 16:37:36

我想我已经解决了,

应该这样做:

String stype = fld.getType().getName();

I guess I solved it,

Should have done this:

String stype = fld.getType().getName();
奢华的一滴泪 2025-01-03 16:37:36

我通过调用它获得了字段的类名
f.getDeclaringClass().getSimpleName()

I got the class name of a field by calling this
f.getDeclaringClass().getSimpleName()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文