diff options
Diffstat (limited to 'src/cuchaz/enigma/analysis/SourceIndexClassVisitor.java')
| -rw-r--r-- | src/cuchaz/enigma/analysis/SourceIndexClassVisitor.java | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/analysis/SourceIndexClassVisitor.java b/src/cuchaz/enigma/analysis/SourceIndexClassVisitor.java new file mode 100644 index 0000000..db0bc0b --- /dev/null +++ b/src/cuchaz/enigma/analysis/SourceIndexClassVisitor.java | |||
| @@ -0,0 +1,112 @@ | |||
| 1 | /******************************************************************************* | ||
| 2 | * Copyright (c) 2015 Jeff Martin. | ||
| 3 | * All rights reserved. This program and the accompanying materials | ||
| 4 | * are made available under the terms of the GNU Lesser General Public | ||
| 5 | * License v3.0 which accompanies this distribution, and is available at | ||
| 6 | * http://www.gnu.org/licenses/lgpl.html | ||
| 7 | * | ||
| 8 | * Contributors: | ||
| 9 | * Jeff Martin - initial API and implementation | ||
| 10 | ******************************************************************************/ | ||
| 11 | package cuchaz.enigma.analysis; | ||
| 12 | |||
| 13 | import com.strobel.assembler.metadata.FieldDefinition; | ||
| 14 | import com.strobel.assembler.metadata.MethodDefinition; | ||
| 15 | import com.strobel.assembler.metadata.TypeDefinition; | ||
| 16 | import com.strobel.assembler.metadata.TypeReference; | ||
| 17 | import com.strobel.decompiler.languages.TextLocation; | ||
| 18 | import com.strobel.decompiler.languages.java.ast.AstNode; | ||
| 19 | import com.strobel.decompiler.languages.java.ast.ConstructorDeclaration; | ||
| 20 | import com.strobel.decompiler.languages.java.ast.EnumValueDeclaration; | ||
| 21 | import com.strobel.decompiler.languages.java.ast.FieldDeclaration; | ||
| 22 | import com.strobel.decompiler.languages.java.ast.Keys; | ||
| 23 | import com.strobel.decompiler.languages.java.ast.MethodDeclaration; | ||
| 24 | import com.strobel.decompiler.languages.java.ast.SimpleType; | ||
| 25 | import com.strobel.decompiler.languages.java.ast.TypeDeclaration; | ||
| 26 | import com.strobel.decompiler.languages.java.ast.VariableInitializer; | ||
| 27 | |||
| 28 | import cuchaz.enigma.mapping.BehaviorEntry; | ||
| 29 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 30 | import cuchaz.enigma.mapping.ConstructorEntry; | ||
| 31 | import cuchaz.enigma.mapping.FieldEntry; | ||
| 32 | import cuchaz.enigma.mapping.ProcyonEntryFactory; | ||
| 33 | |||
| 34 | public class SourceIndexClassVisitor extends SourceIndexVisitor { | ||
| 35 | |||
| 36 | private ClassEntry m_classEntry; | ||
| 37 | |||
| 38 | public SourceIndexClassVisitor(ClassEntry classEntry) { | ||
| 39 | m_classEntry = classEntry; | ||
| 40 | } | ||
| 41 | |||
| 42 | @Override | ||
| 43 | public Void visitTypeDeclaration(TypeDeclaration node, SourceIndex index) { | ||
| 44 | // is this this class, or a subtype? | ||
| 45 | TypeDefinition def = node.getUserData(Keys.TYPE_DEFINITION); | ||
| 46 | ClassEntry classEntry = new ClassEntry(def.getInternalName()); | ||
| 47 | if (!classEntry.equals(m_classEntry)) { | ||
| 48 | // it's a sub-type, recurse | ||
| 49 | index.addDeclaration(node.getNameToken(), classEntry); | ||
| 50 | return node.acceptVisitor(new SourceIndexClassVisitor(classEntry), index); | ||
| 51 | } | ||
| 52 | |||
| 53 | return recurse(node, index); | ||
| 54 | } | ||
| 55 | |||
| 56 | @Override | ||
| 57 | public Void visitSimpleType(SimpleType node, SourceIndex index) { | ||
| 58 | TypeReference ref = node.getUserData(Keys.TYPE_REFERENCE); | ||
| 59 | if (node.getIdentifierToken().getStartLocation() != TextLocation.EMPTY) { | ||
| 60 | ClassEntry classEntry = new ClassEntry(ref.getInternalName()); | ||
| 61 | index.addReference(node.getIdentifierToken(), classEntry, m_classEntry); | ||
| 62 | } | ||
| 63 | |||
| 64 | return recurse(node, index); | ||
| 65 | } | ||
| 66 | |||
| 67 | @Override | ||
| 68 | public Void visitMethodDeclaration(MethodDeclaration node, SourceIndex index) { | ||
| 69 | MethodDefinition def = node.getUserData(Keys.METHOD_DEFINITION); | ||
| 70 | BehaviorEntry behaviorEntry = ProcyonEntryFactory.getBehaviorEntry(def); | ||
| 71 | AstNode tokenNode = node.getNameToken(); | ||
| 72 | |||
| 73 | if (behaviorEntry instanceof ConstructorEntry) { | ||
| 74 | ConstructorEntry constructorEntry = (ConstructorEntry)behaviorEntry; | ||
| 75 | if (constructorEntry.isStatic()) { | ||
| 76 | // for static initializers, check elsewhere for the token node | ||
| 77 | tokenNode = node.getModifiers().firstOrNullObject(); | ||
| 78 | } | ||
| 79 | } | ||
| 80 | index.addDeclaration(tokenNode, behaviorEntry); | ||
| 81 | return node.acceptVisitor(new SourceIndexBehaviorVisitor(behaviorEntry), index); | ||
| 82 | } | ||
| 83 | |||
| 84 | @Override | ||
| 85 | public Void visitConstructorDeclaration(ConstructorDeclaration node, SourceIndex index) { | ||
| 86 | MethodDefinition def = node.getUserData(Keys.METHOD_DEFINITION); | ||
| 87 | ConstructorEntry constructorEntry = ProcyonEntryFactory.getConstructorEntry(def); | ||
| 88 | index.addDeclaration(node.getNameToken(), constructorEntry); | ||
| 89 | return node.acceptVisitor(new SourceIndexBehaviorVisitor(constructorEntry), index); | ||
| 90 | } | ||
| 91 | |||
| 92 | @Override | ||
| 93 | public Void visitFieldDeclaration(FieldDeclaration node, SourceIndex index) { | ||
| 94 | FieldDefinition def = node.getUserData(Keys.FIELD_DEFINITION); | ||
| 95 | FieldEntry fieldEntry = ProcyonEntryFactory.getFieldEntry(def); | ||
| 96 | assert (node.getVariables().size() == 1); | ||
| 97 | VariableInitializer variable = node.getVariables().firstOrNullObject(); | ||
| 98 | index.addDeclaration(variable.getNameToken(), fieldEntry); | ||
| 99 | |||
| 100 | return recurse(node, index); | ||
| 101 | } | ||
| 102 | |||
| 103 | @Override | ||
| 104 | public Void visitEnumValueDeclaration(EnumValueDeclaration node, SourceIndex index) { | ||
| 105 | // treat enum declarations as field declarations | ||
| 106 | FieldDefinition def = node.getUserData(Keys.FIELD_DEFINITION); | ||
| 107 | FieldEntry fieldEntry = ProcyonEntryFactory.getFieldEntry(def); | ||
| 108 | index.addDeclaration(node.getNameToken(), fieldEntry); | ||
| 109 | |||
| 110 | return recurse(node, index); | ||
| 111 | } | ||
| 112 | } | ||