【问题标题】:Add method at runtime that depends on methods with annotations in same class在运行时添加方法,该方法依赖于同一类中带有注释的方法
【发布时间】:2013-03-24 07:17:42
【问题描述】:

目前我的监听器需要一个 switch 树来调用内部方法。

public class Car{


   public void listener(String e){
      if(e.equals("Honk"))
        this.blowHorn();
   }

   @Honk
   private void blowHorn(){...}

}

是否可以利用反射和方法注释,以便可以在运行时生成侦听器方法?它将根据输入是否等于方法注释进行切换。这比使用普通反射更好,因为它减少了开销。

【问题讨论】:

  • 你想完成什么?
  • 运行时不会生成该方法。但是你可以实现它,让它动态调用一个方法,是的。你试过什么?听起来并不难。
  • 我想避免在运行时使用反射来减少开销。理想情况下,它只需要反射一次

标签: java reflection annotations runtime


【解决方案1】:

************************通过重新选择回答****************************

首先你要这样声明你的新注解:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CarListener{
    public String carAction();
}

因此,在您的 Car 类中,您将拥有:

public class Car {

    //Here you´ll be looking at all the methods  you have in the class Car (I´d advice to 
    // put them in another class, so as to keep it clean, I didn´t do it here in order to
    // explain it better. These methods have the corresponding annotation you created

    public void listener(String e) { 
        Method[] methods = Car.class.getMethods();
            for(Method method:methods) {

        //Now that you have all the methods all you need is to figure which one you want
        // you´ll do that according to the "e" string, which represents the car action (for 
        // example "Honk") I´d also advice you to rename that too.

            if(rightMethod(method, e))

                //Now that you have found it, then you invoke the method, "call it"
                // which is what you wre doing in the previos code with "this.blowHorn()"

                return invokeMethod(method);
        }
        //This will help you in case you did NOT find the correct method, it´s just help 
        // if you don´t put it in it won´t break your code
        // fun fact about RuntimExceptions: you don´t have to declare them, meaning
        // you dont have to add them as "throws" or catch

        throw new RuntimeException("No listener found for car Action"+e);
    }

    private boolean rightMethod(Method method, String expression) {

    //First if asks if the method found has an annoation present, and if it does
    // then it asks if it corresponds to the annoation you created

        if (method.isAnnotationPresent(NewAnnotationInterfaceName.class))

    //if the method in fact has the annotation created all you are doing is asking what
    // carAction is associated to that method, you do that with the .carAction()

            return method.getAnnotation(NewAnnotationInterfaceName.class).carAction().equals(expression);
        return false;
    }


    //Now all you have to do is invoke it :) This just follows how to invoke a method
    // I won´t explain it

    private void  invokeMethod(Method method) {
        try {
            return method.invoke(Car.class.newInstance(), new Object[]{});
        } catch (InstantiationException | IllegalAccessException
                 | InvocationTargetException | IllegalArgumentException ex) {
            Logger.getLogger(Car.class.getName()).log(Level.SEVERE, null, ex);
        }
        throw new RuntimeException("Could not invoke method");
    }


    @CarListener(carAction= "Honk")
    public void blowHorn() {  
        ...
    }

     @CarListener(carAction= "SomethingElse")
    public void someOtherAction() {  
        ...
    }

}

希望有帮助!

*********************用Hashmap和命令设计回答************************ ***

public abstract class CarAction {

    public abstract void execute(Car car){};

}

public class HonkAction extends CarAction{

    @Override
    public void execute(Car car) {
           car.blowHorn();
    }    

}

public class Car {

    private HashMap<String, CarAction> carActions;

    public Car() {
        ...
        initializeCarActions();
    }

    public void initializeCarActions() {
        this.carActions = new HashMap<>();
        this.carActions.put("Honk", new HonkAction());
        ...
    }

    public void listener(String e) {
        CarAction action = this.carActions.get(e);
        if(action!=null) action.execute(this);
    }

}

如果你要使用这种方式,我建议让某人注入 HashMap,这样 Car 就不必依赖 CarActions(只是抽象类),要么使用一个类,要么使用 Guice。此外,如果所有 carAction 只需要“Car”即可执行,这也有效。

祝你好运!

【讨论】:

  • ¿谁能澄清我的问题,为什么我的回答被否决了?如果我错了,我愿意接受建设性的批评
【解决方案2】:
public listener(String e){
      if(e.equals("Honk"))
        this.blowHorn();
   }

不是正确的 Java 方法签名。

同样,如果使用反射,则不需要“动态”生成监听器方法

【讨论】:

  • 我想减少开销
  • 然后编写一个代码生成器来为你做这件事。是的,它也必须使用反射。您可以在构建过程中调用它。
【解决方案3】:

如果你真的想用注释来做这件事,你可以用这样的东西来做:

public void listener(String e) {
    for (Method m : this.getClass().getDeclaredMethods()) {
        for (Annotation a : m.getDeclaredAnnotations()) {
            if (a.getClass().getSimpleName().equals(e)) {
                m.setAccessible(true); // Need to do this so we can run private methods
                m.invoke(this);
            }
        }
    }
}

这将使用与给定名称匹配的名称来调用每个方法。坚持这种通用设计,我建议进行两项核心改进:

  • 通过执行上述操作预构建注释方法映射,但将其存储在注释名称中 -> 方法哈希映射而不是调用它

  • 使用注解的实例而不是字符串(listener(Class&lt;? extends Annotation&gt; annotation) 而不是listener(String e)),这样您就可以使用 equals 将输入与方法上的注解进行比较,而不必提取名称并进行比较字符串,这看起来特别讨厌。

虽然这不是一个伟大的设计;更典型的是,您要么将它们硬核为一系列简单的 if 语句(如果有少量选项),要么如果有很多选项,您将使用命令模式之类的东西(将每个可调用方法封装到一个对象中) ),让每个命令在创建时为自己注册一个名称,然后将它们存储在一个映射中,然后您可以从中查找以找到相关的命令。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多