diff options
| author | 2014-09-01 22:52:07 -0400 | |
|---|---|---|
| committer | 2014-09-01 22:52:07 -0400 | |
| commit | 360bbd1c2fca8cbd575907b7d930a8072fccb0c2 (patch) | |
| tree | 93d0f3c4a0901411427df580c5ddb75cf27440f1 /src/cuchaz/enigma/analysis/TranslationIndex.java | |
| parent | added copyright notice (diff) | |
| download | enigma-fork-360bbd1c2fca8cbd575907b7d930a8072fccb0c2.tar.gz enigma-fork-360bbd1c2fca8cbd575907b7d930a8072fccb0c2.tar.xz enigma-fork-360bbd1c2fca8cbd575907b7d930a8072fccb0c2.zip | |
refactored jar,translation index. fixed bug with field renaming when fields are shadowed by subclasses
Diffstat (limited to '')
| -rw-r--r-- | src/cuchaz/enigma/analysis/TranslationIndex.java | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/src/cuchaz/enigma/analysis/TranslationIndex.java b/src/cuchaz/enigma/analysis/TranslationIndex.java new file mode 100644 index 0000000..5311ec7 --- /dev/null +++ b/src/cuchaz/enigma/analysis/TranslationIndex.java | |||
| @@ -0,0 +1,126 @@ | |||
| 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 java.io.Serializable; | ||
| 14 | import java.util.ArrayList; | ||
| 15 | import java.util.List; | ||
| 16 | import java.util.Map; | ||
| 17 | import java.util.Set; | ||
| 18 | |||
| 19 | import javassist.bytecode.Descriptor; | ||
| 20 | |||
| 21 | import com.google.common.collect.HashMultimap; | ||
| 22 | import com.google.common.collect.Lists; | ||
| 23 | import com.google.common.collect.Maps; | ||
| 24 | import com.google.common.collect.Multimap; | ||
| 25 | |||
| 26 | public class TranslationIndex implements Serializable | ||
| 27 | { | ||
| 28 | private static final long serialVersionUID = 738687982126844179L; | ||
| 29 | |||
| 30 | private Map<String,String> m_superclasses; | ||
| 31 | private Multimap<String,String> m_fields; | ||
| 32 | |||
| 33 | public TranslationIndex( ) | ||
| 34 | { | ||
| 35 | m_superclasses = Maps.newHashMap(); | ||
| 36 | m_fields = HashMultimap.create(); | ||
| 37 | } | ||
| 38 | |||
| 39 | public TranslationIndex( TranslationIndex other ) | ||
| 40 | { | ||
| 41 | m_superclasses = Maps.newHashMap( other.m_superclasses ); | ||
| 42 | m_fields = HashMultimap.create( other.m_fields ); | ||
| 43 | } | ||
| 44 | |||
| 45 | public void addSuperclass( String className, String superclassName ) | ||
| 46 | { | ||
| 47 | className = Descriptor.toJvmName( className ); | ||
| 48 | superclassName = Descriptor.toJvmName( superclassName ); | ||
| 49 | |||
| 50 | if( className.equals( superclassName ) ) | ||
| 51 | { | ||
| 52 | throw new IllegalArgumentException( "Class cannot be its own superclass! " + className ); | ||
| 53 | } | ||
| 54 | |||
| 55 | if( !isJre( className ) && !isJre( superclassName ) ) | ||
| 56 | { | ||
| 57 | m_superclasses.put( className, superclassName ); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | |||
| 61 | public void addField( String className, String fieldName ) | ||
| 62 | { | ||
| 63 | m_fields.put( className, fieldName ); | ||
| 64 | } | ||
| 65 | |||
| 66 | public void renameClasses( Map<String,String> renames ) | ||
| 67 | { | ||
| 68 | EntryRenamer.renameClassesInMap( renames, m_superclasses ); | ||
| 69 | EntryRenamer.renameClassesInMultimap( renames, m_fields ); | ||
| 70 | } | ||
| 71 | |||
| 72 | public String getSuperclassName( String className ) | ||
| 73 | { | ||
| 74 | return m_superclasses.get( className ); | ||
| 75 | } | ||
| 76 | |||
| 77 | public List<String> getAncestry( String className ) | ||
| 78 | { | ||
| 79 | List<String> ancestors = new ArrayList<String>(); | ||
| 80 | while( className != null ) | ||
| 81 | { | ||
| 82 | className = getSuperclassName( className ); | ||
| 83 | if( className != null ) | ||
| 84 | { | ||
| 85 | ancestors.add( className ); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | return ancestors; | ||
| 89 | } | ||
| 90 | |||
| 91 | public List<String> getSubclassNames( String className ) | ||
| 92 | { | ||
| 93 | // linear search is fast enough for now | ||
| 94 | List<String> subclasses = Lists.newArrayList(); | ||
| 95 | for( Map.Entry<String,String> entry : m_superclasses.entrySet() ) | ||
| 96 | { | ||
| 97 | String subclass = entry.getKey(); | ||
| 98 | String superclass = entry.getValue(); | ||
| 99 | if( className.equals( superclass ) ) | ||
| 100 | { | ||
| 101 | subclasses.add( subclass ); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | return subclasses; | ||
| 105 | } | ||
| 106 | |||
| 107 | public void getSubclassNamesRecursively( Set<String> out, String className ) | ||
| 108 | { | ||
| 109 | for( String subclassName : getSubclassNames( className ) ) | ||
| 110 | { | ||
| 111 | out.add( subclassName ); | ||
| 112 | getSubclassNamesRecursively( out, subclassName ); | ||
| 113 | } | ||
| 114 | } | ||
| 115 | |||
| 116 | public boolean containsField( String className, String fieldName ) | ||
| 117 | { | ||
| 118 | return m_fields.containsEntry( className, fieldName ); | ||
| 119 | } | ||
| 120 | |||
| 121 | private boolean isJre( String className ) | ||
| 122 | { | ||
| 123 | return className.startsWith( "java/" ) | ||
| 124 | || className.startsWith( "javax/" ); | ||
| 125 | } | ||
| 126 | } | ||