Search in sources :

Example 21 with Method

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

the class OverlyConcreteParameter method buildParameterDefiners.

/**
 * builds a map of method information for each method of each interface that each parameter implements of this method
 *
 * @return a map by parameter id of all the method signatures that interfaces of that parameter implements
 *
 * @throws ClassNotFoundException
 *             if the class can't be loaded
 */
private boolean buildParameterDefiners() throws ClassNotFoundException {
    Method m = getMethod();
    Type[] parms = m.getArgumentTypes();
    if (parms.length == 0) {
        return false;
    }
    ParameterAnnotationEntry[] annotations = m.getParameterAnnotationEntries();
    boolean hasPossiblyOverlyConcreteParm = false;
    for (int i = 0; i < parms.length; i++) {
        if ((annotations.length <= i) || (annotations[i] == null) || (annotations[i].getAnnotationEntries().length == 0)) {
            String parm = parms[i].getSignature();
            if (parm.startsWith(Values.SIG_QUALIFIED_CLASS_PREFIX)) {
                String clsName = SignatureUtils.stripSignature(parm);
                if (clsName.startsWith("java.lang.")) {
                    continue;
                }
                JavaClass clz = Repository.lookupClass(clsName);
                if (clz.isClass() && (!clz.isAbstract())) {
                    Map<JavaClass, List<MethodInfo>> definers = getClassDefiners(clz);
                    if (!definers.isEmpty()) {
                        parameterDefiners.put(Integer.valueOf(i + (methodIsStatic ? 0 : 1)), definers);
                        hasPossiblyOverlyConcreteParm = true;
                    }
                }
            }
        }
    }
    return hasPossiblyOverlyConcreteParm;
}
Also used : Type(org.apache.bcel.generic.Type) BugType(com.mebigfatguy.fbcontrib.utils.BugType) JavaClass(org.apache.bcel.classfile.JavaClass) ParameterAnnotationEntry(org.apache.bcel.classfile.ParameterAnnotationEntry) ArrayList(java.util.ArrayList) List(java.util.List) Method(org.apache.bcel.classfile.Method) ToString(com.mebigfatguy.fbcontrib.utils.ToString)

Example 22 with Method

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

the class OverlyConcreteParameter method hasOverloadedMethod.

/**
 * looks to see if this method has an overloaded method in actuality, this isn't precise and returns true for methods that are named the same, and have same
 * number of args This will miss some cases, but in practicality doesn't matter.
 */
private boolean hasOverloadedMethod() {
    Method thisMethod = getMethod();
    String name = thisMethod.getName();
    String sig = thisMethod.getSignature();
    int numArgs = SignatureUtils.getNumParameters(sig);
    for (Method m : cls.getMethods()) {
        if (m.getName().equals(name)) {
            if (!m.getSignature().equals(sig) && (numArgs == SignatureUtils.getNumParameters(m.getSignature()))) {
                return true;
            }
        }
    }
    return false;
}
Also used : Method(org.apache.bcel.classfile.Method) ToString(com.mebigfatguy.fbcontrib.utils.ToString)

Example 23 with Method

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

the class OverlyPermissiveMethod method visitCode.

@Override
public void visitCode(Code obj) {
    Method m = getMethod();
    String methodName = m.getName();
    String sig = m.getSignature();
    if (isAssumedPublic(methodName)) {
        MethodInfo mi = Statistics.getStatistics().getMethodStatistics(cls.getClassName(), methodName, sig);
        mi.addCallingAccess(Const.ACC_PUBLIC);
    } else {
        if (!hasRuntimeAnnotations(m) && !isGetterSetter(methodName, sig)) {
            MethodInfo mi = Statistics.getStatistics().getMethodStatistics(cls.getClassName(), methodName, sig);
            mi.addCallingAccess(Const.ACC_PUBLIC);
        }
        stack.resetForMethodEntry(this);
        super.visitCode(obj);
    }
}
Also used : MethodInfo(com.mebigfatguy.fbcontrib.collect.MethodInfo) Method(org.apache.bcel.classfile.Method) BootstrapMethod(org.apache.bcel.classfile.BootstrapMethod) FQMethod(com.mebigfatguy.fbcontrib.utils.FQMethod)

Example 24 with Method

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

the class PartiallyConstructedObjectAccess method visitCode.

@Override
public void visitCode(final Code obj) {
    stack.resetForMethodEntry(this);
    String methodName = getMethodName();
    isCtor = Values.CONSTRUCTOR.equals(methodName);
    if (!Values.STATIC_INITIALIZER.equals(methodName)) {
        Method m = getMethod();
        methodToCalledMethods.put(m, new HashMap<Method, SourceLineAnnotation>());
        try {
            super.visitCode(obj);
            if (methodToCalledMethods.get(m).isEmpty()) {
                methodToCalledMethods.remove(getMethod());
            }
        } catch (StopOpcodeParsingException e) {
            methodToCalledMethods.remove(getMethod());
        }
    }
}
Also used : SourceLineAnnotation(edu.umd.cs.findbugs.SourceLineAnnotation) StopOpcodeParsingException(com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException) Method(org.apache.bcel.classfile.Method)

Example 25 with Method

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

the class PartiallyConstructedObjectAccess method foundPrivateInChain.

@Nullable
private Deque<SourceLineAnnotation> foundPrivateInChain(Method m, Set<Method> checkedMethods) {
    Map<Method, SourceLineAnnotation> calledMethods = methodToCalledMethods.get(m);
    if (calledMethods != null) {
        for (Map.Entry<Method, SourceLineAnnotation> entry : calledMethods.entrySet()) {
            Method cm = entry.getKey();
            if (checkedMethods.contains(cm)) {
                continue;
            }
            if (!cm.isPrivate() && !cm.isFinal()) {
                Deque<SourceLineAnnotation> slas = new ArrayDeque<>();
                slas.addLast(entry.getValue());
                return slas;
            }
            checkedMethods.add(cm);
            Deque<SourceLineAnnotation> slas = foundPrivateInChain(cm, checkedMethods);
            if (slas != null) {
                slas.addFirst(entry.getValue());
                return slas;
            }
        }
    }
    return null;
}
Also used : SourceLineAnnotation(edu.umd.cs.findbugs.SourceLineAnnotation) Method(org.apache.bcel.classfile.Method) HashMap(java.util.HashMap) Map(java.util.Map) ArrayDeque(java.util.ArrayDeque) Nullable(javax.annotation.Nullable)

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