Search in sources :

Example 51 with Method

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

the class OverlyConcreteParameter method visitCode.

/**
 * implements the visitor to collect information about the parameters of a this method
 *
 * @param obj
 *            the currently parsed code block
 */
@Override
public void visitCode(final Code obj) {
    try {
        if (methodSignatureIsConstrained) {
            return;
        }
        if (obj.getCode() == null) {
            return;
        }
        Method m = getMethod();
        if (m.isSynthetic()) {
            return;
        }
        if (m.getName().startsWith("access$")) {
            return;
        }
        methodIsStatic = m.isStatic();
        parmCount = m.getArgumentTypes().length;
        if (parmCount == 0) {
            return;
        }
        parameterDefiners.clear();
        usedParameters.clear();
        stack.resetForMethodEntry(this);
        if (buildParameterDefiners()) {
            try {
                super.visitCode(obj);
                reportBugs();
            } catch (StopOpcodeParsingException e) {
            // no more possible parameter definers
            }
        }
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    }
}
Also used : StopOpcodeParsingException(com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException) Method(org.apache.bcel.classfile.Method)

Example 52 with Method

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

the class OverlyConcreteParameter method visitMethod.

/**
 * implements the visitor to look to see if this method is constrained by a superclass or interface.
 *
 * @param obj
 *            the currently parsed method
 */
@Override
public void visitMethod(Method obj) {
    methodSignatureIsConstrained = false;
    String methodName = obj.getName();
    if (!Values.CONSTRUCTOR.equals(methodName) && !Values.STATIC_INITIALIZER.equals(methodName)) {
        String methodSig = obj.getSignature();
        methodSignatureIsConstrained = methodIsSpecial(methodName, methodSig) || methodHasSyntheticTwin(methodName, methodSig);
        if (!methodSignatureIsConstrained) {
            for (AnnotationEntry entry : obj.getAnnotationEntries()) {
                if (CONVERSION_ANNOTATIONS.contains(entry.getAnnotationType())) {
                    methodSignatureIsConstrained = true;
                    break;
                }
            }
        }
        if (!methodSignatureIsConstrained) {
            String parms = methodSig.split("\\(|\\)")[1];
            if (parms.indexOf(Values.SIG_QUALIFIED_CLASS_SUFFIX_CHAR) >= 0) {
                outer: for (JavaClass constrainCls : constrainingClasses) {
                    Method[] methods = constrainCls.getMethods();
                    for (Method m : methods) {
                        if (methodName.equals(m.getName()) && methodSig.equals(m.getSignature())) {
                            methodSignatureIsConstrained = true;
                            break outer;
                        }
                    }
                }
            }
        }
    }
}
Also used : AnnotationEntry(org.apache.bcel.classfile.AnnotationEntry) ParameterAnnotationEntry(org.apache.bcel.classfile.ParameterAnnotationEntry) JavaClass(org.apache.bcel.classfile.JavaClass) ToString(com.mebigfatguy.fbcontrib.utils.ToString) Method(org.apache.bcel.classfile.Method)

Example 53 with Method

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

the class OverlyConcreteParameter method getPublicMethodInfos.

/**
 * returns a list of method information of all public or protected methods in this class
 *
 * @param cls
 *            the class to look for methods
 * @return a map of (method name)(method signature)
 */
private static List<MethodInfo> getPublicMethodInfos(final JavaClass cls) {
    List<MethodInfo> methodInfos = new ArrayList<>();
    Method[] methods = cls.getMethods();
    for (Method m : methods) {
        if ((m.getAccessFlags() & (Const.ACC_PUBLIC | Const.ACC_PROTECTED)) != 0) {
            ExceptionTable et = m.getExceptionTable();
            methodInfos.add(new MethodInfo(m.getName(), m.getSignature(), et == null ? null : et.getExceptionNames()));
        }
    }
    return methodInfos;
}
Also used : ArrayList(java.util.ArrayList) ExceptionTable(org.apache.bcel.classfile.ExceptionTable) Method(org.apache.bcel.classfile.Method)

Example 54 with Method

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

the class OverlyPermissiveMethod method isConstrainedByInterface.

/**
 * looks to see if this method is an implementation of a method in an interface, including generic specified interface methods.
 *
 * @param fqMethod
 *            the method to check
 * @return if this method is constrained by an interface method
 */
private boolean isConstrainedByInterface(FQMethod fqMethod) {
    try {
        JavaClass fqCls = Repository.lookupClass(fqMethod.getClassName());
        if (fqCls.isInterface()) {
            return true;
        }
        for (JavaClass inf : fqCls.getAllInterfaces()) {
            for (Method infMethod : inf.getMethods()) {
                if (infMethod.getName().equals(fqMethod.getMethodName())) {
                    String infMethodSig = infMethod.getSignature();
                    String fqMethodSig = fqMethod.getSignature();
                    if (infMethodSig.equals(fqMethodSig)) {
                        return true;
                    }
                    List<String> infTypes = SignatureUtils.getParameterSignatures(infMethodSig);
                    List<String> fqTypes = SignatureUtils.getParameterSignatures(fqMethodSig);
                    if (infTypes.size() == fqTypes.size()) {
                        boolean matches = true;
                        for (int i = 0; i < infTypes.size(); i++) {
                            String infParmType = infTypes.get(i);
                            String fqParmType = fqTypes.get(i);
                            if (infParmType.equals(fqParmType)) {
                                if ((infParmType.charAt(0) != 'L') || (fqParmType.charAt(0) != 'L')) {
                                    matches = false;
                                    break;
                                }
                                JavaClass infParmClass = Repository.lookupClass(SignatureUtils.stripSignature(infParmType));
                                JavaClass fqParmClass = Repository.lookupClass(SignatureUtils.stripSignature(fqParmType));
                                if (!fqParmClass.instanceOf(infParmClass)) {
                                    matches = false;
                                    break;
                                }
                            }
                        }
                        if (matches) {
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
        return true;
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) Method(org.apache.bcel.classfile.Method) BootstrapMethod(org.apache.bcel.classfile.BootstrapMethod) FQMethod(com.mebigfatguy.fbcontrib.utils.FQMethod)

Example 55 with Method

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

the class PartiallyConstructedObjectAccess method sawOpcode.

@Override
public void sawOpcode(int seen) {
    try {
        stack.precomputation(this);
        if ((seen == Const.INVOKEVIRTUAL) || (seen == Const.INVOKEINTERFACE) || (seen == Const.INVOKESPECIAL)) {
            int parmCount = SignatureUtils.getNumParameters(getSigConstantOperand());
            if (stack.getStackDepth() > parmCount) {
                OpcodeStack.Item itm = stack.getStackItem(parmCount);
                if (itm.getRegisterNumber() == 0) {
                    JavaClass cls = itm.getJavaClass();
                    if (cls != null) {
                        Method m = findMethod(cls, getNameConstantOperand(), getSigConstantOperand());
                        if ((m != null) && (!m.isFinal())) {
                            if (isCtor && (seen != Const.INVOKESPECIAL)) {
                                bugReporter.reportBug(new BugInstance(this, BugType.PCOA_PARTIALLY_CONSTRUCTED_OBJECT_ACCESS.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this, getPC()));
                                throw new StopOpcodeParsingException();
                            } else {
                                if (!Values.CONSTRUCTOR.equals(m.getName())) {
                                    Map<Method, SourceLineAnnotation> calledMethods = methodToCalledMethods.get(getMethod());
                                    calledMethods.put(m, SourceLineAnnotation.fromVisitedInstruction(this));
                                }
                            }
                        }
                    }
                }
            }
        }
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    } finally {
        stack.sawOpcode(this, seen);
    }
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) JavaClass(org.apache.bcel.classfile.JavaClass) SourceLineAnnotation(edu.umd.cs.findbugs.SourceLineAnnotation) StopOpcodeParsingException(com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException) BugInstance(edu.umd.cs.findbugs.BugInstance) Method(org.apache.bcel.classfile.Method)

Aggregations

Method (org.apache.bcel.classfile.Method)79 JavaClass (org.apache.bcel.classfile.JavaClass)28 BugInstance (edu.umd.cs.findbugs.BugInstance)20 ToString (com.mebigfatguy.fbcontrib.utils.ToString)12 StopOpcodeParsingException (com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException)11 FQMethod (com.mebigfatguy.fbcontrib.utils.FQMethod)7 HashMap (java.util.HashMap)7 HashSet (java.util.HashSet)7 Field (org.apache.bcel.classfile.Field)6 Type (org.apache.bcel.generic.Type)6 AnnotationEntry (org.apache.bcel.classfile.AnnotationEntry)5 ExceptionTable (org.apache.bcel.classfile.ExceptionTable)5 OpcodeStack (edu.umd.cs.findbugs.OpcodeStack)4 SourceLineAnnotation (edu.umd.cs.findbugs.SourceLineAnnotation)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 ConstantPoolGen (org.apache.bcel.generic.ConstantPoolGen)4 BugType (com.mebigfatguy.fbcontrib.utils.BugType)3 QMethod (com.mebigfatguy.fbcontrib.utils.QMethod)3 XMethod (edu.umd.cs.findbugs.ba.XMethod)3