Get Object Instantiation and Access Its Related Attributes and Methods with AspectJ

久未见 提交于 2019-12-13 21:23:58

问题


Can we get an object created or instantiated using AspectJ and access object's attributes and methods? I know with AspectJ we can get input parameters of the methods but will it be the same with the object creation?

For example, in the code it has this line

testing test = new testing();

And I would like to get the test object using AspectJ and access the method and variables of class testing(), like method aa().

Here is the code for testing class.

package testMaven;

public class testing {

    public int dd;


    public int getDd() {
        return dd;
    }


    public void setDd(int dd) {
        this.dd = dd;
    }


    public void aa(int a){
        System.out.println(a);
    }
}

Real Coding Problem

The above coding is only merely an example of the abstract problem I currently have the problem. Thanks to the answer below, it works in this example, but in the real project that I have it is not working. Basically the code as follow:

@After("execution(de.hpi.cloudraid.dto.control.storedObjectService.CreateFileFinalResponseDto.new(..))")
    public void constructorInvocation(JoinPoint joinPoint) throws Throwable {
        CreateFileFinalResponseDto instance = (CreateFileFinalResponseDto) joinPoint.getTarget();
        System.err.println("testing new object creation");
        System.err.println("id upload = " + instance.getFileObjectID());

    }

But the code is not working as it gives the warning of advice has not been applied (xlint : adviceDidNotMatch). CreateFileFinalResponseDto class is basically from different project called shared and I already include the shared project in the pom.xml and AspectJ dependencies and plugin as follow:

   <dependencies>
            <!-- Project -->
            <dependency>
                <groupId>${project.groupId}</groupId>
                <artifactId>shared</artifactId>
                <version>${project.server.version}</version>
            </dependency> 
    ...
    <!-- AspectJ -->
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjrt</artifactId>
                <version>${aspectj.version}</version>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjtools</artifactId>
                <version>${aspectj.version}</version>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjweaver</artifactId>
                <version>${aspectj.version}</version>
            </dependency>
...

     <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.8</version>
                <configuration>
                    <complianceLevel>${java.version}</complianceLevel>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                    <showWeaveInfo>true</showWeaveInfo>
                </configuration>
                <executions>
                    <execution>
                        <id>AspectJ-Classes</id>
                        <phase>process-classes</phase>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>AspectJ-Test-Classes</id>
                        <phase>process-test-classes</phase>
                        <goals>
                            <goal>test-compile</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjrt</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>org.aspectj</groupId>
                        <artifactId>aspectjtools</artifactId>
                        <version>${aspectj.version}</version>
                    </dependency>
                </dependencies>
            </plugin>

Any help is appreciated. Thanks


回答1:


To catch class instantiate one could use the following aspect:

@Aspect
public class Interceptor {
    /**
     * Matches constructor testMaven.testing.
     * <p/>
     * *NOTE:* This will only work when class compiled with aspectj.
     */
    @After("execution(* testMaven.testing.new(..))")
    public void constructorInvocation(JoinPoint joinPoint)
            throws Throwable {
        testing instance = (testing) joinPoint.getTarget();
        instance.aa(2);
    }
}

In case you want to catch the instantiate Before there won't be a instance of course.



来源:https://stackoverflow.com/questions/37563258/get-object-instantiation-and-access-its-related-attributes-and-methods-with-aspe

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