]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-2824 Java Squid should use isolated ClassLoader for bytecode analysis
authorEvgeny Mandrikov <mandrikov@gmail.com>
Fri, 23 Sep 2011 10:50:30 +0000 (14:50 +0400)
committerEvgeny Mandrikov <mandrikov@gmail.com>
Fri, 23 Sep 2011 13:03:15 +0000 (17:03 +0400)
plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/ClassworldsClassLoader.java
plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/ClassworldsClassLoaderTest.java

index 4cc10aedce17498b9174c6101fa7c5ef30930d75..6a814c8499f34bc17eec0803b79993ffb461cb55 100644 (file)
@@ -45,7 +45,7 @@ public final class ClassworldsClassLoader {
   public static ClassLoader create(Collection<File> bytecodeFilesOrDirectories) {
     try {
       ClassWorld world = new ClassWorld();
-      ClassRealm realm = world.newRealm("squid.project");
+      ClassRealm realm = world.newRealm("squid.project", null /* explicit declaration that parent should be bootstrap class loader */);
 
       for (File bytecode : bytecodeFilesOrDirectories) {
         URL url = getURL(bytecode);
index ea93533cffca297f481dbab74f1fc507a42ee29d..7f248c6eef305d79153208a474c56f0ba716eee8 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.java.bytecode;
 
+import org.codehaus.classworlds.ClassWorld;
 import org.junit.Test;
 import org.sonar.java.ast.SquidTestUtils;
 
@@ -39,6 +40,25 @@ public class ClassworldsClassLoaderTest {
     assertThat(ClassworldsClassLoader.create(Collections.<File>emptyList()), not(nullValue()));
   }
 
+  /**
+   * See SONAR-2824:
+   * ClassLoader created by {@link ClassworldsClassLoader},
+   * should be able to load classes only from JDK and from provided list of JAR-files,
+   * thus it shouldn't be able to load class {@link ClassWorld}.
+   */
+  @Test
+  public void shouldBeIsolated() throws ClassNotFoundException {
+    ClassLoader classloader = ClassworldsClassLoader.create(Collections.EMPTY_LIST);
+    try {
+      classloader.loadClass(ClassWorld.class.getName());
+      fail();
+    } catch (ClassNotFoundException e) {
+      // ok
+    }
+    assertThat(classloader.loadClass("java.lang.Integer"), not(nullValue()));
+    assertThat(classloader.getResource("java/lang/Integer.class"), not(nullValue()));
+  }
+
   @Test
   public void createFromDirectory() throws ClassNotFoundException {
     File dir = SquidTestUtils.getFile("/bytecode/bin/");