简体   繁体   English

在java中使用Reflection API

[英]Using Reflection API in java

I have an Operator interface for handling math operator that has two method like so: 我有一个用于处理数学运算符的Operator接口,它有两个方法,如下所示:

public interface Operator
{
  double calculate(double firstNumber,double secondNumber);
  char getSign();
}

for each operator I have a class that implement Operator interface like so: 对于每个运算符,我有一个实现Operator接口的类,如下所示:

public class Plus implements Operator
{   
  public double calculate(double firstNumber,double secondNumber)
  {
     return firstNumber + secondNumber;
  }
  public char getSign()
  {
     return '+';
  }
}

And so on... In this code I use Reflections : 等等......在这段代码中我使用了Reflections:

Reflections reflections = new Reflections("mypackage");
Set<Class<? extends Operator>> classes = reflections.getSubTypesOf(Operator.class);

Reflections is not the part of java Reflection API.I should just use java Reflection capability. 反射不是java Reflection API的一部分。我应该只使用java Reflection功能。 Can anyone help me to change this code that only use java Reflection API? 任何人都可以帮我改变只使用java Reflection API的代码吗?

Instead of using the Reflections API, you can 您可以使用Reflections API,而不是使用Reflections API

  • search your class path for directories and JARs 在类路径中搜索目录和JAR
    • for the directories look for each class file 为目录查找每个类文件
    • for the JAR scan through the files for each class file 用于JAR扫描每个类文件的文件
  • read the byte code of the class file with a library like ASM. 使用像ASM这样的库读取类文件的字节代码。
    • if the class implement your interface add it 如果类实现你的接口添加它
    • otherwise check all the super classes and interfaces of the class to see if they implement the interface. 否则检查类的所有超类和接口,看看它们是否实现了接口。

The reason you have to read the byte code is you want to avoid loading all the classes just to see the inheritance hierarchy, esp as some of the classes might not load or could take a long time. 您必须阅读字节代码的原因是您希望避免加载所有类只是为了查看继承层次结构,特别是因为某些类可能无法加载或可能需要很长时间。

Needless to say, using a library which does this for you is easier. 毋庸置疑,使用为您执行此操作的库更容易。 If youw ant to write this yourself I suggest you read the source of the Reflections API to see how it does it. 如果你自己写这篇文章,我建议你阅读Reflections API的来源,看看它是如何做到的。


A simpler solution is to use an enum 一个更简单的解决方案是使用enum

enum Operators implement Operator {
    PLUS {
        public double calculate(double x, double y) {
           return x + y;
        }
        public char getSign() {
           return '+';
        }
    },
    MINUS {
        public double calculate(double x, double y) {
           return x - y;
        }
        public char getSign() {
           return '-';
        }
    },
    TIMES {
        public double calculate(double x, double y) {
           return x * y;
        }
        public char getSign() {
           return '*';
        }
    },
    DIVIDE {
        public double calculate(double x, double y) {
           return x / y;
        }
        public char getSign() {
           return '/';
        }
    }
}

To get all the operators you can use 获得您可以使用的所有操作员

Operator[] operators = Operators.values();

You cannot scan the class path for the sub types. 您无法扫描子类型的类路径。 If you can correlate, even for JPA you would have to specify the entity names as part of configuration. 如果您可以关联,即使对于JPA,您也必须将实体名称指定为配置的一部分。 Use a similar approach to specify the list of classes you like to scan through and check if the instanceof . 使用类似的方法指定要扫描的类列表,并检查instanceof Reflections API only can help with you in that case if not. 如果没有,Reflections API只能帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM