Search in sources :

Example 26 with JavaClass

use of org.apache.bcel.classfile.JavaClass in project fb-contrib by mebigfatguy.

the class UnrelatedCollectionContents method addNewItem.

/**
 * adds this item's type and all of it's superclass/interfaces to the set of possible types that could define this added item
 *
 * @param supers
 *            the current set of superclass items
 * @param addItm
 *            the item we are adding
 * @throws ClassNotFoundException
 *             if a superclass/interface is not found
 */
private static void addNewItem(final Set<String> supers, final OpcodeStack.Item addItm) throws ClassNotFoundException {
    String itemSignature = addItm.getSignature();
    if (itemSignature.length() == 0) {
        return;
    }
    if (itemSignature.startsWith(Values.SIG_ARRAY_PREFIX)) {
        supers.add(itemSignature);
        return;
    }
    JavaClass cls = addItm.getJavaClass();
    if ((cls == null) || Values.DOTTED_JAVA_LANG_OBJECT.equals(cls.getClassName())) {
        return;
    }
    supers.add(cls.getClassName());
    JavaClass[] infs = cls.getAllInterfaces();
    for (JavaClass inf : infs) {
        String infName = inf.getClassName();
        if (!"java.io.Serializable".equals(infName) && !"java.lang.Cloneable".equals(infName)) {
            supers.add(infName);
        }
    }
    JavaClass[] sups = cls.getSuperClasses();
    for (JavaClass sup : sups) {
        String name = sup.getClassName();
        if (!Values.DOTTED_JAVA_LANG_OBJECT.equals(name)) {
            supers.add(name);
        }
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass)

Example 27 with JavaClass

use of org.apache.bcel.classfile.JavaClass in project fb-contrib by mebigfatguy.

the class UnrelatedReturnValues method visitCode.

/**
 * implements the visitor to see if the method returns Object, and if the method is defined in a superclass, or interface.
 *
 * @param obj
 *            the context object of the currently parsed code block
 */
@Override
public void visitCode(Code obj) {
    Method m = getMethod();
    if (m.isSynthetic()) {
        return;
    }
    String signature = m.getSignature();
    if (!signature.endsWith(")Ljava/lang/Object;")) {
        return;
    }
    stack.resetForMethodEntry(this);
    returnTypes.clear();
    super.visitCode(obj);
    if (returnTypes.size() <= 1) {
        return;
    }
    String methodName = m.getName();
    try {
        boolean isInherited = SignatureUtils.isInheritedMethod(currentClass, methodName, signature);
        int priority = NORMAL_PRIORITY;
        for (JavaClass cls : returnTypes.keySet()) {
            if ((cls != null) && Values.DOTTED_JAVA_LANG_OBJECT.equals(cls.getClassName())) {
                priority = LOW_PRIORITY;
                break;
            }
        }
        JavaClass cls = findCommonType(returnTypes.keySet());
        BugInstance bug;
        if (isInherited) {
            bug = new BugInstance(this, BugType.URV_INHERITED_METHOD_WITH_RELATED_TYPES.name(), priority).addClass(this).addMethod(this);
            if (cls != null) {
                bug.addString(cls.getClassName());
            }
        } else if (cls == null) {
            bug = new BugInstance(this, BugType.URV_UNRELATED_RETURN_VALUES.name(), priority).addClass(this).addMethod(this);
        } else {
            bug = new BugInstance(this, BugType.URV_CHANGE_RETURN_TYPE.name(), priority).addClass(this).addMethod(this);
            bug.addString(cls.getClassName());
        }
        for (Integer pc : returnTypes.values()) {
            bug.addSourceLine(this, pc.intValue());
        }
        bugReporter.reportBug(bug);
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) BugInstance(edu.umd.cs.findbugs.BugInstance) Method(org.apache.bcel.classfile.Method)

Example 28 with JavaClass

use of org.apache.bcel.classfile.JavaClass in project fb-contrib by mebigfatguy.

the class UnrelatedReturnValues method findCommonType.

/**
 * looks for a common superclass or interface for all the passed in types
 *
 * @param classes
 *            the set of classes to look for a common super class or interface
 * @return the type that is the common interface or superclass (not Object, tho).
 *
 * @throws ClassNotFoundException
 *             if a superclass or superinterface of one of the class is not found
 */
@Nullable
private static JavaClass findCommonType(Set<JavaClass> classes) throws ClassNotFoundException {
    Set<JavaClass> possibleCommonTypes = new HashSet<>();
    boolean populate = true;
    for (JavaClass cls : classes) {
        if (cls == null) {
            return null;
        }
        if (Values.SLASHED_JAVA_LANG_OBJECT.equals(cls.getClassName())) {
            continue;
        }
        JavaClass[] infs = cls.getAllInterfaces();
        JavaClass[] supers = cls.getSuperClasses();
        if (populate) {
            possibleCommonTypes.addAll(Arrays.asList(infs));
            possibleCommonTypes.addAll(Arrays.asList(supers));
            possibleCommonTypes.remove(Repository.lookupClass(Values.SLASHED_JAVA_LANG_OBJECT));
            populate = false;
        } else {
            Set<JavaClass> retain = new HashSet<>();
            retain.addAll(Arrays.asList(infs));
            retain.addAll(Arrays.asList(supers));
            possibleCommonTypes.retainAll(retain);
        }
    }
    if (possibleCommonTypes.isEmpty()) {
        return null;
    }
    for (JavaClass cls : possibleCommonTypes) {
        if (cls.isInterface()) {
            return cls;
        }
    }
    return possibleCommonTypes.iterator().next();
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) HashSet(java.util.HashSet) Nullable(javax.annotation.Nullable)

Example 29 with JavaClass

use of org.apache.bcel.classfile.JavaClass in project fb-contrib by mebigfatguy.

the class UseAddAll method isFieldCollection.

/**
 * determines if the stack item refers to a collection that is stored in a field
 *
 * @param item
 *            the stack item to check
 *
 * @return the field name of the collection, or null
 * @throws ClassNotFoundException
 *             if the items class cannot be found
 */
@Nullable
private String isFieldCollection(OpcodeStack.Item item) throws ClassNotFoundException {
    Comparable<?> aliasReg = (Comparable<?>) item.getUserValue();
    if (aliasReg instanceof String) {
        return (String) aliasReg;
    }
    XField field = item.getXField();
    if (field == null) {
        return null;
    }
    JavaClass cls = item.getJavaClass();
    if ((cls != null) && cls.implementationOf(collectionClass)) {
        return field.getName();
    }
    return null;
}
Also used : XField(edu.umd.cs.findbugs.ba.XField) JavaClass(org.apache.bcel.classfile.JavaClass) ToString(com.mebigfatguy.fbcontrib.utils.ToString) Nullable(javax.annotation.Nullable)

Example 30 with JavaClass

use of org.apache.bcel.classfile.JavaClass in project fb-contrib by mebigfatguy.

the class UseSplit method visitClassContext.

/**
 * implements the visitor to make sure the class is at least java 1.4 and to reset the opcode stack
 */
@Override
public void visitClassContext(ClassContext classContext) {
    try {
        JavaClass cls = classContext.getJavaClass();
        if (cls.getMajor() >= Const.MAJOR_1_4) {
            stack = new OpcodeStack();
            regValueType = new HashMap<Integer, State>();
            super.visitClassContext(classContext);
        }
    } finally {
        stack = null;
        regValueType = null;
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) OpcodeStack(edu.umd.cs.findbugs.OpcodeStack)

Aggregations

JavaClass (org.apache.bcel.classfile.JavaClass)144 OpcodeStack (edu.umd.cs.findbugs.OpcodeStack)45 BugInstance (edu.umd.cs.findbugs.BugInstance)43 Method (org.apache.bcel.classfile.Method)28 ToString (com.mebigfatguy.fbcontrib.utils.ToString)27 Field (org.apache.bcel.classfile.Field)17 HashSet (java.util.HashSet)14 HashMap (java.util.HashMap)11 ClassParser (org.apache.bcel.classfile.ClassParser)10 ArrayList (java.util.ArrayList)9 IOException (java.io.IOException)8 ExceptionTable (org.apache.bcel.classfile.ExceptionTable)8 XField (edu.umd.cs.findbugs.ba.XField)7 Nullable (javax.annotation.Nullable)7 AnnotationEntry (org.apache.bcel.classfile.AnnotationEntry)7 Type (org.apache.bcel.generic.Type)7 Iterator (java.util.Iterator)6 List (java.util.List)6 Map (java.util.Map)6 Set (java.util.Set)6