Java反射机制获取内部类的静态成员

Java反射机制获取内部类的静态成员,第1张

最近需要些一个可配置的索引构建程序 需要在运行时调用Lucene包的如下类及其成员

引用

Nested Class Summary

static class Field Index

Specifies whether and how a field should be indexed

static class Field Store

Specifies whether and how a field should be stored

static class Field TermVector

Specifies whether and how a field should have term vectors

lucene api中称之为Nested Class 意为嵌套类 而嵌套类内部的File Index的成员又是静态成员

引用

Field Summary

static Field Index ANALYZED

Index the tokens produced by running the field s value through an Analyzer

static Field Index ANALYZED_NO_NORMS

Expert: Index the tokens produced by running the field s value through an Analyzer and also separately disable the storing of norms

static Field Index NO

Do not index the field value

static Field Index NO_NORMS

Deprecated This has been renamed to NOT_ANALYZED_NO_NORMS

static Field Index NOT_ANALYZED

Index the field s value without using an Analyzer so it can be searched

static Field Index NOT_ANALYZED_NO_NORMS

Expert: Index the field s value without an Analyzer and also disable the storing of norms

static Field Index TOKENIZED

Deprecated this has been renamed to ANALYZED

static Field Index UN_TOKENIZED

Deprecated This has been renamed to NOT_ANALYZED

一个棘手的问题 如果获得这些内部静态成员?

最后采用了如下方法

//运行时调用Filed Index类型

Java代码

Class<> cls = apache lucene document Field Index class;

java lang reflect Field indexDeclareField = cls getDeclaredField(field getIndex());

Object indexDeclareFieldType = indexDeclareField get(cls);

apache lucene document Field Index filedIndex = ( apache lucene document Field Index)indexDeclareFieldType;

//运行时调用Field Store类型

Class<> clsStore = apache lucene document Field Index class;

java lang reflect Field storeDeclareField = cls getDeclaredField(field getIndex());

Object indexStoreDeclareField = indexDeclareField get(cls);

lishixinzhi/Article/program/Java/hx/201311/26078

class b {

 private String aa = "hello";

 private String bb = "world";

 private String cc = "xxx!";

}

public class a {

 public static void main(String[] args) throws ClassNotFoundException,

   IllegalArgumentException, IllegalAccessException,

   InstantiationException {

  Class c = ClassforName("b");

  Field[] fs = cgetDeclaredFields();

  Map map = new HashMap();

  for (int i = 0; i < fslength; i++) {

   Field f = fs[i];

   fsetAccessible(true);

   mapput(fgetName(), fget(cnewInstance()));

  }

  Systemoutprintln(map);

 }

}

结果:

以上就是关于Java反射机制获取内部类的静态成员全部的内容,包括:Java反射机制获取内部类的静态成员、java反射获取未知字段名的私有字段、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9777813.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-01
下一篇2023-05-01

发表评论

登录后才能评论

评论列表(0条)

    保存