Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] How do we declare multilple class in @DeclareParents ?

I've downloaded eclipse Indigo and I've updated the adjt and I got the same error with a test project.

@DeclareParents(value="com.nested.web.controller.BaseController || com.nested.web.validator.impl.*",defaultImpl=ServantLogger.class)


Here is my AspectJ class below - Moreover I would like to have the Logger.getLogger(this.getClass() be attached to the correct class (the subclass) but the this.getClass() returns the AspectJ class. If you know a way to initiliaze the logger correctly let me know. If I do the same logging behavior class without annotation but with the native language, it works.

Thank you very much.


--------------------------------------------------------
          CODE WITH ANNOTATION STYLE
--------------------------------------------------------


package com.nested.aspects;

import org.apache.log4j.Logger;
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.DeclareParents;
import org.aspectj.lang.annotation.DeclareWarning;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class LoggingBehavior {

        @DeclareParents(value="com.nested.web.controller.BaseController || com.nested.web.validator.impl.*",defaultImpl=ServantLogger.class)
        private transient Loggable log;


        @Pointcut("execution(* com.nested.web..*Controller*.do*(..))")
        public void controllerLogger() {};

        @Pointcut("execution(public void com.nested.web..*Controller.initialize(..)) && !within(com.intact.crm.web..*Controller.initialize)")
        public void initializerLogger() {};

        @Pointcut("execution(* com.nested.web..*Controller.validate(..))")
        public void validateLogger() {};

        @Pointcut("execution(* com.nested.web.converter..*Converter.getAsObject(..))")
        public void converterLogger() {};

        @Pointcut("execution(* com.nested.web.validator..*Validator.isValid(..))")
        public void validatorLogger() {};

        @DeclareWarning("get(* java.lang.System.out) || get(* java.lang.System.err)")
        static final String systemOutMessage = "Please, if you use System.out, do not forget to remove this call in production or before checking in the code.";

        @DeclareWarning("call(* org.apache.log4j.Logger.get*(..)) && !within(com.nested.aspects.*)")
        static final String dependencyLoggerMessage = "Please, avoid to use LOG4J inside a class - Consider using the LoggingBehavior aspect in com.nested.aspects.";


        @Before(value = "controllerLogger() && this(log)")
        public void loggerController(JoinPoint jp, JoinPoint.StaticPart jps, Loggable log) {
                if (log.getLog().isDebugEnabled()) {
                        final String args = buildParameters(jp.getArgs());
                        log.getLog().debug(jps.getSignature().toShortString() + " - Entering..." + (args.isEmpty() ? "" : " - params: " + args));
                        log.getLog().debug(jps.getSignature().toShortString() + " - Object dump - " + jp.getTarget());
                }
        }

        @Before(value = "initializerLogger() && this(log)")
        public void loggerInitializer(JoinPoint jp, JoinPoint.StaticPart jps, Loggable log) {
                if (log.getLog().isDebugEnabled()) {
                        log.getLog().debug(jps.getSignature().toShortString() + " - Entering initializer... - " + buildParameters(jp.getArgs()));
                }
        }

        @Around(value= "(validateLogger() || validatorLogger()) && this(log)")
        public boolean loggerValidate(ProceedingJoinPoint pjp, JoinPoint jp, JoinPoint.StaticPart jps, Loggable log) throws Throwable {
                Boolean result = (Boolean)pjp.proceed();
                if (log.getLog().isDebugEnabled()) {
                        String param1 = (jp.getArgs() != null && jp.getArgs().length > 0) ? (" - param1: " + jp.getArgs()[0]) : "";
                        log.getLog().debug(jps.getSignature().toShortString() + " - VALIDATOR" + param1 + " - return: " + result);
                }
                return result;
        }

        @Around(value= "converterLogger() && this(log)")
        public Object loggerConverter(ProceedingJoinPoint pjp, JoinPoint jp, JoinPoint.StaticPart jps, Loggable log) throws Throwable {
                Object result = pjp.proceed();
                if (log.getLog().isDebugEnabled()) {
                        log.getLog().debug(jps.getSignature().toShortString() + " - CONVERTER - param3: " + jp.getArgs()[2] + " - return: " + result);
                }
                return result;
        }


        private String buildParameters(Object[] parameters) {
                StringBuilder sb = new StringBuilder(64);
                if (parameters == null) return null;
                        for(int i = 0; i< parameters.length; i++) {
                                sb.append("Param")
                                  .append(i+1)
                                  .append(" : ")
                                  .append("'")
                                  .append(parameters[i])
                                  .append("'");
                                if (i+1 < parameters.length) { sb.append(" - "); }
                        }
                return sb.toString();
        }

        /*
         * The interface and the implementation of Loggable class.
         */
        public interface Loggable {
                public Logger getLog();
        };

        public static class ServantLogger implements Loggable {
                private transient Logger log = Logger.getLogger(this.getClass());

                @Override
                public Logger getLog() {
                        return log;
                }
        }
}


--------------------------------------------------------
          CODE WITHOUT ANNOTATION STYLE
--------------------------------------------------------

public aspect LoggingBehavior1 {

        public interface Loggable { }
        private final transient Logger Loggable.log = Logger.getLogger(this.getClass());
        public Logger Loggable.getLog() {
                return log;
        }

        declare parents : com.nested.web.controller.BaseController implements Loggable;
        declare warning : dependencyLogger() : "Please, avoid to use LOG4J inside a class - Consider using the LoggingBehavior aspect in com.intact.crm.aspects.";
        declare warning : systemOutLogger() : "Please, if you use System.out, do not forget to remove this call in production or before checking in the code.";

        pointcut controllerLogger() : execution(* com.nested.web..*Controller*.do*(..));
        pointcut initializerLogger() : execution(public void com.nested.web..*Controller.initialize(..)) && !within(com.nested.web..*Controller.initialize);
        pointcut validateLogger() : execution(* com.nested.web..*Controller.validate(..));
        pointcut dependencyLogger() : call(* org.apache.log4j.Logger.get*(..)) && !within(com.nested.aspects.*);
        pointcut systemOutLogger() : get(* java.lang.System.out) || get(* java.lang.System.err);

        before(Loggable log) : controllerLogger() && this(log) {
                if (log.getLog().isDebugEnabled()) {
                        log.getLog().debug("Entering... - " + thisJoinPointStaticPart.getSignature().toShortString() + " - " + buildParameters(thisJoinPoint.getArgs()));
                        log.getLog().debug("Object dump - " + thisJoinPoint.getTarget());
                }
        }

        boolean around(Loggable log) : (validateLogger()) && this(log) {
                Boolean result = proceed(log);
                if (log.getLog().isDebugEnabled()) {
                        String param1 = (thisJoinPoint.getArgs() != null && thisJoinPoint.getArgs().length > 0) ? (" - param1: " + thisJoinPoint.getArgs()[0]) : "";
                        log.getLog().debug("VALIDATOR - " + thisJoinPointStaticPart.getSignature().toShortString() + param1 + " - return: " + result);
                }
                return result;
        }

        before(Loggable log) : initializerLogger() && this(log) {
                if (log.getLog().isDebugEnabled()) {
                        log.getLog().debug("Entering backing bean initializer..." + buildParameters(thisJoinPoint.getArgs()));
                }
        }

        private String buildParameters(Object[] parameters) {
                StringBuilder sb = new StringBuilder(64);
                if (parameters == null) return null;
                        for(int i = 0; i< parameters.length; i++) {
                                sb.append("Param")
                                  .append(i+1)
                                  .append(" : ")
                                  .append("'")
                                  .append(parameters[i])
                                  .append("'");
                                if (i+1 < parameters.length) { sb.append(" - "); }
                        }
                return sb.toString();
        }
}



De :        Andy Clement <andrew.clement@xxxxxxxxx>
A :        aspectj-users@xxxxxxxxxxx
Date :        2012-05-02 13:44
Objet :        Re: [aspectj-users] How do we declare multilple class in @DeclareParents ?
Envoyé par :        aspectj-users-bounces@xxxxxxxxxxx




Hi,

What version of AspectJ are you using?

I just tried this (basically the same as yours) on 1.7.0 dev builds
and it was OK:


===== 8<==== Code2.java ===
package com;
import org.aspectj.lang.annotation.*;


class B {}
class C {}

@Aspect
class MoodIndicator {
 interface Moody {
   String getMood();
 };

 static class MoodyImpl implements Moody {
   public String getMood() { return "happy"; }
 }

 @DeclareParents(value="com.B || com.C", defaultImpl = MoodyImpl.class)
 private Moody implementedInterface;
}
===== 8<==== Code2.java ===
ajc -1.5 Code2.java -showWeaveInfo
Extending interface set for type 'com.B' (Code2.java) to include
'com.MoodIndicator$Moody' (Code2.java)
               
Type 'com.B' (Code2.java) has intertyped method from
'com.MoodIndicator' (Code2.java:'java.lang.String
com.MoodIndicator$Moody.getMood()')
               
Extending interface set for type 'com.C' (Code2.java) to include
'com.MoodIndicator$Moody' (Code2.java)
               
Type 'com.C' (Code2.java) has intertyped method from
'com.MoodIndicator' (Code2.java:'java.lang.String
com.MoodIndicator$Moody.getMood()')

I got exceptions (unexpected) when using it on 1.6.12/1.7.0.M1.

Are you getting exceptions or is it just not working?

cheers,
Andy

On 2 May 2012 06:22, Jean Andre <Jean.Andre@xxxxxxxxxx> wrote:
> How do we declare multilple class in @DeclareParents ?
>
> This syntaxe does not work :  @DeclareParents(value="com.c1 ||
> com.c2",defaultImpl=Implclass.class)
>
> Thank you
>
>
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
>
https://dev.eclipse.org/mailman/listinfo/aspectj-users
>
_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users


Back to the top