简体   繁体   中英

Using Reflection API in java

I have an Operator interface for handling math operator that has two method like so:

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

for each operator I have a class that implement Operator interface like so:

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 = 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. Can anyone help me to change this code that only use java Reflection API?

Instead of using the Reflections API, you can

  • search your class path for directories and JARs
    • for the directories look for each class file
    • for the JAR scan through the files for each class file
  • read the byte code of the class file with a library like 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.


A simpler solution is to use an 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. Use a similar approach to specify the list of classes you like to scan through and check if the instanceof . Reflections API only can help with you in that case if not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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