fix cfr upgrade to 0.145

4.0.x
hengyunabc 6 years ago
parent d8c16720f4
commit 24a344b006

@ -22,7 +22,6 @@
<dependency> <dependency>
<groupId>org.benf</groupId> <groupId>org.benf</groupId>
<artifactId>cfr</artifactId> <artifactId>cfr</artifactId>
<version>0.132</version>
<scope>provided</scope> <scope>provided</scope>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>

@ -4,34 +4,17 @@ import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.StringWriter; import java.io.StringWriter;
import java.util.ArrayList; import java.util.Arrays;
import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomStringUtils;
import org.benf.cfr.reader.api.ClassFileSource; import org.benf.cfr.reader.api.CfrDriver;
import org.benf.cfr.reader.bytecode.analysis.parse.utils.Pair; import org.benf.cfr.reader.api.OutputSinkFactory;
import org.benf.cfr.reader.entities.ClassFile;
import org.benf.cfr.reader.entities.Method;
import org.benf.cfr.reader.relationship.MemberNameResolver;
import org.benf.cfr.reader.state.ClassFileSourceImpl;
import org.benf.cfr.reader.state.DCCommonState;
import org.benf.cfr.reader.state.TypeUsageCollector;
import org.benf.cfr.reader.state.TypeUsageInformation;
import org.benf.cfr.reader.util.AnalysisType;
import org.benf.cfr.reader.util.CannotLoadClassException;
import org.benf.cfr.reader.util.ConfusedCFRException;
import org.benf.cfr.reader.util.ListFactory;
import org.benf.cfr.reader.util.getopt.GetOptParser;
import org.benf.cfr.reader.util.getopt.Options;
import org.benf.cfr.reader.util.getopt.OptionsImpl;
import org.benf.cfr.reader.util.output.Dumper;
import org.benf.cfr.reader.util.output.DumperFactory;
import org.benf.cfr.reader.util.output.DumperFactoryImpl;
import org.benf.cfr.reader.util.output.IllegalIdentifierDump;
import org.benf.cfr.reader.util.output.StreamDumper;
import org.benf.cfr.reader.util.output.ToStringDumper;
import com.alibaba.arthas.deps.org.objectweb.asm.tree.AbstractInsnNode; import com.alibaba.arthas.deps.org.objectweb.asm.tree.AbstractInsnNode;
import com.alibaba.arthas.deps.org.objectweb.asm.tree.ClassNode; import com.alibaba.arthas.deps.org.objectweb.asm.tree.ClassNode;
import com.alibaba.arthas.deps.org.objectweb.asm.tree.InsnList; import com.alibaba.arthas.deps.org.objectweb.asm.tree.InsnList;
@ -113,159 +96,48 @@ public class Decompiler {
/** /**
* @see org.benf.cfr.reader.Main#main(String[])
* @param classFilePath * @param classFilePath
* @param methodName * @param methodName
* @return * @return
*/ */
public static String decompile(String classFilePath, String methodName) { public static String decompile(String classFilePath, String methodName) {
StringBuilder result = new StringBuilder(8192); final StringBuilder result = new StringBuilder(8192);
List<String> argList = new ArrayList<String>();
argList.add(classFilePath);
if (methodName != null) {
argList.add("--methodname");
argList.add(methodName);
}
String args[] = argList.toArray(new String[0]);
GetOptParser getOptParser = new GetOptParser();
Options options = null;
List<String> files = null;
try {
Pair processedArgs = getOptParser.parse(args, OptionsImpl.getFactory());
files = (List) processedArgs.getFirst();
options = (Options) processedArgs.getSecond();
} catch (Exception e) {
getOptParser.showHelp(OptionsImpl.getFactory(), e);
System.exit(1);
}
if ((options.optionIsSet(OptionsImpl.HELP)) || (files.isEmpty())) {
getOptParser.showOptionHelp(OptionsImpl.getFactory(), options, OptionsImpl.HELP);
return "";
}
ClassFileSourceImpl classFileSource = new ClassFileSourceImpl(options);
boolean skipInnerClass = (files.size() > 1)
&& (((Boolean) options.getOption(OptionsImpl.SKIP_BATCH_INNER_CLASSES)).booleanValue());
Collections.sort(files); OutputSinkFactory mySink = new OutputSinkFactory() {
@Override
for (String path : files) { public List<SinkClass> getSupportedSinks(SinkType sinkType, Collection<SinkClass> collection) {
classFileSource.clearConfiguration(); return Arrays.asList(SinkClass.STRING, SinkClass.DECOMPILED, SinkClass.DECOMPILED_MULTIVER,
DCCommonState dcCommonState = new DCCommonState(options, classFileSource); SinkClass.EXCEPTION_MESSAGE);
DumperFactory dumperFactory = new DumperFactoryImpl(options);
path = classFileSource.adjustInputPath(path);
AnalysisType type = (AnalysisType) options.getOption(OptionsImpl.ANALYSE_AS);
if (type == null)
type = dcCommonState.detectClsJar(path);
if (type == AnalysisType.JAR) {
// doJar(dcCommonState, path, dumperFactory);
} }
if (type == AnalysisType.CLASS)
result.append(doClass(dcCommonState, path, skipInnerClass, dumperFactory));
}
return result.toString();
}
public static String doClass(DCCommonState dcCommonState, String path, boolean skipInnerClass, @Override
DumperFactory dumperFactory) { public <T> Sink<T> getSink(final SinkType sinkType, SinkClass sinkClass) {
StringBuilder result = new StringBuilder(8192); return new Sink<T>() {
Options options = dcCommonState.getOptions(); @Override
IllegalIdentifierDump illegalIdentifierDump = IllegalIdentifierDump.Factory.get(options); public void write(T sinkable) {
Dumper d = new ToStringDumper(); // skip message like: Analysing type demo.MathGame
try { if (sinkType == SinkType.PROGRESS) {
ClassFile c = dcCommonState.getClassFileMaybePath(path); return;
if ((skipInnerClass) && (c.isInnerClass())) }
return ""; result.append(sinkable);
dcCommonState.configureWith(c); }
dumperFactory.getProgressDumper().analysingType(c.getClassType()); };
try {
c = dcCommonState.getClassFile(c.getClassType());
} catch (CannotLoadClassException e) {
}
if (((Boolean) options.getOption(OptionsImpl.DECOMPILE_INNER_CLASSES)).booleanValue()) {
c.loadInnerClasses(dcCommonState);
} }
if (((Boolean) options.getOption(OptionsImpl.RENAME_DUP_MEMBERS)).booleanValue()) { };
MemberNameResolver.resolveNames(dcCommonState,
ListFactory.newList(dcCommonState.getClassCache().getLoadedTypes())); HashMap<String, String> options = new HashMap<String, String>();
} /**
* @see org.benf.cfr.reader.util.MiscConstants.Version.getVersion() Currently,
c.analyseTop(dcCommonState); * the cfr version is wrong. so disable show cfr version.
*/
TypeUsageCollector collectingDumper = new TypeUsageCollector(c); options.put("showversion", "false");
c.collectTypeUsages(collectingDumper); if (methodName !=null && !methodName.trim().isEmpty()) {
options.put("methodname", methodName);
d = new StringDumper(collectingDumper.getTypeUsageInformation(), options, illegalIdentifierDump);
// d = dumperFactory.getNewTopLevelDumper(c.getClassType(), summaryDumper,
// collectingDumper.getTypeUsageInformation(), illegalIdentifierDump);
String methname = (String) options.getOption(OptionsImpl.METHODNAME);
if (methname == null)
c.dump(d);
else {
try {
for (Method method : c.getMethodByName(methname))
method.dump(d, true);
} catch (NoSuchMethodException e) {
throw new IllegalArgumentException("No such method '" + methname + "'.");
}
}
d.print("");
result.append(d.toString());
} catch (ConfusedCFRException e) {
result.append(e.toString()).append("\n");
for (Object x : e.getStackTrace())
result.append(x).append("\n");
} catch (CannotLoadClassException e) {
result.append("Can't load the class specified:").append("\n");
result.append(e.toString()).append("\n");
} catch (RuntimeException e) {
result.append(e.toString()).append("\n");
for (Object x : e.getStackTrace())
result.append(x).append("\n");
} finally {
if (d != null)
d.close();
}
return result.toString();
}
public static class StringDumper extends StreamDumper {
private StringWriter sw = new StringWriter();
public StringDumper(TypeUsageInformation typeUsageInformation, Options options,
IllegalIdentifierDump illegalIdentifierDump) {
super(typeUsageInformation, options, illegalIdentifierDump);
} }
public void addSummaryError(Method paramMethod, String paramString) { CfrDriver driver = new CfrDriver.Builder().withOptions(options).withOutputSink(mySink).build();
driver.analyse(Collections.singletonList(classFilePath));
} return result.toString();
public void close() {
try {
sw.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
protected void write(String source) {
sw.write(source);
}
public String toString() {
return sw.toString();
}
} }
} }

Loading…
Cancel
Save