熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java反射機制獲取內部類的靜態成員

2022-06-13   來源: 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;


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26078.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.