简体   繁体   English

带注释的方法之后,可以使用注释来调用方法吗?

[英]Can I use annotation to call method after the annotated one?

I am wondering if I can use custom annotation to call some method right after annotated one. 我想知道是否可以在使用注释后立即使用自定义注释来调用某些方法。 For example I have a class that holds some settings that can also notify objects that something has changed (for example user changed something in settings panel). 例如,我有一个包含一些设置的类,这些设置还可以通知对象某些更改(例如,用户在“设置”面板中更改了某些内容)。 Not all listeners are interested in all types of events, so MyEvent is enum. 并非所有的侦听器都对所有类型的事件都感兴趣,因此MyEvent是枚举。 Now I have structure like this: 现在我有这样的结构:

class Settings
{
    private ArrayList<Listeners> listeners;

    private void notifyListeners(MyEvent e)
    {
        // notify all listeners that something was changed
    }

    public void setSomeOption(int value)
    {
        // validate parameter, store it etc.
       notifyListeners(MyEvent.SOME_INTEGER_SETTING_CHANGED);
    }
}

Of course listening object has to check type of event and ignore it or perform some action, but it is not the case here. 当然,侦听对象必须检查事件的类型并忽略它或执行某些操作,但是这里不是这种情况。

I am interested if I can achieve this with annotations, like this 我是否可以通过注释来实现这一点,例如

@NotifyAnnotation(MyEvent.SOME_INTEGER_SETTING_CHANGED)
public void setSomeOption(int value)
{
    // validate parameter, store it etc.
    // NO NEED TO CALL NOTIFY HERE - WILL BE HANDLED BY ANNOTATION
}

In JUnit for example, we have @Before or @After annotations, and I am wondering if JUnit has own annotations parser that handles method annotated this way, or this kind of behavior can be done simpler, since annotations can be @Retention(value=RUNTIME) . 例如,在JUnit中,我们具有@Before@After批注,而我想知道JUnit是否具有自己的批注解析器来处理以这种方式批注的方法,或者这种行为可以更简单地完成,因为批注可以为@Retention(value=RUNTIME)

I know that in this example it might look over-complicated and calling notifyListeners() is much simper, but I wan't to know if annotation can be used the way I described, and if yes, can i get some tips? 我知道在此示例中,它看起来可能过于复杂,并且调用notifyListeners()非常简单,但是我不知道是否可以按照我的描述使用注释,如果可以,可以得到一些提示吗? I don't expect ready solution, just a hint if this is possible and what should I take in consideration. 我不希望有现成的解决方案,只是在可能的情况下提示我,应该考虑什么。

In annotations you need a class that checks for it. 在注释中,您需要一个检查它的类。 they don't work on themselves. 他们不靠自己工作。 The way systems check for them are with reflection. 系统通过反射来检查它们的方式。

Annotation<NotifyAnnotation> a = method.getAnnotation();

And explicitly call their methods 并显式调用其方法

a.notifyListeners(a.evt);

I can't see any advantage with your case. 我看不出你的案子有什么好处。 but I see full of disadvantages. 但是我看到很多弊端。 They should not be used in actual coding, just for test systems or similar scenarios, where an external system has control on your class. 不应将它们用于实际编码中,而仅用于测试系统或类似情况(其中外部系统可以控制您的班级)。

yes, you can do it but you have to use a framework or write one by yourself. 是的,您可以这样做,但是您必须使用一个框架或自己编写一个框架。 you can use for example spring aspects and @After advice (or any other proxy mechanism). 您可以使用例如spring方面和@After建议 (或任何其他代理机制)。 you can also use full aspectj for this. 您也可以为此使用完整的AspectJ another option is to write it by yourself using reflection api. 另一种选择是使用反射API自己编写。 in last case you will need some kind of inversion of control - some mechanism that will launch your method and then the other method 在最后一种情况下,您将需要某种控制方式的反转-一种将启动您的方法的机制,然后是另一种方法

It could be do that using bytecode manipulation (JAssist, Asm, Java Rocks ...). 使用字节码操作(JAssist,Asm,Java Rocks ...)可以做到这一点。 All the classes would be instantiated thru a Factory that would identify annotated methods and would inject in the first line of this method a call to the method specified in its annotation. 所有类都将通过Factory实例化,该Factory将标识带注释的方法,并将在该方法的第一行中插入对其注释中指定的方法的调用。

暂无
暂无

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

相关问题 我可以获取方法调用的带注释参数的值吗? - Can I get the value of an annotated parameter of a method call? 问题 Dagger 不能在没有 @Provides-annotated 方法的情况下提供,当我使用 @Named 注释时 - Problem Dagger Cannot be provided without an @Provides-annotated method, When I use @Named annotation 如何在 Java 11 的注释处理器中获取带注释的方法参数的名称 - How can I get the names of annotated method parameters in an Annotation Processor in Java 11 AspectJ-尝试包装使用一个注释而不是另一个注释进行注释的方法 - AspectJ - Trying to wrap a method that is annotated with one annotation, but not an other 我可以在方法体内使用注释吗? - Can I use an annotation inside a method body? 我可以将saveOrUpdate方法与DynamicUpdate批注一起使用吗? - Can I use saveOrUpdate method with DynamicUpdate annotation? 实现注释处理器,仅当使用注释注释变量时才允许调用方法(如 Rust mut) - Implement annotation processor that allows to call method only if variable annotated with annotation (like Rust mut) 用注解注解的方法的方面,用其他注解注解 - Aspect for method annotated with annotation which are annotated with another annotation 如何在带注释的方法的每个方法调用上执行某些操作? - How do I perform something on each method call of an annotated method? 我可以在带有@Value的资源中使用占位符吗? - Can I use placeholders in an @Value annotated Resource?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM