【问题标题】:How to avoid reflection using enums如何使用枚举避免反射
【发布时间】:2019-10-29 04:09:34
【问题描述】:

我最近问了一个关于我使用反射的一些代码的问题,一位想帮助我解决这个问题的人提到我不应该像这样使用反射,并且有更好的方法。

所以我有一个在外部系统中搜索的接口:

public interface ReferenceController {

  public Map<String, ReferenceElement> searchElements(String searchField, List<String> searchItems, SystemStage systemStage) throws Exception;

  public String getStateMapping(String value);

  public Boolean isAvailable(SystemStage systemStage) throws Exception;
}

我有一个 ENUM,我在其中声明了我拥有哪些外部系统以及它们的类是如何命名的,它使用了这个接口。因此,如果任何其他程序员想要实现一个新的外部系统,他只需要填充接口并将两个值放入这个 ENUM 和 tada 它应该可以工作。

所以我使用反射的部分是

public static void performSingleSearch(ReferenceSystem referenceSystem, String searchField, List<String> searchValues, SystemStage systemStage) throws Exception {

    if(!isAvailable(referenceSystem, systemStage)) return;
    Map<String, ReferenceElement> result = new HashMap<>();
    try {
        Class<?> classTemp = Class.forName(referenceSystem.getClassname());
        Method method = classTemp.getMethod("searchElements", String.class , List.class, SystemStage.class);
        result = (Map<String, ReferenceElement>) method.invoke(classTemp.newInstance(), searchField, searchValues, systemStage);
    } catch (Exception e) { 
        return;
    }
    if(result != null) orderResults(result, referenceSystem);
}

在ENUM中有一个函数getClassname,它用fqcn回答。 枚举看起来像这样:

public enum ReferenceSystem {
    UCMDB    (refSystems.ucmdb.UcmdbFunctions.class),
    PROIPS   (refSystems.proips.ProIPSFunctions.class),
    KV       (refSystems.kv.KvFunctions.class),
    FISERVICE(refSystems.fiservice.FiServiceFunctions.class),
    COMMAND  (refSystems.command.CommandFunctions.class),
    FII          (refSystems.fii.FiiFunctions.class);

    private Class<?> clazz;

    private ReferenceSystem(Class<?> controllerClass) {
        this.clazz = controllerClass;
    }

    public String displayName() {
        ResourceBundle bundle = ResourceBundle.getBundle("EnumI18n", Locale.GERMAN);
        return bundle.getString(toString()); 
    }

    public String localizedDisplayName(Locale locale) {
        ResourceBundle bundle = ResourceBundle.getBundle("EnumI18n", locale);
        return bundle.getString(toString()); 
    }

    public Class<?> getClassname() { return clazz; }
}

我已经根据@jhamon 的回答修改了它。

但我在尝试时遇到错误

classTemp.newInstance().searchElemets(...)

因为它不知道 searchElemts()。

所以这里的另一个用户说有可能将接口实现到枚举中,然后我不必反思。 谁能告诉我怎么做,因为我不知道,也不知道在哪里或搜索什么。 谢谢

【问题讨论】:

  • 在我看来,您可以完全避免反射,只需将private Class&lt;?&gt; clazz; 更改为private Class&lt;? extends ReferenceController&gt; clazz;。那么每个实例在编译时都会有一个 searchElements 方法。

标签: java reflection enums interface


【解决方案1】:

您的所有搜索引擎似乎都有一个通用方法searchElements并且它在界面中定义

知道了,为什么不直接调用这个方法,而不是先寻找它。 -> 没有更多的反思来找到方法。

public interface ReferenceController {

    public Map<String, ReferenceElement> searchElements(String searchField, List<String> searchItems, SystemStage systemStage) throws Exception;

    public String getStateMapping(String value);

    public Boolean isAvailable(SystemStage systemStage) throws Exception;
}

不是将类名作为字符串存储在枚举中,而是存储 .class -> 不再通过反射来查找类。

public static void performSingleSearch(ReferenceSystem referenceSystem, String searchField, List<String> searchValues, SystemStage systemStage) throws Exception {

    if(!isAvailable(referenceSystem, systemStage)) return;
    Map<String, ReferenceElement> result = new HashMap<>();
    try {
        Class<?> classTemp = referenceSystem.getClazz();
        result = ((ReferenceController) classTemp.newInstance()).searchElements(searchField, searchValues, systemStage);
    } catch (Exception e) { 
        return;
    }
    if(result != null) orderResults(result, referenceSystem);
}

【讨论】:

  • 嗨,我在尝试调用 searchElements() 时遇到错误,请查看我的编辑
  • 将实例转换为ReferenceController 。我编辑了我的答案
猜你喜欢
  • 2011-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多