aboutsummaryrefslogtreecommitdiff
path: root/src/netbeansAgent
diff options
context:
space:
mode:
Diffstat (limited to 'src/netbeansAgent')
-rw-r--r--src/netbeansAgent/lombok/netbeans/agent/NetbeansEntryPoint.java75
-rw-r--r--src/netbeansAgent/lombok/netbeans/agent/NetbeansPatcher.java134
-rw-r--r--src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java183
-rw-r--r--src/netbeansAgent/lombok/netbeans/agent/package-info.java26
4 files changed, 0 insertions, 418 deletions
diff --git a/src/netbeansAgent/lombok/netbeans/agent/NetbeansEntryPoint.java b/src/netbeansAgent/lombok/netbeans/agent/NetbeansEntryPoint.java
deleted file mode 100644
index 963b70b5..00000000
--- a/src/netbeansAgent/lombok/netbeans/agent/NetbeansEntryPoint.java
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * 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;
-
-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!
- 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
deleted file mode 100644
index ffe491ae..00000000
--- a/src/netbeansAgent/lombok/netbeans/agent/NetbeansPatcher.java
+++ /dev/null
@@ -1,134 +0,0 @@
-/*
- * 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.core.Agent;
-import lombok.patcher.Hook;
-import lombok.patcher.MethodTarget;
-import lombok.patcher.ScriptManager;
-import lombok.patcher.StackRequest;
-import lombok.patcher.scripts.ScriptBuilder;
-
-/**
- * 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);
-
- 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", "void", "org.netbeans.Module", "java.util.List"))
- .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", "boolean", "java.lang.ClassLoader", "java.lang.String"))
- .valueMethod(new Hook("lombok.netbeans.agent.PatchFixes", "getResource_value", "java.net.URL", "java.lang.ClassLoader", "java.lang.String"))
- .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", "boolean", "java.lang.ClassLoader", "java.lang.String"))
- .valueMethod(new Hook("lombok.netbeans.agent.PatchFixes", "getResources_value", "java.util.Enumeration", "java.lang.ClassLoader", "java.lang.String"))
- .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", "boolean", "java.lang.ClassLoader", "java.lang.String"))
- .valueMethod(new Hook("lombok.netbeans.agent.PatchFixes", "loadClass_value", "java.lang.Class", "java.lang.ClassLoader", "java.lang.String"))
- .build());
- }
-
- 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", "void",
- "com.sun.tools.javac.api.JavacTaskImpl", "com.sun.source.util.TaskListener"))
- .build());
-
- sm.addScript(ScriptBuilder.wrapReturnValue()
- .request(StackRequest.RETURN_VALUE, StackRequest.PARAM1)
- .transplant()
- .target(new MethodTarget("org.netbeans.modules.java.source.parsing.JavacParser", "createJavacTask",
- "com.sun.tools.javac.api.JavacTaskImpl",
- "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", "void",
- "com.sun.tools.javac.api.JavacTaskImpl", "org.netbeans.api.java.source.ClasspathInfo"))
- .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", "com.sun.source.tree.Tree",
- "javax.lang.model.element.Element"))
- .replacementMethod(new Hook("lombok.netbeans.agent.PatchFixes", "returnNullForGeneratedNode", "com.sun.source.tree.Tree",
- "com.sun.source.util.Trees", "javax.lang.model.element.Element", "java.lang.Object"))
- .requestExtra(StackRequest.PARAM1).transplant()
- .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", "long",
- "com.sun.source.tree.CompilationUnitTree", "com.sun.source.tree.Tree"))
- .replacementMethod(new Hook("lombok.netbeans.agent.PatchFixes", "returnMinus1ForGeneratedNode", "long",
- "com.sun.source.util.SourcePositions", "com.sun.source.tree.CompilationUnitTree", "com.sun.source.tree.Tree"))
- .transplant().build());
-
- sm.addScript(ScriptBuilder.wrapMethodCall()
- .target(new MethodTarget("org.netbeans.modules.java.source.save.CasualDiff", "filterHidden"))
- .methodToWrap(new Hook("java.lang.Iterable", "iterator", "java.util.Iterator"))
- .wrapMethod(new Hook("lombok.netbeans.agent.PatchFixes", "filterGenerated", "java.util.Iterator",
- "java.util.Iterator"))
- .transplant().build());
- }
-}
diff --git a/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java b/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java
deleted file mode 100644
index f45a4736..00000000
--- a/src/netbeansAgent/lombok/netbeans/agent/PatchFixes.java
+++ /dev/null
@@ -1,183 +0,0 @@
-/*
- * 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.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;
-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.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 {
- public static boolean loadClass_decision(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(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(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 void 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));
- }
- }
-
- 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();
- }
- }
- }
-
- 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();
- }
- }
-
- 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);
- }
-
- public static void addTaskListenerWhenCallingJavac(JavacTaskImpl task,
- ClasspathInfo cpInfo) throws Exception {
- if (task == null) return;
- Class<?> entryPoint = JavacTaskImpl.class.getClassLoader().loadClass("lombok.netbeans.agent.NetbeansEntryPoint");
- if (entryPoint == null) {
- /* TODO tell the user that lombok is not working. loadClass is not supposed to return null, but netbeans' loader does anyway. */
- System.err.println("[LOMBOK] ClassLoader not patched correctly.");
- } else {
- task.setTaskListener((TaskListener) entryPoint.getConstructor(Context.class).newInstance(task.getContext()));
- }
- }
-
- public static Iterator<JCTree> filterGenerated(final Iterator<JCTree> it) {
- return new Iterator<JCTree>() {
- 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/package-info.java b/src/netbeansAgent/lombok/netbeans/agent/package-info.java
deleted file mode 100644
index a6a2f2db..00000000
--- a/src/netbeansAgent/lombok/netbeans/agent/package-info.java
+++ /dev/null
@@ -1,26 +0,0 @@
-/*
- * 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;