Modify parseInt param before execution with @AspectJ

Isaachjk

I'm using spring boot 3.1.0 with org.springframework.boot:spring-boot-starter-aop

I can't make the aroundParseInt aspect to trigger.

I do another aspect test (test1) and this one is triggering.

What I trying to making is to intercept the param of the parseInt and modify it (needed).

import static java.lang.System.currentTimeMillis;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
public class TransformIntegerAspect {

    // THIS test aspect WORKS
    @Around("execution(* *(..)) && within(bknd.Siam.Dao.cobros.D_Cobros_Admin)")
    public Object test1(ProceedingJoinPoint thisJoinPoint) throws Throwable {
        long startTime = currentTimeMillis();
        Object result = thisJoinPoint.proceed();
        System.out.println(thisJoinPoint + " -> " + (currentTimeMillis() - startTime) + " ms");
        LoggerManager.log("ISAAC measureExecutionTime");

        return result;
    }

    //this dont work
    @Around("execution(int java.lang.Integer.parseInt(String))")
    public Object aroundParseInt(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        String param = (String) args[0];

        // Modify the parameter before the call to Integer.parseInt
        String modifiedParam = modifyParam(param);

        args[0] = modifiedParam;

        Object result = joinPoint.proceed(args);

        return result;
    }

    private String modifyParam(String param) {
        // Your custom logic to modify the parameter
        // Return the modified parameter\
        return param;
    }
    

The config file also has the @EnableAspectJAutoProxy(proxyTargetClass=true) and a Bean

    @Bean
    public TransformIntegerAspect transformParseIntAspect() {
        return new TransformIntegerAspect();
    }

I have some google code and cant make it works.

Also some people saids is possible and other saids is not.

"If they are made by 3rd party libraries, you can still use binary weaving, creating new versions of the 3rd party class files and creating replacement JARs for them. As an alternative, you can use LTW (load-time weaving) and weave them during class-loading." (Spring AOP with aspectj @annotation).

Im expecting to find if is really possible or not and a alternative way without making a utility method.

kriegaex

Spring AOP can only intercept methods of Spring beans/components, but you are trying to intercept a JDK method. This is well-documented and has been asked here dozens of times before. Please do some research and read some documentation next time.

Native AspectJ can intercept any method in any class it is woven into, but with JDK classes this can also be a problem when using load-time weaving, because many (not all) JDK classes are loaded before the AspectJ weaving agent, and then it is too late for AspectJ to modify them. An indirect way is to weave the calling code, not the class containing and executing the method. You would use a call rather than an execution pointcut in that case. Please note that call is unavailable in Spring AOP, which is reason #2 to use native AspectJ. For example:

@Around("call(int java.lang.Integer.parseInt(String))")

In your case, the target method is static, which is reason #3 why you need native AspectJ, because again Spring AOP cannot intercept static methods.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related