Search in sources :

Example 1 with CstArray

use of com.taobao.android.dx.rop.cst.CstArray in project atlas by alibaba.

the class ClassDataItem method makeStaticValuesConstant.

/**
     * Gets a {@link CstArray} corresponding to {@link #staticValues} if
     * it contains any non-zero non-{@code null} values.
     *
     * @return {@code null-ok;} the corresponding constant or {@code null} if
     * there are no values to encode
     */
private CstArray makeStaticValuesConstant() {
    // First sort the statics into their final order.
    Collections.sort(staticFields);
    /*
         * Get the size of staticValues minus any trailing zeros/nulls (both
         * nulls per se as well as instances of CstKnownNull).
         */
    int size = staticFields.size();
    while (size > 0) {
        EncodedField field = staticFields.get(size - 1);
        Constant cst = staticValues.get(field);
        if (cst instanceof CstLiteralBits) {
            // Note: CstKnownNull extends CstLiteralBits.
            if (((CstLiteralBits) cst).getLongBits() != 0) {
                break;
            }
        } else if (cst != null) {
            break;
        }
        size--;
    }
    if (size == 0) {
        return null;
    }
    // There is something worth encoding, so build up a result.
    CstArray.List list = new CstArray.List(size);
    for (int i = 0; i < size; i++) {
        EncodedField field = staticFields.get(i);
        Constant cst = staticValues.get(field);
        if (cst == null) {
            cst = Zeroes.zeroFor(field.getRef().getType());
        }
        list.set(i, cst);
    }
    list.setImmutable();
    return new CstArray(list);
}
Also used : CstArray(com.taobao.android.dx.rop.cst.CstArray) CstLiteralBits(com.taobao.android.dx.rop.cst.CstLiteralBits) Constant(com.taobao.android.dx.rop.cst.Constant) ArrayList(java.util.ArrayList)

Example 2 with CstArray

use of com.taobao.android.dx.rop.cst.CstArray in project atlas by alibaba.

the class ClassDefItem method addContents.

/** {@inheritDoc} */
@Override
public void addContents(DexFile file) {
    TypeIdsSection typeIds = file.getTypeIds();
    MixedItemSection byteData = file.getByteData();
    MixedItemSection wordData = file.getWordData();
    MixedItemSection typeLists = file.getTypeLists();
    StringIdsSection stringIds = file.getStringIds();
    typeIds.intern(thisClass);
    if (!classData.isEmpty()) {
        MixedItemSection classDataSection = file.getClassData();
        classDataSection.add(classData);
        CstArray staticValues = classData.getStaticValuesConstant();
        if (staticValues != null) {
            staticValuesItem = byteData.intern(new EncodedArrayItem(staticValues));
        }
    }
    if (superclass != null) {
        typeIds.intern(superclass);
    }
    if (interfaces != null) {
        interfaces = typeLists.intern(interfaces);
    }
    if (sourceFile != null) {
        stringIds.intern(sourceFile);
    }
    if (!annotationsDirectory.isEmpty()) {
        if (annotationsDirectory.isInternable()) {
            annotationsDirectory = wordData.intern(annotationsDirectory);
        } else {
            wordData.add(annotationsDirectory);
        }
    }
}
Also used : CstArray(com.taobao.android.dx.rop.cst.CstArray)

Example 3 with CstArray

use of com.taobao.android.dx.rop.cst.CstArray in project atlas by alibaba.

the class AnnotationUtils method makeMemberClasses.

/**
     * Constructs a standard {@code MemberClasses} annotation.
     *
     * @param types {@code non-null;} the list of (the types of) the member classes
     * @return {@code non-null;} the annotation
     */
public static Annotation makeMemberClasses(TypeList types) {
    CstArray array = makeCstArray(types);
    Annotation result = new Annotation(MEMBER_CLASSES_TYPE, SYSTEM);
    result.put(new NameValuePair(VALUE_STRING, array));
    result.setImmutable();
    return result;
}
Also used : NameValuePair(com.taobao.android.dx.rop.annotation.NameValuePair) CstArray(com.taobao.android.dx.rop.cst.CstArray) Annotation(com.taobao.android.dx.rop.annotation.Annotation) CstAnnotation(com.taobao.android.dx.rop.cst.CstAnnotation)

Example 4 with CstArray

use of com.taobao.android.dx.rop.cst.CstArray in project atlas by alibaba.

the class ValueEncoder method writeArray.

/**
     * Writes out the encoded form of the given array, that is, as
     * an {@code encoded_array} and not including a
     * {@code value_type} prefix. If the output stream keeps
     * (debugging) annotations and {@code topLevel} is
     * {@code true}, then this method will write (debugging)
     * annotations.
     *
     * @param array {@code non-null;} array instance to write
     * @param topLevel {@code true} iff the given annotation is the
     * top-level annotation or {@code false} if it is a sub-annotation
     * of some other annotation
     */
public void writeArray(CstArray array, boolean topLevel) {
    boolean annotates = topLevel && out.annotates();
    CstArray.List list = ((CstArray) array).getList();
    int size = list.size();
    if (annotates) {
        out.annotate("  size: " + Hex.u4(size));
    }
    out.writeUleb128(size);
    for (int i = 0; i < size; i++) {
        Constant cst = list.get(i);
        if (annotates) {
            out.annotate("  [" + Integer.toHexString(i) + "] " + constantToHuman(cst));
        }
        writeConstant(cst);
    }
    if (annotates) {
        out.endAnnotation();
    }
}
Also used : CstArray(com.taobao.android.dx.rop.cst.CstArray) Constant(com.taobao.android.dx.rop.cst.Constant)

Example 5 with CstArray

use of com.taobao.android.dx.rop.cst.CstArray 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

CstArray (com.taobao.android.dx.rop.cst.CstArray)7 Annotation (com.taobao.android.dx.rop.annotation.Annotation)4 CstAnnotation (com.taobao.android.dx.rop.cst.CstAnnotation)4 NameValuePair (com.taobao.android.dx.rop.annotation.NameValuePair)3 Constant (com.taobao.android.dx.rop.cst.Constant)2 CstString (com.taobao.android.dx.rop.cst.CstString)2 ArrayList (java.util.ArrayList)2 ParseException (com.taobao.android.dx.cf.iface.ParseException)1 AnnotationsList (com.taobao.android.dx.rop.annotation.AnnotationsList)1 CstDouble (com.taobao.android.dx.rop.cst.CstDouble)1 CstEnumRef (com.taobao.android.dx.rop.cst.CstEnumRef)1 CstFloat (com.taobao.android.dx.rop.cst.CstFloat)1 CstInteger (com.taobao.android.dx.rop.cst.CstInteger)1 CstLiteralBits (com.taobao.android.dx.rop.cst.CstLiteralBits)1 CstLong (com.taobao.android.dx.rop.cst.CstLong)1 CstNat (com.taobao.android.dx.rop.cst.CstNat)1 CstType (com.taobao.android.dx.rop.cst.CstType)1 Type (com.taobao.android.dx.rop.type.Type)1 TypeList (com.taobao.android.dx.rop.type.TypeList)1