Java Code Examples for org.apache.bcel.generic.ArrayType#getDimensions()

The following examples show how to use org.apache.bcel.generic.ArrayType#getDimensions() . 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: BCELifier.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
static String printType( final String signature ) {
    final Type type = Type.getType(signature);
    final byte t = type.getType();
    if (t <= Const.T_VOID) {
        return "Type." + Const.getTypeName(t).toUpperCase(Locale.ENGLISH);
    } else if (type.toString().equals("java.lang.String")) {
        return "Type.STRING";
    } else if (type.toString().equals("java.lang.Object")) {
        return "Type.OBJECT";
    } else if (type.toString().equals("java.lang.StringBuffer")) {
        return "Type.STRINGBUFFER";
    } else if (type instanceof ArrayType) {
        final ArrayType at = (ArrayType) type;
        return "new ArrayType(" + printType(at.getBasicType()) + ", " + at.getDimensions()
                + ")";
    } else {
        return "new ObjectType(\"" + Utility.signatureToString(signature, false) + "\")";
    }
}
 
Example 2
Source File: Subtypes2.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private ReferenceType computeFirstCommonSuperclassOfReferenceTypes(ReferenceType a, ReferenceType b)
        throws ClassNotFoundException {
    boolean aIsArrayType = (a instanceof ArrayType);
    boolean bIsArrayType = (b instanceof ArrayType);

    if (aIsArrayType && bIsArrayType) {
        // Merging array types - kind of a pain.

        ArrayType aArrType = (ArrayType) a;
        ArrayType bArrType = (ArrayType) b;

        if (aArrType.getDimensions() == bArrType.getDimensions()) {
            return computeFirstCommonSuperclassOfSameDimensionArrays(aArrType, bArrType);
        } else {
            return computeFirstCommonSuperclassOfDifferentDimensionArrays(aArrType, bArrType);
        }
    }

    if (aIsArrayType || bIsArrayType) {
        // One of a and b is an array type, but not both.
        // Common supertype is Object.
        return Type.OBJECT;
    }

    // Neither a nor b is an array type.
    // Find first common supertypes of ObjectTypes.
    return getFirstCommonSuperclass((ObjectType) a, (ObjectType) b);
}
 
Example 3
Source File: Subtypes2.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Determine whether or not a given ReferenceType is a subtype of another.
 * Throws ClassNotFoundException if the question cannot be answered
 * definitively due to a missing class.
 *
 * @param type
 *            a ReferenceType
 * @param possibleSupertype
 *            another Reference type
 * @return true if <code>type</code> is a subtype of
 *         <code>possibleSupertype</code>, false if not
 * @throws ClassNotFoundException
 *             if a missing class prevents a definitive answer
 */
public boolean isSubtype(ReferenceType type, ReferenceType possibleSupertype) throws ClassNotFoundException {

    // Eliminate some easy cases
    if (type.equals(possibleSupertype)) {
        return true;
    }
    if (possibleSupertype.equals(Type.OBJECT)) {
        return true;
    }
    if (type.equals(Type.OBJECT)) {
        return false;
    }

    boolean typeIsObjectType = (type instanceof ObjectType);
    boolean possibleSupertypeIsObjectType = (possibleSupertype instanceof ObjectType);

    if (typeIsObjectType && possibleSupertypeIsObjectType) {
        // Both types are ordinary object (non-array) types.
        return isSubtype((ObjectType) type, (ObjectType) possibleSupertype);
    }

    boolean typeIsArrayType = (type instanceof ArrayType);
    boolean possibleSupertypeIsArrayType = (possibleSupertype instanceof ArrayType);

    if (typeIsArrayType) {
        // Check superclass/interfaces
        if (possibleSupertype.equals(SERIALIZABLE) || possibleSupertype.equals(CLONEABLE)) {
            return true;
        }

        // We checked all of the possible class/interface supertypes,
        // so if possibleSupertype is not an array type,
        // then we can definitively say no
        if (!possibleSupertypeIsArrayType) {
            return false;
        }

        // Check array/array subtype relationship

        ArrayType typeAsArrayType = (ArrayType) type;
        ArrayType possibleSupertypeAsArrayType = (ArrayType) possibleSupertype;

        // Must have same number of dimensions
        if (typeAsArrayType.getDimensions() < possibleSupertypeAsArrayType.getDimensions()) {
            return false;
        }
        Type possibleSupertypeBasicType = possibleSupertypeAsArrayType.getBasicType();
        if (!(possibleSupertypeBasicType instanceof ObjectType)) {
            return false;
        }
        Type typeBasicType = typeAsArrayType.getBasicType();

        // If dimensions differ, see if element types are compatible.
        if (typeAsArrayType.getDimensions() > possibleSupertypeAsArrayType.getDimensions()) {
            return isSubtype(
                    new ArrayType(typeBasicType, typeAsArrayType.getDimensions()
                            - possibleSupertypeAsArrayType.getDimensions()), (ObjectType) possibleSupertypeBasicType);
        }

        // type's base type must be a subtype of possibleSupertype's base
        // type.
        // Note that neither base type can be a non-ObjectType if we are to
        // answer yes.

        if (!(typeBasicType instanceof ObjectType)) {
            return false;
        }

        return isSubtype((ObjectType) typeBasicType, (ObjectType) possibleSupertypeBasicType);
    }

    // OK, we've exhausted the possibilities now
    return false;
}