Java Code Examples for com.android.dx.rop.type.TypeList#size()

The following examples show how to use com.android.dx.rop.type.TypeList#size() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StdCatchBuilder.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public HashSet<Type> getCatchTypes() {
    HashSet<Type> result = new HashSet<Type>(20);
    BasicBlockList blocks = method.getBlocks();
    int size = blocks.size();

    for (int i = 0; i < size; i++) {
        BasicBlock block = blocks.get(i);
        TypeList catches = block.getLastInsn().getCatches();
        int catchSize = catches.size();

        for (int j = 0; j < catchSize; j++) {
            result.add(catches.getType(j));
        }
    }

    return result;
}
 
Example 2
Source File: ClassReferenceListBuilder.java    From buck with Apache License 2.0 6 votes vote down vote up
private void addClassWithHierarchy(String classBinaryName) {
    if (classNames.contains(classBinaryName)) {
        return;
    }

    try {
        DirectClassFile classFile = path.getClass(classBinaryName + CLASS_EXTENSION);
        if (CLASS_TO_CHECK != null && classBinaryName.contains(CLASS_TO_CHECK)) {
          found();
        }
        classNames.add(classBinaryName);
        CstType superClass = classFile.getSuperclass();
        if (superClass != null) {
            addClassWithHierarchy(superClass.getClassType().getClassName());
        }

        TypeList interfaceList = classFile.getInterfaces();
        int interfaceNumber = interfaceList.size();
        for (int i = 0; i < interfaceNumber; i++) {
            addClassWithHierarchy(interfaceList.getType(i).getClassName());
        }
    } catch (FileNotFoundException e) {
        // Ignore: The referenced type is not in the path it must be part of the libraries.
    }
}
 
Example 3
Source File: ClassReferenceListBuilder.java    From RocooFix with MIT License 6 votes vote down vote up
private void addClassWithHierachy(String classBinaryName) {
    if (classNames.contains(classBinaryName)) {
        return;
    }
    try {
        DirectClassFile classFile = path.getClass(classBinaryName + CLASS_EXTENSION);
        classNames.add(classBinaryName);
        CstType superClass = classFile.getSuperclass();
        if (superClass != null) {
            addClassWithHierachy(superClass.getClassType().getClassName());
        }
        TypeList interfaceList = classFile.getInterfaces();
        int interfaceNumber = interfaceList.size();
        for (int i = 0; i < interfaceNumber; i++) {
            addClassWithHierachy(interfaceList.getType(i).getClassName());
        }
    } catch (FileNotFoundException e) {
        // Ignore: The referenced type is not in the path it must be part of the libraries.
    }
}
 
Example 4
Source File: StdCatchBuilder.java    From buck with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
public HashSet<Type> getCatchTypes() {
    HashSet<Type> result = new HashSet<Type>(20);
    BasicBlockList blocks = method.getBlocks();
    int size = blocks.size();

    for (int i = 0; i < size; i++) {
        BasicBlock block = blocks.get(i);
        TypeList catches = block.getLastInsn().getCatches();
        int catchSize = catches.size();

        for (int j = 0; j < catchSize; j++) {
            result.add(catches.getType(j));
        }
    }

    return result;
}
 
Example 5
Source File: StdCatchBuilder.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public HashSet<Type> getCatchTypes() {
    HashSet<Type> result = new HashSet<Type>(20);
    BasicBlockList blocks = method.getBlocks();
    int size = blocks.size();

    for (int i = 0; i < size; i++) {
        BasicBlock block = blocks.get(i);
        TypeList catches = block.getLastInsn().getCatches();
        int catchSize = catches.size();

        for (int j = 0; j < catchSize; j++) {
            result.add(catches.getType(j));
        }
    }

    return result;
}
 
Example 6
Source File: StdCatchBuilder.java    From Box with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
public boolean hasAnyCatches() {
    BasicBlockList blocks = method.getBlocks();
    int size = blocks.size();

    for (int i = 0; i < size; i++) {
        BasicBlock block = blocks.get(i);
        TypeList catches = block.getLastInsn().getCatches();
        if (catches.size() != 0) {
            return true;
        }
    }

    return false;
}
 
Example 7
Source File: ClassDefItem.java    From J2ME-Loader with Apache License 2.0 6 votes vote down vote up
/**
 * Constructs an instance. Its sets of members and annotations are
 * initially empty.
 *
 * @param thisClass {@code non-null;} type constant for this class
 * @param accessFlags access flags
 * @param superclass {@code null-ok;} superclass or {@code null} if
 * this class is a/the root class
 * @param interfaces {@code non-null;} list of implemented interfaces
 * @param sourceFile {@code null-ok;} source file name or
 * {@code null} if unknown
 */
public ClassDefItem(CstType thisClass, int accessFlags,
        CstType superclass, TypeList interfaces, CstString sourceFile) {
    if (thisClass == null) {
        throw new NullPointerException("thisClass == null");
    }

    /*
     * TODO: Maybe check accessFlags and superclass, at
     * least for easily-checked stuff?
     */

    if (interfaces == null) {
        throw new NullPointerException("interfaces == null");
    }

    this.thisClass = thisClass;
    this.accessFlags = accessFlags;
    this.superclass = superclass;
    this.interfaces =
        (interfaces.size() == 0) ? null :  new TypeListItem(interfaces);
    this.sourceFile = sourceFile;
    this.classData = new ClassDataItem(thisClass);
    this.staticValuesItem = null;
    this.annotationsDirectory = new AnnotationsDirectoryItem();
}
 
Example 8
Source File: Rops.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the appropriate binary arithmetic rop for the given type
 * and arguments. The result is a shared instance.
 *
 * @param types {@code non-null;} sources of the operation
 * @param int1 {@code non-null;} the int-to-constant rop
 * @param long1 {@code non-null;} the long-to-constant rop
 * @param float1 {@code null-ok;} the float-to-constant rop, if any
 * @param double1 {@code null-ok;} the double-to-constant rop, if any
 * @param int2 {@code non-null;} the int-to-int rop
 * @param long2 {@code non-null;} the long-to-long or long-to-int rop
 * @param float2 {@code null-ok;} the float-to-float rop, if any
 * @param double2 {@code null-ok;} the double-to-double rop, if any
 * @return {@code non-null;} an appropriate instance
 */
private static Rop pickBinaryOp(TypeList types, Rop int1, Rop long1,
                                Rop float1, Rop double1, Rop int2,
                                Rop long2, Rop float2, Rop double2) {
    int bt1 = types.getType(0).getBasicFrameType();
    Rop result = null;

    switch (types.size()) {
        case 1: {
            switch(bt1) {
                case Type.BT_INT:    return int1;
                case Type.BT_LONG:   return long1;
                case Type.BT_FLOAT:  result = float1; break;
                case Type.BT_DOUBLE: result = double1; break;
            }
            break;
        }
        case 2: {
            switch(bt1) {
                case Type.BT_INT:    return int2;
                case Type.BT_LONG:   return long2;
                case Type.BT_FLOAT:  result = float2; break;
                case Type.BT_DOUBLE: result = double2; break;
            }
            break;
        }
    }

    if (result == null) {
        return throwBadTypes(types);
    }

    return result;
}
 
Example 9
Source File: AnnotationUtils.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Converts a {@link TypeList} to a {@link CstArray}.
 *
 * @param types {@code non-null;} the type list
 * @return {@code non-null;} the corresponding array constant
 */
private static CstArray makeCstArray(TypeList types) {
    int size = types.size();
    CstArray.List list = new CstArray.List(size);

    for (int i = 0; i < size; i++) {
        list.set(i, CstType.intern(types.getType(i)));
    }

    list.setImmutable();
    return new CstArray(list);
}
 
Example 10
Source File: Rops.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the appropriate binary arithmetic rop for the given type
 * and arguments. The result is a shared instance.
 *
 * @param types {@code non-null;} sources of the operation
 * @param int1 {@code non-null;} the int-to-constant rop
 * @param long1 {@code non-null;} the long-to-constant rop
 * @param float1 {@code null-ok;} the float-to-constant rop, if any
 * @param double1 {@code null-ok;} the double-to-constant rop, if any
 * @param int2 {@code non-null;} the int-to-int rop
 * @param long2 {@code non-null;} the long-to-long or long-to-int rop
 * @param float2 {@code null-ok;} the float-to-float rop, if any
 * @param double2 {@code null-ok;} the double-to-double rop, if any
 * @return {@code non-null;} an appropriate instance
 */
private static Rop pickBinaryOp(TypeList types, Rop int1, Rop long1,
                                Rop float1, Rop double1, Rop int2,
                                Rop long2, Rop float2, Rop double2) {
    int bt1 = types.getType(0).getBasicFrameType();
    Rop result = null;

    switch (types.size()) {
        case 1: {
            switch(bt1) {
                case Type.BT_INT:    return int1;
                case Type.BT_LONG:   return long1;
                case Type.BT_FLOAT:  result = float1; break;
                case Type.BT_DOUBLE: result = double1; break;
            }
            break;
        }
        case 2: {
            switch(bt1) {
                case Type.BT_INT:    return int2;
                case Type.BT_LONG:   return long2;
                case Type.BT_FLOAT:  result = float2; break;
                case Type.BT_DOUBLE: result = double2; break;
            }
            break;
        }
    }

    if (result == null) {
        return throwBadTypes(types);
    }

    return result;
}
 
Example 11
Source File: Rop.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs an instance. This method is private. Use one of the
 * public constructors.
 *
 * @param opcode the opcode; one of the constants in {@link RegOps}
 * @param result {@code non-null;} result type of this operation; {@link
 * Type#VOID} for no-result operations
 * @param sources {@code non-null;} types of all the sources of this operation
 * @param exceptions {@code non-null;} list of possible types thrown by this
 * operation
 * @param branchingness the branchingness of this op; one of the
 * {@code BRANCH_*} constants
 * @param isCallLike whether the op is a function/method call or similar
 * @param nickname {@code null-ok;} optional nickname (used for debugging)
 */
public Rop(int opcode, Type result, TypeList sources,
           TypeList exceptions, int branchingness, boolean isCallLike,
           String nickname) {
    if (result == null) {
        throw new NullPointerException("result == null");
    }

    if (sources == null) {
        throw new NullPointerException("sources == null");
    }

    if (exceptions == null) {
        throw new NullPointerException("exceptions == null");
    }

    if ((branchingness < BRANCH_MIN) || (branchingness > BRANCH_MAX)) {
        throw new IllegalArgumentException("invalid branchingness: " + branchingness);
    }

    if ((exceptions.size() != 0) && (branchingness != BRANCH_THROW)) {
        throw new IllegalArgumentException("exceptions / branchingness " +
                                           "mismatch");
    }

    this.opcode = opcode;
    this.result = result;
    this.sources = sources;
    this.exceptions = exceptions;
    this.branchingness = branchingness;
    this.isCallLike = isCallLike;
    this.nickname = nickname;
}
 
Example 12
Source File: Rops.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Helper for all the {@code if*}-related methods, which
 * checks types and picks one of the four variants, throwing if
 * there's a problem.
 *
 * @param types {@code non-null;} the types
 * @param intZ {@code non-null;} the int-to-0 comparison
 * @param objZ {@code null-ok;} the object-to-null comparison
 * @param intInt {@code non-null;} the int-to-int comparison
 * @param objObj {@code non-null;} the object-to-object comparison
 * @return {@code non-null;} the appropriate instance
 */
private static Rop pickIf(TypeList types, Rop intZ, Rop objZ, Rop intInt,
                          Rop objObj) {
    switch(types.size()) {
        case 1: {
            switch (types.getType(0).getBasicFrameType()) {
                case Type.BT_INT: {
                    return intZ;
                }
                case Type.BT_OBJECT: {
                    if (objZ != null) {
                        return objZ;
                    }
                }
            }
            break;
        }
        case 2: {
            int bt = types.getType(0).getBasicFrameType();
            if (bt == types.getType(1).getBasicFrameType()) {
                switch (bt) {
                    case Type.BT_INT: {
                        return intInt;
                    }
                    case Type.BT_OBJECT: {
                        if (objObj != null) {
                            return objObj;
                        }
                    }
                }
            }
            break;
        }
    }

    return throwBadTypes(types);
}
 
Example 13
Source File: ThrowingInsn.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the string form of a register spec list to be used as a catches
 * list.
 *
 * @param catches {@code non-null;} the catches list
 * @return {@code non-null;} the string form
 */
public static String toCatchString(TypeList catches) {
    StringBuilder sb = new StringBuilder(100);

    sb.append("catch");

    int sz = catches.size();
    for (int i = 0; i < sz; i++) {
        sb.append(" ");
        sb.append(catches.getType(i).toHuman());
    }

    return sb.toString();
}
 
Example 14
Source File: ThrowingInsn.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the string form of a register spec list to be used as a catches
 * list.
 *
 * @param catches {@code non-null;} the catches list
 * @return {@code non-null;} the string form
 */
public static String toCatchString(TypeList catches) {
    StringBuilder sb = new StringBuilder(100);

    sb.append("catch");

    int sz = catches.size();
    for (int i = 0; i < sz; i++) {
        sb.append(" ");
        sb.append(catches.getType(i).toHuman());
    }

    return sb.toString();
}
 
Example 15
Source File: Rops.java    From buck with Apache License 2.0 5 votes vote down vote up
/**
 * Helper for all the {@code if*}-related methods, which
 * checks types and picks one of the four variants, throwing if
 * there's a problem.
 *
 * @param types {@code non-null;} the types
 * @param intZ {@code non-null;} the int-to-0 comparison
 * @param objZ {@code null-ok;} the object-to-null comparison
 * @param intInt {@code non-null;} the int-to-int comparison
 * @param objObj {@code non-null;} the object-to-object comparison
 * @return {@code non-null;} the appropriate instance
 */
private static Rop pickIf(TypeList types, Rop intZ, Rop objZ, Rop intInt,
                          Rop objObj) {
    switch(types.size()) {
        case 1: {
            switch (types.getType(0).getBasicFrameType()) {
                case Type.BT_INT: {
                    return intZ;
                }
                case Type.BT_OBJECT: {
                    if (objZ != null) {
                        return objZ;
                    }
                }
            }
            break;
        }
        case 2: {
            int bt = types.getType(0).getBasicFrameType();
            if (bt == types.getType(1).getBasicFrameType()) {
                switch (bt) {
                    case Type.BT_INT: {
                        return intInt;
                    }
                    case Type.BT_OBJECT: {
                        if (objObj != null) {
                            return objObj;
                        }
                    }
                }
            }
            break;
        }
    }

    return throwBadTypes(types);
}
 
Example 16
Source File: ClassDefsSection.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Helper for {@link #orderItems}, which recursively assigns indices
 * to classes.
 *
 * @param type {@code null-ok;} type ref to assign, if any
 * @param idx {@code >= 0;} the next index to assign
 * @param maxDepth maximum recursion depth; if negative, this will
 * throw an exception indicating class definition circularity
 * @return {@code >= 0;} the next index to assign
 */
private int orderItems0(Type type, int idx, int maxDepth) {
    ClassDefItem c = classDefs.get(type);

    if ((c == null) || (c.hasIndex())) {
        return idx;
    }

    if (maxDepth < 0) {
        throw new RuntimeException("class circularity with " + type);
    }

    maxDepth--;

    CstType superclassCst = c.getSuperclass();
    if (superclassCst != null) {
        Type superclass = superclassCst.getClassType();
        idx = orderItems0(superclass, idx, maxDepth);
    }

    TypeList interfaces = c.getInterfaces();
    int sz = interfaces.size();
    for (int i = 0; i < sz; i++) {
        idx = orderItems0(interfaces.getType(i), idx, maxDepth);
    }

    c.setIndex(idx);
    orderedDefs.add(c);
    return idx + 1;
}
 
Example 17
Source File: Rop.java    From Box with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs an instance. This method is private. Use one of the
 * public constructors.
 *
 * @param opcode the opcode; one of the constants in {@link RegOps}
 * @param result {@code non-null;} result type of this operation; {@link
 * Type#VOID} for no-result operations
 * @param sources {@code non-null;} types of all the sources of this operation
 * @param exceptions {@code non-null;} list of possible types thrown by this
 * operation
 * @param branchingness the branchingness of this op; one of the
 * {@code BRANCH_*} constants
 * @param isCallLike whether the op is a function/method call or similar
 * @param nickname {@code null-ok;} optional nickname (used for debugging)
 */
public Rop(int opcode, Type result, TypeList sources,
           TypeList exceptions, int branchingness, boolean isCallLike,
           String nickname) {
    if (result == null) {
        throw new NullPointerException("result == null");
    }

    if (sources == null) {
        throw new NullPointerException("sources == null");
    }

    if (exceptions == null) {
        throw new NullPointerException("exceptions == null");
    }

    if ((branchingness < BRANCH_MIN) || (branchingness > BRANCH_MAX)) {
        throw new IllegalArgumentException("invalid branchingness: " + branchingness);
    }

    if ((exceptions.size() != 0) && (branchingness != BRANCH_THROW)) {
        throw new IllegalArgumentException("exceptions / branchingness " +
                                           "mismatch");
    }

    this.opcode = opcode;
    this.result = result;
    this.sources = sources;
    this.exceptions = exceptions;
    this.branchingness = branchingness;
    this.isCallLike = isCallLike;
    this.nickname = nickname;
}
 
Example 18
Source File: Rop.java    From J2ME-Loader with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs an instance. This method is private. Use one of the
 * public constructors.
 *
 * @param opcode the opcode; one of the constants in {@link RegOps}
 * @param result {@code non-null;} result type of this operation; {@link
 * Type#VOID} for no-result operations
 * @param sources {@code non-null;} types of all the sources of this operation
 * @param exceptions {@code non-null;} list of possible types thrown by this
 * operation
 * @param branchingness the branchingness of this op; one of the
 * {@code BRANCH_*} constants
 * @param isCallLike whether the op is a function/method call or similar
 * @param nickname {@code null-ok;} optional nickname (used for debugging)
 */
public Rop(int opcode, Type result, TypeList sources,
           TypeList exceptions, int branchingness, boolean isCallLike,
           String nickname) {
    if (result == null) {
        throw new NullPointerException("result == null");
    }

    if (sources == null) {
        throw new NullPointerException("sources == null");
    }

    if (exceptions == null) {
        throw new NullPointerException("exceptions == null");
    }

    if ((branchingness < BRANCH_MIN) || (branchingness > BRANCH_MAX)) {
        throw new IllegalArgumentException("invalid branchingness: " + branchingness);
    }

    if ((exceptions.size() != 0) && (branchingness != BRANCH_THROW)) {
        throw new IllegalArgumentException("exceptions / branchingness " +
                                           "mismatch");
    }

    this.opcode = opcode;
    this.result = result;
    this.sources = sources;
    this.exceptions = exceptions;
    this.branchingness = branchingness;
    this.isCallLike = isCallLike;
    this.nickname = nickname;
}
 
Example 19
Source File: ClassDefItem.java    From Box with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
@Override
public void writeTo(DexFile file, AnnotatedOutput out) {
    boolean annotates = out.annotates();
    TypeIdsSection typeIds = file.getTypeIds();
    int classIdx = typeIds.indexOf(thisClass);
    int superIdx = (superclass == null) ? -1 :
        typeIds.indexOf(superclass);
    int interOff = OffsettedItem.getAbsoluteOffsetOr0(interfaces);
    int annoOff = annotationsDirectory.isEmpty() ? 0 :
        annotationsDirectory.getAbsoluteOffset();
    int sourceFileIdx = (sourceFile == null) ? -1 :
        file.getStringIds().indexOf(sourceFile);
    int dataOff = classData.isEmpty()? 0 : classData.getAbsoluteOffset();
    int staticValuesOff =
        OffsettedItem.getAbsoluteOffsetOr0(staticValuesItem);

    if (annotates) {
        out.annotate(0, indexString() + ' ' + thisClass.toHuman());
        out.annotate(4, "  class_idx:           " + Hex.u4(classIdx));
        out.annotate(4, "  access_flags:        " +
                     AccessFlags.classString(accessFlags));
        out.annotate(4, "  superclass_idx:      " + Hex.u4(superIdx) +
                     " // " + ((superclass == null) ? "<none>" :
                      superclass.toHuman()));
        out.annotate(4, "  interfaces_off:      " + Hex.u4(interOff));
        if (interOff != 0) {
            TypeList list = interfaces.getList();
            int sz = list.size();
            for (int i = 0; i < sz; i++) {
                out.annotate(0, "    " + list.getType(i).toHuman());
            }
        }
        out.annotate(4, "  source_file_idx:     " + Hex.u4(sourceFileIdx) +
                     " // " + ((sourceFile == null) ? "<none>" :
                      sourceFile.toHuman()));
        out.annotate(4, "  annotations_off:     " + Hex.u4(annoOff));
        out.annotate(4, "  class_data_off:      " + Hex.u4(dataOff));
        out.annotate(4, "  static_values_off:   " +
                Hex.u4(staticValuesOff));
    }

    out.writeInt(classIdx);
    out.writeInt(accessFlags);
    out.writeInt(superIdx);
    out.writeInt(interOff);
    out.writeInt(sourceFileIdx);
    out.writeInt(annoOff);
    out.writeInt(dataOff);
    out.writeInt(staticValuesOff);
}
 
Example 20
Source File: StdCatchBuilder.java    From Box with Apache License 2.0 4 votes vote down vote up
/**
 * Makes the {@link CatchHandlerList} for the given basic block.
 *
 * @param block {@code non-null;} block to get entries for
 * @param addresses {@code non-null;} address objects for each block
 * @return {@code non-null;} array of entries
 */
private static CatchHandlerList handlersFor(BasicBlock block,
        BlockAddresses addresses) {
    IntList successors = block.getSuccessors();
    int succSize = successors.size();
    int primary = block.getPrimarySuccessor();
    TypeList catches = block.getLastInsn().getCatches();
    int catchSize = catches.size();

    if (catchSize == 0) {
        return CatchHandlerList.EMPTY;
    }

    if (((primary == -1) && (succSize != catchSize))
            || ((primary != -1) &&
                    ((succSize != (catchSize + 1))
                            || (primary != successors.get(catchSize))))) {
        /*
         * Blocks that throw are supposed to list their primary
         * successor -- if any -- last in the successors list, but
         * that constraint appears to be violated here.
         */
        throw new RuntimeException(
                "shouldn't happen: weird successors list");
    }

    /*
     * Reduce the effective catchSize if we spot a catch-all that
     * isn't at the end.
     */
    for (int i = 0; i < catchSize; i++) {
        Type type = catches.getType(i);
        if (type.equals(Type.OBJECT)) {
            catchSize = i + 1;
            break;
        }
    }

    CatchHandlerList result = new CatchHandlerList(catchSize);

    for (int i = 0; i < catchSize; i++) {
        CstType oneType = new CstType(catches.getType(i));
        CodeAddress oneHandler = addresses.getStart(successors.get(i));
        result.set(i, oneType, oneHandler.getAddress());
    }

    result.setImmutable();
    return result;
}