[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
Re: [aspectj-users] How to inject a static field into a multitude of types with AspectJ?
|
Hi Andy
Can you provide your source code that works? I could not come up
with a working one. I had two problems
1. the getLogger() is a Test class do not get advised by the around statement
2. getLogger() is not available from a static context.
@ILog
public class Test {
public void test(){
getLogger().info("huu");
}
public static void main(String[] args) {
IWantToLog i = new IWantToLog();
i.test();
getLogger().info("huh from static context");
}
}
public aspect LoggerAspect {
private static aspect LoggerHolderAspect pertypewithin(@ILog *) {
private Logger logger;
public void initLogger() {
logger = LogManager.getLogManager().getLogger(getWithinTypeName()); }
public Logger getLogger() {
return null;
}
}
private interface ILoggable {};
declare parents: (@ILog *) implements ILoggable;
public Logger ILoggable.getLogger() {
return null;
}
Logger around(): call(Logger getLogger(..)) && !within(LoggerAspect) {
return getLoggerHolder(thisEnclosingJoinPointStaticPart).getLogger();
}
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(@ILog *) {
getLoggerHolder(thisJoinPointStaticPart).initLogger();
}
}
public @interface ILog {
}
Thanks Denes
2012/1/16 Andy Clement <andrew.clement@xxxxxxxxx>:
> I would agree, *but* private ITD methods onto interfaces currently
> still mangle the method name that is introduced. So you can ITD
> getLogger() but that won't be what gets into the interface and type.
>
> This is due to what 'private' means. If you consider it means
> 'private to the aspect' then we don't want the type or interface
> tripping over it, hence it gets a new name that is unlikely to clash
> with anything in the existing types (and code you write in the class
> is unlikely to see it, use it).
>
> Now this definition of privacy has been evolving over the last few
> releases because it isn't seen as useful, it isn't how users think,
> they think they are creating a private method in the target. I have
> addressed the basic case of private ITD method onto a CLASS (that will
> now not mangle the name) but the more complex form of an ITD onto an
> interface is still mangled. (there is a bugzilla somewhere about
> it...)
>
> cheers,
> Andy
>
> On 15 January 2012 23:54, Mark <mark.kharitonov@xxxxxxxxx> wrote:
>> Would not you agree that getLogger must be made private? Otherwise, someone
>> could have invoked it from an arbitrary context, in which case
>> thisEnclosingJoinPointStaticPart would be something unaffected by the
>> aspect.
>>
>>
>> --
>> View this message in context: http://aspectj.2085585.n4.nabble.com/How-to-inject-a-static-field-into-a-multitude-of-types-with-AspectJ-tp4168355p4298714.html
>> Sent from the AspectJ - users mailing list archive at Nabble.com.
>> _______________________________________________
>> 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