Java Code Examples for org.apache.bcel.classfile.JavaClass#getSuperClass()

The following examples show how to use org.apache.bcel.classfile.JavaClass#getSuperClass() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ReferenceDAO.java    From contribution with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * 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;
}
 
Example 2
Source File: InvalidJUnitTest.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
private boolean hasTestMethods(JavaClass jClass) {
    boolean foundTest = false;
    Method[] methods = jClass.getMethods();
    for (Method m : methods) {
        if (m.isPublic() && m.getName().startsWith("test") && m.getSignature().equals("()V")) {
            return true;
        }
        if (m.getName().startsWith("runTest") && m.getSignature().endsWith("()V")) {
            return true;
        }
    }
    if (hasSuite(methods)) {
        return true;
    }

    try {
        JavaClass sClass = jClass.getSuperClass();
        if (sClass != null) {
            return hasTestMethods(sClass);
        }
    } catch (ClassNotFoundException e) {
        AnalysisContext.reportMissingClass(e);
    }

    return false;
}
 
Example 3
Source File: BadlyOverriddenAdapter.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void visit(JavaClass obj) {
    try {
        methodMap.clear();
        badOverrideMap.clear();
        JavaClass superClass = obj.getSuperClass();
        if (superClass == null) {
            return;
        }
        String packageName = superClass.getPackageName();
        String className = superClass.getClassName();

        // A more generic way to add Adapters would be nice here
        isAdapter = ((className.endsWith("Adapter")) && ("java.awt.event".equals(packageName) || "javax.swing.event".equals(packageName)))
                || (("DefaultHandler".equals(className) && ("org.xml.sax.helpers".equals(packageName))));
        if (isAdapter) {
            Method[] methods = superClass.getMethods();
            for (Method method1 : methods) {
                methodMap.put(method1.getName(), method1.getSignature());
            }
        }
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    }
}
 
Example 4
Source File: ReferenceDAO.java    From contribution with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * 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();
    }
}
 
Example 5
Source File: Hierarchy.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static @CheckForNull JavaClassAndMethod findInvocationLeastUpperBound(JavaClass jClass, String methodName, String methodSig,
        JavaClassAndMethodChooser methodChooser, boolean invokeInterface) throws ClassNotFoundException {
    JavaClassAndMethod result = findMethod(jClass, methodName, methodSig, methodChooser);
    if (result != null) {
        return result;
    }
    if (invokeInterface) {
        for (JavaClass i : jClass.getInterfaces()) {
            result = findInvocationLeastUpperBound(i, methodName, methodSig, methodChooser, invokeInterface);
            if (result != null) {
                return null;
            }
        }
    } else {
        JavaClass sClass = jClass.getSuperClass();
        if (sClass != null) {
            return findInvocationLeastUpperBound(sClass, methodName, methodSig, methodChooser, invokeInterface);
        }
    }
    return null;

}
 
Example 6
Source File: Hierarchy.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Find a field with given name defined in given class.
 *
 * @param className
 *            the name of the class
 * @param fieldName
 *            the name of the field
 * @return the Field, or null if no such field could be found
 */
public static Field findField(String className, String fieldName) throws ClassNotFoundException {
    JavaClass jclass = Repository.lookupClass(className);

    while (jclass != null) {
        Field[] fieldList = jclass.getFields();
        for (Field field : fieldList) {
            if (field.getName().equals(fieldName)) {
                return field;
            }
        }

        jclass = jclass.getSuperClass();
    }

    return null;
}
 
Example 7
Source File: Lookup.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static @CheckForNull JavaClass findSuperDefiner(JavaClass clazz, String name, String signature, BugReporter bugReporter) {
    try {
        JavaClass c = clazz;
        while (true) {
            c = c.getSuperClass();
            if (c == null) {
                return null;
            }
            Method m = findImplementation(c, name, signature);
            if (m != null) {
                return c;
            }
        }
    } catch (ClassNotFoundException e) {
        bugReporter.reportMissingClass(e);
        return null;
    }
}
 
Example 8
Source File: Lookup.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static @CheckForNull JavaClass findSuperImplementor(JavaClass clazz, String name, String signature, BugReporter bugReporter) {
    try {
        JavaClass c = clazz;
        while (true) {
            c = c.getSuperClass();
            if (c == null) {
                return null;
            }
            Method m = findImplementation(c, name, signature);
            if (m != null && !m.isAbstract()) {
                return c;
            }

        }
    } catch (ClassNotFoundException e) {
        bugReporter.reportMissingClass(e);
        return null;
    }
}
 
Example 9
Source File: Lookup.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static @CheckForNull XMethod findSuperImplementorAsXMethod(JavaClass clazz, String name, String signature, BugReporter bugReporter) {
    try {
        JavaClass c = clazz;
        while (true) {
            c = c.getSuperClass();
            if (c == null) {
                return null;
            }
            Method m = findImplementation(c, name, signature);
            if (m != null && !m.isAbstract()) {
                return XFactory.createXMethod(c, m);
            }

        }
    } catch (ClassNotFoundException e) {
        bugReporter.reportMissingClass(e);
        return null;
    }
}
 
Example 10
Source File: ReferenceDAO.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * 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;
}
 
Example 11
Source File: ReferenceDAO.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * 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();
    }
}
 
Example 12
Source File: JavaClassDefinition.java    From contribution with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 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;
}
 
Example 13
Source File: ReferenceDAO.java    From contribution with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Adds a reference for an invocation in the invoked method definition.
 * The invocation is of the form class.method(args).
 * @param aInvokeRef the invocation reference.
 */
public void addInvokeReference(InvokeReference aInvokeRef)
{
    // find the class for the instruction
    final String className = aInvokeRef.getClassName();
    JavaClass javaClass = Repository.lookupClass(className);
    final String methodName = aInvokeRef.getName();
    final Type[] argTypes = aInvokeRef.getArgTypes();

    // search up the class hierarchy for the class containing the
    // method definition.
    MethodDefinition narrowest = null;
    while ((javaClass != null) && (narrowest == null)) {
        final JavaClassDefinition javaClassDef =
            (JavaClassDefinition) mJavaClasses.get(javaClass);
        if (javaClassDef != null) {
            // find narrowest compatible in the current class
            narrowest =
                javaClassDef.findNarrowestMethod(
                    className,
                    methodName,
                    argTypes);
            if (narrowest != null) {
                narrowest.addReference(aInvokeRef);
            }
        }
        // search the parent
        javaClass = javaClass.getSuperClass();
    }
}
 
Example 14
Source File: JavaClassDefinition.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 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;
}
 
Example 15
Source File: ReferenceDAO.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Adds a reference for an invocation in the invoked method definition.
 * The invocation is of the form class.method(args).
 * @param aInvokeRef the invocation reference.
 */
public void addInvokeReference(InvokeReference aInvokeRef)
{
    // find the class for the instruction
    final String className = aInvokeRef.getClassName();
    JavaClass javaClass = Repository.lookupClass(className);
    final String methodName = aInvokeRef.getName();
    final Type[] argTypes = aInvokeRef.getArgTypes();

    // search up the class hierarchy for the class containing the
    // method definition.
    MethodDefinition narrowest = null;
    while ((javaClass != null) && (narrowest == null)) {
        final JavaClassDefinition javaClassDef =
            (JavaClassDefinition) mJavaClasses.get(javaClass);
        if (javaClassDef != null) {
            // find narrowest compatible in the current class
            narrowest =
                javaClassDef.findNarrowestMethod(
                    className,
                    methodName,
                    argTypes);
            if (narrowest != null) {
                narrowest.addReference(aInvokeRef);
            }
        }
        // search the parent
        javaClass = javaClass.getSuperClass();
    }
}
 
Example 16
Source File: UncallableMethodOfAnonymousClass.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
boolean definedInSuperClassOrInterface(JavaClass clazz, String method) throws ClassNotFoundException {
    if (clazz == null) {
        return false;
    }
    JavaClass superClass = clazz.getSuperClass();
    if (superClass == null) {
        return false;
    }
    try {
        XClass xClass = Global.getAnalysisCache().getClassAnalysis(XClass.class,
                DescriptorFactory.createClassDescriptorFromDottedClassName(superClass.getClassName()));
        if (xClass.hasStubs()) {
            return true;
        }
    } catch (CheckedAnalysisException e) {
        return true;
    }

    if (definedInThisClassOrSuper(superClass, method)) {
        return true;
    }
    for (JavaClass i : clazz.getInterfaces()) {
        if (definedInThisClassOrSuper(i, method)) {
            return true;
        }
    }
    return false;
}
 
Example 17
Source File: BetaDetector.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
/** Returns the superclass of the specified class. */
private JavaClass getSuperclass(JavaClass javaClass) {
  try {
    return javaClass.getSuperClass();
  } catch (ClassNotFoundException e) {
    bugReporter.reportMissingClass(e);
    return null;
  }
}
 
Example 18
Source File: RedundantInterfaces.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void visitClassContext(ClassContext classContext) {
    JavaClass obj = classContext.getJavaClass();

    String superClassName = obj.getSuperclassName();
    if (Values.DOTTED_JAVA_LANG_OBJECT.equals(superClassName)) {
        return;
    }

    String[] interfaceNames = obj.getInterfaceNames();
    if ((interfaceNames == null) || (interfaceNames.length == 0)) {
        return;
    }

    try {
        JavaClass superObj = obj.getSuperClass();
        SortedSet<String> redundantInfNames = new TreeSet<>();

        for (String interfaceName : interfaceNames) {
            if (!"java.io.Serializable".equals(interfaceName)) {
                JavaClass inf = Repository.lookupClass(interfaceName);
                if (superObj.instanceOf(inf)) {
                    redundantInfNames.add(inf.getClassName());
                }
            }
        }

        if (redundantInfNames.size() > 0) {
            BugInstance bug = new BugInstance(this, "RI_REDUNDANT_INTERFACES", LOW_PRIORITY).addClass(obj);
            for (String redundantInfName : redundantInfNames) {
                bug.addClass(redundantInfName).describe("INTERFACE_TYPE");
            }

            bugReporter.reportBug(bug);
        }

    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    }
}
 
Example 19
Source File: PrintClass.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static void printClass(ClassParser parser) throws IOException {
    JavaClass java_class;
    java_class = parser.parse();

    if (superClasses) {
        try {
            while (java_class != null) {
                System.out.print(java_class.getClassName() + "  ");
                java_class = java_class.getSuperClass();
            }
        } catch (ClassNotFoundException e) {
            System.out.println(e.getMessage());

        }
        System.out.println();
        return;
    }
    if (constants || code) {
        System.out.println(java_class); // Dump the contents
    }
    if (constants) {
        System.out.println(java_class.getConstantPool());
    }

    if (code) {
        printCode(java_class.getMethods());
    }
}
 
Example 20
Source File: LinkageChecker.java    From cloud-opensource-java with Apache License 2.0 4 votes vote down vote up
private ImmutableList<SymbolProblem> findAbstractParentProblems(
    ClassFile classFile, SuperClassSymbol superClassSymbol) {
  ImmutableList.Builder<SymbolProblem> builder = ImmutableList.builder();
  String superClassName = superClassSymbol.getClassBinaryName();
  if (classDumper.isSystemClass(superClassName)) {
    return ImmutableList.of();
  }

  try {
    String className = classFile.getBinaryName();
    JavaClass implementingClass = classDumper.loadJavaClass(className);
    if (implementingClass.isAbstract()) {
      return ImmutableList.of();
    }

    JavaClass superClass = classDumper.loadJavaClass(superClassName);
    if (!superClass.isAbstract()) {
      return ImmutableList.of();
    }

    JavaClass abstractClass = superClass;

    // Equality of BCEL's Method class is on its name and descriptor field
    Set<Method> implementedMethods = new HashSet<>();
    implementedMethods.addAll(ImmutableList.copyOf(implementingClass.getMethods()));

    while (abstractClass.isAbstract()) {
      for (Method abstractMethod : abstractClass.getMethods()) {
        if (!abstractMethod.isAbstract()) {
          // This abstract method has implementation. Subclass does not have to implement it.
          implementedMethods.add(abstractMethod);
        } else if (!implementedMethods.contains(abstractMethod)) {
          String unimplementedMethodName = abstractMethod.getName();
          String unimplementedMethodDescriptor = abstractMethod.getSignature();

          MethodSymbol missingMethodOnClass =
              new MethodSymbol(
                  className, unimplementedMethodName, unimplementedMethodDescriptor, false);
          builder.add(
              new SymbolProblem(missingMethodOnClass, ErrorType.ABSTRACT_METHOD, classFile));
        }
      }
      abstractClass = abstractClass.getSuperClass();
    }
  } catch (ClassNotFoundException ex) {
    // Missing classes are reported by findSymbolProblem method.
  }
  return builder.build();
}