[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] MethodPattern for Methods with arguments without wildcard '..'

Hi,

You did mention Object+ didn't work for you, which confuses me - did
you have exactly what I show above?

I am really sorry, I retested it, and it works with Object and Object+ (I do not know what I did the first time).


You highlight a point that I did totally missed :

Weaving is done with the method signature, which is for the contains method of a Collection [1] :
boolean contains(Object o)

In my example [2] trying to match call(* Collection.contains(String)) is wrong for that reason. Even if the object is a String after all (the method does not exists, so it can not be weaved).


Your solutions works also by me :
before(Collection c, String s): target(c) && args(s) && call(*
Collection.contains(Object)) {}

before(Collection c, String s): target(c) && args(s) && call(*
Collection.contains(Object+)) {}

And it ensure that s is a String.


Thanks a lot,

Jérémie



[1] see Javadoc : http://java.sun.com/javase/6/docs/api/java/util/Collection.html#contains(java.lang.Object)

[2] see my previous post :
http://dev.eclipse.org/mhonarc/lists/aspectj-users/msg11588.html



Quoting "Andy Clement" <andrew.clement@xxxxxx>:

Hi,

The call pointcut takes a declared signature.  You have to specify the
pattern that would match the declared method.  The contains method
takes an Object , so "call(* Collection.contains(Object))" will match
it.  Both these work for me:

before(Collection c, String s): target(c) && args(s) && call(*
Collection.contains(Object)) {}

before(Collection c, String s): target(c) && args(s) && call(*
Collection.contains(Object+)) {}

Of course the original method was declared generically but the erasure
of the declaration is Object (as you can see with javap on
Collection).

You did mention Object+ didn't work for you, which confuses me - did
you have exactly what I show above?

You also say how do you test the runtime type without exposing it:

before(Collection c): target(c) && args(String) && call(*
Collection.contains(Object)) {}

this/target/args check runtime type information.

Andy

2009/12/10 <jbresson@xxxxxxxxxx>:
Hi,

I am looking for the point-cut for contains method (line 9) in the following
Java code:

  Collection<String> c;
  c = new ArrayList<String>();
  c.add("x");
  c.add("y");

  String s;
  s = "z";

  if(!c.contains(s)) {
       c.add(s);
  }


This Pointcut works:

  public aspect DequeAspect {

     pointcut test(Collection c, String s): target(c) &&
                                            args(s) &&
                                            call(* Collection.contains(..));

     after(Collection c, String s): test(c, s) {
        System.err.println("AFTER: "+c.toString()+" CONTAINS "+s);
     }
  }


After a look at the page <Appendix B. Language Semantics> http://www.eclipse.org/aspectj/doc/released/progguide/semantics-pointcuts.html

It seems to be that the wildcard '..' in the contains Method can also be a
TypePattern.

But I tested :
  pointcut test(Collection c, String s): target(c) && args(s) &&
                                         call(*
Collection.contains(String));

and that is not working (I also tried Object+ or String+).


In this example, using the wildcard is not big deal, but what if you just want to test the Type of the argument (without exposing this values) :   pointcut test(Collection c): target(c) &&                                call(* Collection.contains(String));

produce a AdviceDidNotUsed warning (on the Advice using this pointcut)


How do you define correctly the MethodPattern (especially the TypePattern) without the wildcard '..'? Is that possible?


Regards,

Jérémie



_______________________________________________
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