]> source.dussan.org Git - aspectj.git/commitdiff
Fix for 368046: exclude certain loaders
authorAndy Clement <aclement@gopivotal.com>
Tue, 30 Jul 2013 05:24:55 +0000 (22:24 -0700)
committerAndy Clement <aclement@gopivotal.com>
Fri, 18 Oct 2013 18:36:11 +0000 (11:36 -0700)
loadtime/src/org/aspectj/weaver/loadtime/Aj.java
loadtime/src/org/aspectj/weaver/loadtime/ClassLoaderWeavingAdaptor.java
loadtime/src/org/aspectj/weaver/loadtime/Options.java
tests/bugs174/pr368046/Azpect.java [new file with mode: 0644]
tests/bugs174/pr368046/Code.java [new file with mode: 0644]
tests/bugs174/pr368046/aop1.xml [new file with mode: 0644]
tests/bugs174/pr368046/aop2.xml [new file with mode: 0644]
tests/bugs174/pr368046/aop3.xml [new file with mode: 0644]
tests/src/org/aspectj/systemtest/ajc174/Ajc174Tests.java
tests/src/org/aspectj/systemtest/ajc174/ajc174.xml

index 900d59ffe624eed2623ce39f3b8d33fcdd5d9cd7..42d7a258b8d548574f69915d849e7fbb32a8059b 100644 (file)
@@ -14,12 +14,14 @@ package org.aspectj.weaver.loadtime;
 import java.lang.ref.ReferenceQueue;
 import java.lang.ref.WeakReference;
 import java.security.ProtectionDomain;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Iterator;
-import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
+import java.util.StringTokenizer;
 
 import org.aspectj.bridge.context.CompilationAndWeavingContext;
 import org.aspectj.weaver.Dump;
@@ -76,7 +78,7 @@ public class Aj implements ClassPreProcessor {
         * @param className
         * @param bytes
         * @param loader
-        * @return weaved bytes
+        * @return woven bytes
         */
        public byte[] preProcess(String className, byte[] bytes, ClassLoader loader, ProtectionDomain protectionDomain) {
                // TODO AV needs to doc that
@@ -84,6 +86,14 @@ public class Aj implements ClassPreProcessor {
                        // skip boot loader, null classes (hibernate), or those from a reflection loader
                        return bytes;
                }
+               
+               if (loadersToSkip != null) {
+                       // Check whether to reject it
+                       if (loadersToSkip.contains(loader.getClass().getName())) {
+//                             System.out.println("debug: no weaver created for loader '"+loader.getClass().getName()+"'");
+                               return bytes;
+                       }
+               }
 
                if (trace.isTraceEnabled())
                        trace.enter("preProcess", this, new Object[] { className, bytes, loader });
@@ -248,10 +258,28 @@ public class Aj implements ClassPreProcessor {
                        }
                }
        }
+       
+       public static List<String> loadersToSkip = null;
 
        static {
                // pr271840 - touch the types early and outside the locks
                new ExplicitlyInitializedClassLoaderWeavingAdaptor(new ClassLoaderWeavingAdaptor());
+               try {
+                       String loadersToSkipProperty = System.getProperty("aj.weaving.loadersToSkip","");
+                       StringTokenizer st = new StringTokenizer(loadersToSkipProperty, ",");
+                       if (loadersToSkipProperty != null && loadersToSkip == null) {
+                               if (st.hasMoreTokens()) {
+//                                     System.out.println("aj.weaving.loadersToSkip is set. Skipping loaders: '"+loadersToSkipProperty+"'");
+                                       loadersToSkip = new ArrayList<String>();
+                               }
+                               while (st.hasMoreTokens()) {
+                                       String nextLoader = st.nextToken();
+                                       loadersToSkip.add(nextLoader);
+                               }
+                       }
+               } catch (Exception e) {
+                       // Likely security issue related to property access...
+               }
        }
 
        /**
index 6952ec00819f9a67d1310095ae35356d6e3df12c..2aa6307d3a4bdd438edfc83f9424d340f03922e5 100644 (file)
@@ -23,6 +23,7 @@ import java.util.*;
 
 import org.aspectj.bridge.AbortException;
 import org.aspectj.bridge.Constants;
+import org.aspectj.bridge.MessageUtil;
 import org.aspectj.util.LangUtil;
 import org.aspectj.weaver.Lint;
 import org.aspectj.weaver.Lint.Kind;
@@ -345,6 +346,12 @@ public class ClassLoaderWeavingAdaptor extends WeavingAdaptor {
                world.setOptionalJoinpoints(weaverOption.optionalJoinpoints);
                world.setPinpointMode(weaverOption.pinpoint);
                weaver.setReweavableMode(weaverOption.notReWeavable);
+               if (weaverOption.loadersToSkip != null && weaverOption.loadersToSkip.length() > 0) {
+                       Aj.loadersToSkip = LangUtil.anySplit(weaverOption.loadersToSkip, ",");
+               }
+               if (Aj.loadersToSkip != null) {
+                       MessageUtil.info(world.getMessageHandler(),"no longer creating weavers for these classloaders: "+Aj.loadersToSkip);
+               }
                world.performExtraConfiguration(weaverOption.xSet);
                world.setXnoInline(weaverOption.noInline);
                // AMC - autodetect as per line below, needed for AtAjLTWTests.testLTWUnweavable
index 319f6d844fff6b8aa512b8f3c52c867812da890d..0c9611e4f5575ee440e19fb7f5763f047fcaabf0 100644 (file)
@@ -47,6 +47,7 @@ public class Options {
        private static final String OPTIONVALUED_joinpoints = "-Xjoinpoints:";
        private static final String OPTIONVALUED_Xset = "-Xset:";
        private static final String OPTION_timers = "-timers";
+       private static final String OPTIONVALUED_loadersToSkip = "-loadersToSkip:";
 
        public static WeaverOption parse(String options, ClassLoader laoder, IMessageHandler imh) {
                WeaverOption weaverOption = new WeaverOption(imh);
@@ -55,12 +56,12 @@ public class Options {
                        return weaverOption;
                }
                // the first option wins
-               List flags = LangUtil.anySplit(options, " ");
+               List<String> flags = LangUtil.anySplit(options, " ");
                Collections.reverse(flags);
 
                // do a first round on the message handler since it will report the options themselves
-               for (Iterator iterator = flags.iterator(); iterator.hasNext();) {
-                       String arg = (String) iterator.next();
+               for (Iterator<String> iterator = flags.iterator(); iterator.hasNext();) {
+                       String arg = iterator.next();
                        if (arg.startsWith(OPTIONVALUED_messageHandler)) {
                                if (arg.length() > OPTIONVALUED_messageHandler.length()) {
                                        String handlerClass = arg.substring(OPTIONVALUED_messageHandler.length()).trim();
@@ -76,7 +77,7 @@ public class Options {
                }
 
                // configure the other options
-               for (Iterator iterator = flags.iterator(); iterator.hasNext();) {
+               for (Iterator<String> iterator = flags.iterator(); iterator.hasNext();) {
                        String arg = (String) iterator.next();
                        if (arg.equals(OPTION_15)) {
                                weaverOption.java5 = true;
@@ -122,6 +123,11 @@ public class Options {
                                }
                        } else if (arg.equalsIgnoreCase(OPTION_timers)) {
                                weaverOption.timers = true;
+                       } else if (arg.startsWith(OPTIONVALUED_loadersToSkip)) {
+                               if (arg.length() > OPTIONVALUED_loadersToSkip.length()) {
+                                       String value = arg.substring(OPTIONVALUED_loadersToSkip.length()).trim();
+                                       weaverOption.loadersToSkip = value;
+                               }
                        } else {
                                weaverOption.messageHandler.handleMessage(new Message("Cannot configure weaver with option '" + arg
                                                + "': unknown option", IMessage.WARNING, null, null));
@@ -164,6 +170,7 @@ public class Options {
                String lint;
                String lintFile;
                String xSet;
+               String loadersToSkip;
 
                public WeaverOption(IMessageHandler imh) {
                        // messageHandler = new DefaultMessageHandler();//default
diff --git a/tests/bugs174/pr368046/Azpect.java b/tests/bugs174/pr368046/Azpect.java
new file mode 100644 (file)
index 0000000..ab3bf6d
--- /dev/null
@@ -0,0 +1,5 @@
+public aspect Azpect {
+  before(): execution(* *(..)) {
+    System.out.println("advice");
+  }
+}
diff --git a/tests/bugs174/pr368046/Code.java b/tests/bugs174/pr368046/Code.java
new file mode 100644 (file)
index 0000000..618e3db
--- /dev/null
@@ -0,0 +1,5 @@
+public class Code {
+  public static void main(String []argv) {
+    System.out.println("running");
+  }
+}
diff --git a/tests/bugs174/pr368046/aop1.xml b/tests/bugs174/pr368046/aop1.xml
new file mode 100644 (file)
index 0000000..be9a87c
--- /dev/null
@@ -0,0 +1,6 @@
+<aspectj>
+ <aspects>
+  <aspect name="Azpect"/>
+ </aspects>
+ <weaver options="-verbose"/>
+</aspectj>
diff --git a/tests/bugs174/pr368046/aop2.xml b/tests/bugs174/pr368046/aop2.xml
new file mode 100644 (file)
index 0000000..f9d201e
--- /dev/null
@@ -0,0 +1,6 @@
+<aspectj>
+ <aspects>
+  <aspect name="Azpect"/>
+ </aspects>
+ <weaver options="-verbose -loadersToSkip:com.foo.Bar"/>
+</aspectj>
diff --git a/tests/bugs174/pr368046/aop3.xml b/tests/bugs174/pr368046/aop3.xml
new file mode 100644 (file)
index 0000000..45c0885
--- /dev/null
@@ -0,0 +1,6 @@
+<aspectj>
+ <aspects>
+  <aspect name="Azpect"/>
+ </aspects>
+ <weaver options="-verbose -loadersToSkip:org.aspectj.weaver.loadtime.WeavingURLClassLoader"/>
+</aspectj>
index 464c948e5877792d6c5fe1102bcd5515c877a6d9..06af6a53bea16de4f9c32e3b28cfbb56d18790a2 100644 (file)
@@ -25,6 +25,33 @@ public class Ajc174Tests extends org.aspectj.testing.XMLBasedAjcTestCase {
                runTest("super itd ctor");
        }
        
+       // no exclusion, this is how it should work
+       public void testCLExclusion_pr368046_1_noskippedloaders() {
+               runTest("classloader exclusion - 1");
+       }
+
+       public void testCLExclusion_pr368046_1_syspropset() {
+               try {
+                       System.setProperty("aj.weaving.loadersToSkip", "foo");
+                       runTest("classloader exclusion - 2");
+               } finally {
+                       System.setProperty("aj.weaving.loadersToSkip", "");
+               }
+       }
+
+       // final repeat this test, to confirm no lingering static
+       public void testCLExclusion_pr368046_1_again_noskippedloaders() {
+               runTest("classloader exclusion - 3");
+       }
+       
+       public void testCLExclusion_pr368046_2_usingaopxml() {
+               runTest("classloader exclusion - 4");
+       }
+
+       public void testCLExclusion_pr368046_2_usingaopxmlReal() {
+               runTest("classloader exclusion - 5");
+       }
+       
        // ---
 
        public static Test suite() {
index 0a64273e53d41479d036ea6dea3f2430b75a9017..85ff2af8bb9b2b0e7e3a4bc2847990255342ce0f 100644 (file)
                </stdout>
                </run>
        </ajc-test>
+       
+       <ajc-test dir="bugs174/pr368046" title="classloader exclusion - 1">
+         <compile files="Azpect.java" outjar="foo.jar"/>
+      <compile files="Code.java" classpath="$sandbox/foo.jar"/>
+      <run class="Code" classpath="$sandbox/foo.jar" ltw="aop1.xml">
+        <stdout>
+          <line text="advice"/>
+          <line text="running"/>
+           </stdout>
+           <stderr>
+           <line text="AspectJ Weaver"/>
+           <line text="register classloader"/>
+           <line text="using configuration"/>
+           <line text="register aspect"/>
+           <line text="processing reweavable"/>
+           </stderr>
+      </run>
+       </ajc-test>
+       
+       <ajc-test dir="bugs174/pr368046" title="classloader exclusion - 2">
+         <compile files="Azpect.java" outjar="foo.jar"/>
+      <compile files="Code.java" classpath="$sandbox/foo.jar"/>
+      <run class="Code" classpath="$sandbox/foo.jar" ltw="aop1.xml">
+        <stdout>
+          <line text="advice"/>
+          <line text="running"/>
+           </stdout>
+           <stderr>
+           <line text="AspectJ Weaver"/>
+           <line text="register classloader"/>
+           <line text="using configuration"/>
+           <line text="no longer creating weavers for these classloaders: [foo]"/>
+           <line text="register aspect"/>
+           <line text="processing reweavable"/>
+           </stderr>
+      </run>
+       </ajc-test>
+       
+       <ajc-test dir="bugs174/pr368046" title="classloader exclusion - 3">
+         <compile files="Azpect.java" outjar="foo.jar"/>
+      <compile files="Code.java" classpath="$sandbox/foo.jar"/>
+      <run class="Code" classpath="$sandbox/foo.jar" ltw="aop1.xml">
+        <stdout>
+          <line text="advice"/>
+          <line text="running"/>
+           </stdout>
+           <stderr>
+           <line text="AspectJ Weaver"/>
+           <line text="register classloader"/>
+           <line text="using configuration"/>
+           <line text="register aspect"/>
+           <line text="processing reweavable"/>
+           </stderr>
+      </run>
+       </ajc-test>
+       
+       <ajc-test dir="bugs174/pr368046" title="classloader exclusion - 4">
+         <compile files="Azpect.java" outjar="foo.jar"/>
+      <compile files="Code.java" classpath="$sandbox/foo.jar"/>
+      <run class="Code" classpath="$sandbox/foo.jar" ltw="aop2.xml">
+        <stdout>
+          <line text="advice"/>
+          <line text="running"/>
+           </stdout>
+           <stderr>
+           <line text="AspectJ Weaver"/>
+           <line text="register classloader"/>
+           <line text="using configuration"/>
+           <line text="no longer creating weavers for these classloaders: [com.foo.Bar]"/>
+           <line text="register aspect"/>
+           <line text="processing reweavable"/>
+           </stderr>
+      </run>
+       </ajc-test>
+       
+       <ajc-test dir="bugs174/pr368046" title="classloader exclusion - 5">
+         <compile files="Azpect.java" outjar="foo.jar"/>
+      <compile files="Code.java" classpath="$sandbox/foo.jar"/>
+      <run class="Code" classpath="$sandbox/foo.jar" ltw="aop3.xml">
+        <stdout>
+          <line text="advice"/>
+          <line text="running"/>
+           </stdout>
+           <stderr>
+           <line text="AspectJ Weaver"/>
+           <line text="register classloader"/>
+           <line text="using configuration"/>
+           <line text="no longer creating weavers for these classloaders: [org.aspectj.weaver.loadtime.WeavingURLClassLoader]"/>
+           <line text="register aspect"/>
+           <line text="processing reweavable"/>
+           </stderr>
+      </run>
+       </ajc-test>
 
 </suite>