]> source.dussan.org Git - aspectj.git/commitdiff
266165: if(enabled) where enabled is constant boolean is optimized
authoraclement <aclement>
Wed, 25 Feb 2009 20:11:15 +0000 (20:11 +0000)
committeraclement <aclement>
Wed, 25 Feb 2009 20:11:15 +0000 (20:11 +0000)
org.aspectj.matcher/src/org/aspectj/weaver/Advice.java
org.aspectj.matcher/src/org/aspectj/weaver/patterns/IfPointcut.java

index 9d79ff7e34c546acf2715f10a38f4937dc46be36..08426a197cc1c7a129158286b3eab70669f037d3 100644 (file)
@@ -294,7 +294,6 @@ public abstract class Advice extends ShadowMunger {
                return result;
        }
 
-
        /**
         * Return the type of the 'extra argument'. For either after returning or after throwing advice, the extra argument will be the
         * returned value or the thrown exception respectively. With annotation style the user may declare the parameters in any order,
@@ -459,12 +458,15 @@ public abstract class Advice extends ShadowMunger {
 
        // ---- fields
 
-       public static final int ExtraArgument = 1;
-       public static final int ThisJoinPoint = 2;
-       public static final int ThisJoinPointStaticPart = 4;
-       public static final int ThisEnclosingJoinPointStaticPart = 8;
-       public static final int ParameterMask = 0xf;
-
+       public static final int ExtraArgument = 0x01;
+       public static final int ThisJoinPoint = 0x02;
+       public static final int ThisJoinPointStaticPart = 0x04;
+       public static final int ThisEnclosingJoinPointStaticPart = 0x08;
+       public static final int ParameterMask = 0x0f;
+       // For an if pointcut, this indicates it is hard wired to access a constant of either true or false
+       public static final int ConstantReference = 0x10; 
+       // When the above flag is set, this indicates whether it is true or false
+       public static final int ConstantValue = 0x20; 
        public static final int CanInline = 0x40;
 
        // for testing only
index 1e5b3cb437686b205660dd28b854612bd1d938fb..34b7d51a0a5f2f6267c61969c884385518665b29 100644 (file)
@@ -82,6 +82,13 @@ public class IfPointcut extends Pointcut {
        }
 
        protected FuzzyBoolean matchInternal(Shadow shadow) {
+               if ((extraParameterFlags & Advice.ConstantReference) != 0) {
+                       if ((extraParameterFlags & Advice.ConstantValue) != 0) {
+                               return FuzzyBoolean.YES;
+                       } else {
+                               return FuzzyBoolean.NO;
+                       }
+               }
                // ??? this is not maximally efficient
                return FuzzyBoolean.MAYBE;
        }
@@ -160,8 +167,9 @@ public class IfPointcut extends Pointcut {
         * At each shadow that matched, the residue can be different.
         */
        protected Test findResidueInternal(Shadow shadow, ExposedState state) {
-               if (findingResidue)
+               if (findingResidue) {
                        return Literal.TRUE;
+               }
                findingResidue = true;
                try {
 
@@ -174,6 +182,22 @@ public class IfPointcut extends Pointcut {
 
                        // code style
                        if (extraParameterFlags >= 0) {
+                               if ((extraParameterFlags & Advice.ConstantReference) != 0) {
+                                       // it is either always true or always false, no need for test
+                                       if ((extraParameterFlags & Advice.ConstantValue) != 0) {
+                                               ret = Literal.TRUE;
+                                               ifLastMatchedShadowId = shadow.shadowId;
+                                               ifLastMatchedShadowResidue = ret;
+                                               return ret;
+                                       } else {
+                                               // Dont think we should be in here as the match cannot have succeeded...
+                                               ret = Literal.FALSE;
+                                               ifLastMatchedShadowId = shadow.shadowId;
+                                               ifLastMatchedShadowResidue = ret;
+                                               return ret;
+                                       }
+                               }
+
                                // If there are no args to sort out, don't bother with the recursive call
                                if (baseArgsCount > 0) {
                                        ExposedState myState = new ExposedState(baseArgsCount);
@@ -528,4 +552,15 @@ public class IfPointcut extends Pointcut {
                }
        }
 
+       /**
+        * Called when it is determined that the pointcut refers to a constant value of TRUE or FALSE - enabling exact matching and no
+        * unnecessary calls to the method representing the if body.
+        */
+       public void setAlways(boolean matches) {
+               extraParameterFlags |= Advice.ConstantReference;
+               if (matches) {
+                       extraParameterFlags |= Advice.ConstantValue;
+               }
+       }
+
 }