aboutsummaryrefslogtreecommitdiff
path: root/src/netbeansAgent/lombok/netbeans/agent/NetbeansPatcher.java
blob: e6ae5c03261036e7c745cff2cb4eaaa805ff40b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * 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", "(Lorg/netbeans/Module;Ljava/util/List;)V"))
				.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)
				.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(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",
						"(Lcom/sun/tools/javac/api/JavacTaskImpl;Lorg/netbeans/api/java/source/ClasspathInfo;)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).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",
						"(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"))
				.transplant().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());
	}
}