From 72e918a5134c2bf747a476284bcfa1bd2ef2fa21 Mon Sep 17 00:00:00 2001 From: jeff Date: Sun, 14 Sep 2014 23:56:43 -0400 Subject: added tests to check constructor tokens fixed a bug with constructor tokens too --- test/cuchaz/enigma/TokenChecker.java | 71 ++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 test/cuchaz/enigma/TokenChecker.java (limited to 'test/cuchaz/enigma/TokenChecker.java') 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 @@ +/******************************************************************************* + * Copyright (c) 2014 Jeff Martin. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the GNU Public License v3.0 + * which accompanies this distribution, and is available at + * http://www.gnu.org/licenses/gpl.html + * + * Contributors: + * Jeff Martin - initial API and implementation + ******************************************************************************/ +package cuchaz.enigma; + +import java.io.File; +import java.io.IOException; +import java.util.Collection; +import java.util.List; + +import com.beust.jcommander.internal.Lists; +import com.strobel.decompiler.languages.java.ast.CompilationUnit; + +import cuchaz.enigma.analysis.EntryReference; +import cuchaz.enigma.analysis.SourceIndex; +import cuchaz.enigma.analysis.Token; +import cuchaz.enigma.analysis.TreeDumpVisitor; +import cuchaz.enigma.mapping.Entry; + +public class TokenChecker +{ + private Deobfuscator m_deobfuscator; + + protected TokenChecker( File jarFile ) + throws IOException + { + m_deobfuscator = new Deobfuscator( jarFile ); + } + + protected String getDeclarationToken( Entry entry ) + { + // decompile the class + CompilationUnit tree = m_deobfuscator.getSourceTree( entry.getClassName() ); + // DEBUG + //tree.acceptVisitor( new TreeDumpVisitor( new File( "tree." + entry.getClassName().replace( '/', '.' ) + ".txt" ) ), null ); + String source = m_deobfuscator.getSource( tree ); + SourceIndex index = m_deobfuscator.getSourceIndex( tree, source ); + + // get the token value + Token token = index.getDeclarationToken( entry ); + if( token == null ) + { + return null; + } + return source.substring( token.start, token.end ); + } + + @SuppressWarnings( "unchecked" ) + protected Collection getReferenceTokens( EntryReference reference ) + { + // decompile the class + CompilationUnit tree = m_deobfuscator.getSourceTree( reference.context.getClassName() ); + String source = m_deobfuscator.getSource( tree ); + SourceIndex index = m_deobfuscator.getSourceIndex( tree, source ); + + // get the token values + List values = Lists.newArrayList(); + for( Token token : index.getReferenceTokens( (EntryReference)reference ) ) + { + values.add( source.substring( token.start, token.end ) ); + } + return values; + } +} -- cgit v1.2.3