Search in sources :

Example 86 with JavaClass

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

the class UnusedMethodCheck method leaveSet.

/**
 * @see com.puppycrawl.tools.checkstyle.bcel.IObjectSetVisitor
 */
public void leaveSet(Set aJavaClasses) {
    final Iterator it = aJavaClasses.iterator();
    while (it.hasNext()) {
        final JavaClass javaClass = (JavaClass) it.next();
        final String className = javaClass.getClassName();
        final JavaClassDefinition classDef = findJavaClassDef(javaClass);
        final MethodDefinition[] methodDefs = classDef.getMethodDefs();
        for (int i = 0; i < methodDefs.length; i++) {
            if (!classDef.hasReference(methodDefs[i], getReferenceDAO())) {
                final Method method = methodDefs[i].getMethod();
                if (!ignore(className, method)) {
                    log(javaClass, 0, "unused.method", new Object[] { methodDefs[i] });
                }
            }
        }
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) MethodDefinition(com.puppycrawl.tools.checkstyle.bcel.classfile.MethodDefinition) Iterator(java.util.Iterator) JavaClassDefinition(com.puppycrawl.tools.checkstyle.bcel.classfile.JavaClassDefinition) Method(org.apache.bcel.classfile.Method)

Example 87 with JavaClass

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

the class JavaClassDefinition method hasReference.

/**
 * Determines whether there is reference to a given Method in this JavaClass
 * definition or a definition in a superclass.
 * @param aMethodDef the Method to check.
 * @param aReferenceDAO reference DAO.
 * @return true if there is a reference to the method of aMethodDef in
 * this JavaClass or a superclass.
 */
public boolean hasReference(MethodDefinition aMethodDef, ReferenceDAO aReferenceDAO) {
    final String methodName = aMethodDef.getName();
    final Type[] argTypes = aMethodDef.getArgumentTypes();
    // search the inheritance hierarchy
    JavaClass currentJavaClass = getJavaClass();
    while (currentJavaClass != null) {
        final JavaClassDefinition javaClassDef = aReferenceDAO.findJavaClassDef(currentJavaClass);
        if (javaClassDef != null) {
            final MethodDefinition methodDef = javaClassDef.findNarrowestMethod(getJavaClass().getClassName(), methodName, argTypes);
            if ((methodDef != null) && (methodDef.hasReference(getJavaClass()))) {
                return true;
            }
        }
        currentJavaClass = currentJavaClass.getSuperClass();
    }
    return false;
}
Also used : Type(org.apache.bcel.generic.Type) JavaClass(org.apache.bcel.classfile.JavaClass)

Example 88 with JavaClass

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

the class ReferenceDAO method findFieldDef.

/**
 * Finds the definition of the field of a field reference.
 * @param aFieldRef the reference to a field.
 * @return the definition of the field for aFieldRef.
 */
public FieldDefinition findFieldDef(FieldReference aFieldRef) {
    final String className = aFieldRef.getClassName();
    JavaClass javaClass = Repository.lookupClass(className);
    final String fieldName = aFieldRef.getName();
    // search up the class hierarchy for the class containing the
    // method definition.
    FieldDefinition result = null;
    while ((javaClass != null) && (result == null)) {
        final JavaClassDefinition javaClassDef = (JavaClassDefinition) mJavaClasses.get(javaClass);
        if (javaClassDef != null) {
            result = javaClassDef.findFieldDef(fieldName);
        }
        // search the parent
        javaClass = javaClass.getSuperClass();
    }
    return result;
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass)

Example 89 with JavaClass

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

the class ReferenceDAO method addFieldReference.

/**
 * Adds a reference to a field.
 * @param aFieldRef the field reference.
 */
public void addFieldReference(FieldReference aFieldRef) {
    final String className = aFieldRef.getClassName();
    JavaClass javaClass = Repository.lookupClass(className);
    final String fieldName = aFieldRef.getName();
    // search up the class hierarchy for the class containing the
    // method definition.
    FieldDefinition fieldDef = null;
    while ((javaClass != null) && (fieldDef == null)) {
        final JavaClassDefinition javaClassDef = (JavaClassDefinition) mJavaClasses.get(javaClass);
        if (javaClassDef != null) {
            fieldDef = javaClassDef.findFieldDef(fieldName);
            if (fieldDef != null) {
                fieldDef.addReference(aFieldRef);
            }
        }
        // search the parent
        javaClass = javaClass.getSuperClass();
    }
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass)

Example 90 with JavaClass

use of org.apache.bcel.classfile.JavaClass in project android-classyshark by google.

the class RootBuilder method fillFromJar.

private ClassNode fillFromJar(File file) {
    ClassNode rootNode = new ClassNode(file.getName());
    try {
        JarFile theJar = new JarFile(file);
        Enumeration<? extends JarEntry> en = theJar.entries();
        while (en.hasMoreElements()) {
            JarEntry entry = en.nextElement();
            if (entry.getName().endsWith(".class")) {
                ClassParser cp = new ClassParser(theJar.getInputStream(entry), entry.getName());
                JavaClass jc = cp.parse();
                ClassInfo classInfo = new ClassInfo(jc.getClassName(), jc.getMethods().length);
                rootNode.add(classInfo);
            }
        }
    } catch (IOException e) {
        System.err.println("Error reading file: " + file + ". " + e.getMessage());
        e.printStackTrace(System.err);
    }
    return rootNode;
}
Also used : JavaClass(org.apache.bcel.classfile.JavaClass) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) JarEntry(java.util.jar.JarEntry) ClassParser(org.apache.bcel.classfile.ClassParser)

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