Search in sources :

Example 1 with Method

use of org.apache.bcel.classfile.Method in project contribution by checkstyle.

the class HiddenStaticMethodCheck method visitObject.

/**
 * @see com.puppycrawl.tools.checkstyle.bcel.IObjectSetVisitor
 */
public void visitObject(Object aJavaClass) {
    final JavaClass javaClass = (JavaClass) aJavaClass;
    final String className = javaClass.getClassName();
    final JavaClass[] superClasses = javaClass.getSuperClasses();
    final Method[] methods = javaClass.getMethods();
    // Check all methods
    for (int i = 0; i < methods.length; i++) {
        final Method method = methods[i];
        // Check that the method is a possible match
        if (!method.isPrivate() && method.isStatic()) {
            // Go through all their superclasses
            for (int j = 0; j < superClasses.length; j++) {
                final JavaClass superClass = superClasses[j];
                final String superClassName = superClass.getClassName();
                final Method[] superClassMethods = superClass.getMethods();
                // Go through the methods of the superclasses
                for (int k = 0; k < superClassMethods.length; k++) {
                    final Method superClassMethod = superClassMethods[k];
                    if (superClassMethod.getName().equals(method.getName()) && !ignore(className, method)) {
                        Type[] methodTypes = method.getArgumentTypes();
                        Type[] superTypes = superClassMethod.getArgumentTypes();
                        if (methodTypes.length == superTypes.length) {
                            boolean match = true;
                            for (int arg = 0; arg < methodTypes.length; arg++) {
                                if (!methodTypes[arg].equals(superTypes[arg])) {
                                    match = false;
                                }
                            }
                            // Same method parameters
                            if (match) {
                                log(javaClass, 0, "hidden.static.method", new Object[] { method, superClassName });
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : Type(org.apache.bcel.generic.Type) JavaClass(org.apache.bcel.classfile.JavaClass) Method(org.apache.bcel.classfile.Method)

Example 2 with Method

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

the class DeprecatedTypesafeEnumPattern method visitClassContext.

/**
 * implements the visitor to look for classes compiled with 1.5 or better that have all constructors that are private
 *
 * @param context
 *            the currently parsed class context object
 */
@Override
public void visitClassContext(ClassContext context) {
    try {
        JavaClass cls = context.getJavaClass();
        if (!cls.isEnum() && (cls.getMajor() >= Const.MAJOR_1_5)) {
            Method[] methods = cls.getMethods();
            for (Method m : methods) {
                if (Values.CONSTRUCTOR.equals(m.getName()) && !m.isPrivate()) {
                    return;
                }
            }
            firstEnumPC = 0;
            enumCount = 0;
            enumConstNames = new HashSet<String>(10);
            super.visitClassContext(context);
        }
    } finally {
        enumConstNames = null;
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) Method(org.apache.bcel.classfile.Method)

Example 3 with Method

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

the class ExceptionSoftening method getConstrainingInfo.

/**
 * finds the super class or interface that constrains the types of exceptions that can be thrown from the given method
 *
 * @param cls
 *            the currently parsed class
 * @param m
 *            the method to check
 * @return a map containing the class name to a set of exceptions that constrain this method
 *
 * @throws ClassNotFoundException
 *             if a super class or super interface can't be loaded from the repository
 */
@Nullable
private Map<String, Set<String>> getConstrainingInfo(JavaClass cls, Method m) throws ClassNotFoundException {
    String methodName = m.getName();
    String methodSig = m.getSignature();
    {
        // First look for the method in interfaces of the class
        JavaClass[] infClasses = cls.getInterfaces();
        for (JavaClass infCls : infClasses) {
            Method infMethod = findMethod(infCls, methodName, methodSig);
            if (infMethod != null) {
                return buildConstrainingInfo(infCls, infMethod);
            }
            Map<String, Set<String>> constrainingExs = getConstrainingInfo(infCls, m);
            if (constrainingExs != null) {
                return constrainingExs;
            }
        }
    }
    {
        // Next look at the superclass
        JavaClass superCls = cls.getSuperClass();
        if (superCls == null) {
            return null;
        }
        Method superMethod = findMethod(superCls, methodName, methodSig);
        if (superMethod != null) {
            return buildConstrainingInfo(superCls, superMethod);
        }
        // Otherwise recursively call this on the super class
        return getConstrainingInfo(superCls, m);
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) ToString(com.mebigfatguy.fbcontrib.utils.ToString) Method(org.apache.bcel.classfile.Method) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Nullable(javax.annotation.Nullable)

Example 4 with Method

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

the class ExceptionSoftening method visitCode.

/**
 * overrides the visitor to look for methods that catch checked exceptions and rethrow runtime exceptions
 *
 * @param obj
 *            the context object of the currently parsed code block
 */
@Override
public void visitCode(Code obj) {
    try {
        Method method = getMethod();
        if (method.isSynthetic()) {
            return;
        }
        isBooleanMethod = Type.BOOLEAN.equals(method.getReturnType());
        if (isBooleanMethod || prescreen(method)) {
            catchHandlerPCs = collectExceptions(obj.getExceptionTable());
            if (!catchHandlerPCs.isEmpty()) {
                stack.resetForMethodEntry(this);
                catchInfos = new ArrayList<>();
                lvt = method.getLocalVariableTable();
                constrainingInfo = null;
                hasValidFalseReturn = false;
                catchFalseReturnPC = -1;
                super.visitCode(obj);
                if (!hasValidFalseReturn && (catchFalseReturnPC >= 0) && !method.getName().startsWith("is")) {
                    bugReporter.reportBug(new BugInstance(this, BugType.EXS_EXCEPTION_SOFTENING_RETURN_FALSE.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this, catchFalseReturnPC));
                }
            }
        }
    } finally {
        catchInfos = null;
        catchHandlerPCs = null;
        lvt = null;
        constrainingInfo = null;
    }
}
Also used : BugInstance(edu.umd.cs.findbugs.BugInstance) Method(org.apache.bcel.classfile.Method)

Example 5 with Method

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

the class PossibleIncompleteSerialization method hasSerializingMethods.

/**
 * looks to see if this class implements method described by Serializable or Externalizable
 *
 * @param cls
 *            the class to examine for serializing methods
 * @return whether the class handles it's own serializing/externalizing
 */
private static boolean hasSerializingMethods(JavaClass cls) {
    Method[] methods = cls.getMethods();
    for (Method m : methods) {
        if (!m.isStatic()) {
            String methodName = m.getName();
            String methodSig = m.getSignature();
            if ("writeObject".equals(methodName) && SIG_OBJECT_OUTPUT_STREAM_TO_VOID.equals(methodSig)) {
                return true;
            }
            if ("writeExternal".equals(methodName) && SIG_OBJECT_OUTPUT_TO_VOID.equals(methodSig)) {
                return true;
            }
        }
    }
    return false;
}
Also used : Method(org.apache.bcel.classfile.Method) ToString(com.mebigfatguy.fbcontrib.utils.ToString)

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