[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [aspectj-users] A pointcut to match all static methods in a class with some restrictions

So everything is working as you want, you just want to know about the warnings.

> [AppClassLoader@517590db] warning at javax/servlet/jsp/aspects/Users/joe/Documents/uni/research/LuMi/workspace/tomcat6.0.9
> _aspects/src/javax/servlet/jsp/aspects/JspFactory_Initialized.aj:31::0 does not match because declaring type is java.lang.Object,
> if match desired use target(javax.servlet.jsp.JspFactory) [Xlint:unmatchedSuperTypeInCall]
> see also: org/apache/catalina/connector/Connector.java:1013::0

Suppose the toString() method is called on a JspFactory, or hashCode() - those are methods inherited from Object, perhaps not implemented
by your JspFactory.  Should call(* JspFactory.*(..)) match calls to them? well JspFactory doesn't define them, so no.  And just in case you
really wanted to include them but accidentally used the declaring type inappropriately in call(), there is a warning from the compiler.

The warning is telling you the declaring type of the method involved is Object and if you want a match, you will need to use target, if you
don't want a match then it doesn't matter and you can ignore it (turn off the warning if you like, it is an xlint).

Here is a trivial case:
-- A.java --
public class A {
  public void foo() {
    int i = hashCode();
  }
}

aspect X {
  before(): call(* A.*(..)) {}
}
---
C:\aspectj164-dev>ajc A.java
C:\aspectj164-dev\A.java:8 [warning] does not match because declaring type is java.lang.Object, if match desired use target(A) [Xlint:unmatchedSuperTypeInCall]
before(): call(* A.*(..)) {}
          ^^^^^^^^^^^^
        [Xlint:unmatchedSuperTypeInCall]
        see also: C:\aspectj164-dev\A.java:3::0

1 warning

Doesn't matter for static methods.  And target can be used to match non-static methods:
--- A.java ---
public class A {
  void foo() {
  }

  static void goo() {
  }

}

aspect X {
  before(): execution(* *(..)) && !target(Object) {}
}
---
C:\aspectj164-dev>ajc A.java -showWeaveInfo
Join point 'method-execution(void A.goo())' in Type 'A' (A.java:5) advised by before advice from 'X' (A.java:11)

if target is not a derivative of Object then it must be a static method.  But qualifying it in your pointcut is a more optimal way to express it.


Andy



2009/3/13 Jochen Wuttke <jochen.wuttke@xxxxxx>
Hi,

I've been playing a lot with pointcuts that should address static methods (in classes that potentially also have non-static members).
My basic pointcuts looks like this:

   pointcut initMethod():
       call(* javax.servlet.jsp.JspFactory.setDefaultFactory(..));

       pointcut blockedMethods():               !cflow(initMethod())
               && !initMethod()
               && call(* javax.servlet.jsp.JspFactory.*(..))
               && !call(* java.lang.Object.getClass(..)); ;
               

The method setDefaultFactory is static and correctly addressed by the initMethod() pointcut. In the second pointcut I get a warning on the line

&& call(* javax.servlet.jsp.JspFactory.*(..))

saying that

"does not match because declaring class is java.lang.Object, if match desired user target(....)"

Now obviously I cannot use target() when I'm working with static methods. Further, in a simple test case the advice using the blockedMethods() pointcut gets woven in exactly the places I expect. But when I run it in a larger project with LTW, I get lots of warnings like this one:

[AppClassLoader@517590db] warning at javax/servlet/jsp/aspects/Users/joe/Documents/uni/research/LuMi/workspace/tomcat6.0.9_aspects/src/javax/servlet/jsp/aspects/JspFactory_Initialized.aj:31::0 does not match because declaring type is java.lang.Object, if match desired use target(javax.servlet.jsp.JspFactory) [Xlint:unmatchedSuperTypeInCall]
       see also: org/apache/catalina/connector/Connector.java:1013::0

What's funny is that the class Connector in this example doesn't even reference JspFactory or any of the methods therein.

Additionally, when I change the offending line in the pointcut to

&& call(static * javax.servlet.jsp.JspFactory.*(..))

then everything works fine.

I have a little trouble interpreting this behavior. I would expect the '*' without the explicit 'static' to cover both, static and non-static methods. Hence I do not understand the warning I get from the static weaver in the first example with the simple test case. Second, I simply don't understand the warnings produced by the load-time weaver. What are they supposed to mean, and how can the pointcut above possibly match anything that is not declared in JspFactory?

Any help or pointers to detailed documentation are greatly appreciated.

Jochen_______________________________________________
aspectj-users mailing list
aspectj-users@xxxxxxxxxxx
https://dev.eclipse.org/mailman/listinfo/aspectj-users