use of com.taobao.android.dx.dex.file.ClassDefItem in project atlas by alibaba.
the class Main method dumpMethod.
/**
* Dumps any method with the given name in the given file.
*
* @param dex {@code non-null;} the dex file
* @param fqName {@code non-null;} the fully-qualified name of the
* method(s)
* @param out {@code non-null;} where to dump to
*/
private void dumpMethod(DexFile dex, String fqName, OutputStreamWriter out) {
boolean wildcard = fqName.endsWith("*");
int lastDot = fqName.lastIndexOf('.');
if ((lastDot <= 0) || (lastDot == (fqName.length() - 1))) {
dxConsole.err.println("bogus fully-qualified method name: " + fqName);
return;
}
String className = fqName.substring(0, lastDot).replace('.', '/');
String methodName = fqName.substring(lastDot + 1);
ClassDefItem clazz = dex.getClassOrNull(className);
if (clazz == null) {
dxConsole.err.println("no such class: " + className);
return;
}
if (wildcard) {
methodName = methodName.substring(0, methodName.length() - 1);
}
ArrayList<EncodedMethod> allMeths = clazz.getMethods();
TreeMap<CstNat, EncodedMethod> meths = new TreeMap<CstNat, EncodedMethod>();
/*
* Figure out which methods to include in the output, and get them
* all sorted, so that the printout code is robust with respect to
* changes in the underlying order.
*/
for (EncodedMethod meth : allMeths) {
String methName = meth.getName().getString();
if ((wildcard && methName.startsWith(methodName)) || (!wildcard && methName.equals(methodName))) {
meths.put(meth.getRef().getNat(), meth);
}
}
if (meths.size() == 0) {
dxConsole.err.println("no such method: " + fqName);
return;
}
PrintWriter pw = new PrintWriter(out);
for (EncodedMethod meth : meths.values()) {
// TODO: Better stuff goes here, perhaps.
meth.debugPrint(pw, args.verboseDump);
/*
* The (default) source file is an attribute of the class, but
* it's useful to see it in method dumps.
*/
CstString sourceFile = clazz.getSourceFile();
if (sourceFile != null) {
pw.println(" source file: " + sourceFile.toQuoted());
}
Annotations methodAnnotations = clazz.getMethodAnnotations(meth.getRef());
AnnotationsList parameterAnnotations = clazz.getParameterAnnotations(meth.getRef());
if (methodAnnotations != null) {
pw.println(" method annotations:");
for (Annotation a : methodAnnotations.getAnnotations()) {
pw.println(" " + a);
}
}
if (parameterAnnotations != null) {
pw.println(" parameter annotations:");
int sz = parameterAnnotations.size();
for (int i = 0; i < sz; i++) {
pw.println(" parameter " + i);
Annotations annotations = parameterAnnotations.get(i);
for (Annotation a : annotations.getAnnotations()) {
pw.println(" " + a);
}
}
}
}
pw.flush();
}
use of com.taobao.android.dx.dex.file.ClassDefItem in project atlas by alibaba.
the class CfTranslator method translate0.
/**
* Performs the main act of translation. This method is separated
* from {@link #translate} just to keep things a bit simpler in
* terms of exception handling.
*
* @param cf {@code non-null;} the class file
* @param bytes {@code non-null;} contents of the file
* @param cfOptions options for class translation
* @param dexOptions options for dex output
* @param dexFile {@code non-null;} dex output
* @param optimizerOptions options for the optimizer
* @return {@code non-null;} the translated class
*/
private static ClassDefItem translate0(DirectClassFile cf, byte[] bytes, CfOptions cfOptions, DexOptions dexOptions, OptimizerOptions optimizerOptions, DexFile dexFile) {
optimizerOptions.loadOptimizeLists(cfOptions.optimizeListFile, cfOptions.dontOptimizeListFile);
// Build up a class to output.
CstType thisClass = cf.getThisClass();
int classAccessFlags = cf.getAccessFlags() & ~AccessFlags.ACC_SUPER;
CstString sourceFile = (cfOptions.positionInfo == PositionList.NONE) ? null : cf.getSourceFile();
ClassDefItem out = new ClassDefItem(thisClass, classAccessFlags, cf.getSuperclass(), cf.getInterfaces(), sourceFile);
Annotations classAnnotations = AttributeTranslator.getClassAnnotations(cf, cfOptions);
if (classAnnotations.size() != 0) {
out.setClassAnnotations(classAnnotations, dexFile);
}
FieldIdsSection fieldIdsSection = dexFile.getFieldIds();
MethodIdsSection methodIdsSection = dexFile.getMethodIds();
processFields(cf, out, dexFile);
processMethods(cf, cfOptions, dexOptions, optimizerOptions, out, dexFile);
// intern constant pool method, field and type references
ConstantPool constantPool = cf.getConstantPool();
int constantPoolSize = constantPool.size();
for (int i = 0; i < constantPoolSize; i++) {
Constant constant = constantPool.getOrNull(i);
if (constant instanceof CstMethodRef) {
methodIdsSection.intern((CstBaseMethodRef) constant);
} else if (constant instanceof CstInterfaceMethodRef) {
methodIdsSection.intern(((CstInterfaceMethodRef) constant).toMethodRef());
} else if (constant instanceof CstFieldRef) {
fieldIdsSection.intern((CstFieldRef) constant);
} else if (constant instanceof CstEnumRef) {
fieldIdsSection.intern(((CstEnumRef) constant).getFieldRef());
}
}
return out;
}
use of com.taobao.android.dx.dex.file.ClassDefItem in project atlas by alibaba.
the class Main method processClass.
/**
* Processes one classfile.
*
* @param name {@code non-null;} name of the file, clipped such that it
* <i>should</i> correspond to the name of the class it contains
* @param bytes {@code non-null;} contents of the file
* @return whether processing was successful
*/
private boolean processClass(String name, byte[] bytes) {
if (!args.coreLibrary) {
checkClassName(name);
}
DirectClassFile cf = new DirectClassFile(bytes, name, args.cfOptions.strictNameCheck);
cf.setAttributeFactory(StdAttributeFactory.THE_ONE);
cf.getMagic();
int numMethodIds = outputDex.getMethodIds().items().size();
int numFieldIds = outputDex.getFieldIds().items().size();
int constantPoolSize = cf.getConstantPool().size();
int maxMethodIdsInDex = numMethodIds + constantPoolSize + cf.getMethods().size() + MAX_METHOD_ADDED_DURING_DEX_CREATION;
int maxFieldIdsInDex = numFieldIds + constantPoolSize + cf.getFields().size() + MAX_FIELD_ADDED_DURING_DEX_CREATION;
if (args.multiDex && // Never switch to the next dex if current dex is already empty
(outputDex.getClassDefs().items().size() > 0) && ((maxMethodIdsInDex > args.maxNumberOfIdxPerDex) || (maxFieldIdsInDex > args.maxNumberOfIdxPerDex))) {
DexFile completeDex = outputDex;
createDexFile();
assert (completeDex.getMethodIds().items().size() <= numMethodIds + MAX_METHOD_ADDED_DURING_DEX_CREATION) && (completeDex.getFieldIds().items().size() <= numFieldIds + MAX_FIELD_ADDED_DURING_DEX_CREATION);
}
try {
ClassDefItem clazz = CfTranslator.translate(cf, bytes, args.cfOptions, args.dexOptions, args.optimizerOptions, outputDex);
synchronized (outputDex) {
outputDex.add(clazz);
}
return true;
} catch (ParseException ex) {
dxConsole.err.println("\ntrouble processing:");
if (args.debug) {
ex.printStackTrace(dxConsole.err);
} else {
ex.printContext(dxConsole.err);
}
}
errors.incrementAndGet();
return false;
}
Aggregations