summaryrefslogtreecommitdiff
path: root/src/main/java/cuchaz/enigma/analysis/IndexReferenceVisitor.java
blob: f37f1e906a662e60262f02d2f6c8018dd3312d79 (plain) (blame)
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
package cuchaz.enigma.analysis;

import cuchaz.enigma.bytecode.AccessFlags;
import cuchaz.enigma.mapping.MethodDescriptor;
import cuchaz.enigma.mapping.Signature;
import cuchaz.enigma.mapping.entry.ClassEntry;
import cuchaz.enigma.mapping.entry.MethodDefEntry;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.Handle;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

public class IndexReferenceVisitor extends ClassVisitor {
	private final JarIndex index;
	private ClassEntry classEntry;

	public IndexReferenceVisitor(JarIndex index, int api) {
		super(api);
		this.index = index;
	}

	@Override
	public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
		this.classEntry = new ClassEntry(name);
	}

	@Override
	public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
		MethodDefEntry entry = new MethodDefEntry(classEntry, name, new MethodDescriptor(desc), Signature.createSignature(signature), new AccessFlags(access));
		return new Method(this.index, entry, this.api);
	}

	private class Method extends MethodVisitor {
		private final JarIndex index;
		private final MethodDefEntry callerEntry;

		public Method(JarIndex index, MethodDefEntry callerEntry, int api) {
			super(api);
			this.index = index;
			this.callerEntry = callerEntry;
		}

		@Override
		public void visitFieldInsn(int opcode, String owner, String name, String desc) {
			this.index.indexFieldAccess(callerEntry, owner, name, desc);
		}

		@Override
		public void visitMethodInsn(int opcode, String owner, String name, String desc, boolean itf) {
			this.index.indexMethodCall(callerEntry, owner, name, desc);
		}

		@Override
		public void visitInvokeDynamicInsn(String name, String desc, Handle bsm, Object... bsmArgs) {
			for (Object bsmArg : bsmArgs){
				if (bsmArg instanceof Handle){
					Handle handle = (Handle)bsmArg;
					switch (handle.getTag()){
						case Opcodes.H_GETFIELD:
						case Opcodes.H_GETSTATIC:
						case Opcodes.H_PUTFIELD:
						case Opcodes.H_PUTSTATIC:
							this.index.indexFieldAccess(callerEntry, handle.getOwner(), handle.getName(), handle.getDesc());
							break;
						case Opcodes.H_INVOKEINTERFACE:
						case Opcodes.H_INVOKESPECIAL:
						case Opcodes.H_INVOKESTATIC:
						case Opcodes.H_INVOKEVIRTUAL:
						case Opcodes.H_NEWINVOKESPECIAL:
							this.index.indexMethodCall(callerEntry, handle.getOwner(), handle.getName(), handle.getDesc());
							break;
					}
				}
			}
		}
	}
}