Search in sources :

Example 56 with JavaClass

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

the class CollectionNamingConfusion method checkConfusedName.

/**
 * looks for a name that mentions a collection type but the wrong type for the variable
 *
 * @param methodOrVariableName
 *            the method or variable name
 * @param signature
 *            the variable signature
 * @return whether the name doesn't match the type
 */
@edu.umd.cs.findbugs.annotations.SuppressFBWarnings(value = "EXS_EXCEPTION_SOFTENING_RETURN_FALSE", justification = "No other simple way to determine whether class exists")
private boolean checkConfusedName(String methodOrVariableName, String signature) {
    try {
        String name = methodOrVariableName.toLowerCase(Locale.ENGLISH);
        if ((name.endsWith("map") || (name.endsWith("set") && !name.endsWith("toset")) || name.endsWith("list") || name.endsWith("queue")) && signature.startsWith("Ljava/util/")) {
            String clsName = SignatureUtils.stripSignature(signature);
            JavaClass cls = Repository.lookupClass(clsName);
            if ((cls.implementationOf(mapInterface) && !name.endsWith("map")) || (cls.implementationOf(setInterface) && !name.endsWith("set")) || ((cls.implementationOf(listInterface) || cls.implementationOf(queueInterface)) && !name.endsWith("list") && !name.endsWith("queue"))) {
                return true;
            }
        }
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    }
    return false;
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass)

Example 57 with JavaClass

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

the class IncorrectInternalClassUse method visitClassContext.

/**
 * implements the visitor to look for classes that reference com.sun.xxx, or org.apache.xerces.xxx classes by looking for class Const in the constant pool
 *
 * @param context
 *            the context object of the currently parsed class
 */
@Override
public void visitClassContext(ClassContext context) {
    JavaClass cls = context.getJavaClass();
    if (!isInternal(cls.getClassName())) {
        ConstantPool pool = cls.getConstantPool();
        int numItems = pool.getLength();
        for (int i = 0; i < numItems; i++) {
            Constant c = pool.getConstant(i);
            if (c instanceof ConstantClass) {
                String clsName = ((ConstantClass) c).getBytes(pool);
                if (isInternal(clsName)) {
                    bugReporter.reportBug(new BugInstance(this, BugType.IICU_INCORRECT_INTERNAL_CLASS_USE.name(), NORMAL_PRIORITY).addClass(cls).addString(clsName));
                }
            }
        }
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) ConstantPool(org.apache.bcel.classfile.ConstantPool) Constant(org.apache.bcel.classfile.Constant) BugInstance(edu.umd.cs.findbugs.BugInstance) ToString(com.mebigfatguy.fbcontrib.utils.ToString) ConstantClass(org.apache.bcel.classfile.ConstantClass)

Example 58 with JavaClass

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

the class JPAIssues method getAnnotatedRollbackExceptions.

/**
 * parses an spring-tx @Transactional annotations for rollbackFor/noRollbackfor attributes of a @Transactional annotation.
 *
 * @param method
 *            the currently parsed method
 *
 * @return the exception classes declared in the @Transactional annotation
 *
 * @throws ClassNotFoundException
 *             if exception classes are not found
 */
private Set<JavaClass> getAnnotatedRollbackExceptions(Method method) throws ClassNotFoundException {
    for (AnnotationEntry annotation : method.getAnnotationEntries()) {
        if ("Lorg/springframework/transaction/annotation/Transactional;".equals(annotation.getAnnotationType())) {
            if (annotation.getNumElementValuePairs() == 0) {
                return Collections.<JavaClass>emptySet();
            }
            Set<JavaClass> rollbackExceptions = new HashSet<>();
            for (ElementValuePair pair : annotation.getElementValuePairs()) {
                if ("rollbackFor".equals(pair.getNameString()) || "noRollbackFor".equals(pair.getNameString())) {
                    String exNames = pair.getValue().stringifyValue();
                    Matcher m = annotationClassPattern.matcher(exNames);
                    while (m.find()) {
                        String exName = m.group(1);
                        JavaClass exCls = Repository.lookupClass(SignatureUtils.trimSignature(exName));
                        if (!exCls.instanceOf(runtimeExceptionClass)) {
                            rollbackExceptions.add(exCls);
                        }
                    }
                }
            }
            return rollbackExceptions;
        }
    }
    return Collections.<JavaClass>emptySet();
}
Also used : AnnotationEntry(org.apache.bcel.classfile.AnnotationEntry) JavaClass(org.apache.bcel.classfile.JavaClass) ElementValuePair(org.apache.bcel.classfile.ElementValuePair) Matcher(java.util.regex.Matcher) HashSet(java.util.HashSet)

Example 59 with JavaClass

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

the class ConfusingFunctionSemantics method visitCode.

/**
 * implements the visitor to look for any non-immutable typed parameters are assignable to the return type. If found, the method is parsed.
 *
 * @param obj
 *            the context object of the currently parsed code block
 */
@Override
public void visitCode(Code obj) {
    try {
        possibleParmRegs.clear();
        Method m = getMethod();
        String methodSignature = m.getSignature();
        String retSignature = SignatureUtils.getReturnSignature(methodSignature);
        JavaClass returnClass = null;
        int[] parmRegs = null;
        if (retSignature.startsWith(Values.SIG_QUALIFIED_CLASS_PREFIX) && !knownImmutables.contains(retSignature)) {
            List<String> parmTypes = SignatureUtils.getParameterSignatures(methodSignature);
            for (int p = 0; p < parmTypes.size(); p++) {
                String parmSignature = parmTypes.get(p);
                if (parmSignature.startsWith(Values.SIG_QUALIFIED_CLASS_PREFIX) && !knownImmutables.contains(parmSignature)) {
                    if (returnClass == null) {
                        returnClass = Repository.lookupClass(SignatureUtils.trimSignature(retSignature));
                        parmRegs = RegisterUtils.getParameterRegisters(m);
                    }
                    if (parmRegs != null) {
                        JavaClass parmClass = Repository.lookupClass(SignatureUtils.stripSignature(parmSignature));
                        if (parmClass.instanceOf(returnClass)) {
                            possibleParmRegs.put(Integer.valueOf(parmRegs[p]), new ParmUsage());
                        }
                    }
                }
            }
            if (!possibleParmRegs.isEmpty()) {
                try {
                    stack.resetForMethodEntry(this);
                    super.visitCode(obj);
                    for (ParmUsage pu : possibleParmRegs.values()) {
                        if ((pu.returnPC >= 0) && (pu.alteredPC >= 0) && (pu.returnPC > pu.alteredPC)) {
                            bugReporter.reportBug(new BugInstance(this, BugType.CFS_CONFUSING_FUNCTION_SEMANTICS.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this, pu.returnPC).addSourceLine(this, pu.alteredPC));
                        }
                    }
                } catch (StopOpcodeParsingException e) {
                // no parm regs left
                }
            }
        }
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) StopOpcodeParsingException(com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException) BugInstance(edu.umd.cs.findbugs.BugInstance) Method(org.apache.bcel.classfile.Method) ToString(com.mebigfatguy.fbcontrib.utils.ToString)

Example 60 with JavaClass

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

the class CopiedOverriddenMethod method visitClassContext.

/**
 * overrides the visitor to accept classes derived from non java.lang.Object classes.
 *
 * @param clsContext
 *            the context object of the currently parsed class
 */
@Override
public void visitClassContext(ClassContext clsContext) {
    try {
        JavaClass cls = clsContext.getJavaClass();
        String superName = cls.getSuperclassName();
        if (!Values.DOTTED_JAVA_LANG_OBJECT.equals(superName)) {
            this.classContext = clsContext;
            superclassCode = new HashMap<>();
            JavaClass superCls = cls.getSuperClass();
            childPoolGen = new ConstantPoolGen(cls.getConstantPool());
            parentPoolGen = new ConstantPoolGen(superCls.getConstantPool());
            Method[] methods = superCls.getMethods();
            for (Method m : methods) {
                String methodName = m.getName();
                if (m.isPublic() && !m.isAbstract() && !m.isSynthetic() && !Values.CONSTRUCTOR.equals(methodName) && !Values.STATIC_INITIALIZER.equals(methodName)) {
                    String methodInfo = methodName + ':' + m.getSignature();
                    superclassCode.put(methodInfo, new CodeInfo(m.getCode(), m.getAccessFlags()));
                }
            }
            cls.accept(this);
        }
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    } finally {
        superclassCode = null;
        this.classContext = null;
        childPoolGen = null;
        parentPoolGen = null;
    }
}
Also used : ConstantPoolGen(org.apache.bcel.generic.ConstantPoolGen) JavaClass(org.apache.bcel.classfile.JavaClass) ToString(com.mebigfatguy.fbcontrib.utils.ToString) Method(org.apache.bcel.classfile.Method)

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