Search in sources :

Example 11 with Annotation

use of com.taobao.android.dx.rop.annotation.Annotation in project atlas by alibaba.

the class AnnotationLister method visitPackageAnnotation.

/**
 * Inspects a package annotation
 *
 * @param cf {@code non-null;} class file of "package-info" pseudo-class
 * @param ann {@code non-null;} annotation
 */
private void visitPackageAnnotation(DirectClassFile cf, BaseAnnotations ann) {
    if (!args.eTypes.contains(ElementType.PACKAGE)) {
        return;
    }
    String packageName = cf.getThisClass().getClassType().getClassName();
    int slashIndex = packageName.lastIndexOf('/');
    if (slashIndex == -1) {
        packageName = "";
    } else {
        packageName = packageName.substring(0, slashIndex);
    }
    for (Annotation anAnn : ann.getAnnotations().getAnnotations()) {
        String annClassName = anAnn.getType().getClassType().getClassName();
        if (args.aclass.equals(annClassName)) {
            printMatchPackage(packageName);
        }
    }
}
Also used : Annotation(com.taobao.android.dx.rop.annotation.Annotation)

Example 12 with Annotation

use of com.taobao.android.dx.rop.annotation.Annotation in project atlas by alibaba.

the class AttributeTranslator method translateAnnotationDefaults.

/**
 * Gets the {@code AnnotationDefault} attributes out of a
 * given class, if any, reforming them as an
 * {@code AnnotationDefault} annotation.
 *
 * @param cf {@code non-null;} the class in question
 * @return {@code null-ok;} an appropriately-constructed
 * {@code AnnotationDefault} annotation, if there were any
 * annotation defaults in the class, or {@code null} if not
 */
private static Annotation translateAnnotationDefaults(DirectClassFile cf) {
    CstType thisClass = cf.getThisClass();
    MethodList methods = cf.getMethods();
    int sz = methods.size();
    Annotation result = new Annotation(thisClass, AnnotationVisibility.EMBEDDED);
    boolean any = false;
    for (int i = 0; i < sz; i++) {
        Method one = methods.get(i);
        AttributeList attribs = one.getAttributes();
        AttAnnotationDefault oneDefault = (AttAnnotationDefault) attribs.findFirst(AttAnnotationDefault.ATTRIBUTE_NAME);
        if (oneDefault != null) {
            NameValuePair pair = new NameValuePair(one.getNat().getName(), oneDefault.getValue());
            result.add(pair);
            any = true;
        }
    }
    if (!any) {
        return null;
    }
    result.setImmutable();
    return AnnotationUtils.makeAnnotationDefault(result);
}
Also used : NameValuePair(com.taobao.android.dx.rop.annotation.NameValuePair) AttributeList(com.taobao.android.dx.cf.iface.AttributeList) CstType(com.taobao.android.dx.rop.cst.CstType) MethodList(com.taobao.android.dx.cf.iface.MethodList) Method(com.taobao.android.dx.cf.iface.Method) AttEnclosingMethod(com.taobao.android.dx.cf.attrib.AttEnclosingMethod) Annotation(com.taobao.android.dx.rop.annotation.Annotation) AttAnnotationDefault(com.taobao.android.dx.cf.attrib.AttAnnotationDefault)

Example 13 with Annotation

use of com.taobao.android.dx.rop.annotation.Annotation in project atlas by alibaba.

the class AttributeTranslator method getClassAnnotations.

/**
 * Gets the annotations out of a given class, similar to {@link
 * #getAnnotations}, also including annotations for translations
 * of class-level attributes {@code EnclosingMethod} and
 * {@code InnerClasses}, if present. Additionally, if the
 * class is an annotation class, then this also includes a
 * representation of all the {@code AnnotationDefault}
 * values.
 *
 * @param cf {@code non-null;} the class in question
 * @param args {@code non-null;} the high-level options
 * @return {@code non-null;} the set of annotations, which may be empty
 */
public static Annotations getClassAnnotations(DirectClassFile cf, CfOptions args) {
    CstType thisClass = cf.getThisClass();
    AttributeList attribs = cf.getAttributes();
    Annotations result = getAnnotations(attribs);
    Annotation enclosingMethod = translateEnclosingMethod(attribs);
    try {
        Annotations innerClassAnnotations = translateInnerClasses(thisClass, attribs, enclosingMethod == null);
        if (innerClassAnnotations != null) {
            result = Annotations.combine(result, innerClassAnnotations);
        }
    } catch (Warning warn) {
        args.warn.println("warning: " + warn.getMessage());
    }
    if (enclosingMethod != null) {
        result = Annotations.combine(result, enclosingMethod);
    }
    if (AccessFlags.isAnnotation(cf.getAccessFlags())) {
        Annotation annotationDefault = translateAnnotationDefaults(cf);
        if (annotationDefault != null) {
            result = Annotations.combine(result, annotationDefault);
        }
    }
    return result;
}
Also used : Warning(com.taobao.android.dx.util.Warning) AttRuntimeVisibleParameterAnnotations(com.taobao.android.dx.cf.attrib.AttRuntimeVisibleParameterAnnotations) AttRuntimeInvisibleParameterAnnotations(com.taobao.android.dx.cf.attrib.AttRuntimeInvisibleParameterAnnotations) AttRuntimeInvisibleAnnotations(com.taobao.android.dx.cf.attrib.AttRuntimeInvisibleAnnotations) Annotations(com.taobao.android.dx.rop.annotation.Annotations) AttRuntimeVisibleAnnotations(com.taobao.android.dx.cf.attrib.AttRuntimeVisibleAnnotations) AttributeList(com.taobao.android.dx.cf.iface.AttributeList) CstType(com.taobao.android.dx.rop.cst.CstType) Annotation(com.taobao.android.dx.rop.annotation.Annotation)

Example 14 with Annotation

use of com.taobao.android.dx.rop.annotation.Annotation in project atlas by alibaba.

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.taobao.android.dx.rop.annotation.Annotations) Annotation(com.taobao.android.dx.rop.annotation.Annotation) CstAnnotation(com.taobao.android.dx.rop.cst.CstAnnotation)

Example 15 with Annotation

use of com.taobao.android.dx.rop.annotation.Annotation in project atlas by alibaba.

the class AnnotationParser method parseValue.

/**
 * Parses an annotation value.
 *
 * @return {@code non-null;} the parsed value
 */
private Constant parseValue() throws IOException {
    int tag = input.readUnsignedByte();
    if (observer != null) {
        CstString humanTag = new CstString(Character.toString((char) tag));
        parsed(1, "tag: " + humanTag.toQuoted());
    }
    switch(tag) {
        case 'B':
            {
                CstInteger value = (CstInteger) parseConstant();
                return CstByte.make(value.getValue());
            }
        case 'C':
            {
                CstInteger value = (CstInteger) parseConstant();
                int intValue = value.getValue();
                return CstChar.make(value.getValue());
            }
        case 'D':
            {
                CstDouble value = (CstDouble) parseConstant();
                return value;
            }
        case 'F':
            {
                CstFloat value = (CstFloat) parseConstant();
                return value;
            }
        case 'I':
            {
                CstInteger value = (CstInteger) parseConstant();
                return value;
            }
        case 'J':
            {
                CstLong value = (CstLong) parseConstant();
                return value;
            }
        case 'S':
            {
                CstInteger value = (CstInteger) parseConstant();
                return CstShort.make(value.getValue());
            }
        case 'Z':
            {
                CstInteger value = (CstInteger) parseConstant();
                return CstBoolean.make(value.getValue());
            }
        case 'c':
            {
                int classInfoIndex = input.readUnsignedShort();
                CstString value = (CstString) pool.get(classInfoIndex);
                Type type = Type.internReturnType(value.getString());
                if (observer != null) {
                    parsed(2, "class_info: " + type.toHuman());
                }
                return new CstType(type);
            }
        case 's':
            {
                return parseConstant();
            }
        case 'e':
            {
                requireLength(4);
                int typeNameIndex = input.readUnsignedShort();
                int constNameIndex = input.readUnsignedShort();
                CstString typeName = (CstString) pool.get(typeNameIndex);
                CstString constName = (CstString) pool.get(constNameIndex);
                if (observer != null) {
                    parsed(2, "type_name: " + typeName.toHuman());
                    parsed(2, "const_name: " + constName.toHuman());
                }
                return new CstEnumRef(new CstNat(constName, typeName));
            }
        case '@':
            {
                Annotation annotation = parseAnnotation(AnnotationVisibility.EMBEDDED);
                return new CstAnnotation(annotation);
            }
        case '[':
            {
                requireLength(2);
                int numValues = input.readUnsignedShort();
                CstArray.List list = new CstArray.List(numValues);
                if (observer != null) {
                    parsed(2, "num_values: " + numValues);
                    changeIndent(1);
                }
                for (int i = 0; i < numValues; i++) {
                    if (observer != null) {
                        changeIndent(-1);
                        parsed(0, "element_value[" + i + "]:");
                        changeIndent(1);
                    }
                    list.set(i, parseValue());
                }
                if (observer != null) {
                    changeIndent(-1);
                }
                list.setImmutable();
                return new CstArray(list);
            }
        default:
            {
                throw new ParseException("unknown annotation tag: " + Hex.u1(tag));
            }
    }
}
Also used : CstFloat(com.taobao.android.dx.rop.cst.CstFloat) CstNat(com.taobao.android.dx.rop.cst.CstNat) CstArray(com.taobao.android.dx.rop.cst.CstArray) CstLong(com.taobao.android.dx.rop.cst.CstLong) CstString(com.taobao.android.dx.rop.cst.CstString) CstAnnotation(com.taobao.android.dx.rop.cst.CstAnnotation) CstDouble(com.taobao.android.dx.rop.cst.CstDouble) CstEnumRef(com.taobao.android.dx.rop.cst.CstEnumRef) Annotation(com.taobao.android.dx.rop.annotation.Annotation) CstAnnotation(com.taobao.android.dx.rop.cst.CstAnnotation) CstType(com.taobao.android.dx.rop.cst.CstType) Type(com.taobao.android.dx.rop.type.Type) CstInteger(com.taobao.android.dx.rop.cst.CstInteger) CstType(com.taobao.android.dx.rop.cst.CstType) AnnotationsList(com.taobao.android.dx.rop.annotation.AnnotationsList) ParseException(com.taobao.android.dx.cf.iface.ParseException)

Aggregations

Annotation (com.taobao.android.dx.rop.annotation.Annotation)16 CstAnnotation (com.taobao.android.dx.rop.cst.CstAnnotation)10 NameValuePair (com.taobao.android.dx.rop.annotation.NameValuePair)9 Annotations (com.taobao.android.dx.rop.annotation.Annotations)5 CstArray (com.taobao.android.dx.rop.cst.CstArray)4 CstString (com.taobao.android.dx.rop.cst.CstString)4 CstType (com.taobao.android.dx.rop.cst.CstType)4 AttRuntimeInvisibleAnnotations (com.taobao.android.dx.cf.attrib.AttRuntimeInvisibleAnnotations)3 AttRuntimeInvisibleParameterAnnotations (com.taobao.android.dx.cf.attrib.AttRuntimeInvisibleParameterAnnotations)3 AttRuntimeVisibleAnnotations (com.taobao.android.dx.cf.attrib.AttRuntimeVisibleAnnotations)3 AttRuntimeVisibleParameterAnnotations (com.taobao.android.dx.cf.attrib.AttRuntimeVisibleParameterAnnotations)3 AttributeList (com.taobao.android.dx.cf.iface.AttributeList)2 AnnotationsList (com.taobao.android.dx.rop.annotation.AnnotationsList)2 CstNat (com.taobao.android.dx.rop.cst.CstNat)2 TypeList (com.taobao.android.dx.rop.type.TypeList)2 AttAnnotationDefault (com.taobao.android.dx.cf.attrib.AttAnnotationDefault)1 AttEnclosingMethod (com.taobao.android.dx.cf.attrib.AttEnclosingMethod)1 Method (com.taobao.android.dx.cf.iface.Method)1 MethodList (com.taobao.android.dx.cf.iface.MethodList)1 ParseException (com.taobao.android.dx.cf.iface.ParseException)1