java

位置:IT落伍者 >> java >> 浏览文章

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


发布日期:2020年05月19日
 
Java反射机制获取内部类的静态成员

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

引用

Nested Class Summary

static class FieldIndex

Specifies whether and how a field should be indexed

static class FieldStore

Specifies whether and how a field should be stored

static class FieldTermVector

Specifies whether and how a field should have term vectors

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

引用

Field Summary

static FieldIndex ANALYZED

Index the tokens produced by running the fields value through an Analyzer

static FieldIndex ANALYZED_NO_NORMS

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

static FieldIndex NO

Do not index the field value

static FieldIndex NO_NORMS

Deprecated This has been renamed to NOT_ANALYZED_NO_NORMS

static FieldIndex NOT_ANALYZED

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

static FieldIndex NOT_ANALYZED_NO_NORMS

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

static FieldIndex TOKENIZED

Deprecated this has been renamed to ANALYZED

static FieldIndex UN_TOKENIZED

Deprecated This has been renamed to NOT_ANALYZED

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

最后采用了如下方法

//运行时调用FiledIndex类型

Java代码

Class<?> cls = orgapachelucenedocumentFieldIndexclass;

javalangreflectField indexDeclareField = clsgetDeclaredField(fieldgetIndex());

Object indexDeclareFieldType = indexDeclareFieldget(cls);

orgapachelucenedocumentFieldIndex filedIndex = (orgapachelucenedocumentFieldIndex)indexDeclareFieldType;

//运行时调用FieldStore类型

Class<?> clsStore = orgapachelucenedocumentFieldIndexclass;

javalangreflectField storeDeclareField = clsgetDeclaredField(fieldgetIndex());

Object indexStoreDeclareField = indexDeclareFieldget(cls);

orgapachelucenedocumentFieldStore filedStore = (orgapachelucenedocumentFieldStore)indexStoreDeclareField;

上一篇:Java中获取键盘输入值的三种方法

下一篇:使用JavaMail 实现邮件发送