aboutsummaryrefslogtreecommitdiff
path: root/src/lombok/agent/eclipse/EclipseParserTransformer.java
blob: fa549ff860dd64ec25f380b9883cd3f97d4b8915 (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
package lombok.agent.eclipse;

import org.objectweb.asm.ClassAdapter;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodAdapter;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

class EclipseParserTransformer {
	private static final String COMPILATION_UNIT_DECLARATION_SIG =
		"Lorg/eclipse/jdt/internal/compiler/ast/CompilationUnitDeclaration;";
	
	private static final String TOPATCH_METHOD_NAME = "endParse";
	private static final String TOPATCH_METHOD_DESC = "(I)" + COMPILATION_UNIT_DECLARATION_SIG;
	
	private final byte[] in;
	
	EclipseParserTransformer(byte[] classfileBuffer) {
		in = classfileBuffer;
	}
	
	byte[] transform() {
		ClassReader reader = new ClassReader(in);
		ClassWriter writer = new ClassWriter(reader, 0);
		ClassAdapter adapter = new ParserPatcherAdapter(writer);
		reader.accept(adapter, 0);
		return writer.toByteArray();
	}
	
	private static class ParserPatcherAdapter extends ClassAdapter {
		public ParserPatcherAdapter(ClassVisitor cv) {
			super(cv);
		}
		
		@Override public MethodVisitor visitMethod(int access, String name, String desc,
				String signature, String[] exceptions) {
			MethodVisitor writerVisitor = super.visitMethod(access, name, desc, signature, exceptions);
			if ( !TOPATCH_METHOD_NAME.equals(name) || !TOPATCH_METHOD_DESC.equals(desc) ) return writerVisitor;
			
			return new PatcherMethodVisitor(writerVisitor);
		}
	}
	
	static class PatcherMethodVisitor extends MethodAdapter {
		private static final String TARGET_STATIC_CLASS = "java/lombok/ClassLoaderWorkaround";
		private static final String TARGET_STATIC_METHOD_NAME = "transformCompilationUnitDeclaration";
		private static final String TARGET_STATIC_METHOD_DESC = "(Ljava/lang/Object;)V";
		
		private boolean alreadyCalled = false;
		
		PatcherMethodVisitor(MethodVisitor mv) {
			super(mv);
		}
		
		@Override public void visitInsn(int opcode) {
			if ( opcode == Opcodes.ARETURN ) insertHookCall();
			
			super.visitInsn(opcode);
		}
		
		@Override public void visitMethodInsn(int opcode, String owner, String name,
				String desc) {
			if ( opcode == Opcodes.INVOKESTATIC &&
					TARGET_STATIC_CLASS.equals(owner) && TARGET_STATIC_METHOD_NAME.equals(name) ) alreadyCalled = true;
			super.visitMethodInsn(opcode, owner, name, desc);
		}
		
		/** When this method is called, the stack should hold the reference to the
		 * just-parsed CompilationUnitDeclaration object that is about to be returned
		 * to whomever wants it. We will put a call to a method of our choosing in,
		 * which will transform the CUD. The stack is not modified (that is, that method
		 * returns a CUD).
		 */
		private void insertHookCall() {
			if ( alreadyCalled ) return;
			super.visitInsn(Opcodes.DUP);
			super.visitMethodInsn(Opcodes.INVOKESTATIC, TARGET_STATIC_CLASS,
					TARGET_STATIC_METHOD_NAME, TARGET_STATIC_METHOD_DESC);
		}
	}
}