Java Code Examples for org.apache.bcel.generic.ObjectType#equals()

The following examples show how to use org.apache.bcel.generic.ObjectType#equals() . 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: Subtypes2.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Determine whether or not a given ObjectType 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(ObjectType type, ObjectType possibleSupertype) throws ClassNotFoundException {
    if (DEBUG_QUERIES) {
        System.out.println("isSubtype: check " + type + " subtype of " + possibleSupertype);
    }

    if (type.equals(possibleSupertype)) {
        if (DEBUG_QUERIES) {
            System.out.println("  ==> yes, types are same");
        }
        return true;
    }
    ClassDescriptor typeClassDescriptor = DescriptorFactory.getClassDescriptor(type);
    ClassDescriptor possibleSuperclassClassDescriptor = DescriptorFactory.getClassDescriptor(possibleSupertype);

    return isSubtype(typeClassDescriptor, possibleSuperclassClassDescriptor);
}
 
Example 2
Source File: Hierarchy.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Determine if the given ObjectType refers to an unchecked exception
 * (RuntimeException or Error).
 */
public static boolean isUncheckedException(ObjectType type) throws ClassNotFoundException {
    if (type.equals(Type.THROWABLE) || type.equals(RUNTIME_EXCEPTION_TYPE) || type.equals(ERROR_TYPE)) {
        return true;
    }
    ClassDescriptor c = DescriptorFactory.getClassDescriptor(type);
    Subtypes2 subtypes2 = Global.getAnalysisCache().getDatabase(Subtypes2.class);
    return subtypes2.isSubtype(c, RUNTIME_EXCEPTION, ERROR);

}
 
Example 3
Source File: Subtypes2.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
private ObjectType computeFirstCommonSuperclassOfObjectTypes(ObjectType a, ObjectType b) throws ClassNotFoundException {
    ObjectType firstCommonSupertype;
    ClassDescriptor aDesc = DescriptorFactory.getClassDescriptor(a);
    ClassDescriptor bDesc = DescriptorFactory.getClassDescriptor(b);

    ClassVertex aVertex = resolveClassVertex(aDesc);
    ClassVertex bVertex = resolveClassVertex(bDesc);

    Set<ClassDescriptor> aSuperTypes = computeKnownSupertypes(aDesc);
    Set<ClassDescriptor> bSuperTypes = computeKnownSupertypes(bDesc);
    if (bSuperTypes.contains(aDesc)) {
        return a;
    }
    if (aSuperTypes.contains(bDesc)) {
        return b;
    }
    ArrayList<ClassVertex> aSuperList = getAllSuperclassVertices(aVertex);
    ArrayList<ClassVertex> bSuperList = getAllSuperclassVertices(bVertex);

    // Work backwards until the lists diverge.
    // The last element common to both lists is the first
    // common superclass.
    int aIndex = aSuperList.size() - 1;
    int bIndex = bSuperList.size() - 1;

    ClassVertex lastCommonInBackwardsSearch = null;
    while (aIndex >= 0 && bIndex >= 0) {
        if (aSuperList.get(aIndex) != bSuperList.get(bIndex)) {
            break;
        }
        lastCommonInBackwardsSearch = aSuperList.get(aIndex);
        aIndex--;
        bIndex--;
    }
    if (lastCommonInBackwardsSearch == null) {
        firstCommonSupertype = Type.OBJECT;
    } else {
        firstCommonSupertype = ObjectTypeFactory.getInstance(lastCommonInBackwardsSearch.getClassDescriptor()
                .toDottedClassName());
    }
    if (firstCommonSupertype.equals(Type.OBJECT)) {
        // see if we can't do better
        ClassDescriptor objDesc = DescriptorFactory.getClassDescriptor(Type.OBJECT);
        aSuperTypes.retainAll(bSuperTypes);
        aSuperTypes.remove(objDesc);
        for (ClassDescriptor c : aSuperTypes) {
            if (c.getPackageName().equals(aDesc.getPackageName()) || c.getPackageName().equals(bDesc.getPackageName())) {
                return ObjectTypeFactory.getInstance(c.toDottedClassName());
            }
        }

        if (!aSuperTypes.isEmpty()) {
            return ObjectTypeFactory.getInstance(aSuperTypes.iterator().next().toDottedClassName());
        }
    }

    return firstCommonSupertype;
}
 
Example 4
Source File: Subtypes2.java    From spotbugs with GNU Lesser General Public License v2.1 3 votes vote down vote up
/**
 * Get the first common superclass of the given object types. Note that an
 * interface type is never returned unless <code>a</code> and <code>b</code>
 * are the same type. Otherwise, we try to return as accurate a type as
 * possible. This method is used as the meet operator in
 * TypeDataflowAnalysis, and is intended to follow (more or less) the JVM
 * bytecode verifier semantics.
 *
 * <p>
 * This method should be used in preference to the
 * getFirstCommonSuperclass() method in {@link ReferenceType}.
 * </p>
 *
 * @param a
 *            an ObjectType
 * @param b
 *            another ObjectType
 * @return the first common superclass of <code>a</code> and <code>b</code>
 * @throws ClassNotFoundException
 */
public ObjectType getFirstCommonSuperclass(ObjectType a, ObjectType b) throws ClassNotFoundException {
    // Easy case
    if (a.equals(b)) {
        return a;
    }

    ObjectType firstCommonSupertype = (ObjectType) checkFirstCommonSuperclassQueryCache(a, b);
    if (firstCommonSupertype == null) {
        firstCommonSupertype = computeFirstCommonSuperclassOfObjectTypes(a, b);
        firstCommonSuperclassQueryCache.put(a, b, firstCommonSupertype);
    }

    return firstCommonSupertype;
}
 
Example 5
Source File: Hierarchy.java    From spotbugs with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Determine if the given ObjectType reference represents a
 * <em>universal</em> exception handler. That is, one that will catch any
 * kind of exception.
 *
 * @param catchType
 *            the ObjectType of the exception handler
 * @return true if catchType is null, or if catchType is java.lang.Throwable
 */
public static boolean isUniversalExceptionHandler(ObjectType catchType) {
    return catchType == null || catchType.equals(Type.THROWABLE);
}