diff options
author | Reinier Zwitserloot <reinier@tipit.to> | 2009-09-01 00:08:40 +0200 |
---|---|---|
committer | Reinier Zwitserloot <reinier@tipit.to> | 2009-09-01 00:08:40 +0200 |
commit | 72cc800fd0c3e122a71a42536d5ced5297d6fa4e (patch) | |
tree | bc3e6677f50bb7ca9955857ae0a0ae6484bf0332 /src_eclipseagent/java | |
parent | 65b9365b5419f69ac78ad895308024b4bac15fbd (diff) | |
download | lombok-72cc800fd0c3e122a71a42536d5ced5297d6fa4e.tar.gz lombok-72cc800fd0c3e122a71a42536d5ced5297d6fa4e.tar.bz2 lombok-72cc800fd0c3e122a71a42536d5ced5297d6fa4e.zip |
Fixed issue #26: Starting eclipse's help feature just shows you a 500 error, ond depending on your eclipse version, a long stack trace.
The problem boiled down to the JSP compiler used by the help system also being instrumented with lombok, but that's not exactly the environment lombok was expecting. Fixed by simply disabling lombok when the environments don't match what we expect. In the process, the instrumentation has been made a little more robust; multiple separate OSGi modules can all be instrumented now, instead of the first one winning.
Diffstat (limited to 'src_eclipseagent/java')
-rw-r--r-- | src_eclipseagent/java/lombok/eclipse/ClassLoaderWorkaround.java | 83 |
1 files changed, 62 insertions, 21 deletions
diff --git a/src_eclipseagent/java/lombok/eclipse/ClassLoaderWorkaround.java b/src_eclipseagent/java/lombok/eclipse/ClassLoaderWorkaround.java index f804ef1d..42eeb02d 100644 --- a/src_eclipseagent/java/lombok/eclipse/ClassLoaderWorkaround.java +++ b/src_eclipseagent/java/lombok/eclipse/ClassLoaderWorkaround.java @@ -26,6 +26,8 @@ import java.io.IOException; import java.io.InputStream; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; /** @@ -48,24 +50,65 @@ public class ClassLoaderWorkaround { throw (T)t; } - private static boolean initialized; - private static Method transform; + private static final Map<ClassLoader, Method> transform = new HashMap<ClassLoader, Method>(); public static void transformCompilationUnitDeclaration(Object parser, Object cud) throws Exception { - initialize(cud); + Method transformMethod = getTransformMethod(cud); try { - transform.invoke(null, parser, cud); + checkTypeCompatible(parser.getClass(), "org.eclipse.jdt.internal.compiler.parser.Parser"); + checkTypeCompatible(cud.getClass(), "org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration"); + try { + transformMethod.invoke(null, parser, cud); + } catch ( IllegalArgumentException ex ) { + checkTypeCompatible2(parser.getClass(), "org.eclipse.jdt.internal.compiler.parser.Parser"); + checkTypeCompatible2(cud.getClass(), "org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration"); + throw ex; + } } catch ( InvocationTargetException e ) { throw sneakyThrow(e.getCause()); } } - private static void initialize(Object cud) throws ClassNotFoundException { - if ( initialized ) { - if ( transform == null ) throw new ClassNotFoundException("lombok.eclipse.TransformEclipseAST"); - return; + private static void checkTypeCompatible(Class<? extends Object> c, String expected) { + StringBuilder sb = new StringBuilder(); + while ( c != null ) { + if ( c.getName().equals(expected) ) return; + sb.append(" ").append(c.getName()); + c = c.getSuperclass(); + } + + System.err.println("Not a match to " + expected); + System.err.println(sb.toString()); + } + + private static void checkTypeCompatible2(Class<? extends Object> c, String expected) { + StringBuilder sb = new StringBuilder(); + while ( c != null ) { + sb.append(" ").append(c.getName()); + c = c.getSuperclass(); } + System.err.println("Expecting " + expected); + System.err.println(sb.toString()); + } + + private static Method getTransformMethod(Object cud) throws ClassNotFoundException { + ClassLoader contextLoader = Thread.currentThread().getContextClassLoader(); + + synchronized ( transform ) { + if ( !transform.containsKey(contextLoader)) { + System.out.println("Creating classloader: " + Thread.currentThread()); + transform.put(contextLoader, findTransformMethod(cud)); + } + + Method m = transform.get(contextLoader); + if ( m == null ) throw new ClassNotFoundException("lombok.eclipse.TransformEclipseAST"); + return m; + + } + } + + private static Method findTransformMethod(Object cud) throws ClassNotFoundException { final ClassLoader parent = cud.getClass().getClassLoader(); ClassLoader loader = new ClassLoader() { @Override public Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException { @@ -103,19 +146,17 @@ public class ClassLoaderWorkaround { } }; - try { - Class<?> c = loader.loadClass("lombok.eclipse.TransformEclipseAST"); - for ( Method m : c.getMethods() ) { - if ( m.getName().equals("transform") ) { - Class<?>[] types = m.getParameterTypes(); - if ( types.length != 2 ) continue; - if ( !types[0].getName().equals("org.eclipse.jdt.internal.compiler.parser.Parser") ) continue; - if ( !types[1].getName().equals("org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration") ) continue; - transform = m; - break; - } + Class<?> c = loader.loadClass("lombok.eclipse.TransformEclipseAST"); + for ( Method method : c.getMethods() ) { + if ( method.getName().equals("transform") ) { + Class<?>[] types = method.getParameterTypes(); + if ( types.length != 2 ) continue; + if ( !types[0].getName().equals("org.eclipse.jdt.internal.compiler.parser.Parser") ) continue; + if ( !types[1].getName().equals("org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration") ) continue; + return method; } - } catch ( ClassNotFoundException ignore ) {} - initialized = true; + } + + throw new ClassNotFoundException("lombok.eclipse.TransformEclipseAST"); } } |