I have implemented this approach, but I have a problem with inheritance. Please, observe: =========================================================== public abstract aspect LoggerAspect { private static aspect LoggerHolderAspect pertypewithin(@LogMe *) { private Logger logger; public void initLogger() { logger = LogManager.getLogger(getWithinTypeName()); } public Logger getLogger() { return logger; } }
private interface ILoggable {}; declare parents: (@LogMe *) implements ILoggable; // ---------------------------------------------------------------------------------------- // These declarations are never used by the aspect itself. They are for the subject classes. public Logger ILoggable.getLogger() { return LoggerAspect.getLogger(this.getClass()); } public static Logger getLogger(@SuppressWarnings("rawtypes") Class clazz) { return LoggerHolderAspect.aspectOf(clazz).getLogger(); } // ---------------------------------------------------------------------------------------- private static Logger getLogger(JoinPoint.StaticPart jp) { return getLoggerHolder(jp).getLogger(); } private static LoggerHolderAspect getLoggerHolder(JoinPoint.StaticPart jp) { return LoggerHolderAspect.aspectOf(jp.getSignature().getDeclaringType()); } before(): staticinitialization(@LogMe *) { getLoggerHolder(thisJoinPointStaticPart).initLogger(); } ... } =========================================================== Now, I have two subject classes forming a hierarchy: =========================================================== @LogMe public class BaseDummyInterceptor implements IDummyInterceptor { @Override public void Intercept(JoinPoint jp) { getLogger().trace("BaseDummyInterceptor: " + jp); } } =========================================================== @LogMe public class DummyInterceptor extends BaseDummyInterceptor { @Override public void Intercept(JoinPoint jp) { getLogger().trace("DummyInterceptor: " + jp); super.Intercept(jp); } } =========================================================== Eclipse reports two markers for the DummyInterceptor, one of which is an error: =========================================================== Multiple markers at this line - The hierarchy of the type DummyInterceptor is inconsistent - advised by LoggerAspect.before(): <anonymous pointcut> =========================================================== Interestingly enough, the error marker does not appear in the Problems view and does not prevent the code from compiling and running. Anyway, when I run the application, I get a log like this: =========================================================== 2011-12-18 14:57:05,276 null[Dispatcher-0] DEBUG com.shunra.poc.handlers.UserHandler - getUser(id: 1) 2011-12-18 14:57:05,279 null[Dispatcher-0] DEBUG com.shunra.poc.UserService - get(id: 1) 2011-12-18 14:57:05,280 null[Dispatcher-0] TRACE com.shunra.poc.dummy.DummyInterceptor - DummyInterceptor: execution(User com.shunra.poc.InMemoryUserRepository.fetch(int)) 2011-12-18 14:57:05,281 null[Dispatcher-0] TRACE com.shunra.poc.dummy.DummyInterceptor - BaseDummyInterceptor: execution(User com.shunra.poc.InMemoryUserRepository.fetch(int)) 2011-12-18 14:57:05,281 null[Dispatcher-0] TRACE com.shunra.poc.UserService - get(id: 1) = User [Id=1, FirstName=Mark, LastName=Kharitonov, Age=36] 2011-12-18 14:57:05,282 null[Dispatcher-0] TRACE com.shunra.poc.handlers.UserHandler - getUser(id: 1) = User [Id=1, FirstName=Mark, LastName=Kharitonov, Age=36] =========================================================== Notice two messages from the com.shunra.poc.dummy.DummyInterceptor logger - one for DummyInterceptor and the other - for BaseDummyInterceptor, invoked though the super reference from within the DummyInterceptor.Intercept method. The problem is that ILoggable.getLogger method retrieves the LoggerHolderAspect instance using this.getClass() as the key, which is determined polymorphically, while I need a statically determined value. How do I fix it? Thank you very much in advance. -- View this message in context: http://aspectj.2085585.n4.nabble.com/How-to-inject-a-static-field-into-a-multitude-of-types-with-AspectJ-tp4168355p4211057.html Sent from the AspectJ - users mailing list archive at Nabble.com. _______________________________________________ aspectj-users mailing list aspectj-users@eclipse.org https://dev.eclipse.org/mailman/listinfo/aspectj-users