use of com.android.dx.rop.cst.CstString in project buck by facebook.
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))) {
context.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) {
context.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) {
context.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.android.dx.rop.cst.CstString in project buck by facebook.
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 context
* @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
* @return {@code non-null;} the translated class
*/
private static ClassDefItem translate0(DxContext context, DirectClassFile cf, byte[] bytes, CfOptions cfOptions, DexOptions dexOptions, DexFile dexFile) {
context.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(context, cf, cfOptions, dexOptions, 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.android.dx.rop.cst.CstString in project buck by facebook.
the class LocalVariableList method mergeDescriptorsAndSignatures.
/**
* Returns an instance which is the result of merging the two
* given instances, where one instance should have only type
* descriptors and the other only type signatures. The merged
* result is identical to the one with descriptors, except that
* any element whose {name, index, start, length} matches an
* element in the signature list gets augmented with the
* corresponding signature. The result is immutable.
*
* @param descriptorList {@code non-null;} list with descriptors
* @param signatureList {@code non-null;} list with signatures
* @return {@code non-null;} the merged result
*/
public static LocalVariableList mergeDescriptorsAndSignatures(LocalVariableList descriptorList, LocalVariableList signatureList) {
int descriptorSize = descriptorList.size();
LocalVariableList result = new LocalVariableList(descriptorSize);
for (int i = 0; i < descriptorSize; i++) {
Item item = descriptorList.get(i);
Item signatureItem = signatureList.itemToLocal(item);
if (signatureItem != null) {
CstString signature = signatureItem.getSignature();
item = item.withSignature(signature);
}
result.set(i, item);
}
result.setImmutable();
return result;
}
use of com.android.dx.rop.cst.CstString in project buck by facebook.
the class InsnFormat method cstString.
/**
* Helper method to return the constant string for a {@link CstInsn}
* in human form.
*
* @param insn {@code non-null;} a constant-bearing instruction
* @return {@code non-null;} the human string form of the contained
* constant
*/
protected static String cstString(DalvInsn insn) {
CstInsn ci = (CstInsn) insn;
Constant cst = ci.getConstant();
return cst instanceof CstString ? ((CstString) cst).toQuoted() : cst.toHuman();
}
use of com.android.dx.rop.cst.CstString in project buck by facebook.
the class ConstantPoolParser method parse0.
/**
* Parses the constant for the given index if it hasn't already been
* parsed, also storing it in the constant pool. This will also
* have the side effect of parsing any entries the indicated one
* depends on.
*
* @param idx which constant
* @return {@code non-null;} the parsed constant
*/
private Constant parse0(int idx, BitSet wasUtf8) {
Constant cst = pool.getOrNull(idx);
if (cst != null) {
return cst;
}
int at = offsets[idx];
try {
int tag = bytes.getUnsignedByte(at);
switch(tag) {
case CONSTANT_Utf8:
{
cst = parseUtf8(at);
wasUtf8.set(idx);
break;
}
case CONSTANT_Integer:
{
int value = bytes.getInt(at + 1);
cst = CstInteger.make(value);
break;
}
case CONSTANT_Float:
{
int bits = bytes.getInt(at + 1);
cst = CstFloat.make(bits);
break;
}
case CONSTANT_Long:
{
long value = bytes.getLong(at + 1);
cst = CstLong.make(value);
break;
}
case CONSTANT_Double:
{
long bits = bytes.getLong(at + 1);
cst = CstDouble.make(bits);
break;
}
case CONSTANT_Class:
{
int nameIndex = bytes.getUnsignedShort(at + 1);
CstString name = (CstString) parse0(nameIndex, wasUtf8);
cst = new CstType(Type.internClassName(name.getString()));
break;
}
case CONSTANT_String:
{
int stringIndex = bytes.getUnsignedShort(at + 1);
cst = parse0(stringIndex, wasUtf8);
break;
}
case CONSTANT_Fieldref:
{
int classIndex = bytes.getUnsignedShort(at + 1);
CstType type = (CstType) parse0(classIndex, wasUtf8);
int natIndex = bytes.getUnsignedShort(at + 3);
CstNat nat = (CstNat) parse0(natIndex, wasUtf8);
cst = new CstFieldRef(type, nat);
break;
}
case CONSTANT_Methodref:
{
int classIndex = bytes.getUnsignedShort(at + 1);
CstType type = (CstType) parse0(classIndex, wasUtf8);
int natIndex = bytes.getUnsignedShort(at + 3);
CstNat nat = (CstNat) parse0(natIndex, wasUtf8);
cst = new CstMethodRef(type, nat);
break;
}
case CONSTANT_InterfaceMethodref:
{
int classIndex = bytes.getUnsignedShort(at + 1);
CstType type = (CstType) parse0(classIndex, wasUtf8);
int natIndex = bytes.getUnsignedShort(at + 3);
CstNat nat = (CstNat) parse0(natIndex, wasUtf8);
cst = new CstInterfaceMethodRef(type, nat);
break;
}
case CONSTANT_NameAndType:
{
int nameIndex = bytes.getUnsignedShort(at + 1);
CstString name = (CstString) parse0(nameIndex, wasUtf8);
int descriptorIndex = bytes.getUnsignedShort(at + 3);
CstString descriptor = (CstString) parse0(descriptorIndex, wasUtf8);
cst = new CstNat(name, descriptor);
break;
}
case CONSTANT_MethodHandle:
{
throw new ParseException("MethodHandle not supported");
}
case CONSTANT_MethodType:
{
throw new ParseException("MethodType not supported");
}
case CONSTANT_InvokeDynamic:
{
throw new ParseException("InvokeDynamic not supported");
}
default:
{
throw new ParseException("unknown tag byte: " + Hex.u1(tag));
}
}
} catch (ParseException ex) {
ex.addContext("...while parsing cst " + Hex.u2(idx) + " at offset " + Hex.u4(at));
throw ex;
} catch (RuntimeException ex) {
ParseException pe = new ParseException(ex);
pe.addContext("...while parsing cst " + Hex.u2(idx) + " at offset " + Hex.u4(at));
throw pe;
}
pool.set(idx, cst);
return cst;
}
Aggregations