Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[aspectj-users] Re: getting 'around' constructor execution

Gijs, thanks, works like a charm :)

(I thought I attempted something like that, but apparently the "something like" was not "alike enough" ;) - thanks again)

Danny

Gijs Peek wrote:
Not calling the super constructor is impossible, if A extends some class it has to call one super constructor or the other. Now, assuming that you want to call the parameterless super constructor instead of the constructor with the Type parameter, you can use aspectj to do that. You are currently trying to use a constructor execution join point. This join point, according to The AspectJTM Programming Guide, matches 'When the body of code for an actual constructor executes, after its this or super constructor call'. Thus, you cannot use this construction. You can, however, match constructor call join points, and call another constructor instead. You can define a new constructor in your aspect. Let me illustrate this with an example:

public abstract class ASuper {
	public ASuper() {
		System.out.println("Goodness has occurred");
	}
	
	public ASuper(Type t) {
		System.out.println("Badness has occurred");
	}
}

public class A extends ASuper {
	private Type var1;
	public A(Type arg) {
		super(arg);
		System.out.println("1");
		var1 = arg;
		System.out.println("2");
	}
	
	public static void main(String[] args) {
		new A(null);
	}
}

public privileged aspect AAsp {
	pointcut constructorCall(Type arg):
		call(A.new(Type))
		&& args(arg);
	
	private A.new(Type arg, AAsp a) {
		super();
		var1 = arg;
	}
	
	A around(Type arg): constructorCall(arg) {
		System.out.println("advice A constructor");
		return new A(arg, this);
	}
}

PS pre-initialization and initialization join points won't do you any good either, they respectively match the code before and after the super call.

cheers,
 - Gijs

On Tuesday 18 September 2007 11:12:54 danny wrote:
oops forgot the pointcut declaration :

pointcut constructor(A instance, Type arg):
		execution (A.new (Type))
		&& this(instance)
		&& args(arg);


the class to be woven is something like
public class A {
   private Type var1;
   public A(Type arg) {
      super(arg);
      var1=arg;
   }
}

the actual goal is to replace the constructor so that 'super(arg)' is
not called.

as before, all help much appreciated.

danny wrote:
the advice -I just want to see if I enter the advice for now- :
A around(A instance, Type arg): constructor(instance, arg) {
  System.out.println("advice A constructor");

  return instance ();
}

I get the following error when weaving :
[WeavingURLClassLoader] error at.......incompatible return type applying
to constructor-execution(void A.<init>(Type))

I've tried some other constructs, this is the only one I actually
trigger something, all other don't even try to weave :(

What do do ?
All help appreciated

Danny
_______________________________________________
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