Skip to content

Commit be75154

Browse files
committed
support JPMS classloading
clean up
1 parent 917a2ee commit be75154

File tree

5 files changed

+78
-28
lines changed

5 files changed

+78
-28
lines changed

src/main/java/org/codehaus/plexus/classworlds/ClassWorld.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,10 @@ public ClassRealm newRealm( String id, ClassLoader classLoader )
7474

7575
/**
7676
* Shortcut for {@link #newRealm(String, ClassLoader, Set)} with the class loader of the current class.
77-
* @param id
78-
* @param allowedResourceNamePrefixes
77+
* @param id The identifier for this realm, must not be <code>null</code>.
78+
* @param allowedResourceNamePrefixes the prefixes of resource names which should be exposed. Separator '/' is used here (even for classes).
7979
* @return the created class realm
80-
* @throws DuplicateRealmException
80+
* @throws DuplicateRealmException in case a realm with the given id does already exist
8181
* @since 2.7.0
8282
* @see FilteredClassRealm
8383
*/
@@ -90,11 +90,12 @@ public synchronized ClassRealm newRealm( String id, Set<String> allowedResourceN
9090
/**
9191
* Adds a class realm with filtering.
9292
* Only resources/classes starting with one of the given prefixes are exposed.
93-
* @param id
94-
* @param classLoader
93+
* @param id The identifier for this realm, must not be <code>null</code>.
94+
* @param baseClassLoader The base class loader for this realm, may be <code>null</code> to use the bootstrap class
95+
* loader.
9596
* @param allowedResourceNamePrefixes the prefixes of resource names which should be exposed. Separator '/' is used here (even for classes).
9697
* @return the created class realm
97-
* @throws DuplicateRealmException
98+
* @throws DuplicateRealmException in case a realm with the given id does already exist
9899
* @since 2.7.0
99100
* @see FilteredClassRealm
100101
*/

src/main/java/org/codehaus/plexus/classworlds/realm/ClassRealm.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,8 @@ private Class<?> unsynchronizedLoadClass( String name, boolean resolve )
272272
}
273273
}
274274

275-
// java11
275+
// overwrites https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/ClassLoader.html#findClass(java.lang.String,java.lang.String)
276+
// introduced in Java9
276277
protected Class<?> findClass( String moduleName, String name )
277278
{
278279
if ( moduleName != null )
@@ -281,7 +282,7 @@ protected Class<?> findClass( String moduleName, String name )
281282
}
282283
try
283284
{
284-
return super.findClass( name );
285+
return findClassInternal( name );
285286
}
286287
catch ( ClassNotFoundException e )
287288
{

src/main/java/org/codehaus/plexus/classworlds/realm/FilteredClassRealm.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
package org.codehaus.plexus.classworlds.realm;
2-
31
/*
42
* Licensed to the Apache Software Foundation (ASF) under one
53
* or more contributor license agreements. See the NOTICE file
@@ -18,6 +16,7 @@
1816
* specific language governing permissions and limitations
1917
* under the License.
2018
*/
19+
package org.codehaus.plexus.classworlds.realm;
2120

2221
import java.io.IOException;
2322
import java.net.URL;

src/test/java/org/codehaus/plexus/classworlds/realm/ClassRealmImplTest.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ public void testLoadClass_ClassWorldsClassRepeatedly()
443443
}
444444

445445
@Test
446-
public void testLoadClass_Java11()
446+
public void testLoadClassWithModuleName_Java9()
447447
{
448448
final ExtendedClassRealm mainRealm = new ExtendedClassRealm( world );
449449
mainRealm.addURL( getJarUrl( "a.jar" ) );
@@ -503,13 +503,17 @@ public void testGetResources_SelfBeforeParent()
503503
assertEquals( Arrays.asList( childUrl, parentUrl ), urls );
504504
}
505505

506-
// simulate new loadClass(Module,String) from java11
507-
// it is reversed in terms of inheritance but enables to simulate the same behavior in these tests
506+
/**
507+
* Simulates new {@code java.lang.ClassLoader#findClass(String,String)} introduced with Java 9.
508+
* It is reversed in terms of inheritance but enables to simulate the same behavior in these tests.
509+
* @see <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/ClassLoader.html#findClass(java.lang.String,java.lang.String)">ClassLoader#findClass(String,String)</a>
510+
*/
508511
private static class ExtendedClassRealm extends ClassRealm
509512
{
513+
510514
public ExtendedClassRealm(final ClassWorld world)
511515
{
512-
super( world, "java11", Thread.currentThread().getContextClassLoader() );
516+
super( world, "java9", Thread.currentThread().getContextClassLoader() );
513517
}
514518

515519
public Class<?> simulateLoadClassFromModule(final String name)

src/test/java/org/codehaus/plexus/classworlds/realm/FilteredClassRealmTest.java

Lines changed: 59 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
package org.codehaus.plexus.classworlds.realm;
2-
31
/*
42
* Licensed to the Apache Software Foundation (ASF) under one
53
* or more contributor license agreements. See the NOTICE file
@@ -18,7 +16,10 @@
1816
* specific language governing permissions and limitations
1917
* under the License.
2018
*/
19+
package org.codehaus.plexus.classworlds.realm;
2120

21+
import java.io.IOException;
22+
import java.util.Collections;
2223
import java.util.HashSet;
2324
import java.util.Set;
2425

@@ -36,34 +37,78 @@
3637
public class FilteredClassRealmTest extends AbstractClassWorldsTestCase
3738
{
3839
private ClassWorld world;
40+
private ClassRealm realmA;
3941

4042
@Before
41-
public void setUp()
43+
public void setUp() throws DuplicateRealmException
4244
{
4345
this.world = new ClassWorld();
46+
// only allow loading resources whose names start with "a."
47+
Set<String> allowedResourcePrefixes = new HashSet<>();
48+
allowedResourcePrefixes.add( "a." );
49+
allowedResourcePrefixes.add( "a/Aa" );
50+
realmA = this.world.newRealm( "realmA", allowedResourcePrefixes );
4451
}
4552

4653
@Test
47-
public void testLoadClassAndResourcesFiltered()
54+
public void testLoadResources()
4855
throws Exception
4956
{
50-
// only allow loading resources whose names start with "a."
51-
Set<String> allowedResourcePrefixes = new HashSet<>();
52-
allowedResourcePrefixes.add( "a." );
53-
allowedResourcePrefixes.add( "a/Aa" );
54-
ClassRealm realmA = this.world.newRealm( "realmA", allowedResourcePrefixes );
57+
realmA.addURL( getJarUrl( "a.jar" ) );
58+
assertNull( realmA.getResource( "common.properties" ) );
59+
assertFalse( realmA.getResources( "common.properties" ).hasMoreElements() );
60+
61+
assertNotNull( realmA.getResource( "a.properties" ) );
62+
assertTrue( realmA.getResources( "a.properties" ).hasMoreElements() );
63+
}
5564

65+
@Test
66+
public void testLoadClass() throws ClassNotFoundException
67+
{
5668
assertThrows( ClassNotFoundException.class, () -> realmA.loadClass( "a.Aa" ) );
5769
realmA.addURL( getJarUrl( "a.jar" ) );
5870

5971
assertNotNull( realmA.loadClass( "a.Aa" ) );
6072
assertThrows( ClassNotFoundException.class, () -> realmA.loadClass( "a.A" ) );
6173

62-
assertNull( realmA.getResource( "common.properties" ) );
63-
assertFalse( realmA.getResources( "common.properties" ).hasMoreElements() );
64-
65-
assertNotNull( realmA.getResource( "a.properties" ) );
66-
assertTrue( realmA.getResources( "a.properties" ).hasMoreElements() );
74+
assertNotNull( realmA.loadClass( "a.Aa" ) );
75+
assertThrows( ClassNotFoundException.class, () -> realmA.loadClass( "a.A" ) );
6776
}
6877

78+
@Test
79+
public void testLoadClassWithModule() throws IOException
80+
{
81+
try (ExtendedFilteredClassRealm realmA = new ExtendedFilteredClassRealm( world, Collections.singleton( "a/Aa" ) )) {
82+
realmA.addURL( getJarUrl( "a.jar" ) );
83+
assertNotNull( realmA.simulateLoadClassFromModule( "a.Aa" ) );
84+
assertNull( realmA.simulateLoadClassFromModule( "a.A" ) );
85+
}
86+
}
87+
88+
/**
89+
* Simulates new {@code java.lang.ClassLoader#findClass(String,String)} introduced with Java 9.
90+
* It is reversed in terms of inheritance but enables to simulate the same behavior in these tests.
91+
* @see <a href="https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/ClassLoader.html#findClass(java.lang.String,java.lang.String)">ClassLoader#findClass(String,String)</a>
92+
* @see ClassRealmImplTest.ExtendedClassRealm
93+
*/
94+
static class ExtendedFilteredClassRealm extends FilteredClassRealm
95+
{
96+
97+
public ExtendedFilteredClassRealm( final ClassWorld world, Set<String> allowedResourcePrefixes )
98+
{
99+
super( allowedResourcePrefixes, world, "java9", Thread.currentThread().getContextClassLoader() );
100+
}
101+
102+
public Class<?> simulateLoadClassFromModule(final String name)
103+
{
104+
synchronized (getClassLoadingLock(name))
105+
{
106+
Class<?> c = findLoadedClass(name);
107+
if (c == null) {
108+
c = findClass(null, name);
109+
}
110+
return c;
111+
}
112+
}
113+
}
69114
}

0 commit comments

Comments
 (0)