Java Code Examples for org.apache.bcel.Const#ACC_ABSTRACT

The following examples show how to use org.apache.bcel.Const#ACC_ABSTRACT . 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: InvalidJUnitTest.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void visitClassContext(ClassContext classContext) {
    if (!enabled()) {
        return;
    }

    JavaClass jClass = classContext.getJavaClass();
    XClass xClass = classContext.getXClass();

    try {

        if (!isJunit3TestCase(xClass)) {
            return;
        }
        if ((jClass.getAccessFlags() & Const.ACC_ABSTRACT) == 0) {
            if (!hasTestMethods(jClass)) {
                bugReporter.reportBug(new BugInstance(this, "IJU_NO_TESTS", LOW_PRIORITY).addClass(jClass));
            }
        }
        directChildOfTestCase = "junit.framework.TestCase".equals(jClass.getSuperclassName());
        jClass.accept(this);
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    }

}
 
Example 2
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 3
Source File: ClassParser.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
/**
 * Reads information about the class and its super class.
 * @throws  IOException
 * @throws  ClassFormatException
 */
private void readClassInfo() throws IOException, ClassFormatException {
    accessFlags = dataInputStream.readUnsignedShort();
    /* Interfaces are implicitely abstract, the flag should be set
     * according to the JVM specification.
     */
    if ((accessFlags & Const.ACC_INTERFACE) != 0) {
        accessFlags |= Const.ACC_ABSTRACT;
    }
    if (((accessFlags & Const.ACC_ABSTRACT) != 0)
            && ((accessFlags & Const.ACC_FINAL) != 0)) {
        throw new ClassFormatException("Class " + fileName + " can't be both final and abstract");
    }
    classNameIndex = dataInputStream.readUnsignedShort();
    superclassNameIndex = dataInputStream.readUnsignedShort();
}
 
Example 4
Source File: UselessSubclassMethod.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void visitMethod(Method obj) {
    if ((interfaceMethods != null) && ((obj.getAccessFlags() & Const.ACC_ABSTRACT) != 0)) {
        String curDetail = obj.getName() + obj.getSignature();
        for (String infMethodDetail : interfaceMethods) {
            if (curDetail.equals(infMethodDetail)) {
                bugReporter.reportBug(new BugInstance(this, "USM_USELESS_ABSTRACT_METHOD", LOW_PRIORITY).addClassAndMethod(
                        getClassContext().getJavaClass(), obj));
            }
        }
    }
    super.visitMethod(obj);
}
 
Example 5
Source File: ClassDumper.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * Processes information about the class and its super class.
 * @throws  IOException
 * @throws  ClassFormatException
 */
private final void processClassInfo () throws IOException, ClassFormatException {
    access_flags = file.readUnsignedShort();
    /* Interfaces are implicitely abstract, the flag should be set
     * according to the JVM specification.
     */
    if ((access_flags & Const.ACC_INTERFACE) != 0) {
        access_flags |= Const.ACC_ABSTRACT;
    }
    if (((access_flags & Const.ACC_ABSTRACT) != 0)
            && ((access_flags & Const.ACC_FINAL) != 0)) {
        throw new ClassFormatException("Class " + file_name +
                " can't be both final and abstract");
    }

    System.out.printf("%nClass info:%n");
    System.out.println("  flags: " + BCELifier.printFlags(access_flags,
            BCELifier.FLAGS.CLASS));
    class_name_index = file.readUnsignedShort();
    System.out.printf("  this_class: %d (", class_name_index);
    System.out.println(constantToString(class_name_index) + ")");

    superclass_name_index = file.readUnsignedShort();
    System.out.printf("  super_class: %d (", superclass_name_index);
    if (superclass_name_index > 0) {
        System.out.printf("%s", constantToString(superclass_name_index));
    }
    System.out.println(")");
}
 
Example 6
Source File: Pass2Verifier.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
@Override
public void visitInnerClasses(final InnerClasses obj) {//vmspec2 4.7.5

    // exactly one InnerClasses attr per ClassFile if some inner class is refernced: see visitJavaClass()

    checkIndex(obj, obj.getNameIndex(), CONST_Utf8);

    final String name = ((ConstantUtf8) cp.getConstant(obj.getNameIndex())).getBytes();
    if (! name.equals("InnerClasses")) {
        throw new ClassConstraintException(
            "The InnerClasses attribute '"+tostring(obj)+"' is not correctly named 'InnerClasses' but '"+name+"'.");
    }

    final InnerClass[] ics = obj.getInnerClasses();

    for (final InnerClass ic : ics) {
        checkIndex(obj, ic.getInnerClassIndex(), CONST_Class);
        final int outer_idx = ic.getOuterClassIndex();
        if (outer_idx != 0) {
            checkIndex(obj, outer_idx, CONST_Class);
        }
        final int innername_idx = ic.getInnerNameIndex();
        if (innername_idx != 0) {
            checkIndex(obj, innername_idx, CONST_Utf8);
        }
        int acc = ic.getInnerAccessFlags();
        acc = acc & (~ (Const.ACC_PUBLIC | Const.ACC_PRIVATE | Const.ACC_PROTECTED |
                        Const.ACC_STATIC | Const.ACC_FINAL | Const.ACC_INTERFACE | Const.ACC_ABSTRACT));
        if (acc != 0) {
            addMessage(
                "Unknown access flag for inner class '"+tostring(ic)+"' set (InnerClasses attribute '"+tostring(obj)+"').");
        }
    }
    // Semantical consistency is not yet checked by Sun, see vmspec2 4.7.5.
    // [marked TODO in JustIce]
}
 
Example 7
Source File: Hierarchy.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
public static boolean accessFlagsAreConcrete(int accessFlags) {
    return (accessFlags & Const.ACC_ABSTRACT) == 0 && (accessFlags & Const.ACC_NATIVE) == 0;
}
 
Example 8
Source File: Hierarchy.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Resolve possible instance method call targets.
 *
 * @param receiverType
 *            type of the receiver object
 * @param invokeInstruction
 *            the InvokeInstruction
 * @param cpg
 *            the ConstantPoolGen
 * @param receiverTypeIsExact
 *            if true, the receiver type is known exactly, which should
 *            allow a precise result
 * @return Set of methods which might be called
 * @throws ClassNotFoundException
 */
public static Set<JavaClassAndMethod> resolveMethodCallTargets(ReferenceType receiverType,
        InvokeInstruction invokeInstruction, ConstantPoolGen cpg, boolean receiverTypeIsExact) throws ClassNotFoundException {
    HashSet<JavaClassAndMethod> result = new HashSet<>();

    if (invokeInstruction.getOpcode() == Const.INVOKESTATIC) {
        throw new IllegalArgumentException();
    }

    String methodName = invokeInstruction.getName(cpg);
    String methodSig = invokeInstruction.getSignature(cpg);

    // Array method calls aren't virtual.
    // They should just resolve to Object methods.
    if (receiverType instanceof ArrayType) {
        JavaClass javaLangObject = AnalysisContext.currentAnalysisContext().lookupClass(Values.DOTTED_JAVA_LANG_OBJECT);
        JavaClassAndMethod classAndMethod = findMethod(javaLangObject, methodName, methodSig, INSTANCE_METHOD);
        if (classAndMethod != null) {
            result.add(classAndMethod);
        }
        return result;
    }

    if (receiverType instanceof NullType) {
        return Collections.<JavaClassAndMethod>emptySet();
    }
    AnalysisContext analysisContext = AnalysisContext.currentAnalysisContext();

    // Get the receiver class.
    String receiverClassName = ((ObjectType) receiverType).getClassName();
    JavaClass receiverClass = analysisContext.lookupClass(receiverClassName);
    ClassDescriptor receiverDesc = DescriptorFactory.createClassDescriptorFromDottedClassName(receiverClassName);

    // Figure out the upper bound for the method.
    // This is what will be called if this is not a virtual call site.
    JavaClassAndMethod upperBound = findMethod(receiverClass, methodName, methodSig, CONCRETE_METHOD);
    if (upperBound == null) {
        upperBound = findInvocationLeastUpperBound(receiverClass, methodName, methodSig, CONCRETE_METHOD, false);
    }
    if (upperBound != null) {
        if (DEBUG_METHOD_LOOKUP) {
            System.out.println("Adding upper bound: "
                    + SignatureConverter.convertMethodSignature(upperBound.getJavaClass(), upperBound.getMethod()));
        }
        result.add(upperBound);
    }

    // Is this a virtual call site?
    boolean virtualCall = (invokeInstruction.getOpcode() == Const.INVOKEVIRTUAL || invokeInstruction.getOpcode() == Const.INVOKEINTERFACE)
            && (upperBound == null || !upperBound.getJavaClass().isFinal() && !upperBound.getMethod().isFinal())
            && !receiverTypeIsExact;

    if (virtualCall) {
        if (!Values.DOTTED_JAVA_LANG_OBJECT.equals(receiverClassName)) {

            // This is a true virtual call: assume that any concrete
            // subtype method may be called.
            Set<ClassDescriptor> subTypeSet = analysisContext.getSubtypes2().getSubtypes(receiverDesc);
            for (ClassDescriptor subtype : subTypeSet) {
                XMethod concreteSubtypeMethod = findMethod(subtype, methodName, methodSig, false);
                if (concreteSubtypeMethod != null && (concreteSubtypeMethod.getAccessFlags() & Const.ACC_ABSTRACT) == 0) {
                    result.add(new JavaClassAndMethod(concreteSubtypeMethod));
                }
            }
            if (false && subTypeSet.size() > 500) {
                new RuntimeException(receiverClassName + " has " + subTypeSet.size() + " subclasses, " + result.size()
                        + " of which implement " + methodName + methodSig + " " + invokeInstruction)
                                .printStackTrace(System.out);
            }

        }
    }
    return result;
}
 
Example 9
Source File: MethodGen.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
/**
 * Instantiate from existing method.
 *
 * @param method method
 * @param className class name containing this method
 * @param cp constant pool
 */
public MethodGen(final Method method, final String className, final ConstantPoolGen cp) {
    this(method.getAccessFlags(), Type.getReturnType(method.getSignature()),
        Type.getArgumentTypes(method.getSignature()), null /* may be overridden anyway */
        , method.getName(), className,
        ((method.getAccessFlags() & (Const.ACC_ABSTRACT | Const.ACC_NATIVE)) == 0)
            ? new InstructionList(getByteCodes(method))
            : null,
        cp);
    final Attribute[] attributes = method.getAttributes();
    for (final Attribute attribute : attributes) {
        Attribute a = attribute;
        if (a instanceof Code) {
            final Code c = (Code) a;
            setMaxStack(c.getMaxStack());
            setMaxLocals(c.getMaxLocals());
            final CodeException[] ces = c.getExceptionTable();
            if (ces != null) {
                for (final CodeException ce : ces) {
                    final int type = ce.getCatchType();
                    ObjectType c_type = null;
                    if (type > 0) {
                        final String cen = method.getConstantPool().getConstantString(type, Const.CONSTANT_Class);
                        c_type = ObjectType.getInstance(cen);
                    }
                    final int end_pc = ce.getEndPC();
                    final int length = getByteCodes(method).length;
                    InstructionHandle end;
                    if (length == end_pc) { // May happen, because end_pc is exclusive
                        end = il.getEnd();
                    } else {
                        end = il.findHandle(end_pc);
                        end = end.getPrev(); // Make it inclusive
                    }
                    addExceptionHandler(il.findHandle(ce.getStartPC()), end, il.findHandle(ce.getHandlerPC()),
                        c_type);
                }
            }
            final Attribute[] c_attributes = c.getAttributes();
            for (final Attribute c_attribute : c_attributes) {
                a = c_attribute;
                if (a instanceof LineNumberTable) {
                    final LineNumber[] ln = ((LineNumberTable) a).getLineNumberTable();
                    for (final LineNumber l : ln) {
                        final InstructionHandle ih = il.findHandle(l.getStartPC());
                        if (ih != null) {
                            addLineNumber(ih, l.getLineNumber());
                        }
                    }
                } else if (a instanceof LocalVariableTable) {
                    updateLocalVariableTable((LocalVariableTable) a);
                } else if (a instanceof LocalVariableTypeTable) {
                    this.localVariableTypeTable = (LocalVariableTypeTable) a.copy(cp.getConstantPool());
                } else {
                    addCodeAttribute(a);
                }
            }
        } else if (a instanceof ExceptionTable) {
            final String[] names = ((ExceptionTable) a).getExceptionNames();
            for (final String name2 : names) {
                addException(name2);
            }
        } else if (a instanceof Annotations) {
            final Annotations runtimeAnnotations = (Annotations) a;
            final AnnotationEntry[] aes = runtimeAnnotations.getAnnotationEntries();
            for (final AnnotationEntry element : aes) {
                addAnnotationEntry(new AnnotationEntryGen(element, cp, false));
            }
        } else {
            addAttribute(a);
        }
    }
}
 
Example 10
Source File: AccessFlags.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
public final boolean isAbstract() {
    return (access_flags & Const.ACC_ABSTRACT) != 0;
}
 
Example 11
Source File: Hierarchy.java    From spotbugs with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Return whether or not the given method is concrete.
 *
 * @param xmethod
 *            the method
 * @return true if the method is concrete, false otherwise
 */
@Deprecated
public static boolean isConcrete(XMethod xmethod) {
    int accessFlags = xmethod.getAccessFlags();
    return (accessFlags & Const.ACC_ABSTRACT) == 0 && (accessFlags & Const.ACC_NATIVE) == 0;
}