From 72cc800fd0c3e122a71a42536d5ced5297d6fa4e Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Tue, 1 Sep 2009 00:08:40 +0200 Subject: 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. --- .../java/lombok/eclipse/ClassLoaderWorkaround.java | 83 ++++++++++++++++------ 1 file changed, 62 insertions(+), 21 deletions(-) (limited to 'src_eclipseagent/java') 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 transform = new HashMap(); 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 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 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"); } } -- cgit