Java源码示例:com.strobel.assembler.metadata.MetadataSystem
示例1
MetadataSystem createMetadataSystem() {
return new MetadataSystem(loader) {
Set<String> loadedTypes = new HashSet<>();
@Override
protected TypeDefinition resolveType(String descriptor, boolean mightBePrimitive) {
if(missingClasses.contains(descriptor)) {
return null;
}
try {
if(loadedTypes.add(descriptor))
incStat("ClassLoadingEfficiency.Total");
if(classes.add(descriptor))
incStat("ClassLoadingEfficiency");
return super.resolveType(descriptor, mightBePrimitive);
} catch (Throwable t) {
addError(new ErrorMessage(null, descriptor, null, null, -1, t));
missingClasses.add(descriptor);
return null;
}
}
};
}
示例2
public void closeFile() {
for (OpenFile co : hmap) {
int pos = house.indexOfTab(co.name);
if (pos >= 0)
house.remove(pos);
co.close();
}
final State oldState = state;
Model.this.state = null;
if (oldState != null) {
Closer.tryClose(oldState);
}
hmap.clear();
tree.setModel(new DefaultTreeModel(null));
metadataSystem = new MetadataSystem(typeLoader);
file = null;
treeExpansionState = null;
open = false;
mainWindow.onFileLoadEnded(file, open);
}
示例3
@Override
public String decompile(String name) {
ITypeLoader loader = new ComposedTypeLoader(Arrays.asList(
new RecafTypeLoader(getController()), new InputTypeLoader()
));
Map<String, Boolean> options = getOptions();
DecompilerSettings settings = new DecompilerSettings();
settings.setFlattenSwitchBlocks(options.get("flatten-switch-blocks"));
settings.setForceExplicitImports(!options.get("collapse-imports"));
settings.setForceExplicitTypeArguments(options.get("force-explicit-type-arguments"));
settings.setRetainRedundantCasts(options.get("retain-redundant-casts"));
settings.setShowSyntheticMembers(options.get("show-synthetic-members"));
settings.setExcludeNestedTypes(options.get("exclude-nested-types"));
settings.setRetainPointlessSwitches(options.get("retain-pointless-switches"));
settings.setUnicodeOutputEnabled(options.get("unicode-output"));
settings.setMergeVariables(options.get("merge-variables"));
settings.setShowDebugLineNumbers(options.get("show-debug-line-numbers"));
settings.setSimplifyMemberReferences(options.get("simplify-member-references"));
settings.setForceFullyQualifiedReferences(options.get("force-fully-qualified-references"));
settings.setDisableForEachTransforms(options.get("disable-for-each-transforms"));
settings.setTypeLoader(loader);
settings.setJavaFormattingOptions(JavaFormattingOptions.createDefault());
MetadataSystem system = new MetadataSystem(loader);
system.setEagerMethodLoadingEnabled(options.get("eager-methods-loading"));
TypeReference ref = system.lookupType(name);
DecompilationOptions decompilationOptions = new DecompilationOptions();
decompilationOptions.setSettings(settings);
decompilationOptions.setFullDecompilation(true);
StringWriter writer = new StringWriter();
settings.getLanguage().decompileType(ref.resolve(), new PlainTextOutput(writer), decompilationOptions);
return writer.toString();
}
示例4
private TypeDefinition lookUp(MetadataSystem ms, String className) {
TypeReference tr = ms.lookupType(className);
if(tr == null) {
missingClasses.add(className);
return null;
}
return ms.resolve(tr);
}
示例5
private void analyzingClasses(Set<String> classes) {
MetadataSystem ms = createMetadataSystem();
classesCount.set(0);
for (String className : classes) {
if(classesCount.get() % options.classesPerFlush == 0)
ms = createMetadataSystem();
if (!fireEvent("Analyzing classes", className, classesCount.get(), classes.size()))
return;
analyzeClass(ms, className);
}
if (!fireEvent("Analyzing classes", null, classes.size(), classes.size()))
return;
}
示例6
void analyzeClass(MetadataSystem ms, String name) {
classesCount.incrementAndGet();
TypeDefinition type;
try {
type = lookUp(ms, name);
} catch (Throwable t) {
addError(new ErrorMessage(null, name, null, null, -1, t));
return;
}
if (type != null)
registry.analyzeClass(type);
}
示例7
@Override
public void visitClass(String className) {
String name = className.replace('/', '.');
try {
ctx.incStat("Detectors.Total");
if (addDetector(MetadataSystem.class.getClassLoader().loadClass(name))) {
ctx.incStat("Detectors");
if (external) {
ctx.incStat("Detectors from HuntBugs plugins");
}
}
} catch (ClassNotFoundException e) {
ctx.addError(new ErrorMessage(name, null, null, null, -1, e));
}
}
示例8
private static TypeReference lookupType(String path, ITypeLoader loader) {
MetadataSystem metadataSystem = new MetadataSystem(loader);
/* Hack to get around classes whose descriptors clash with primitive types. */
if (path.length() == 1) {
MetadataParser parser = new MetadataParser(IMetadataResolver.EMPTY);
return metadataSystem.resolve(parser.parseTypeDescriptor(path));
}
return metadataSystem.lookupType(path);
}
示例9
private void doSaveClassDecompiled(File inFile, File outFile) throws Exception {
DecompilerSettings settings = cloneSettings();
LuytenTypeLoader typeLoader = new LuytenTypeLoader();
MetadataSystem metadataSystem = new MetadataSystem(typeLoader);
TypeReference type = metadataSystem.lookupType(inFile.getCanonicalPath());
DecompilationOptions decompilationOptions = new DecompilationOptions();
decompilationOptions.setSettings(settings);
decompilationOptions.setFullDecompilation(true);
boolean isUnicodeEnabled = decompilationOptions.getSettings().isUnicodeOutputEnabled();
TypeDefinition resolvedType = null;
if (type == null || ((resolvedType = type.resolve()) == null)) {
throw new Exception("Unable to resolve type.");
}
StringWriter stringwriter = new StringWriter();
PlainTextOutput plainTextOutput = new PlainTextOutput(stringwriter);
plainTextOutput.setUnicodeOutputEnabled(isUnicodeEnabled);
settings.getLanguage().decompileType(resolvedType, plainTextOutput, decompilationOptions);
String decompiledSource = stringwriter.toString();
System.out.println("[SaveAll]: " + inFile.getName() + " -> " + outFile.getName());
try (FileOutputStream fos = new FileOutputStream(outFile);
OutputStreamWriter writer = isUnicodeEnabled ? new OutputStreamWriter(fos, "UTF-8")
: new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(writer);) {
bw.write(decompiledSource);
bw.flush();
}
}
示例10
public String decompile(byte[] b, MethodNode mn) {
try {
//TODO decompile method only
DecompilerSettings settings = new DecompilerSettings();
try {
for (Field f : settings.getClass().getDeclaredFields()) {
if (f.getType() == boolean.class) {
f.setAccessible(true);
f.setBoolean(settings, JByteMod.ops.get("procyon" + f.getName()).getBoolean());
}
}
} catch (Throwable t) {
t.printStackTrace();
}
settings.setShowSyntheticMembers(true);
MetadataSystem metadataSystem = new MetadataSystem(new ITypeLoader() {
private InputTypeLoader backLoader = new InputTypeLoader();
@Override
public boolean tryLoadType(String s, Buffer buffer) {
if (s.equals(cn.name)) {
buffer.putByteArray(b, 0, b.length);
buffer.position(0);
return true;
} else {
return backLoader.tryLoadType(s, buffer);
}
}
});
TypeReference type = metadataSystem.lookupType(cn.name);
DecompilationOptions decompilationOptions = new DecompilationOptions();
decompilationOptions.setSettings(DecompilerSettings.javaDefaults());
decompilationOptions.setFullDecompilation(true);
TypeDefinition resolvedType = null;
if (type == null || ((resolvedType = type.resolve()) == null)) {
new ErrorDisplay("Unable to resolve type.");
return "error";
}
StringWriter stringwriter = new StringWriter();
settings.getLanguage().decompileType(resolvedType, new PlainTextOutput(stringwriter), decompilationOptions);
String decompiledSource = stringwriter.toString();
return decompiledSource;
} catch (Exception e) {
e.printStackTrace();
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
e.printStackTrace(pw);
return sw.toString();
}
}
示例11
public void setDecompilerReferences(MetadataSystem metadataSystem, DecompilerSettings settings,
DecompilationOptions decompilationOptions) {
this.metadataSystem = metadataSystem;
this.settings = settings;
this.decompilationOptions = decompilationOptions;
}
示例12
public void setDecompilerReferences(MetadataSystem metadataSystem, DecompilerSettings settings,
DecompilationOptions decompilationOptions) {
this.metadataSystem = metadataSystem;
this.settings = settings;
this.decompilationOptions = decompilationOptions;
}