Abderrazak BOUADMA
Abderrazak BOUADMA

Reputation: 1606

How to access to the returning Object instance of an advised method

I'm using spring AOP to advice my services methods and especially ones returning an object, I want to have access to that object during advice processing.

My configuration is working fine, no problems with that.

Here's the signature of the adviced method, method returns a new instance based on data within method argument, so argument is unusable

@Traceable(ETraceableMessages.SAUVER_APPORTEUR)
public ElementNiveauUn save(ElementNiveauUn apporteur) throws ATPBusinessException {
    String identifiant = instanceService.sauverInstance(null, apporteur);
    List<String> extensions = new ArrayList<String>();
    extensions.add(ELEMENTSCONTENUS);
    extensions.add(TYPEELEMENT);
    extensions.add(VERSIONING);
    extensions.add(PARAMETRAGES);
    extensions.add(PARAMETRAGES + "." + PARAMETRES);
    return (ElementNiveauUn ) instanceService.lireInstanceParId(identifiant, extensions.toArray(new String[]{}));
}

Here's what I'm wondering to do

@Around(value = "execution(elementNiveauUn fr.generali.nova.atp.service.metier.impl.*.*(..)) && @annotation(traceable) && args(element)", argNames = "element,traceable")
public void serviceLayerTraceAdviceBasedElementInstanceAfter2(final ProceedingJoinPoint pjp,
                final ElementNiveauUn element, final Traceable traceable) throws SecurityException,
                NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {

    // current user
    String currentUserId = findCurrentUserId();

    // wether user is found or not
    boolean isUserFound = StringUtils.isBlank(currentUserId);

    // retrieve the oid of the returning type
    MethodSignature signature = (MethodSignature ) pjp.getSignature();
    Class<ElementNiveauUn> returnType = signature.getReturnType();

    Method[] methods = returnType.getMethods();
    Method method = returnType.getMethod("getOid", (Class< ? >[] ) null);
    String oid = (String ) method.invoke(null, (Object[] ) null);

    // log to database
    simpleTraceService.trace(element.getOid(), element.getVersioning().toString(), traceable.value(),
                    isUserFound ? UTILISATEUR_NON_TROUVE : currentUserId);
}

My problem is that this line of code

Class<ElementNiveauUn> returnType = signature.getReturnType();

permits me to have access to the returning type not to the instance

Upvotes: 2

Views: 11136

Answers (1)

axtavt
axtavt

Reputation: 242686

Since you have an around advice, you need to call pjp.proceed() in order to execute a method being adviced, and return its value:

@Around(...)
public Object serviceLayerTraceAdviceBasedElementInstanceAfter2(final ProceedingJoinPoint pjp,
                    final ElementNiveauUn element, final Traceable traceable) throws SecurityException,
                    NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
    ...
    Object result = pjp.proceed();
    ...
    return result;
}

Upvotes: 9

Related Questions