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

The following examples show how to use org.apache.bcel.Const#ACC_STATIC . 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: TestArrayAccess04Creator.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
private void createMethod_1() {
  final InstructionList il = new InstructionList();
  final MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, new Type[] { Type.OBJECT },
          new String[] { "arg0" }, "test", TEST_PACKAGE+".TestArrayAccess04", il, _cp);

  final InstructionHandle ih_0 = il.append(new PUSH(_cp, 1));
  Assert.assertNotNull(ih_0); // TODO why is this not used
  il.append(_factory.createNewArray(Type.OBJECT, (short) 1));
  il.append(InstructionFactory.createStore(Type.OBJECT, 1));
  final InstructionHandle ih_5 = il.append(new PUSH(_cp, 1));
  Assert.assertNotNull(ih_5); // TODO why is this not used
  il.append(InstructionFactory.createStore(Type.INT, 2));
  final InstructionHandle ih_7 = il.append(InstructionFactory.createLoad(Type.OBJECT, 1));
  Assert.assertNotNull(ih_7); // TODO why is this not used
  il.append(new PUSH(_cp, 0));
  il.append(InstructionFactory.createLoad(Type.INT, 2));
  il.append(InstructionConst.AASTORE);
  final InstructionHandle ih_11 = il.append(InstructionFactory.createReturn(Type.VOID));
  Assert.assertNotNull(ih_11); // TODO why is this not used
  method.setMaxStack();
  method.setMaxLocals();
  _cg.addMethod(method.getMethod());
  il.dispose();
}
 
Example 2
Source File: TestReturn01Creator.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
private void createMethod_1() {
  final InstructionList il = new InstructionList();
  final MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, Type.NO_ARGS,
          new String[] {  }, "foo", TEST_PACKAGE+".TestReturn01", il, _cp);

  final InstructionHandle ih_0 = il.append(_factory.createNew("java.lang.Object"));
  Assert.assertNotNull(ih_0); // TODO why is this not used
  il.append(InstructionConst.DUP);
  il.append(_factory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
  il.append(InstructionConst.NOP);
  final InstructionHandle ih_8 = il.append(InstructionFactory.createReturn(Type.OBJECT));
  Assert.assertNotNull(ih_8); // TODO why is this not used
  method.setMaxStack();
  method.setMaxLocals();
  _cg.addMethod(method.getMethod());
  il.dispose();
}
 
Example 3
Source File: FindReturnRef.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void visit(Method obj) {
    check = publicClass && (obj.getAccessFlags() & (Const.ACC_PUBLIC)) != 0;
    if (!check) {
        return;
    }
    staticMethod = (obj.getAccessFlags() & (Const.ACC_STATIC)) != 0;
    // variableNames = obj.getLocalVariableTable();
    parameterCount = getNumberMethodArguments();

    if (!staticMethod) {
        parameterCount++;
    }

    thisOnTOS = false;
    fieldOnTOS = false;
    super.visit(obj);
    thisOnTOS = false;
    fieldOnTOS = false;
}
 
Example 4
Source File: Naming.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void visit(Field obj) {
    if (getFieldName().length() == 1) {
        return;
    }

    if (isEclipseNLS) {
        int flags = obj.getAccessFlags();
        if ((flags & Const.ACC_STATIC) != 0 && ((flags & Const.ACC_PUBLIC) != 0) && "Ljava/lang/String;".equals(getFieldSig())) {
            // ignore "public statis String InstallIUCommandTooltip;"
            // messages from Eclipse NLS bundles
            return;
        }
    }
    if (badFieldName(obj)) {
        bugReporter.reportBug(new BugInstance(this, "NM_FIELD_NAMING_CONVENTION", classIsPublicOrProtected
                && (obj.isPublic() || obj.isProtected()) && !hasBadFieldNames ? NORMAL_PRIORITY : LOW_PRIORITY)
                        .addClass(this).addVisitedField(this));
    }
}
 
Example 5
Source File: UnresolvedXField.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected UnresolvedXField(FieldDescriptor m) {
    super(m.getClassDescriptor().getDottedClassName(), m.getName(), m.getSignature(), m.isStatic() ? Const.ACC_STATIC : 0);
    if (m.getSignature().charAt(0) == '(') {
        throw new IllegalArgumentException("Bad signature: " + m.getSignature());
    }
    if (XFactory.DEBUG_UNRESOLVED) {
        System.out.println("Unresolved xmethod: " + this);
    }
}
 
Example 6
Source File: TestReturn03Creator.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
private void createMethod_1() {
  final InstructionList il = new InstructionList();
  final MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.INT, Type.NO_ARGS,
          new String[] {  }, "test3", TEST_PACKAGE+".TestReturn03", il, _cp);

  final InstructionHandle ih_0 = il.append(InstructionConst.ACONST_NULL);
  Assert.assertNotNull(ih_0); // TODO why is this not used
  il.append(InstructionFactory.createReturn(Type.OBJECT));
  method.setMaxStack();
  method.setMaxLocals();
  _cg.addMethod(method.getMethod());
  il.dispose();
}
 
Example 7
Source File: GeneratingAnnotatedClassesTestCase.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
private MethodGen createMethodGen(final String methodname, final InstructionList il,
        final ConstantPoolGen cp)
{
    return new MethodGen(Const.ACC_STATIC | Const.ACC_PUBLIC, // access
            // flags
            Type.VOID, // return type
            new Type[] { new ArrayType(Type.STRING, 1) }, // argument
            // types
            new String[] { "argv" }, // arg names
            methodname, "HelloWorld", // method, class
            il, cp);
}
 
Example 8
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 9
Source File: UnresolvedXMethod.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected UnresolvedXMethod(MethodDescriptor m) {
    super(m.getClassDescriptor().getDottedClassName(), m.getName(), m.getSignature(),
            m.isStatic() ? Const.ACC_STATIC : 0);
    if (XFactory.DEBUG_UNRESOLVED) {
        System.out.println("Unresolved xmethod: " + this);
    }
}
 
Example 10
Source File: TestArrayAccess02Creator.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
private void createMethod_1() {
  final InstructionList il = new InstructionList();
  final MethodGen method = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, Type.NO_ARGS, new String[] {  },
          "test", TEST_PACKAGE+".TestArrayAccess02", il, _cp);

  final InstructionHandle ih_0 = il.append(new PUSH(_cp, 1));
  Assert.assertNotNull(ih_0); // TODO why is this not used
  il.append(_factory.createNewArray(new ObjectType(TEST_PACKAGE+".TestArrayAccess02"), (short) 1));
  il.append(InstructionFactory.createStore(Type.OBJECT, 0));
  final InstructionHandle ih_5 = il.append(new PUSH(_cp, 1));
  Assert.assertNotNull(ih_5); // TODO why is this not used
  il.append(_factory.createNewArray(Type.STRING, (short) 1));
  il.append(InstructionFactory.createStore(Type.OBJECT, 1));
  final InstructionHandle ih_10 = il.append(InstructionFactory.createLoad(Type.OBJECT, 1));
  Assert.assertNotNull(ih_10); // TODO why is this not used
  il.append(new PUSH(_cp, 0));
  il.append(_factory.createNew(TEST_PACKAGE+".TestArrayAccess02"));
  il.append(InstructionConst.DUP);
  il.append(_factory.createInvoke(TEST_PACKAGE+".TestArrayAccess02", "<init>", Type.VOID, Type.NO_ARGS, Const.INVOKESPECIAL));
  il.append(InstructionConst.AASTORE);
  final InstructionHandle ih_20 = il.append(InstructionFactory.createReturn(Type.VOID));
  Assert.assertNotNull(ih_20); // TODO why is this not used
  method.setMaxStack();
  method.setMaxLocals();
  _cg.addMethod(method.getMethod());
  il.dispose();
}
 
Example 11
Source File: FieldInfo.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private FieldInfo(@SlashedClassName String className, String fieldName, String fieldSignature,
        @CheckForNull String fieldSourceSignature, int accessFlags, Map<ClassDescriptor, AnnotationValue> fieldAnnotations,
        boolean isResolved) {
    super(className, fieldName, fieldSignature, (accessFlags & Const.ACC_STATIC) != 0);
    this.accessFlags = accessFlags | (fieldName.startsWith("this$") ? Const.ACC_FINAL : 0);
    this.fieldSourceSignature = fieldSourceSignature;
    this.fieldAnnotations = Util.immutableMap(fieldAnnotations);
    this.isResolved = isResolved;
}
 
Example 12
Source File: MethodInfo.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
MethodInfo(@SlashedClassName String className, String methodName, String methodSignature, String methodSourceSignature,
        int accessFlags, boolean isUnconditionalThrower, boolean isUnsupported, boolean usesConcurrency,
        boolean hasBackBranch, boolean isStub, boolean isIdentity,
        boolean usesInvokeDynamic, int methodCallCount, @CheckForNull String[] exceptions,
        @CheckForNull MethodDescriptor accessMethodForMethod,
        @CheckForNull FieldDescriptor accessMethodForField,
        Map<ClassDescriptor, AnnotationValue> methodAnnotations, Map<Integer, Map<ClassDescriptor, AnnotationValue>> methodParameterAnnotations,
        long variableIsSynthetic) {
    super(className, methodName, methodSignature, (accessFlags & Const.ACC_STATIC) != 0);
    this.accessFlags = accessFlags;
    this.exceptions = exceptions;
    this.methodSourceSignature = methodSourceSignature;
    this.methodAnnotations = Util.immutableMap(methodAnnotations);
    this.methodParameterAnnotations = Util.immutableMap(methodParameterAnnotations);
    if (isUnconditionalThrower) {
        getUnconditionalthrowers().put(this, null);
    }
    if (isUnsupported) {
        getUnconditionalthrowers().put(this, null);
    }
    if (accessMethodForMethod != null) {
        getAccessmethodformethod().put(this, accessMethodForMethod);
    }
    if (accessMethodForField != null) {
        getAccessmethodforfield().put(this, accessMethodForField);
    }
    if (isIdentity) {
        getIdentitymethods().put(this, null);
    }
    if (usesInvokeDynamic) {
        getInvokeDynamicMethods().put(this, null);
    }

    this.usesConcurrency = usesConcurrency;
    this.hasBackBranch = hasBackBranch;
    this.isStub = isStub;
    this.methodCallCount = methodCallCount;
    this.variableIsSynthetic = variableIsSynthetic;
}
 
Example 13
Source File: FindHEmismatch.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void visit(Field obj) {
    int accessFlags = obj.getAccessFlags();
    if ((accessFlags & Const.ACC_STATIC) != 0) {
        return;
    }
    if (!obj.getName().startsWith("this$") && !BCELUtil.isSynthetic(obj) && !obj.isTransient()) {
        hasFields = true;
    }
}
 
Example 14
Source File: MutableStaticFields.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void visit(Field obj) {
    super.visit(obj);
    int flags = obj.getAccessFlags();
    boolean isStatic = (flags & Const.ACC_STATIC) != 0;
    if (!isStatic) {
        return;
    }
    boolean isVolatile = (flags & Const.ACC_VOLATILE) != 0;
    if (isVolatile) {
        return;
    }
    boolean isFinal = (flags & Const.ACC_FINAL) != 0;
    boolean isPublic = publicClass && (flags & Const.ACC_PUBLIC) != 0;
    boolean isProtected = publicClass && (flags & Const.ACC_PROTECTED) != 0;
    if (!isPublic && !isProtected) {
        return;
    }

    boolean isArray = getFieldSig().charAt(0) == '[';

    if (isFinal && !(isArray || isCollection(getFieldSig()))) {
        return;
    }
    if (isEclipseNLS && getFieldSig().equals("Ljava/lang/String;")) {
        return;
    }

    seen.add(getXField());
}
 
Example 15
Source File: AbstractClassMember.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public boolean isStatic() {
    return (accessFlags & Const.ACC_STATIC) != 0;
}
 
Example 16
Source File: MethodAnnotation.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Factory method to create the MethodAnnotation from the classname, method
 * name, signature, etc. The method tries to look up source line information
 * for the method.
 *
 * @param className
 *            name of the class containing the method
 * @param methodName
 *            name of the method
 * @param methodSig
 *            signature of the method
 * @param accessFlags
 *            the access flags of the method
 * @return the MethodAnnotation
 */
public static MethodAnnotation fromForeignMethod(@SlashedClassName String className, String methodName, String methodSig, int accessFlags) {

    className = ClassName.toDottedClassName(className);

    // Create MethodAnnotation.
    // It won't have source lines yet.
    MethodAnnotation methodAnnotation = new MethodAnnotation(className, methodName, methodSig,
            (accessFlags & Const.ACC_STATIC) != 0);

    SourceLineAnnotation sourceLines = SourceLineAnnotation.getSourceAnnotationForMethod(className, methodName, methodSig);

    methodAnnotation.setSourceLines(sourceLines);

    return methodAnnotation;
}
 
Example 17
Source File: AccessFlags.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
public final boolean isStatic() {
    return (access_flags & Const.ACC_STATIC) != 0;
}
 
Example 18
Source File: Pass2Verifier.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
@Override
public void visitField(final Field obj) {

    if (jc.isClass()) {
        int maxone=0;
        if (obj.isPrivate()) {
            maxone++;
        }
        if (obj.isProtected()) {
            maxone++;
        }
        if (obj.isPublic()) {
            maxone++;
        }
        if (maxone > 1) {
            throw new ClassConstraintException("Field '"+tostring(obj)+
                "' must only have at most one of its ACC_PRIVATE, ACC_PROTECTED, ACC_PUBLIC modifiers set.");
        }

        if (obj.isFinal() && obj.isVolatile()) {
            throw new ClassConstraintException("Field '"+tostring(obj)+
                "' must only have at most one of its ACC_FINAL, ACC_VOLATILE modifiers set.");
        }
    }
    else{ // isInterface!
        if (!obj.isPublic()) {
            throw new ClassConstraintException("Interface field '"+tostring(obj)+
                "' must have the ACC_PUBLIC modifier set but hasn't!");
        }
        if (!obj.isStatic()) {
            throw new ClassConstraintException("Interface field '"+tostring(obj)+
                "' must have the ACC_STATIC modifier set but hasn't!");
        }
        if (!obj.isFinal()) {
            throw new ClassConstraintException("Interface field '"+tostring(obj)+
                "' must have the ACC_FINAL modifier set but hasn't!");
        }
    }

    if ((obj.getAccessFlags() & ~(Const.ACC_PUBLIC|Const.ACC_PRIVATE|Const.ACC_PROTECTED|Const.ACC_STATIC|
                                  Const.ACC_FINAL|Const.ACC_VOLATILE|Const.ACC_TRANSIENT)) > 0) {
        addMessage("Field '"+tostring(obj)+
            "' has access flag(s) other than ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED,"+
                " ACC_STATIC, ACC_FINAL, ACC_VOLATILE, ACC_TRANSIENT set (ignored).");
    }

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

    final String name = obj.getName();
    if (! validFieldName(name)) {
        throw new ClassConstraintException("Field '"+tostring(obj)+"' has illegal name '"+obj.getName()+"'.");
    }

    // A descriptor is often named signature in BCEL
    checkIndex(obj, obj.getSignatureIndex(), CONST_Utf8);

    final String sig  = ((ConstantUtf8) (cp.getConstant(obj.getSignatureIndex()))).getBytes(); // Field or Method sig.(=descriptor)

    try{
        Type.getType(sig);  /* Don't need the return value */
    }
    catch (final ClassFormatException cfe) {
        throw new ClassConstraintException("Illegal descriptor (==signature) '"+sig+"' used by '"+tostring(obj)+"'.", cfe);
    }

    final String nameanddesc = name+sig;
    if (field_names_and_desc.contains(nameanddesc)) {
        throw new ClassConstraintException("No two fields (like '"+tostring(obj)+
            "') are allowed have same names and descriptors!");
    }
    if (field_names.contains(name)) {
        addMessage("More than one field of name '"+name+
            "' detected (but with different type descriptors). This is very unusual.");
    }
    field_names_and_desc.add(nameanddesc);
    field_names.add(name);

    final Attribute[] atts = obj.getAttributes();
    for (final Attribute att : atts) {
        if ((!(att instanceof ConstantValue)) &&
                (!(att instanceof Synthetic)) &&
                (!(att instanceof Deprecated))) {
            addMessage("Attribute '" + tostring(att) + "' as an attribute of Field '" +
                tostring(obj) + "' is unknown and will therefore be ignored.");
        }
        if (!(att instanceof ConstantValue)) {
            addMessage("Attribute '" + tostring(att) + "' as an attribute of Field '" + tostring(obj) +
                "' is not a ConstantValue and is therefore only of use for debuggers and such.");
        }
    }
}
 
Example 19
Source File: UselessSubclassMethod.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void visitCode(Code obj) {
    try {
        String methodName = getMethodName();

        if (!Const.CONSTRUCTOR_NAME.equals(methodName) && !"clone".equals(methodName)
                && ((getMethod().getAccessFlags() & (Const.ACC_STATIC | Const.ACC_SYNTHETIC)) == 0)) {

            /*
             * for some reason, access flags doesn't return Synthetic, so do
             * this hocus pocus
             */
            Attribute[] atts = getMethod().getAttributes();
            for (Attribute att : atts) {
                if (att.getClass().equals(Synthetic.class)) {
                    return;
                }
            }

            byte[] codeBytes = obj.getCode();
            if ((codeBytes.length == 0) || (codeBytes[0] != Const.ALOAD_0)) {
                return;
            }

            state = State.SEEN_NOTHING;
            invokePC = 0;
            super.visitCode(obj);
            if ((state == State.SEEN_RETURN) && (invokePC != 0)) {
                // Do this check late, as it is potentially expensive
                Method superMethod = findSuperclassMethod(superclassName, getMethod());
                if ((superMethod == null) || differentAttributes(getMethod(), superMethod)
                        || getMethod().isProtected()
                                && !samePackage(getDottedClassName(), superclassName)) {
                    return;
                }

                bugReporter.reportBug(new BugInstance(this, "USM_USELESS_SUBCLASS_METHOD", LOW_PRIORITY).addClassAndMethod(
                        this).addSourceLine(this, invokePC));
            }
        }
    } catch (ClassNotFoundException cnfe) {
        bugReporter.reportMissingClass(cnfe);
    }
}
 
Example 20
Source File: FieldAnnotation.java    From spotbugs with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Constructor.
 *
 * @param className
 *            the name of the class containing the field
 * @param fieldName
 *            the name of the field
 * @param fieldSig
 *            the type signature of the field
 * @param accessFlags
 *            accessFlags for the field
 */
public FieldAnnotation(@DottedClassName String className, String fieldName, String fieldSig, int accessFlags) {
    this(className, fieldName, fieldSig, (accessFlags & Const.ACC_STATIC) != 0);
}