From ace47f7e67331dc0003bfff8e233086c80fcb893 Mon Sep 17 00:00:00 2001 From: Andreas Gudian Date: Sun, 16 Feb 2014 22:24:59 +0100 Subject: [PATCH] extend current classloader instead of creating a classloader chain. --- .../mapstruct/ap/testutil/MapperTestBase.java | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/processor/src/test/java/org/mapstruct/ap/testutil/MapperTestBase.java b/processor/src/test/java/org/mapstruct/ap/testutil/MapperTestBase.java index 3c52916c1..d2be5601c 100644 --- a/processor/src/test/java/org/mapstruct/ap/testutil/MapperTestBase.java +++ b/processor/src/test/java/org/mapstruct/ap/testutil/MapperTestBase.java @@ -72,6 +72,7 @@ public abstract class MapperTestBase { private static final String LINE_SEPARATOR = System.getProperty( "line.separator" ); private static final DiagnosticDescriptorComparator COMPARATOR = new DiagnosticDescriptorComparator(); + private static volatile boolean enhancedClassloader = false; private JavaCompiler compiler; private String sourceDir; @@ -104,12 +105,16 @@ public abstract class MapperTestBase { createOutputDirs(); - Thread.currentThread().setContextClassLoader( - new URLClassLoader( - new URL[] { new File( classOutputDir ).toURI().toURL() }, - Thread.currentThread().getContextClassLoader() - ) - ); + if ( !enhancedClassloader ) { + // we need to make sure that the the generated classes are loaded by the same classloader as the test has + // been loaded already. Otherwise some tests won't work. + URLClassLoader classLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); + Class clazz = URLClassLoader.class; + Method method = clazz.getDeclaredMethod( "addURL", new Class[] { URL.class } ); + method.setAccessible( true ); + method.invoke( classLoader, new File( classOutputDir ).toURI().toURL() ); + enhancedClassloader = true; + } } @BeforeMethod