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 ?

On 2 May 2012 12:40, Jean Andre <Jean.Andre@xxxxxxxxxx> wrote:
> I've downloaded eclipse Indigo and I've updated the adjt and I got the same
> error with a test project.

Which AJDT did you install?  You need a recent one, not the previous
release - that is getting a bit old now.  A new release will be out
shortly - we have just had approval from eclipse.  But you need one
from:

http://download.eclipse.org/tools/ajdt/37/dev/update

The exception you have been mentioning is the same as I saw.  It is
https://bugs.eclipse.org/bugs/show_bug.cgi?id=371998 which was fixed
in 1.7.0.M1.


> 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.

Some options:

1) you can put advice on staticinitialization of the class to setup the logger:

  before(): staticinitialization(*) {
    log = Logger.getLogger(thisJoinPointStaticPart.getSignature().getDeclaringTypeName());
  }

2) you could use a pertypewithin aspect instead of what you have:

aspect X pertypewithin(Code||Bar) {
	
	private Logger log;
	
	interface I {};
	
	declare parents: Code implements I;
	
	before(): staticinitialization(*) {
		log = Logger.getLogger(getWithinTypeName());
	}
}

This means instead of stuffing state into the target you are creating
an instance of the aspect per class and that manages extra state for
the class.  Within pertypewithin aspects, the getWithinTypeName() will
return the name of the type for which the aspect instance exists. (So
Code or Bar in my example above).

3) or you could switch  @DeclareParents to @DeclareMixin which tries
to give you more flexibility around delegate instance creation.  Here
is a cut down variation of your aspect that uses @DeclareMixin where
you are using @DeclareParents:


@Aspect
class LoggingBehaviour {

	public interface Loggable {
		public Logger getLog();
	};

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

		public ServantLogger(Class<? extends Object> clazz) {
			System.out.println("initializing servantlogger for "+clazz);
			this.log = Logger.getLogger(clazz);
		}

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

	@DeclareMixin("com.Code2 || com.Bar")
	public static Loggable createLoggerDelegate(Object o) {
		return new ServantLogger(o.getClass());
	}
}

@DeclareMixin goes on a factory method that can serve up instances of
the delegate - the optional parameter 'o' is the object for which the
delegate is being created (so we can grab its class).  More docs here:
http://www.eclipse.org/aspectj/doc/released/adk15notebook/ataspectj-itds.html

hope some of that helps,

cheers,
Andy


> 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
>
>
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/aspectj-users
>


Back to the top