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

The following examples show how to use org.apache.bcel.classfile.JavaClass#isClass() . 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: UselessSubclassMethod.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void visitClassContext(ClassContext classContext) {
    try {
        JavaClass cls = classContext.getJavaClass();
        superclassName = cls.getSuperclassName();
        JavaClass[] interfaces = null;
        if (cls.isClass() && ((cls.getAccessFlags() & Const.ACC_ABSTRACT) != 0)) {
            interfaces = cls.getAllInterfaces();
            interfaceMethods = new HashSet<>();
            for (JavaClass aInterface : interfaces) {
                Method[] infMethods = aInterface.getMethods();
                for (Method meth : infMethods) {
                    interfaceMethods.add(meth.getName() + meth.getSignature());
                }
            }
        }
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    }
    super.visitClassContext(classContext);
}
 
Example 2
Source File: Pass3aVerifier.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
/** Checks if the constraints of operands of the said instruction(s) are satisfied. */
@Override
public void visitINVOKEINTERFACE(final INVOKEINTERFACE o) {
    try {
    // INVOKEINTERFACE is a LoadClass; the Class where the referenced method is declared in,
    // is therefore resolved/verified.
    // INVOKEINTERFACE is an InvokeInstruction, the argument and return types are resolved/verified,
    // too. So are the allowed method names.
    final String classname = o.getClassName(constantPoolGen);
    final JavaClass jc = Repository.lookupClass(classname);
    final Method m = getMethodRecursive(jc, o);
    if (m == null) {
        constraintViolated(o, "Referenced method '"+o.getMethodName(constantPoolGen)+"' with expected signature '"+o.getSignature(constantPoolGen)+
            "' not found in class '"+jc.getClassName()+"'.");
    }
    if (jc.isClass()) {
        constraintViolated(o, "Referenced class '"+jc.getClassName()+"' is a class, but not an interface as expected.");
    }
    } catch (final ClassNotFoundException e) {
    // FIXME: maybe not the best way to handle this
    throw new AssertionViolatedException("Missing class: " + e, e);
    }
}
 
Example 3
Source File: Pass3aVerifier.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
/** Checks if the constraints of operands of the said instruction(s) are satisfied. */
@Override
public void visitINVOKEVIRTUAL(final INVOKEVIRTUAL o) {
    try {
    // INVOKEVIRTUAL is a LoadClass; the Class where the referenced method is declared in,
    // is therefore resolved/verified.
    // INVOKEVIRTUAL is an InvokeInstruction, the argument and return types are resolved/verified,
    // too. So are the allowed method names.
    final String classname = o.getClassName(constantPoolGen);
    final JavaClass jc = Repository.lookupClass(classname);
    final Method m = getMethodRecursive(jc, o);
    if (m == null) {
        constraintViolated(o, "Referenced method '"+o.getMethodName(constantPoolGen)+"' with expected signature '"+
            o.getSignature(constantPoolGen)+"' not found in class '"+jc.getClassName()+"'.");
    }
    if (! (jc.isClass())) {
        constraintViolated(o, "Referenced class '"+jc.getClassName()+"' is an interface, but not a class as expected.");
    }

    } catch (final ClassNotFoundException e) {
    // FIXME: maybe not the best way to handle this
    throw new AssertionViolatedException("Missing class: " + e, e);
    }
}
 
Example 4
Source File: ObjectType.java    From ApkToolPlus with Apache License 2.0 5 votes vote down vote up
/**
 * If "this" doesn't reference a class, it references an interface
 * or a non-existant entity.
 */
public boolean referencesClass(){
  JavaClass jc = Repository.lookupClass(class_name);
  if (jc == null)
    return false;
  else
    return jc.isClass();
}
 
Example 5
Source File: ObjectType.java    From ApkToolPlus with Apache License 2.0 5 votes vote down vote up
/**
 * If "this" doesn't reference an interface, it references a class
 * or a non-existant entity.
 */
public boolean referencesInterface(){
  JavaClass jc = Repository.lookupClass(class_name);
  if (jc == null)
    return false;
  else
    return !jc.isClass();
}
 
Example 6
Source File: MultithreadedInstanceAccess.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void visitClassContext(ClassContext classContext) {
    try {
        JavaClass cls = classContext.getJavaClass();
        String superClsName = cls.getSuperclassName();
        if (Values.DOTTED_JAVA_LANG_OBJECT.equals(superClsName)) {
            return;
        }

        if (STRUTS_ACTION_NAME.equals(superClsName)) {
            mtClassName = STRUTS_ACTION_NAME;
            super.visitClassContext(classContext);
        } else if (SERVLET_NAME.equals(superClsName)) {
            mtClassName = SERVLET_NAME;
            super.visitClassContext(classContext);
        } else {
            for (JavaClass mtClass : getMtClasses()) {
                /*
                 * note: We could just call cls.instanceOf(mtClass) and it
                 * would work for both classes and interfaces, but if
                 * mtClass is an interface it is more efficient to call
                 * cls.implementationOf() and since we're doing this on each
                 * visit that's what we'll do. also note:
                 * implementationOf(mtClass) throws an
                 * IllegalArgumentException when mtClass is not an
                 * interface. See bug#1428253.
                 */
                if (mtClass.isClass() ? cls.instanceOf(mtClass) : cls.implementationOf(mtClass)) {
                    mtClassName = mtClass.getClassName();
                    super.visitClassContext(classContext);
                    return;
                }
            }
        }
    } catch (Exception e) {
        // already reported
    }
}
 
Example 7
Source File: ObjectType.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * If "this" doesn't reference a class, it references an interface
 * or a non-existant entity.
 * @deprecated (since 6.0) this method returns an inaccurate result
 *   if the class or interface referenced cannot
 *   be found: use referencesClassExact() instead
 */
@Deprecated
public boolean referencesClass() {
    try {
        final JavaClass jc = Repository.lookupClass(className);
        return jc.isClass();
    } catch (final ClassNotFoundException e) {
        return false;
    }
}
 
Example 8
Source File: ObjectType.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * If "this" doesn't reference an interface, it references a class
 * or a non-existant entity.
 * @deprecated (since 6.0) this method returns an inaccurate result
 *   if the class or interface referenced cannot
 *   be found: use referencesInterfaceExact() instead
 */
@Deprecated
public boolean referencesInterface() {
    try {
        final JavaClass jc = Repository.lookupClass(className);
        return !jc.isClass();
    } catch (final ClassNotFoundException e) {
        return false;
    }
}
 
Example 9
Source File: Pass3aVerifier.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
/** Checks if the constraints of operands of the said instruction(s) are satisfied. */
@Override
public void visitPUTSTATIC(final PUTSTATIC o) {
    try {
    final String field_name = o.getFieldName(constantPoolGen);
    final JavaClass jc = Repository.lookupClass(getObjectType(o).getClassName());
    final Field[] fields = jc.getFields();
    Field f = null;
    for (final Field field : fields) {
        if (field.getName().equals(field_name)) {
            f = field;
            break;
        }
    }
    if (f == null) {
        throw new AssertionViolatedException("Field '" + field_name + "' not found in " + jc.getClassName());
    }

    if (f.isFinal()) {
        if (!(myOwner.getClassName().equals(getObjectType(o).getClassName()))) {
            constraintViolated(o,
                "Referenced field '"+f+"' is final and must therefore be declared in the current class '"+
                    myOwner.getClassName()+"' which is not the case: it is declared in '"+o.getReferenceType(constantPoolGen)+"'.");
        }
    }

    if (! (f.isStatic())) {
        constraintViolated(o, "Referenced field '"+f+"' is not static which it should be.");
    }

    final String meth_name = Repository.lookupClass(myOwner.getClassName()).getMethods()[methodNo].getName();

    // If it's an interface, it can be set only in <clinit>.
    if ((!(jc.isClass())) && (!(meth_name.equals(Const.STATIC_INITIALIZER_NAME)))) {
        constraintViolated(o, "Interface field '"+f+"' must be set in a '"+Const.STATIC_INITIALIZER_NAME+"' method.");
    }
    } catch (final ClassNotFoundException e) {
    // FIXME: maybe not the best way to handle this
    throw new AssertionViolatedException("Missing class: " + e, e);
    }
}
 
Example 10
Source File: ObjectType.java    From commons-bcel with Apache License 2.0 2 votes vote down vote up
/**
 * Return true if this type references a class,
 * false if it references an interface.
 * @return true if the type references a class, false if
 *   it references an interface
 * @throws ClassNotFoundException if the class or interface
 *   referenced by this type can't be found
 */
public boolean referencesClassExact() throws ClassNotFoundException {
    final JavaClass jc = Repository.lookupClass(className);
    return jc.isClass();
}
 
Example 11
Source File: ObjectType.java    From commons-bcel with Apache License 2.0 2 votes vote down vote up
/**
 * Return true if this type references an interface,
 * false if it references a class.
 * @return true if the type references an interface, false if
 *   it references a class
 * @throws ClassNotFoundException if the class or interface
 *   referenced by this type can't be found
 */
public boolean referencesInterfaceExact() throws ClassNotFoundException {
    final JavaClass jc = Repository.lookupClass(className);
    return !jc.isClass();
}