Search in sources :

Example 1 with AttributeList

use of com.taobao.android.dx.cf.iface.AttributeList in project atlas by alibaba.

the class AnnotationLister method process.

/** Processes based on configuration specified in constructor. */
void process() {
    for (String path : args.files) {
        ClassPathOpener opener;
        opener = new ClassPathOpener(path, true, new ClassPathOpener.Consumer() {

            public boolean processFileBytes(String name, long lastModified, byte[] bytes) {
                if (!name.endsWith(".class")) {
                    return true;
                }
                ByteArray ba = new ByteArray(bytes);
                DirectClassFile cf = new DirectClassFile(ba, name, true);
                cf.setAttributeFactory(StdAttributeFactory.THE_ONE);
                AttributeList attributes = cf.getAttributes();
                Attribute att;
                String cfClassName = cf.getThisClass().getClassType().getClassName();
                if (cfClassName.endsWith(PACKAGE_INFO)) {
                    att = attributes.findFirst(AttRuntimeInvisibleAnnotations.ATTRIBUTE_NAME);
                    for (; att != null; att = attributes.findNext(att)) {
                        BaseAnnotations ann = (BaseAnnotations) att;
                        visitPackageAnnotation(cf, ann);
                    }
                    att = attributes.findFirst(AttRuntimeVisibleAnnotations.ATTRIBUTE_NAME);
                    for (; att != null; att = attributes.findNext(att)) {
                        BaseAnnotations ann = (BaseAnnotations) att;
                        visitPackageAnnotation(cf, ann);
                    }
                } else if (isMatchingInnerClass(cfClassName) || isMatchingPackage(cfClassName)) {
                    printMatch(cf);
                } else {
                    att = attributes.findFirst(AttRuntimeInvisibleAnnotations.ATTRIBUTE_NAME);
                    for (; att != null; att = attributes.findNext(att)) {
                        BaseAnnotations ann = (BaseAnnotations) att;
                        visitClassAnnotation(cf, ann);
                    }
                    att = attributes.findFirst(AttRuntimeVisibleAnnotations.ATTRIBUTE_NAME);
                    for (; att != null; att = attributes.findNext(att)) {
                        BaseAnnotations ann = (BaseAnnotations) att;
                        visitClassAnnotation(cf, ann);
                    }
                }
                return true;
            }

            public void onException(Exception ex) {
                throw new RuntimeException(ex);
            }

            public void onProcessArchiveStart(File file) {
            }
        });
        opener.process();
    }
}
Also used : ClassPathOpener(com.taobao.android.dx.cf.direct.ClassPathOpener) DirectClassFile(com.taobao.android.dx.cf.direct.DirectClassFile) Attribute(com.taobao.android.dx.cf.iface.Attribute) AttributeList(com.taobao.android.dx.cf.iface.AttributeList) BaseAnnotations(com.taobao.android.dx.cf.attrib.BaseAnnotations) ByteArray(com.taobao.android.dx.util.ByteArray) DirectClassFile(com.taobao.android.dx.cf.direct.DirectClassFile) File(java.io.File)

Example 2 with AttributeList

use of com.taobao.android.dx.cf.iface.AttributeList in project atlas by alibaba.

the class AttributeTranslator method getExceptions.

/**
     * Gets the list of thrown exceptions for a given method.
     *
     * @param method {@code non-null;} the method in question
     * @return {@code non-null;} the list of thrown exceptions
     */
public static TypeList getExceptions(Method method) {
    AttributeList attribs = method.getAttributes();
    AttExceptions exceptions = (AttExceptions) attribs.findFirst(AttExceptions.ATTRIBUTE_NAME);
    if (exceptions == null) {
        return StdTypeList.EMPTY;
    }
    return exceptions.getExceptions();
}
Also used : AttExceptions(com.taobao.android.dx.cf.attrib.AttExceptions) AttributeList(com.taobao.android.dx.cf.iface.AttributeList)

Example 3 with AttributeList

use of com.taobao.android.dx.cf.iface.AttributeList 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 4 with AttributeList

use of com.taobao.android.dx.cf.iface.AttributeList in project atlas by alibaba.

the class AttributeTranslator method getParameterAnnotations.

/**
     * Gets the parameter annotations out of a given method. This
     * combines both visible and invisible annotations into a single
     * result set.
     *
     * @param method {@code non-null;} the method in question
     * @return {@code non-null;} the list of annotation sets, which may be
     * empty
     */
public static AnnotationsList getParameterAnnotations(Method method) {
    AttributeList attribs = method.getAttributes();
    AttRuntimeVisibleParameterAnnotations visible = (AttRuntimeVisibleParameterAnnotations) attribs.findFirst(AttRuntimeVisibleParameterAnnotations.ATTRIBUTE_NAME);
    AttRuntimeInvisibleParameterAnnotations invisible = (AttRuntimeInvisibleParameterAnnotations) attribs.findFirst(AttRuntimeInvisibleParameterAnnotations.ATTRIBUTE_NAME);
    if (visible == null) {
        if (invisible == null) {
            return AnnotationsList.EMPTY;
        }
        return invisible.getParameterAnnotations();
    }
    if (invisible == null) {
        return visible.getParameterAnnotations();
    }
    return AnnotationsList.combine(visible.getParameterAnnotations(), invisible.getParameterAnnotations());
}
Also used : AttributeList(com.taobao.android.dx.cf.iface.AttributeList) AttRuntimeVisibleParameterAnnotations(com.taobao.android.dx.cf.attrib.AttRuntimeVisibleParameterAnnotations) AttRuntimeInvisibleParameterAnnotations(com.taobao.android.dx.cf.attrib.AttRuntimeInvisibleParameterAnnotations)

Example 5 with AttributeList

use of com.taobao.android.dx.cf.iface.AttributeList 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)

Aggregations

AttributeList (com.taobao.android.dx.cf.iface.AttributeList)6 AttRuntimeInvisibleParameterAnnotations (com.taobao.android.dx.cf.attrib.AttRuntimeInvisibleParameterAnnotations)2 AttRuntimeVisibleParameterAnnotations (com.taobao.android.dx.cf.attrib.AttRuntimeVisibleParameterAnnotations)2 Attribute (com.taobao.android.dx.cf.iface.Attribute)2 Annotation (com.taobao.android.dx.rop.annotation.Annotation)2 CstType (com.taobao.android.dx.rop.cst.CstType)2 AttAnnotationDefault (com.taobao.android.dx.cf.attrib.AttAnnotationDefault)1 AttEnclosingMethod (com.taobao.android.dx.cf.attrib.AttEnclosingMethod)1 AttExceptions (com.taobao.android.dx.cf.attrib.AttExceptions)1 AttRuntimeInvisibleAnnotations (com.taobao.android.dx.cf.attrib.AttRuntimeInvisibleAnnotations)1 AttRuntimeVisibleAnnotations (com.taobao.android.dx.cf.attrib.AttRuntimeVisibleAnnotations)1 AttSourceFile (com.taobao.android.dx.cf.attrib.AttSourceFile)1 BaseAnnotations (com.taobao.android.dx.cf.attrib.BaseAnnotations)1 ClassPathOpener (com.taobao.android.dx.cf.direct.ClassPathOpener)1 DirectClassFile (com.taobao.android.dx.cf.direct.DirectClassFile)1 Method (com.taobao.android.dx.cf.iface.Method)1 MethodList (com.taobao.android.dx.cf.iface.MethodList)1 StdAttributeList (com.taobao.android.dx.cf.iface.StdAttributeList)1 Annotations (com.taobao.android.dx.rop.annotation.Annotations)1 NameValuePair (com.taobao.android.dx.rop.annotation.NameValuePair)1