简体   繁体   中英

How to retrieve an object from a method that is invoked using Java Reflection API?

I have a class:

package builderpattern;

import builderpattern.parts.Brakes;
import builderpattern.parts.Gearbox;
import builderpattern.parts.Motor;
import builderpattern.parts.Windshield;

public class Car extends VehicleClass implements IVehicle{

    @Override
    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    private Motor motor;
    private Gearbox gearbox;
    private Windshield windshield;
    private Brakes brakes;

    public Brakes getBrakes() {
        return brakes;
    }

    public void setBrakes(Brakes brakes) {
        this.brakes = brakes;
    }

    public Motor getMotor() {
        return motor;
    }

    public void setMotor(Motor motor) {
        this.motor = motor;
    }

    public Gearbox getGearbox() {
        return gearbox;
    }

    public void setGearbox(Gearbox gearbox) {
        this.gearbox = gearbox;
    }

    public Windshield getWindshield() {
        return windshield;
    }

    public void setWindshield(Windshield windshield) {
        this.windshield = windshield;
    }

}

This class has a private field of the class Motor:

package builderpattern.parts;

public class Motor {

    private double horsepower;
    private String name;
    private int numberOfCylinders;

    public double getHorsepower() {
        return horsepower;
    }

    public void setHorsepower(double horsepower) {
        this.horsepower = horsepower;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNumberOfCylinders() {
        return numberOfCylinders;
    }

    public void setNumberOfCylinders(int numberOfCylinders) {
        this.numberOfCylinders = numberOfCylinders;
    }

    public Motor(double horsepower, String name, int numberOfCylinders) {
        super();
        this.horsepower = horsepower;
        this.name = name;
        this.numberOfCylinders = numberOfCylinders;
    }   
}

So what I am trying to do is to invoke the getMotor() method from the class Car using the invoke method from the Java Reflection API, but I want to get the Motor object so I can invoke the getName() object from the Motor class. Can this be done? Maybe not by retrieving the Motor object, but somehow access the getName() method. Can someone help me with this? Thanks in advance. Here is what I have done so far:

Car c = new Car();
Class cClass = c.getClass();
cClass.getDeclaredMethod("getMotor").invoke(c, null);

This only returns a Method object with metadata about the method.

You almost answered your own question. To get the name of the Moter class all you need to do is something like this:

Car c = new Car();
Class cClass = c.getClass();
Object uncastedMotor = cClass.getDeclaredMethod("getMotor").invoke(c, null);
String motorName = ((Motor)uncastedMotor).getName();

Keep in mind that the invoke method can return an Object in case the method is not void.

Also if you own the classes Car and VehicleClass then a better way would be to create a new interface declaring all functions specific to the Car or VehicleClass and add the getMotor to that. You can then verify if the Object is of this interface with instanceof and cast it. This is much cleaner code and ensures the code keeps working if the classes get modified.

Using reflection is generally speaking the last resort.

Just doing:

Motor m = (Motor) cClass.getDeclaredMethod("getMotor").invoke(c);

should give you the motor object.

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