diff options
Diffstat (limited to 'src/cuchaz/enigma/analysis/SourceIndexBehaviorVisitor.java')
| -rw-r--r-- | src/cuchaz/enigma/analysis/SourceIndexBehaviorVisitor.java | 164 |
1 files changed, 164 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/analysis/SourceIndexBehaviorVisitor.java b/src/cuchaz/enigma/analysis/SourceIndexBehaviorVisitor.java new file mode 100644 index 0000000..4155128 --- /dev/null +++ b/src/cuchaz/enigma/analysis/SourceIndexBehaviorVisitor.java | |||
| @@ -0,0 +1,164 @@ | |||
| 1 | /******************************************************************************* | ||
| 2 | * Copyright (c) 2014 Jeff Martin. | ||
| 3 | * All rights reserved. This program and the accompanying materials | ||
| 4 | * are made available under the terms of the GNU Public License v3.0 | ||
| 5 | * which accompanies this distribution, and is available at | ||
| 6 | * http://www.gnu.org/licenses/gpl.html | ||
| 7 | * | ||
| 8 | * Contributors: | ||
| 9 | * Jeff Martin - initial API and implementation | ||
| 10 | ******************************************************************************/ | ||
| 11 | package cuchaz.enigma.analysis; | ||
| 12 | |||
| 13 | import com.strobel.assembler.metadata.MemberReference; | ||
| 14 | import com.strobel.assembler.metadata.MethodDefinition; | ||
| 15 | import com.strobel.assembler.metadata.MethodReference; | ||
| 16 | import com.strobel.assembler.metadata.ParameterDefinition; | ||
| 17 | import com.strobel.assembler.metadata.TypeReference; | ||
| 18 | import com.strobel.decompiler.languages.TextLocation; | ||
| 19 | import com.strobel.decompiler.languages.java.ast.AstNode; | ||
| 20 | import com.strobel.decompiler.languages.java.ast.ConstructorDeclaration; | ||
| 21 | import com.strobel.decompiler.languages.java.ast.IdentifierExpression; | ||
| 22 | import com.strobel.decompiler.languages.java.ast.InvocationExpression; | ||
| 23 | import com.strobel.decompiler.languages.java.ast.Keys; | ||
| 24 | import com.strobel.decompiler.languages.java.ast.MemberReferenceExpression; | ||
| 25 | import com.strobel.decompiler.languages.java.ast.MethodDeclaration; | ||
| 26 | import com.strobel.decompiler.languages.java.ast.ObjectCreationExpression; | ||
| 27 | import com.strobel.decompiler.languages.java.ast.ParameterDeclaration; | ||
| 28 | import com.strobel.decompiler.languages.java.ast.SimpleType; | ||
| 29 | import com.strobel.decompiler.languages.java.ast.SuperReferenceExpression; | ||
| 30 | import com.strobel.decompiler.languages.java.ast.ThisReferenceExpression; | ||
| 31 | |||
| 32 | import cuchaz.enigma.mapping.ArgumentEntry; | ||
| 33 | import cuchaz.enigma.mapping.BehaviorEntry; | ||
| 34 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 35 | import cuchaz.enigma.mapping.ConstructorEntry; | ||
| 36 | import cuchaz.enigma.mapping.FieldEntry; | ||
| 37 | import cuchaz.enigma.mapping.MethodEntry; | ||
| 38 | import cuchaz.enigma.mapping.Signature; | ||
| 39 | |||
| 40 | public class SourceIndexBehaviorVisitor extends SourceIndexVisitor { | ||
| 41 | |||
| 42 | private BehaviorEntry m_behaviorEntry; | ||
| 43 | |||
| 44 | public SourceIndexBehaviorVisitor(BehaviorEntry behaviorEntry) { | ||
| 45 | m_behaviorEntry = behaviorEntry; | ||
| 46 | } | ||
| 47 | |||
| 48 | @Override | ||
| 49 | public Void visitMethodDeclaration(MethodDeclaration node, SourceIndex index) { | ||
| 50 | return recurse(node, index); | ||
| 51 | } | ||
| 52 | |||
| 53 | @Override | ||
| 54 | public Void visitConstructorDeclaration(ConstructorDeclaration node, SourceIndex index) { | ||
| 55 | return recurse(node, index); | ||
| 56 | } | ||
| 57 | |||
| 58 | @Override | ||
| 59 | public Void visitInvocationExpression(InvocationExpression node, SourceIndex index) { | ||
| 60 | MemberReference ref = node.getUserData(Keys.MEMBER_REFERENCE); | ||
| 61 | |||
| 62 | // get the behavior entry | ||
| 63 | ClassEntry classEntry = new ClassEntry(ref.getDeclaringType().getInternalName()); | ||
| 64 | BehaviorEntry behaviorEntry = null; | ||
| 65 | if (ref instanceof MethodReference) { | ||
| 66 | MethodReference methodRef = (MethodReference)ref; | ||
| 67 | if (methodRef.isConstructor()) { | ||
| 68 | behaviorEntry = new ConstructorEntry(classEntry, new Signature(ref.getSignature())); | ||
| 69 | } else if (methodRef.isTypeInitializer()) { | ||
| 70 | behaviorEntry = new ConstructorEntry(classEntry); | ||
| 71 | } else { | ||
| 72 | behaviorEntry = new MethodEntry(classEntry, ref.getName(), new Signature(ref.getSignature())); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | if (behaviorEntry != null) { | ||
| 76 | // get the node for the token | ||
| 77 | AstNode tokenNode = null; | ||
| 78 | if (node.getTarget() instanceof MemberReferenceExpression) { | ||
| 79 | tokenNode = ((MemberReferenceExpression)node.getTarget()).getMemberNameToken(); | ||
| 80 | } else if (node.getTarget() instanceof SuperReferenceExpression) { | ||
| 81 | tokenNode = node.getTarget(); | ||
| 82 | } else if (node.getTarget() instanceof ThisReferenceExpression) { | ||
| 83 | tokenNode = node.getTarget(); | ||
| 84 | } | ||
| 85 | if (tokenNode != null) { | ||
| 86 | index.addReference(tokenNode, behaviorEntry, m_behaviorEntry); | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | return recurse(node, index); | ||
| 91 | } | ||
| 92 | |||
| 93 | @Override | ||
| 94 | public Void visitMemberReferenceExpression(MemberReferenceExpression node, SourceIndex index) { | ||
| 95 | MemberReference ref = node.getUserData(Keys.MEMBER_REFERENCE); | ||
| 96 | if (ref != null) { | ||
| 97 | // make sure this is actually a field | ||
| 98 | if (ref.getSignature().indexOf('(') >= 0) { | ||
| 99 | throw new Error("Expected a field here! got " + ref); | ||
| 100 | } | ||
| 101 | |||
| 102 | ClassEntry classEntry = new ClassEntry(ref.getDeclaringType().getInternalName()); | ||
| 103 | FieldEntry fieldEntry = new FieldEntry(classEntry, ref.getName()); | ||
| 104 | index.addReference(node.getMemberNameToken(), fieldEntry, m_behaviorEntry); | ||
| 105 | } | ||
| 106 | |||
| 107 | return recurse(node, index); | ||
| 108 | } | ||
| 109 | |||
| 110 | @Override | ||
| 111 | public Void visitSimpleType(SimpleType node, SourceIndex index) { | ||
| 112 | TypeReference ref = node.getUserData(Keys.TYPE_REFERENCE); | ||
| 113 | if (node.getIdentifierToken().getStartLocation() != TextLocation.EMPTY) { | ||
| 114 | ClassEntry classEntry = new ClassEntry(ref.getInternalName()); | ||
| 115 | index.addReference(node.getIdentifierToken(), classEntry, m_behaviorEntry); | ||
| 116 | } | ||
| 117 | |||
| 118 | return recurse(node, index); | ||
| 119 | } | ||
| 120 | |||
| 121 | @Override | ||
| 122 | public Void visitParameterDeclaration(ParameterDeclaration node, SourceIndex index) { | ||
| 123 | ParameterDefinition def = node.getUserData(Keys.PARAMETER_DEFINITION); | ||
| 124 | ClassEntry classEntry = new ClassEntry(def.getDeclaringType().getInternalName()); | ||
| 125 | MethodDefinition methodDef = (MethodDefinition)def.getMethod(); | ||
| 126 | BehaviorEntry behaviorEntry; | ||
| 127 | if (methodDef.isConstructor()) { | ||
| 128 | behaviorEntry = new ConstructorEntry(classEntry, new Signature(methodDef.getSignature())); | ||
| 129 | } else { | ||
| 130 | behaviorEntry = new MethodEntry(classEntry, methodDef.getName(), new Signature(methodDef.getSignature())); | ||
| 131 | } | ||
| 132 | ArgumentEntry argumentEntry = new ArgumentEntry(behaviorEntry, def.getPosition(), node.getName()); | ||
| 133 | index.addDeclaration(node.getNameToken(), argumentEntry); | ||
| 134 | |||
| 135 | return recurse(node, index); | ||
| 136 | } | ||
| 137 | |||
| 138 | @Override | ||
| 139 | public Void visitIdentifierExpression(IdentifierExpression node, SourceIndex index) { | ||
| 140 | MemberReference ref = node.getUserData(Keys.MEMBER_REFERENCE); | ||
| 141 | if (ref != null) { | ||
| 142 | ClassEntry classEntry = new ClassEntry(ref.getDeclaringType().getInternalName()); | ||
| 143 | FieldEntry fieldEntry = new FieldEntry(classEntry, ref.getName()); | ||
| 144 | index.addReference(node.getIdentifierToken(), fieldEntry, m_behaviorEntry); | ||
| 145 | } | ||
| 146 | |||
| 147 | return recurse(node, index); | ||
| 148 | } | ||
| 149 | |||
| 150 | @Override | ||
| 151 | public Void visitObjectCreationExpression(ObjectCreationExpression node, SourceIndex index) { | ||
| 152 | MemberReference ref = node.getUserData(Keys.MEMBER_REFERENCE); | ||
| 153 | if (ref != null) { | ||
| 154 | ClassEntry classEntry = new ClassEntry(ref.getDeclaringType().getInternalName()); | ||
| 155 | ConstructorEntry constructorEntry = new ConstructorEntry(classEntry, new Signature(ref.getSignature())); | ||
| 156 | if (node.getType() instanceof SimpleType) { | ||
| 157 | SimpleType simpleTypeNode = (SimpleType)node.getType(); | ||
| 158 | index.addReference(simpleTypeNode.getIdentifierToken(), constructorEntry, m_behaviorEntry); | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | return recurse(node, index); | ||
| 163 | } | ||
| 164 | } | ||