diff options
| author | 2014-09-14 23:56:43 -0400 | |
|---|---|---|
| committer | 2014-09-14 23:56:43 -0400 | |
| commit | 72e918a5134c2bf747a476284bcfa1bd2ef2fa21 (patch) | |
| tree | 2fd256d6a8bbe38b7b9fe1892f444a8c29de08ef /test/cuchaz/enigma/TokenChecker.java | |
| parent | added test to check constructor references (diff) | |
| download | enigma-fork-72e918a5134c2bf747a476284bcfa1bd2ef2fa21.tar.gz enigma-fork-72e918a5134c2bf747a476284bcfa1bd2ef2fa21.tar.xz enigma-fork-72e918a5134c2bf747a476284bcfa1bd2ef2fa21.zip | |
added tests to check constructor tokens
fixed a bug with constructor tokens too
Diffstat (limited to 'test/cuchaz/enigma/TokenChecker.java')
| -rw-r--r-- | test/cuchaz/enigma/TokenChecker.java | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/test/cuchaz/enigma/TokenChecker.java b/test/cuchaz/enigma/TokenChecker.java new file mode 100644 index 0000000..07ab9ec --- /dev/null +++ b/test/cuchaz/enigma/TokenChecker.java | |||
| @@ -0,0 +1,71 @@ | |||
| 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; | ||
| 12 | |||
| 13 | import java.io.File; | ||
| 14 | import java.io.IOException; | ||
| 15 | import java.util.Collection; | ||
| 16 | import java.util.List; | ||
| 17 | |||
| 18 | import com.beust.jcommander.internal.Lists; | ||
| 19 | import com.strobel.decompiler.languages.java.ast.CompilationUnit; | ||
| 20 | |||
| 21 | import cuchaz.enigma.analysis.EntryReference; | ||
| 22 | import cuchaz.enigma.analysis.SourceIndex; | ||
| 23 | import cuchaz.enigma.analysis.Token; | ||
| 24 | import cuchaz.enigma.analysis.TreeDumpVisitor; | ||
| 25 | import cuchaz.enigma.mapping.Entry; | ||
| 26 | |||
| 27 | public class TokenChecker | ||
| 28 | { | ||
| 29 | private Deobfuscator m_deobfuscator; | ||
| 30 | |||
| 31 | protected TokenChecker( File jarFile ) | ||
| 32 | throws IOException | ||
| 33 | { | ||
| 34 | m_deobfuscator = new Deobfuscator( jarFile ); | ||
| 35 | } | ||
| 36 | |||
| 37 | protected String getDeclarationToken( Entry entry ) | ||
| 38 | { | ||
| 39 | // decompile the class | ||
| 40 | CompilationUnit tree = m_deobfuscator.getSourceTree( entry.getClassName() ); | ||
| 41 | // DEBUG | ||
| 42 | //tree.acceptVisitor( new TreeDumpVisitor( new File( "tree." + entry.getClassName().replace( '/', '.' ) + ".txt" ) ), null ); | ||
| 43 | String source = m_deobfuscator.getSource( tree ); | ||
| 44 | SourceIndex index = m_deobfuscator.getSourceIndex( tree, source ); | ||
| 45 | |||
| 46 | // get the token value | ||
| 47 | Token token = index.getDeclarationToken( entry ); | ||
| 48 | if( token == null ) | ||
| 49 | { | ||
| 50 | return null; | ||
| 51 | } | ||
| 52 | return source.substring( token.start, token.end ); | ||
| 53 | } | ||
| 54 | |||
| 55 | @SuppressWarnings( "unchecked" ) | ||
| 56 | protected Collection<String> getReferenceTokens( EntryReference<? extends Entry,? extends Entry> reference ) | ||
| 57 | { | ||
| 58 | // decompile the class | ||
| 59 | CompilationUnit tree = m_deobfuscator.getSourceTree( reference.context.getClassName() ); | ||
| 60 | String source = m_deobfuscator.getSource( tree ); | ||
| 61 | SourceIndex index = m_deobfuscator.getSourceIndex( tree, source ); | ||
| 62 | |||
| 63 | // get the token values | ||
| 64 | List<String> values = Lists.newArrayList(); | ||
| 65 | for( Token token : index.getReferenceTokens( (EntryReference<Entry,Entry>)reference ) ) | ||
| 66 | { | ||
| 67 | values.add( source.substring( token.start, token.end ) ); | ||
| 68 | } | ||
| 69 | return values; | ||
| 70 | } | ||
| 71 | } | ||