From 96b7c8ed471745366378d5b7cec8890d16532dee Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Sun, 29 Nov 2009 16:55:23 +0100 Subject: work in progress. --- .../lombok/netbeans/agent/NetbeansPatcher.java | 98 ++++++++++++++++++++++ .../lombok/netbeans/agent/PatchFixes.java | 75 +++++++++++++++++ .../netbeans/agent/TaskListenerProvider.java | 5 ++ .../lombok/netbeans/agent/package-info.java | 26 ++++++ 4 files changed, 204 insertions(+) create mode 100644 src/netbeansAgent/lombok/netbeans/agent/NetbeansPatcher.java create mode 100644 src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java create mode 100644 src/netbeansAgent/lombok/netbeans/agent/TaskListenerProvider.java create mode 100644 src/netbeansAgent/lombok/netbeans/agent/package-info.java (limited to 'src') diff --git a/src/netbeansAgent/lombok/netbeans/agent/NetbeansPatcher.java b/src/netbeansAgent/lombok/netbeans/agent/NetbeansPatcher.java new file mode 100644 index 00000000..4210aa87 --- /dev/null +++ b/src/netbeansAgent/lombok/netbeans/agent/NetbeansPatcher.java @@ -0,0 +1,98 @@ +/* + * 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.instrument.Instrumentation; + +import lombok.patcher.Hook; +import lombok.patcher.MethodTarget; +import lombok.patcher.ScriptManager; +import lombok.patcher.StackRequest; +import lombok.patcher.equinox.EquinoxClassLoader; +import lombok.patcher.scripts.ScriptBuilder; + +public class NetbeansPatcher { + private NetbeansPatcher() {} + + public static void agentmain(@SuppressWarnings("unused") String agentArgs, Instrumentation instrumentation) throws Exception { + registerPatchScripts(instrumentation, true); + } + + public static void premain(@SuppressWarnings("unused") String agentArgs, Instrumentation instrumentation) throws Exception { + registerPatchScripts(instrumentation, false); + } + + private static void registerPatchScripts(Instrumentation instrumentation, boolean reloadExistingClasses) { + ScriptManager sm = new ScriptManager(); + sm.registerTransformer(instrumentation); + EquinoxClassLoader.addPrefix("lombok."); + EquinoxClassLoader.registerScripts(sm); + + patchNetbeansJavac(sm); + patchNetbeansMissingPositionAwareness(sm); + + if (reloadExistingClasses) sm.reloadClasses(instrumentation); + } + + private static void patchNetbeansJavac(ScriptManager sm) { + sm.addScript(ScriptBuilder.wrapReturnValue() + .request(StackRequest.THIS, StackRequest.PARAM1) + .transplant() + .target(new MethodTarget("com.sun.tools.javac.api.JavacTaskImpl", "setTaskListener")) + .wrapMethod(new Hook("lombok/netbeans/agent/PatchFixes", "fixContentOnSetTaskListener", + "(Lcom/sun/tools/javac/api/JavacTaskImpl;Lcom/sun/source/util/TaskListener;)V")) + .build()); + + sm.addScript(ScriptBuilder.wrapReturnValue() + .request() + .transplant() + .target(new MethodTarget("org.netbeans.modules.java.source.parsing.JavacParser", "createJavacTask", + "com.sun.tools.javac.api.JavacTaskImpl", + "ClasspathInfo", "DiagnosticListener", "java.lang.String", "boolean", + "ClassNamesForFileOraculum", "CancelService")) + .wrapMethod(new Hook("lombok/netbeans/agent/PatchFixes", "addTaskListenerWhenCallingJavac", + "()V")) + .build()); + } + + private static void patchNetbeansMissingPositionAwareness(ScriptManager sm) { + sm.addScript(ScriptBuilder.replaceMethodCall() + .target(new MethodTarget("org.netbeans.modules.java.editor.overridden.ComputeAnnotations", + "createAnnotations")) + .methodToReplace(new Hook("com/sun/source/util/Trees", "getTree", + "(Ljavax/lang/model/element/Element;)Lcom/sun/source/tree/Tree;")) + .replacementMethod(new Hook("lombok/netbeans/agent/PatchFixes", "returnNullForGeneratedNode", + "(Lcom/sun/source/util/Trees;Ljavax/lang/model/element/Element;Ljava/lang/Object;)" + + "Lcom/sun/source/tree/Tree;")) + .requestExtra(StackRequest.PARAM1) + .build()); + + sm.addScript(ScriptBuilder.replaceMethodCall() + .target(new MethodTarget("org.netbeans.modules.java.source.parsing.FindMethodRegionsVisitor", + "visitMethod")) + .methodToReplace(new Hook("com/sun/source/util/SourcePositions", "getEndPosition", + "(Lcom/sun/source/tree/CompilationUnitTree;Lcom/sun/source/tree/Tree;)J")) + .replacementMethod(new Hook("lombok/netbeans/agent/PatchFixes", "returnMinus1ForGeneratedNode", + "(Lcom/sun/source/util/SourcePositions;Lcom/sun/source/tree/CompilationUnitTree;Lcom/sun/source/tree/Tree;)J")) + .build()); + } +} diff --git a/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java b/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java new file mode 100644 index 00000000..26ddd1e4 --- /dev/null +++ b/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java @@ -0,0 +1,75 @@ +package lombok.netbeans.agent; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import javax.lang.model.element.Element; + +import com.sun.source.tree.CompilationUnitTree; +import com.sun.source.tree.Tree; +import com.sun.source.util.SourcePositions; +import com.sun.source.util.TaskListener; +import com.sun.source.util.Trees; +import com.sun.tools.javac.api.JavacTaskImpl; +import com.sun.tools.javac.util.Context; + +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) + context.put(TaskListener.class, (TaskListener)null); + if (taskListener != null) { + try { + Method m = JavacTaskImpl.class.getDeclaredMethod("wrap", TaskListener.class); + try { + m.setAccessible(true); + } catch (SecurityException ignore) {} + TaskListener w = (TaskListener)m.invoke(that, taskListener); + context.put(TaskListener.class, w); + } catch (InvocationTargetException e) { + throw e.getCause(); + } + } + } + + //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 Tree returnNullForGeneratedNode(Trees trees, Element element, Object o) throws Throwable { + try { + Tree tree = trees.getTree(element); + if (tree == null) return null; + CompilationUnitTree unit = (CompilationUnitTree) o.getClass().getMethod("getCompilationUnit").invoke(o); + int startPos = (int) trees.getSourcePositions().getStartPosition(unit, tree); + if (startPos == -1) return null; + return tree; + } catch (InvocationTargetException e) { + throw e.getCause(); + } + } + + //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() { + TaskListenerProvider p = /* Lookup.getDefault().lookup(TLP.class) */; + if (p != null) { + TaskListener l = p.create(context, cpInfo); + task.setTaskListener(l); + } + + return; + } +} diff --git a/src/netbeansAgent/lombok/netbeans/agent/TaskListenerProvider.java b/src/netbeansAgent/lombok/netbeans/agent/TaskListenerProvider.java new file mode 100644 index 00000000..59ac6c54 --- /dev/null +++ b/src/netbeansAgent/lombok/netbeans/agent/TaskListenerProvider.java @@ -0,0 +1,5 @@ +package lombok.netbeans.agent; + +public interface TaskListenerProvider { + public TaskListener create(Context c, ClasspathInfo info); +} diff --git a/src/netbeansAgent/lombok/netbeans/agent/package-info.java b/src/netbeansAgent/lombok/netbeans/agent/package-info.java new file mode 100644 index 00000000..a6a2f2db --- /dev/null +++ b/src/netbeansAgent/lombok/netbeans/agent/package-info.java @@ -0,0 +1,26 @@ +/* + * 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. + */ + +/** + * Contains the mechanism that instruments netbeans by being loaded as a javaagent. + */ +package lombok.netbeans.agent; -- cgit From 29c2ba1bbba7393423463b6c92da2068b6fea7bb Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Sun, 29 Nov 2009 20:53:54 +0100 Subject: work in progress --- src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java | 6 ++++-- src/netbeansAgent/lombok/netbeans/agent/TaskListenerProvider.java | 5 +++++ 2 files changed, 9 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java b/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java index 26ddd1e4..c12e6dad 100644 --- a/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java +++ b/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java @@ -5,6 +5,8 @@ import java.lang.reflect.Method; import javax.lang.model.element.Element; +import org.netbeans.api.java.source.ClasspathInfo; + import com.sun.source.tree.CompilationUnitTree; import com.sun.source.tree.Tree; import com.sun.source.util.SourcePositions; @@ -63,8 +65,8 @@ 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 addTaskListenerWhenCallingJavac() { - TaskListenerProvider p = /* Lookup.getDefault().lookup(TLP.class) */; + public static void addTaskListenerWhenCallingJavac(Context context, ClasspathInfo cpInfo) { + TaskListenerProvider p = /* Lookup.getDefault().lookup(TaskListenerProvider.class) */; if (p != null) { TaskListener l = p.create(context, cpInfo); task.setTaskListener(l); diff --git a/src/netbeansAgent/lombok/netbeans/agent/TaskListenerProvider.java b/src/netbeansAgent/lombok/netbeans/agent/TaskListenerProvider.java index 59ac6c54..3c721930 100644 --- a/src/netbeansAgent/lombok/netbeans/agent/TaskListenerProvider.java +++ b/src/netbeansAgent/lombok/netbeans/agent/TaskListenerProvider.java @@ -1,5 +1,10 @@ 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); } -- cgit 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. --- .classpath | 1 + deps/lombok/netbeans/org-openide-util_6.8beta.jar | Bin 0 -> 645380 bytes src/core/lombok/javac/JavacAST.java | 1 + .../lombok/netbeans/agent/NetbeansEntryPoint.java | 53 ++++++++++++++++++++ .../lombok/netbeans/agent/NetbeansPatcher.java | 11 ++++- .../lombok/netbeans/agent/PatchFixes.java | 55 +++++++++++++++++++-- .../netbeans/agent/TaskListenerProviderImpl.java | 12 +++++ 7 files changed, 126 insertions(+), 7 deletions(-) create mode 100644 deps/lombok/netbeans/org-openide-util_6.8beta.jar create mode 100644 src/netbeansAgent/lombok/netbeans/agent/NetbeansEntryPoint.java create mode 100644 src/netbeansAgent/lombok/netbeans/agent/TaskListenerProviderImpl.java (limited to 'src') diff --git a/.classpath b/.classpath index dcd7bd2f..5873a158 100644 --- a/.classpath +++ b/.classpath @@ -15,6 +15,7 @@ + diff --git a/deps/lombok/netbeans/org-openide-util_6.8beta.jar b/deps/lombok/netbeans/org-openide-util_6.8beta.jar new file mode 100644 index 00000000..b3dbeac6 Binary files /dev/null and b/deps/lombok/netbeans/org-openide-util_6.8beta.jar differ diff --git a/src/core/lombok/javac/JavacAST.java b/src/core/lombok/javac/JavacAST.java index 33c167f9..9cfe7f34 100644 --- a/src/core/lombok/javac/JavacAST.java +++ b/src/core/lombok/javac/JavacAST.java @@ -125,6 +125,7 @@ public class JavacAST extends AST { /** @return A TreeMaker instance that you can use to create new AST nodes. */ public TreeMaker getTreeMaker() { + treeMaker.at(-1); return treeMaker; } 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())); + } + } +} diff --git a/src/netbeansAgent/lombok/netbeans/agent/NetbeansPatcher.java b/src/netbeansAgent/lombok/netbeans/agent/NetbeansPatcher.java index 4210aa87..108b4c45 100644 --- a/src/netbeansAgent/lombok/netbeans/agent/NetbeansPatcher.java +++ b/src/netbeansAgent/lombok/netbeans/agent/NetbeansPatcher.java @@ -63,14 +63,14 @@ public class NetbeansPatcher { .build()); sm.addScript(ScriptBuilder.wrapReturnValue() - .request() + .request(StackRequest.RETURN_VALUE, StackRequest.PARAM1) .transplant() .target(new MethodTarget("org.netbeans.modules.java.source.parsing.JavacParser", "createJavacTask", "com.sun.tools.javac.api.JavacTaskImpl", "ClasspathInfo", "DiagnosticListener", "java.lang.String", "boolean", "ClassNamesForFileOraculum", "CancelService")) .wrapMethod(new Hook("lombok/netbeans/agent/PatchFixes", "addTaskListenerWhenCallingJavac", - "()V")) + "(Lcom/sun/tools/javac/api/JavacTaskImpl;Lorg/netbeans/api/java/source/ClasspathInfo;)V")) .build()); } @@ -94,5 +94,12 @@ public class NetbeansPatcher { .replacementMethod(new Hook("lombok/netbeans/agent/PatchFixes", "returnMinus1ForGeneratedNode", "(Lcom/sun/source/util/SourcePositions;Lcom/sun/source/tree/CompilationUnitTree;Lcom/sun/source/tree/Tree;)J")) .build()); + + sm.addScript(ScriptBuilder.wrapMethodCall() + .target(new MethodTarget("org.netbeans.modules.java.source.save.CasualDiff", "filterHidden")) + .methodToWrap(new Hook("java/lang/Iterable", "iterator", "()L/java/util/Iterator;")) + .wrapMethod(new Hook("lombok/netbeans/agent/PatchFixes", "filterGenerated", + "(Ljava/util/Iterator;)L/java/util/Iterator;")) + .transplant().build()); } } diff --git a/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java b/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java index c12e6dad..aea01b1a 100644 --- a/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java +++ b/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java @@ -2,10 +2,13 @@ package lombok.netbeans.agent; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.Iterator; +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; @@ -13,6 +16,7 @@ import com.sun.source.util.SourcePositions; import com.sun.source.util.TaskListener; import com.sun.source.util.Trees; import com.sun.tools.javac.api.JavacTaskImpl; +import com.sun.tools.javac.tree.JCTree; import com.sun.tools.javac.util.Context; public class PatchFixes { @@ -65,13 +69,54 @@ 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 addTaskListenerWhenCallingJavac(Context context, ClasspathInfo cpInfo) { - TaskListenerProvider p = /* Lookup.getDefault().lookup(TaskListenerProvider.class) */; + public static void addTaskListenerWhenCallingJavac(JavacTaskImpl task, ClasspathInfo cpInfo) { + TaskListenerProvider p = Lookup.getDefault().lookup(TaskListenerProvider.class); if (p != null) { - TaskListener l = p.create(context, cpInfo); + TaskListener l = p.create(task.getContext(), cpInfo); task.setTaskListener(l); } - - return; + } + + //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; + private boolean hasNext; + + { + calc(); + } + + private void calc() { + while (it.hasNext()) { + JCTree n = it.next(); + if (n.pos != -1) { + hasNext = true; + next = n; + return; + } + } + + hasNext = false; + next = null; + } + + @Override public boolean hasNext() { + return hasNext; + } + + @Override public JCTree next() { + if (!hasNext) throw new NoSuchElementException(); + JCTree n = next; + calc(); + return n; + } + + @Override public void remove() { + throw new UnsupportedOperationException(); + } + }; } } diff --git a/src/netbeansAgent/lombok/netbeans/agent/TaskListenerProviderImpl.java b/src/netbeansAgent/lombok/netbeans/agent/TaskListenerProviderImpl.java new file mode 100644 index 00000000..3ebf5475 --- /dev/null +++ b/src/netbeansAgent/lombok/netbeans/agent/TaskListenerProviderImpl.java @@ -0,0 +1,12 @@ +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 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') 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 92e2afe9b0dccc7d0055b52d07ab3128c2adf4de Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Tue, 1 Dec 2009 00:24:42 +0100 Subject: Fixes --- .../lombok/netbeans/agent/NetbeansPatcher.java | 8 ++++---- .../lombok/netbeans/agent/PatchFixes.java | 23 ++++++++++------------ 2 files changed, 14 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/netbeansAgent/lombok/netbeans/agent/NetbeansPatcher.java b/src/netbeansAgent/lombok/netbeans/agent/NetbeansPatcher.java index 108b4c45..2a87ad6c 100644 --- a/src/netbeansAgent/lombok/netbeans/agent/NetbeansPatcher.java +++ b/src/netbeansAgent/lombok/netbeans/agent/NetbeansPatcher.java @@ -67,8 +67,8 @@ public class NetbeansPatcher { .transplant() .target(new MethodTarget("org.netbeans.modules.java.source.parsing.JavacParser", "createJavacTask", "com.sun.tools.javac.api.JavacTaskImpl", - "ClasspathInfo", "DiagnosticListener", "java.lang.String", "boolean", - "ClassNamesForFileOraculum", "CancelService")) + "org.netbeans.api.java.source.ClasspathInfo", "javax.tools.DiagnosticListener", "java.lang.String", "boolean", + "com.sun.tools.javac.api.ClassNamesForFileOraculum", "com.sun.tools.javac.util.CancelService")) .wrapMethod(new Hook("lombok/netbeans/agent/PatchFixes", "addTaskListenerWhenCallingJavac", "(Lcom/sun/tools/javac/api/JavacTaskImpl;Lorg/netbeans/api/java/source/ClasspathInfo;)V")) .build()); @@ -83,7 +83,7 @@ public class NetbeansPatcher { .replacementMethod(new Hook("lombok/netbeans/agent/PatchFixes", "returnNullForGeneratedNode", "(Lcom/sun/source/util/Trees;Ljavax/lang/model/element/Element;Ljava/lang/Object;)" + "Lcom/sun/source/tree/Tree;")) - .requestExtra(StackRequest.PARAM1) + .requestExtra(StackRequest.PARAM1).transplant() .build()); sm.addScript(ScriptBuilder.replaceMethodCall() @@ -93,7 +93,7 @@ public class NetbeansPatcher { "(Lcom/sun/source/tree/CompilationUnitTree;Lcom/sun/source/tree/Tree;)J")) .replacementMethod(new Hook("lombok/netbeans/agent/PatchFixes", "returnMinus1ForGeneratedNode", "(Lcom/sun/source/util/SourcePositions;Lcom/sun/source/tree/CompilationUnitTree;Lcom/sun/source/tree/Tree;)J")) - .build()); + .transplant().build()); sm.addScript(ScriptBuilder.wrapMethodCall() .target(new MethodTarget("org.netbeans.modules.java.source.save.CasualDiff", "filterHidden")) diff --git a/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java b/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java index cd5ef965..4aa7f77c 100644 --- a/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java +++ b/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java @@ -48,22 +48,19 @@ public class PatchFixes { if (context.get(TaskListener.class) != null) context.put(TaskListener.class, (TaskListener)null); if (taskListener != null) { - try { - Method m = JavacTaskImpl.class.getDeclaredMethod("wrap", TaskListener.class); - try { - m.setAccessible(true); - } catch (SecurityException ignore) {} - TaskListener w = (TaskListener)m.invoke(that, taskListener); - context.put(TaskListener.class, w); - } catch (InvocationTargetException e) { - throw e.getCause(); - } +// try { +// Method m = JavacTaskImpl.class.getDeclaredMethod("wrap", TaskListener.class); +// try { +// m.setAccessible(true); +// } catch (SecurityException ignore) {} +// TaskListener w = (TaskListener)m.invoke(that, taskListener); + context.put(TaskListener.class, taskListener); +// } catch (InvocationTargetException e) { +// throw e.getCause(); +// } } } - //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 Tree returnNullForGeneratedNode(Trees trees, Element element, Object o) throws Throwable { try { Tree tree = trees.getTree(element); -- cgit From 199f7f74fd35462dced087280af066cf2cad7596 Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Tue, 1 Dec 2009 13:53:07 +0100 Subject: Generalized the agent mechanism; now 1 lombok.jar can serve as the agent for BOTH netbeans AND eclipse, and is future-ready for an IntelliJ agent. --- buildScripts/compile.ant.xml | 3 +- src/core/lombok/core/Agent.java | 62 ++++++++++++++++++++++ .../lombok/eclipse/agent/EclipsePatcher.java | 14 ++--- .../lombok/netbeans/agent/NetbeansPatcher.java | 24 ++++----- .../lombok/netbeans/agent/PatchFixes.java | 20 +++---- 5 files changed, 90 insertions(+), 33 deletions(-) create mode 100644 src/core/lombok/core/Agent.java (limited to 'src') diff --git a/buildScripts/compile.ant.xml b/buildScripts/compile.ant.xml index a1181f87..1cc6933c 100644 --- a/buildScripts/compile.ant.xml +++ b/buildScripts/compile.ant.xml @@ -159,8 +159,7 @@ lombok code including the various agents. - - + diff --git a/src/core/lombok/core/Agent.java b/src/core/lombok/core/Agent.java new file mode 100644 index 00000000..2ba6bb20 --- /dev/null +++ b/src/core/lombok/core/Agent.java @@ -0,0 +1,62 @@ +/* + * 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.core; + +import java.lang.instrument.Instrumentation; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +public abstract class Agent { + protected abstract void runAgent(String agentArgs, Instrumentation instrumentation, boolean injected) throws Exception; + + public static void agentmain(String agentArgs, Instrumentation instrumentation) throws Exception { + runAgents(agentArgs, instrumentation, true); + } + + public static void premain(String agentArgs, Instrumentation instrumentation) throws Exception { + runAgents(agentArgs, instrumentation, false); + } + + private static final List AGENT_NAMES = Collections.unmodifiableList(Arrays.asList( + "lombok.netbeans.agent.NetbeansPatcher", + "lombok.eclipse.agent.EclipsePatcher" + )); + + private static void runAgents(String agentArgs, Instrumentation instrumentation, boolean injected) throws Exception { + for (String agentName : AGENT_NAMES) { + try { + Class agentClass = Class.forName(agentName); + Agent agent = (Agent) agentClass.newInstance(); + agent.runAgent(agentArgs, instrumentation, injected); + } catch (ClassNotFoundException e) { + //That's okay - this lombok evidently is a version with support for something stripped out. + } catch (ClassCastException e) { + throw new InternalError("Lombok bug. Class: " + agentName + " is not an implementation of lombok.core.Agent"); + } catch (IllegalAccessException e) { + throw new InternalError("Lombok bug. Class: " + agentName + " is not public"); + } catch (InstantiationException e) { + throw new InternalError("Lombok bug. Class: " + agentName + " is not concrete or has no public no-args constructor"); + } + } + } +} diff --git a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java index 7d2a28bc..741328f1 100644 --- a/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java +++ b/src/eclipseAgent/lombok/eclipse/agent/EclipsePatcher.java @@ -26,6 +26,7 @@ import java.util.Collection; import java.util.Collections; import java.util.List; +import lombok.core.Agent; import lombok.patcher.Hook; import lombok.patcher.MethodTarget; import lombok.patcher.ScriptManager; @@ -41,15 +42,10 @@ import lombok.patcher.scripts.ScriptBuilder; * classes in this package for more information about which classes are transformed and how they are * transformed. */ -public class EclipsePatcher { - private EclipsePatcher() {} - - public static void agentmain(@SuppressWarnings("unused") String agentArgs, Instrumentation instrumentation) throws Exception { - registerPatchScripts(instrumentation, true); - } - - public static void premain(@SuppressWarnings("unused") String agentArgs, Instrumentation instrumentation) throws Exception { - registerPatchScripts(instrumentation, false); +public class EclipsePatcher extends Agent { + @Override + public void runAgent(String agentArgs, Instrumentation instrumentation, boolean injected) throws Exception { + registerPatchScripts(instrumentation, injected); } private static void registerPatchScripts(Instrumentation instrumentation, boolean reloadExistingClasses) { diff --git a/src/netbeansAgent/lombok/netbeans/agent/NetbeansPatcher.java b/src/netbeansAgent/lombok/netbeans/agent/NetbeansPatcher.java index 2a87ad6c..bffedfc2 100644 --- a/src/netbeansAgent/lombok/netbeans/agent/NetbeansPatcher.java +++ b/src/netbeansAgent/lombok/netbeans/agent/NetbeansPatcher.java @@ -23,29 +23,29 @@ package lombok.netbeans.agent; import java.lang.instrument.Instrumentation; +import lombok.core.Agent; import lombok.patcher.Hook; import lombok.patcher.MethodTarget; import lombok.patcher.ScriptManager; import lombok.patcher.StackRequest; -import lombok.patcher.equinox.EquinoxClassLoader; import lombok.patcher.scripts.ScriptBuilder; -public class NetbeansPatcher { - private NetbeansPatcher() {} - - public static void agentmain(@SuppressWarnings("unused") String agentArgs, Instrumentation instrumentation) throws Exception { - registerPatchScripts(instrumentation, true); - } - - public static void premain(@SuppressWarnings("unused") String agentArgs, Instrumentation instrumentation) throws Exception { - registerPatchScripts(instrumentation, false); +/** + * This is a java-agent that patches some of netbeans's classes so that lombok is initialized as Javac TaskListener, + * allowing us to change AST nodes anytime netbeans parses source code. It also fixes some of the places in netbeans that + * can't deal with generated code. + * + * The hard work on figuring out where to patch has been done by Jan Lahoda (jlahoda@netbeans.org) + */ +public class NetbeansPatcher extends Agent { + @Override + public void runAgent(String agentArgs, Instrumentation instrumentation, boolean injected) throws Exception { + registerPatchScripts(instrumentation, injected); } private static void registerPatchScripts(Instrumentation instrumentation, boolean reloadExistingClasses) { ScriptManager sm = new ScriptManager(); sm.registerTransformer(instrumentation); - EquinoxClassLoader.addPrefix("lombok."); - EquinoxClassLoader.registerScripts(sm); patchNetbeansJavac(sm); patchNetbeansMissingPositionAwareness(sm); diff --git a/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java b/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java index 4aa7f77c..3a58d381 100644 --- a/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java +++ b/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java @@ -48,16 +48,16 @@ public class PatchFixes { if (context.get(TaskListener.class) != null) context.put(TaskListener.class, (TaskListener)null); if (taskListener != null) { -// try { -// Method m = JavacTaskImpl.class.getDeclaredMethod("wrap", TaskListener.class); -// try { -// m.setAccessible(true); -// } catch (SecurityException ignore) {} -// TaskListener w = (TaskListener)m.invoke(that, taskListener); - context.put(TaskListener.class, taskListener); -// } catch (InvocationTargetException e) { -// throw e.getCause(); -// } + try { + Method m = JavacTaskImpl.class.getDeclaredMethod("wrap", TaskListener.class); + try { + m.setAccessible(true); + } catch (SecurityException ignore) {} + TaskListener w = (TaskListener)m.invoke(that, taskListener); + context.put(TaskListener.class, w); + } catch (InvocationTargetException e) { + throw e.getCause(); + } } } -- 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') 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 From 5bd3abdfe528344f0ae965b99332e20d7581680c Mon Sep 17 00:00:00 2001 From: Reinier Zwitserloot Date: Wed, 2 Dec 2009 00:05:28 +0100 Subject: [TRIVIAL] --- src/core/lombok/javac/HandlerLibrary.java | 49 ++++++++++++------------------- 1 file changed, 19 insertions(+), 30 deletions(-) (limited to 'src') diff --git a/src/core/lombok/javac/HandlerLibrary.java b/src/core/lombok/javac/HandlerLibrary.java index bbe9dec0..48218513 100644 --- a/src/core/lombok/javac/HandlerLibrary.java +++ b/src/core/lombok/javac/HandlerLibrary.java @@ -21,14 +21,12 @@ */ package lombok.javac; +import java.io.IOException; import java.lang.annotation.Annotation; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; -import java.util.Iterator; import java.util.Map; -import java.util.ServiceConfigurationError; -import java.util.ServiceLoader; import javax.annotation.processing.Messager; import javax.tools.Diagnostic; @@ -85,46 +83,36 @@ public class HandlerLibrary { public static HandlerLibrary load(Messager messager) { HandlerLibrary library = new HandlerLibrary(messager); - loadAnnotationHandlers(library); - loadVisitorHandlers(library); + try { + loadAnnotationHandlers(library); + loadVisitorHandlers(library); + } catch (IOException e) { + System.err.println("Lombok isn't running due to misconfigured SPI files: " + e); + } return library; } /** Uses SPI Discovery to find implementations of {@link JavacAnnotationHandler}. */ @SuppressWarnings("unchecked") - private static void loadAnnotationHandlers(HandlerLibrary lib) { + private static void loadAnnotationHandlers(HandlerLibrary lib) throws IOException { //No, that seemingly superfluous reference to JavacAnnotationHandler's classloader is not in fact superfluous! - Iterator it = ServiceLoader.load(JavacAnnotationHandler.class, - JavacAnnotationHandler.class.getClassLoader()).iterator(); - while (it.hasNext()) { - try { - JavacAnnotationHandler handler = it.next(); - Class annotationClass = - SpiLoadUtil.findAnnotationClass(handler.getClass(), JavacAnnotationHandler.class); - AnnotationHandlerContainer container = new AnnotationHandlerContainer(handler, annotationClass); - if (lib.annotationHandlers.put(container.annotationClass.getName(), container) != null) { - lib.javacWarning("Duplicate handlers for annotation type: " + container.annotationClass.getName()); - } - lib.typeLibrary.addType(container.annotationClass.getName()); - } catch (ServiceConfigurationError e) { - lib.javacWarning("Can't load Lombok annotation handler for javac", e); + for (JavacAnnotationHandler handler : SpiLoadUtil.findServices(JavacAnnotationHandler.class, JavacAnnotationHandler.class.getClassLoader())) { + Class annotationClass = + SpiLoadUtil.findAnnotationClass(handler.getClass(), JavacAnnotationHandler.class); + AnnotationHandlerContainer container = new AnnotationHandlerContainer(handler, annotationClass); + if (lib.annotationHandlers.put(container.annotationClass.getName(), container) != null) { + lib.javacWarning("Duplicate handlers for annotation type: " + container.annotationClass.getName()); } + lib.typeLibrary.addType(container.annotationClass.getName()); } } /** Uses SPI Discovery to find implementations of {@link JavacASTVisitor}. */ - private static void loadVisitorHandlers(HandlerLibrary lib) { + private static void loadVisitorHandlers(HandlerLibrary lib) throws IOException { //No, that seemingly superfluous reference to JavacASTVisitor's classloader is not in fact superfluous! - Iterator it = ServiceLoader.load(JavacASTVisitor.class, - JavacASTVisitor.class.getClassLoader()).iterator(); - while (it.hasNext()) { - try { - JavacASTVisitor handler = it.next(); - lib.visitorHandlers.add(handler); - } catch (ServiceConfigurationError e) { - lib.javacWarning("Can't load Lombok visitor handler for javac", e); - } + for (JavacASTVisitor visitor : SpiLoadUtil.findServices(JavacASTVisitor.class, JavacASTVisitor.class.getClassLoader())) { + lib.visitorHandlers.add(visitor); } } @@ -177,6 +165,7 @@ public class HandlerLibrary { if (container == null) continue; try { + System.out.println("Calling handle on: "+ container.handler.getClass().getName()); handled |= container.handle(node); } catch (AnnotationValueDecodeFail fail) { fail.owner.setError(fail.getMessage(), fail.idx); -- cgit