diff options
author | Reinier Zwitserloot <reinier@tipit.to> | 2009-12-02 00:05:21 +0100 |
---|---|---|
committer | Reinier Zwitserloot <reinier@tipit.to> | 2009-12-02 00:05:21 +0100 |
commit | 8165f4628f75618f1cd105ad8901f273ada261e4 (patch) | |
tree | df16c2db4f585d5dfb2f2473a3484edd772107e0 /src | |
parent | 199f7f74fd35462dced087280af066cf2cad7596 (diff) | |
download | lombok-8165f4628f75618f1cd105ad8901f273ada261e4.tar.gz lombok-8165f4628f75618f1cd105ad8901f273ada261e4.tar.bz2 lombok-8165f4628f75618f1cd105ad8901f273ada261e4.zip |
netbeans agent now works!
Diffstat (limited to 'src')
3 files changed, 86 insertions, 3 deletions
diff --git a/src/netbeansAgent/lombok/netbeans/agent/NetbeansEntryPoint.java b/src/netbeansAgent/lombok/netbeans/agent/NetbeansEntryPoint.java index 0a13a474..963b70b5 100644 --- a/src/netbeansAgent/lombok/netbeans/agent/NetbeansEntryPoint.java +++ b/src/netbeansAgent/lombok/netbeans/agent/NetbeansEntryPoint.java @@ -68,7 +68,8 @@ public class NetbeansEntryPoint implements TaskListener { @Override public void finished(TaskEvent event) { if (TaskEvent.Kind.PARSE == event.getKind()) { JavacTransformer transformer = new JavacTransformer(new DummyMessager()); //TODO hook into netbeans error reporting! - transformer.transform(context, Collections.singleton((JCCompilationUnit)event.getCompilationUnit())); + JCCompilationUnit compilationUnit = (JCCompilationUnit) event.getCompilationUnit(); + transformer.transform(context, Collections.singleton(compilationUnit)); } } } diff --git a/src/netbeansAgent/lombok/netbeans/agent/NetbeansPatcher.java b/src/netbeansAgent/lombok/netbeans/agent/NetbeansPatcher.java index bffedfc2..39df269b 100644 --- a/src/netbeansAgent/lombok/netbeans/agent/NetbeansPatcher.java +++ b/src/netbeansAgent/lombok/netbeans/agent/NetbeansPatcher.java @@ -47,12 +47,42 @@ public class NetbeansPatcher extends Agent { ScriptManager sm = new ScriptManager(); sm.registerTransformer(instrumentation); + patchNetbeansClassLoader(sm); patchNetbeansJavac(sm); patchNetbeansMissingPositionAwareness(sm); if (reloadExistingClasses) sm.reloadClasses(instrumentation); } + private static void patchNetbeansClassLoader(ScriptManager sm) { + sm.addScript(ScriptBuilder.exitEarly() + .transplant().request(StackRequest.PARAM1, StackRequest.PARAM2) + .target(new MethodTarget("org.netbeans.StandardModule$OneModuleClassLoader", "<init>")) + .decisionMethod(new Hook("lombok/netbeans/agent/PatchFixes", "addSelfToClassLoader", "(Lorg/netbeans/Module;Ljava/util/List;)Z")) + .build()); + sm.addScript(ScriptBuilder.exitEarly() + .transplant() + .request(StackRequest.THIS, StackRequest.PARAM1) + .target(new MethodTarget("org.netbeans.ProxyClassLoader", "getResource")) + .decisionMethod(new Hook("lombok/netbeans/agent/PatchFixes", "getResource_decision", "(Ljava/lang/ClassLoader;Ljava/lang/String;)Z")) + .valueMethod(new Hook("lombok/netbeans/agent/PatchFixes", "getResource_value", "(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/net/URL;")) + .build()); + sm.addScript(ScriptBuilder.exitEarly() + .transplant() + .request(StackRequest.THIS, StackRequest.PARAM1) + .target(new MethodTarget("org.netbeans.ProxyClassLoader", "getResources")) + .decisionMethod(new Hook("lombok/netbeans/agent/PatchFixes", "getResources_decision", "(Ljava/lang/ClassLoader;Ljava/lang/String;)Z")) + .valueMethod(new Hook("lombok/netbeans/agent/PatchFixes", "getResources_value", "(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/util/Enumeration;")) + .build()); + sm.addScript(ScriptBuilder.exitEarly() + .transplant() + .target(new MethodTarget("org.netbeans.ProxyClassLoader", "loadClass")) + .request(StackRequest.THIS, StackRequest.PARAM1) + .decisionMethod(new Hook("lombok/netbeans/agent/PatchFixes", "loadClass_decision", "(Ljava/lang/ClassLoader;Ljava/lang/String;)Z")) + .valueMethod(new Hook("lombok/netbeans/agent/PatchFixes", "loadClass_value", "(Ljava/lang/ClassLoader;Ljava/lang/String;)Ljava/lang/Class;")) + .build()); + } + private static void patchNetbeansJavac(ScriptManager sm) { sm.addScript(ScriptBuilder.wrapReturnValue() .request(StackRequest.THIS, StackRequest.PARAM1) diff --git a/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java b/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java index 3a58d381..d9b858cd 100644 --- a/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java +++ b/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java @@ -21,13 +21,22 @@ */ package lombok.netbeans.agent; +import java.io.File; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.net.URL; +import java.util.Enumeration; import java.util.Iterator; +import java.util.List; import java.util.NoSuchElementException; import javax.lang.model.element.Element; +import lombok.Lombok; +import lombok.patcher.inject.LiveInjector; + +import org.netbeans.Module; +import org.netbeans.ProxyClassLoader; import org.netbeans.api.java.source.ClasspathInfo; import com.sun.source.tree.CompilationUnitTree; @@ -43,8 +52,50 @@ import com.sun.tools.javac.util.Context; // This footwork was converted into a patch script form by me (rzwitserloot). See: // http://code.google.com/p/projectlombok/issues/detail?id=20#c3 public class PatchFixes { + public static boolean loadClass_decision(@SuppressWarnings("unused") ClassLoader loader, String name) throws Exception { + return name.startsWith("lombok."); + } + + public static Class<?> loadClass_value(ClassLoader loader, String name) throws Exception { + int last = name.lastIndexOf('.'); + String pkg = (last >= 0) ? name.substring(0, last) : ""; + Method m = ProxyClassLoader.class.getDeclaredMethod("selfLoadClass", String.class, String.class); + m.setAccessible(true); + return (Class<?>)m.invoke(loader, pkg, name); + } + + public static boolean getResource_decision(@SuppressWarnings("unused") ClassLoader loader, String name) throws Exception { + return name.startsWith("META-INF/services/lombok."); + } + + public static URL getResource_value(ClassLoader loader, String name) throws Exception { + Method m = ProxyClassLoader.class.getDeclaredMethod("findResource", String.class); + m.setAccessible(true); + return (URL) m.invoke(loader, name); + } + + public static boolean getResources_decision(@SuppressWarnings("unused") ClassLoader loader, String name) throws Exception { + return name.startsWith("META-INF/services/lombok."); + } + + public static Enumeration<?> getResources_value(ClassLoader loader, String name) throws Exception { + Method m = ProxyClassLoader.class.getDeclaredMethod("findResources", String.class); + m.setAccessible(true); + return (Enumeration<?>) m.invoke(loader, name); + } + + public static boolean addSelfToClassLoader(Module module, List<File> classPath) { + if (module.getJarFile().getName().equals("org-netbeans-libs-javacimpl.jar")) { + String lombokJarLoc = LiveInjector.findPathJar(Lombok.class); + classPath.add(new File(lombokJarLoc)); + } + + return false; + } + public static void fixContentOnSetTaskListener(JavacTaskImpl that, TaskListener taskListener) throws Throwable { Context context = that.getContext(); + if (context.get(TaskListener.class) != null) context.put(TaskListener.class, (TaskListener)null); if (taskListener != null) { @@ -81,8 +132,9 @@ public class PatchFixes { } public static void addTaskListenerWhenCallingJavac(JavacTaskImpl task, - @SuppressWarnings("unused") /* Will come in handy later */ ClasspathInfo cpInfo) { - task.setTaskListener(new NetbeansEntryPoint(task.getContext())); + @SuppressWarnings("unused") /* Will come in handy later */ ClasspathInfo cpInfo) throws Exception { + Class<?> entryPoint = JavacTaskImpl.class.getClassLoader().loadClass("lombok.netbeans.agent.NetbeansEntryPoint"); + task.setTaskListener((TaskListener) entryPoint.getConstructor(Context.class).newInstance(task.getContext())); } public static Iterator<JCTree> filterGenerated(final Iterator<JCTree> it) { |