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

前端 未结 2 1139
长发绾君心
长发绾君心 2021-01-26 20:20

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 thi

相关标签:
2条回答
  • 2021-01-26 20:37

    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!

    0 讨论(0)
  • 2021-01-26 20:41

    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.

    0 讨论(0)
提交回复
热议问题