diff options
| author | 2014-08-10 01:03:40 -0400 | |
|---|---|---|
| committer | 2014-08-10 01:03:40 -0400 | |
| commit | d24d2b9ad9b5c895020b56f700a72906346482e5 (patch) | |
| tree | da360c07209e6e327325db53dbb4df05e77cb7e9 /src/cuchaz/enigma/analysis/SourcedAst.java | |
| parent | added sorting for deobfuscated classes (diff) | |
| download | enigma-fork-d24d2b9ad9b5c895020b56f700a72906346482e5.tar.gz enigma-fork-d24d2b9ad9b5c895020b56f700a72906346482e5.tar.xz enigma-fork-d24d2b9ad9b5c895020b56f700a72906346482e5.zip | |
completely re-wrote token recognizer to bootstrap from Procyon's AST
changed imports to guava instead of whatever collections library happened to be on my classpath
Diffstat (limited to 'src/cuchaz/enigma/analysis/SourcedAst.java')
| -rw-r--r-- | src/cuchaz/enigma/analysis/SourcedAst.java | 140 |
1 files changed, 0 insertions, 140 deletions
diff --git a/src/cuchaz/enigma/analysis/SourcedAst.java b/src/cuchaz/enigma/analysis/SourcedAst.java deleted file mode 100644 index a88cc75..0000000 --- a/src/cuchaz/enigma/analysis/SourcedAst.java +++ /dev/null | |||
| @@ -1,140 +0,0 @@ | |||
| 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.IOException; | ||
| 14 | import java.util.HashMap; | ||
| 15 | |||
| 16 | import javassist.bytecode.Descriptor; | ||
| 17 | |||
| 18 | import com.google.common.collect.Maps; | ||
| 19 | import com.sun.source.tree.CompilationUnitTree; | ||
| 20 | import com.sun.source.tree.ImportTree; | ||
| 21 | import com.sun.source.tree.Tree; | ||
| 22 | import com.sun.source.util.SourcePositions; | ||
| 23 | import com.sun.source.util.Trees; | ||
| 24 | |||
| 25 | public class SourcedAst | ||
| 26 | { | ||
| 27 | private CompilationUnitTree m_tree; | ||
| 28 | private Trees m_trees; | ||
| 29 | private SourcePositions m_positions; | ||
| 30 | private HashMap<String,String> m_classNameIndex; | ||
| 31 | private String m_packageName; | ||
| 32 | |||
| 33 | public SourcedAst( CompilationUnitTree tree, Trees trees ) | ||
| 34 | { | ||
| 35 | m_tree = tree; | ||
| 36 | m_trees = trees; | ||
| 37 | m_positions = m_trees.getSourcePositions(); | ||
| 38 | m_classNameIndex = Maps.newHashMap(); | ||
| 39 | |||
| 40 | // index all the class names from package imports | ||
| 41 | for( ImportTree importTree : m_tree.getImports() ) | ||
| 42 | { | ||
| 43 | // ignore static imports for now | ||
| 44 | if( importTree.isStatic() ) | ||
| 45 | { | ||
| 46 | continue; | ||
| 47 | } | ||
| 48 | |||
| 49 | // get the full and simple class names | ||
| 50 | String fullName = Descriptor.toJvmName( importTree.getQualifiedIdentifier().toString() ); | ||
| 51 | String simpleName = fullName; | ||
| 52 | |||
| 53 | if( fullName.startsWith( "__DEFAULT__/" ) ) | ||
| 54 | { | ||
| 55 | // remove the default package flag | ||
| 56 | fullName = fullName.substring( 12 ); | ||
| 57 | } | ||
| 58 | |||
| 59 | String[] parts = fullName.split( "/" ); | ||
| 60 | if( parts.length > 0 ) | ||
| 61 | { | ||
| 62 | simpleName = parts[parts.length - 1]; | ||
| 63 | } | ||
| 64 | |||
| 65 | m_classNameIndex.put( simpleName, fullName ); | ||
| 66 | } | ||
| 67 | |||
| 68 | // get the package name | ||
| 69 | m_packageName = null; | ||
| 70 | if( m_tree.getPackageName() != null ) | ||
| 71 | { | ||
| 72 | m_packageName = Descriptor.toJvmName( m_tree.getPackageName().toString() ); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | public int getStart( Tree node ) | ||
| 77 | { | ||
| 78 | int pos = (int)m_positions.getStartPosition( m_tree, node ); | ||
| 79 | assert( pos >= 0 ); | ||
| 80 | return pos; | ||
| 81 | } | ||
| 82 | |||
| 83 | public int getEnd( Tree node ) | ||
| 84 | { | ||
| 85 | int pos = (int)m_positions.getEndPosition( m_tree, node ); | ||
| 86 | assert( pos >= 0 ); | ||
| 87 | return pos; | ||
| 88 | } | ||
| 89 | |||
| 90 | public int getLine( Tree node ) | ||
| 91 | { | ||
| 92 | return getLine( getStart( node ) ); | ||
| 93 | } | ||
| 94 | |||
| 95 | public int getLine( int pos ) | ||
| 96 | { | ||
| 97 | return (int)m_tree.getLineMap().getLineNumber( pos ); | ||
| 98 | } | ||
| 99 | |||
| 100 | public CharSequence getSource( ) | ||
| 101 | { | ||
| 102 | try | ||
| 103 | { | ||
| 104 | return m_tree.getSourceFile().getCharContent( true ); | ||
| 105 | } | ||
| 106 | catch( IOException ex ) | ||
| 107 | { | ||
| 108 | throw new Error( ex ); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | public CharSequence getSource( Tree node ) | ||
| 113 | { | ||
| 114 | return getSource().subSequence( getStart( node ), getEnd( node ) ); | ||
| 115 | } | ||
| 116 | |||
| 117 | public void visit( TreeVisitor visitor ) | ||
| 118 | { | ||
| 119 | m_tree.accept( visitor, this ); | ||
| 120 | } | ||
| 121 | |||
| 122 | public String getFullClassName( String simpleClassName ) | ||
| 123 | { | ||
| 124 | String fullClassName = m_classNameIndex.get( simpleClassName ); | ||
| 125 | if( fullClassName == null ) | ||
| 126 | { | ||
| 127 | if( m_packageName != null ) | ||
| 128 | { | ||
| 129 | // no mapping was found, assume it's in the package | ||
| 130 | fullClassName = m_packageName + "/" + simpleClassName; | ||
| 131 | } | ||
| 132 | else | ||
| 133 | { | ||
| 134 | // this must be in the default package | ||
| 135 | fullClassName = simpleClassName; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | return fullClassName; | ||
| 139 | } | ||
| 140 | } | ||