Search in sources :

Example 16 with Annotations

use of com.android.dx.rop.annotation.Annotations 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();
}
Also used : CstNat(com.android.dx.rop.cst.CstNat) CstString(com.android.dx.rop.cst.CstString) AnnotationsList(com.android.dx.rop.annotation.AnnotationsList) CstString(com.android.dx.rop.cst.CstString) TreeMap(java.util.TreeMap) Annotation(com.android.dx.rop.annotation.Annotation) Annotations(com.android.dx.rop.annotation.Annotations) ClassDefItem(com.android.dx.dex.file.ClassDefItem) EncodedMethod(com.android.dx.dex.file.EncodedMethod) PrintWriter(java.io.PrintWriter)

Example 17 with Annotations

use of com.android.dx.rop.annotation.Annotations 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;
}
Also used : Constant(com.android.dx.rop.cst.Constant) TypedConstant(com.android.dx.rop.cst.TypedConstant) CstString(com.android.dx.rop.cst.CstString) CstFieldRef(com.android.dx.rop.cst.CstFieldRef) CstEnumRef(com.android.dx.rop.cst.CstEnumRef) FieldIdsSection(com.android.dx.dex.file.FieldIdsSection) MethodIdsSection(com.android.dx.dex.file.MethodIdsSection) CstMethodRef(com.android.dx.rop.cst.CstMethodRef) Annotations(com.android.dx.rop.annotation.Annotations) ClassDefItem(com.android.dx.dex.file.ClassDefItem) ConstantPool(com.android.dx.rop.cst.ConstantPool) CstType(com.android.dx.rop.cst.CstType) CstInterfaceMethodRef(com.android.dx.rop.cst.CstInterfaceMethodRef)

Example 18 with Annotations

use of com.android.dx.rop.annotation.Annotations in project buck by facebook.

the class AnnotationParser method parseAnnotations.

/**
     * Parses an annotation list.
     *
     * @param visibility {@code non-null;} visibility of the parsed annotations
     * @return {@code non-null;} the list of annotations read from the attribute
     * data
     */
private Annotations parseAnnotations(AnnotationVisibility visibility) throws IOException {
    int count = input.readUnsignedShort();
    if (observer != null) {
        parsed(2, "num_annotations: " + Hex.u2(count));
    }
    Annotations annotations = new Annotations();
    for (int i = 0; i < count; i++) {
        if (observer != null) {
            parsed(0, "annotations[" + i + "]:");
            changeIndent(1);
        }
        Annotation annotation = parseAnnotation(visibility);
        annotations.add(annotation);
        if (observer != null) {
            observer.changeIndent(-1);
        }
    }
    annotations.setImmutable();
    return annotations;
}
Also used : Annotations(com.android.dx.rop.annotation.Annotations) Annotation(com.android.dx.rop.annotation.Annotation) CstAnnotation(com.android.dx.rop.cst.CstAnnotation)

Example 19 with Annotations

use of com.android.dx.rop.annotation.Annotations in project J2ME-Loader by nikita36078.

the class AnnotationParser method parseAnnotationsList.

/**
 * Parses a list of annotation lists.
 *
 * @param visibility {@code non-null;} visibility of the parsed annotations
 * @return {@code non-null;} the list of annotation lists read from the attribute
 * data
 */
private AnnotationsList parseAnnotationsList(AnnotationVisibility visibility) throws IOException {
    int count = input.readUnsignedByte();
    if (observer != null) {
        parsed(1, "num_parameters: " + Hex.u1(count));
    }
    AnnotationsList outerList = new AnnotationsList(count);
    for (int i = 0; i < count; i++) {
        if (observer != null) {
            parsed(0, "parameter_annotations[" + i + "]:");
            changeIndent(1);
        }
        Annotations annotations = parseAnnotations(visibility);
        outerList.set(i, annotations);
        if (observer != null) {
            observer.changeIndent(-1);
        }
    }
    outerList.setImmutable();
    return outerList;
}
Also used : Annotations(com.android.dx.rop.annotation.Annotations) AnnotationsList(com.android.dx.rop.annotation.AnnotationsList)

Example 20 with Annotations

use of com.android.dx.rop.annotation.Annotations in project J2ME-Loader by nikita36078.

the class AttributeTranslator method getMethodAnnotations.

/**
 * Gets the annotations out of a given method, similar to {@link
 * #getAnnotations}, also including an annotation for the translation
 * of the method-specific attribute {@code Exceptions}.
 *
 * @param method {@code non-null;} the method in question
 * @return {@code non-null;} the set of annotations, which may be empty
 */
public static Annotations getMethodAnnotations(Method method) {
    Annotations result = getAnnotations(method.getAttributes());
    TypeList exceptions = getExceptions(method);
    if (exceptions.size() != 0) {
        Annotation throwsAnnotation = AnnotationUtils.makeThrows(exceptions);
        result = Annotations.combine(result, throwsAnnotation);
    }
    return result;
}
Also used : AttRuntimeVisibleAnnotations(com.android.dx.cf.attrib.AttRuntimeVisibleAnnotations) AttRuntimeVisibleParameterAnnotations(com.android.dx.cf.attrib.AttRuntimeVisibleParameterAnnotations) Annotations(com.android.dx.rop.annotation.Annotations) AttRuntimeInvisibleAnnotations(com.android.dx.cf.attrib.AttRuntimeInvisibleAnnotations) AttRuntimeInvisibleParameterAnnotations(com.android.dx.cf.attrib.AttRuntimeInvisibleParameterAnnotations) TypeList(com.android.dx.rop.type.TypeList) StdTypeList(com.android.dx.rop.type.StdTypeList) Annotation(com.android.dx.rop.annotation.Annotation)

Aggregations

Annotations (com.android.dx.rop.annotation.Annotations)24 AttRuntimeInvisibleAnnotations (com.android.dx.cf.attrib.AttRuntimeInvisibleAnnotations)12 AttRuntimeInvisibleParameterAnnotations (com.android.dx.cf.attrib.AttRuntimeInvisibleParameterAnnotations)12 AttRuntimeVisibleAnnotations (com.android.dx.cf.attrib.AttRuntimeVisibleAnnotations)12 AttRuntimeVisibleParameterAnnotations (com.android.dx.cf.attrib.AttRuntimeVisibleParameterAnnotations)12 Annotation (com.android.dx.rop.annotation.Annotation)10 CstType (com.android.dx.rop.cst.CstType)10 CstString (com.android.dx.rop.cst.CstString)8 AnnotationsList (com.android.dx.rop.annotation.AnnotationsList)6 ClassDefItem (com.android.dx.dex.file.ClassDefItem)4 EncodedMethod (com.android.dx.dex.file.EncodedMethod)4 CstFieldRef (com.android.dx.rop.cst.CstFieldRef)4 CstMethodRef (com.android.dx.rop.cst.CstMethodRef)4 TypedConstant (com.android.dx.rop.cst.TypedConstant)4 StdTypeList (com.android.dx.rop.type.StdTypeList)4 TypeList (com.android.dx.rop.type.TypeList)4 Warning (com.android.dx.util.Warning)4 AttInnerClasses (com.android.dx.cf.attrib.AttInnerClasses)2 InnerClassList (com.android.dx.cf.attrib.InnerClassList)2 ConcreteMethod (com.android.dx.cf.code.ConcreteMethod)2