How to write an annotation/aspect to not enter a method but return null if a given condition is false?

眉间皱痕 提交于 2019-12-20 05:22:33

问题


I currently have a requirement where I need to return null from 100s of methods if a given condition is false. I was thinking of using Java Annotations or Spring Aspects for this so that I don't have to write that if-else block of code everywhere. Any idea as to how we can do this using Java Annotations or Spring Aspects?

Any pointers might be helpful.


回答1:


If I get you correctly, Spring @Conditional annotation is what you want. You create some public class that implements Spring's Condition interface:

public class Test implements Condition {
...
}

Then you use the forementioned annotation with the parameter which takes as an argument that public class.

@Conditional(Test.class)
public Object someMethod(boolean context){
/*and so do some logics; if the value of 'context' variable is false, you can return 
null; otherwise, just return like this*/
return new someMethodImpl1();
}

Hope I helped. And I'm glad of any kind of corrections. Cheers!




回答2:


Without spring you could use pure AspectJ:

Demo.java : example of the method we want to modify

public class Demo {
    public String boom(String base) {
        return base;
    }
}

DemoAspect.aj : configuration file. Getting started. In AspectJ, pointcuts pick out certain join points in the program flow. To actually implement crosscutting behavior, we use advice. Advice brings together a pointcut (to pick out join points) and a body of code (to run at each of those join points): before, around, after...

public aspect DemoAspect {
    pointcut callBoom(String base, Demo demo) : 
            call(String Demo.boom(String)) && args(base) && target(demo);

    String around(String base, Demo demo) : callBoom(base, demo){
        if ("detonate".equals(base)) {
            return "boom!";
        } else {
            return proceed(base, demo);
        }
    }
}

DemoTest.java

public class DemoTest {
    @Test
    void name() {
        Demo demo = new Demo();
        assertEquals("boom!", demo.boom("detonate"));
        assertEquals("fake", demo.boom("fake"));
    }
}

Add dependencies into pom.xml

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.13</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.13</version>
    </dependency>

And plugin, pay attention to the versions. For example 1.11 plugin expects 1.8.13 libraries.

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>aspectj-maven-plugin</artifactId>
    <version>1.11</version>
    <configuration>
        <complianceLevel>1.8</complianceLevel>
        <source>1.8</source>
        <target>1.8</target>
        <showWeaveInfo>true</showWeaveInfo>
        <verbose>true</verbose>
        <Xlint>ignore</Xlint>
        <encoding>UTF-8 </encoding>
    </configuration>
    <executions>
        <execution>
            <goals>
                <!-- use this goal to weave all your main classes -->
                <goal>compile</goal>
                <!-- use this goal to weave all your test classes -->
                <goal>test-compile</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Good example with more details, Baeldung. Official documentation.



来源:https://stackoverflow.com/questions/53788769/how-to-write-an-annotation-aspect-to-not-enter-a-method-but-return-null-if-a-giv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!