From dfa401018d0eff370b34f1e9d7bb1d5fd498a631 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Mon, 30 Nov 2009 00:49:42 +0100 Subject: All basic elements of netbeans support are here except the actual agent part. --- .../lombok/netbeans/agent/NetbeansEntryPoint.java | 53 ++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/netbeansAgent/lombok/netbeans/agent/NetbeansEntryPoint.java (limited to 'src/netbeansAgent/lombok/netbeans/agent/NetbeansEntryPoint.java') diff --git a/src/netbeansAgent/lombok/netbeans/agent/NetbeansEntryPoint.java b/src/netbeansAgent/lombok/netbeans/agent/NetbeansEntryPoint.java new file mode 100644 index 00000000..d825ae62 --- /dev/null +++ b/src/netbeansAgent/lombok/netbeans/agent/NetbeansEntryPoint.java @@ -0,0 +1,53 @@ +package lombok.netbeans.agent; + +import java.util.Collections; + +import javax.annotation.processing.Messager; +import javax.lang.model.element.AnnotationMirror; +import javax.lang.model.element.AnnotationValue; +import javax.lang.model.element.Element; +import javax.tools.Diagnostic.Kind; + +import lombok.javac.JavacTransformer; + +import com.sun.source.util.TaskEvent; +import com.sun.source.util.TaskListener; +import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; +import com.sun.tools.javac.util.Context; + +public class NetbeansEntryPoint implements TaskListener { + public class DummyMessager implements Messager { + @Override public void printMessage(Kind kind, CharSequence msg) { + System.err.printf("%s: %s\n", kind, msg); + } + + @Override public void printMessage(Kind kind, CharSequence msg, Element e) { + printMessage(kind, msg); + } + + @Override public void printMessage(Kind kind, CharSequence msg, Element e, AnnotationMirror a) { + printMessage(kind, msg); + } + + @Override public void printMessage(Kind kind, CharSequence msg, Element e, AnnotationMirror a, AnnotationValue v) { + printMessage(kind, msg); + } + } + + private final Context context; + + public NetbeansEntryPoint(Context context) { + this.context = context; + } + + @Override public void started(TaskEvent event) { + //we run at the end, so all the action is in #finished. + } + + @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())); + } + } +} -- cgit From 2886277b27299ecdb845e3e967554d9ef041c0a8 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Mon, 30 Nov 2009 23:15:55 +0100 Subject: Made it simpler. --- .../lombok/netbeans/agent/NetbeansEntryPoint.java | 21 ++++++++++ .../lombok/netbeans/agent/PatchFixes.java | 46 +++++++++++++--------- .../netbeans/agent/TaskListenerProvider.java | 10 ----- .../netbeans/agent/TaskListenerProviderImpl.java | 12 ------ 4 files changed, 48 insertions(+), 41 deletions(-) delete mode 100644 src/netbeansAgent/lombok/netbeans/agent/TaskListenerProvider.java delete mode 100644 src/netbeansAgent/lombok/netbeans/agent/TaskListenerProviderImpl.java (limited to 'src/netbeansAgent/lombok/netbeans/agent/NetbeansEntryPoint.java') diff --git a/src/netbeansAgent/lombok/netbeans/agent/NetbeansEntryPoint.java b/src/netbeansAgent/lombok/netbeans/agent/NetbeansEntryPoint.java index d825ae62..0a13a474 100644 --- a/src/netbeansAgent/lombok/netbeans/agent/NetbeansEntryPoint.java +++ b/src/netbeansAgent/lombok/netbeans/agent/NetbeansEntryPoint.java @@ -1,3 +1,24 @@ +/* + * Copyright © 2009 Reinier Zwitserloot and Roel Spilker. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package lombok.netbeans.agent; import java.util.Collections; diff --git a/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java b/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java index aea01b1a..cd5ef965 100644 --- a/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java +++ b/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java @@ -1,3 +1,24 @@ +/* + * Copyright © 2009 Reinier Zwitserloot and Roel Spilker. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package lombok.netbeans.agent; import java.lang.reflect.InvocationTargetException; @@ -8,7 +29,6 @@ import java.util.NoSuchElementException; import javax.lang.model.element.Element; import org.netbeans.api.java.source.ClasspathInfo; -import org.openide.util.Lookup; import com.sun.source.tree.CompilationUnitTree; import com.sun.source.tree.Tree; @@ -19,10 +39,10 @@ import com.sun.tools.javac.api.JavacTaskImpl; import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.util.Context; +// A lot of the footwork on the netbeans support has been done by Jan Lahoda, who is awesome. (jlahoda@netbeans.org) +// 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 { - //Contributed by Jan Lahoda (jlahoda@netbeans.org) - //Turned into a patch script by rzwitserloot. - //see http://code.google.com/p/projectlombok/issues/detail?id=20#c3 public static void fixContentOnSetTaskListener(JavacTaskImpl that, TaskListener taskListener) throws Throwable { Context context = that.getContext(); if (context.get(TaskListener.class) != null) @@ -57,29 +77,17 @@ public class PatchFixes { } } - //Contributed by Jan Lahoda (jlahoda@netbeans.org) - //Turned into a patch script by rzwitserloot. - //see http://code.google.com/p/projectlombok/issues/detail?id=20#c3 public static long returnMinus1ForGeneratedNode(SourcePositions that, CompilationUnitTree cu, Tree tree) { int start = (int) that.getStartPosition(cu, tree); if (start < 0) return -1; return that.getEndPosition(cu, tree); } - //Contributed by Jan Lahoda (jlahoda@netbeans.org) - //Turned into a patch script by rzwitserloot. - //see http://code.google.com/p/projectlombok/issues/detail?id=20#c3 - public static void addTaskListenerWhenCallingJavac(JavacTaskImpl task, ClasspathInfo cpInfo) { - TaskListenerProvider p = Lookup.getDefault().lookup(TaskListenerProvider.class); - if (p != null) { - TaskListener l = p.create(task.getContext(), cpInfo); - task.setTaskListener(l); - } + public static void addTaskListenerWhenCallingJavac(JavacTaskImpl task, + @SuppressWarnings("unused") /* Will come in handy later */ ClasspathInfo cpInfo) { + task.setTaskListener(new NetbeansEntryPoint(task.getContext())); } - //Contributed by Jan Lahoda (jlahoda@netbeans.org) - //Turned into a patch script by rzwitserloot. - //see http://code.google.com/p/projectlombok/issues/detail?id=20#c3 public static Iterator filterGenerated(final Iterator it) { return new Iterator() { private JCTree next; diff --git a/src/netbeansAgent/lombok/netbeans/agent/TaskListenerProvider.java b/src/netbeansAgent/lombok/netbeans/agent/TaskListenerProvider.java deleted file mode 100644 index 3c721930..00000000 --- a/src/netbeansAgent/lombok/netbeans/agent/TaskListenerProvider.java +++ /dev/null @@ -1,10 +0,0 @@ -package lombok.netbeans.agent; - -import org.netbeans.api.java.source.ClasspathInfo; - -import com.sun.source.util.TaskListener; -import com.sun.tools.javac.util.Context; - -public interface TaskListenerProvider { - public TaskListener create(Context c, ClasspathInfo info); -} diff --git a/src/netbeansAgent/lombok/netbeans/agent/TaskListenerProviderImpl.java b/src/netbeansAgent/lombok/netbeans/agent/TaskListenerProviderImpl.java deleted file mode 100644 index 3ebf5475..00000000 --- a/src/netbeansAgent/lombok/netbeans/agent/TaskListenerProviderImpl.java +++ /dev/null @@ -1,12 +0,0 @@ -package lombok.netbeans.agent; - -import org.netbeans.api.java.source.ClasspathInfo; - -import com.sun.source.util.TaskListener; -import com.sun.tools.javac.util.Context; - -public class TaskListenerProviderImpl implements TaskListenerProvider { - public TaskListener create(final Context context, ClasspathInfo cpInfo) { - return new NetbeansEntryPoint(context); - } -} -- cgit From 8165f4628f75618f1cd105ad8901f273ada261e4 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Wed, 2 Dec 2009 00:05:21 +0100 Subject: netbeans agent now works! --- .classpath | 3 +- deps/lombok/netbeans/boot_6.8beta.jar | Bin 0 -> 263129 bytes .../lombok/netbeans/agent/NetbeansEntryPoint.java | 3 +- .../lombok/netbeans/agent/NetbeansPatcher.java | 30 +++++++++++ .../lombok/netbeans/agent/PatchFixes.java | 56 ++++++++++++++++++++- 5 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 deps/lombok/netbeans/boot_6.8beta.jar (limited to 'src/netbeansAgent/lombok/netbeans/agent/NetbeansEntryPoint.java') diff --git a/.classpath b/.classpath index 5873a158..02129162 100644 --- a/.classpath +++ b/.classpath @@ -14,8 +14,9 @@ - + + diff --git a/deps/lombok/netbeans/boot_6.8beta.jar b/deps/lombok/netbeans/boot_6.8beta.jar new file mode 100644 index 00000000..f82709d2 Binary files /dev/null and b/deps/lombok/netbeans/boot_6.8beta.jar differ 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", "")) + .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 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 filterGenerated(final Iterator it) { -- cgit