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

The following examples show how to use org.apache.bcel.Const#NEW . 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: DoInsideDoPrivileged.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    if (seen == Const.INVOKEVIRTUAL && "setAccessible".equals(getNameConstantOperand())) {
        @DottedClassName
        String className = getDottedClassConstantOperand();
        if ("java.lang.reflect.Field".equals(className) || "java.lang.reflect.Method".equals(className)) {
            bugAccumulator.accumulateBug(
                    new BugInstance(this, "DP_DO_INSIDE_DO_PRIVILEGED", LOW_PRIORITY).addClassAndMethod(this)
                            .addCalledMethod(this), this);
        }

    }
    if (seen == Const.NEW) {
        @DottedClassName
        String classOfConstructedClass = getDottedClassConstantOperand();
        if (Subtypes2.instanceOf(classOfConstructedClass, "java.lang.ClassLoader")
                && !("main".equals(getMethodName()) && "([Ljava/lang/String;)V".equals(getMethodSig()) && getMethod()
                        .isStatic())) {
            bugAccumulator.accumulateBug(new BugInstance(this, "DP_CREATE_CLASSLOADER_INSIDE_DO_PRIVILEGED", NORMAL_PRIORITY)
                    .addClassAndMethod(this).addClass(classOfConstructedClass), this);
        }
    }

}
 
Example 2
Source File: DismantleBytecode.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void printOpCode(int seen) {
    System.out.print("  " + this.getClass().getSimpleName() + ": [" + formatter.format(getPC()) + "]  " + Const.getOpcodeName(seen));
    if ((seen == Const.INVOKEVIRTUAL) || (seen == Const.INVOKESPECIAL) || (seen == Const.INVOKEINTERFACE) || (seen == Const.INVOKESTATIC)) {
        System.out.print("   " + getClassConstantOperand() + "." + getNameConstantOperand() + " " + getSigConstantOperand());
    } else if (seen == Const.LDC || seen == Const.LDC_W || seen == Const.LDC2_W) {
        Constant c = getConstantRefOperand();
        if (c instanceof ConstantString) {
            System.out.print("   \"" + getStringConstantOperand() + "\"");
        } else if (c instanceof ConstantClass) {
            System.out.print("   " + getClassConstantOperand());
        } else {
            System.out.print("   " + c);
        }
    } else if ((seen == Const.ALOAD) || (seen == Const.ASTORE)) {
        System.out.print("   " + getRegisterOperand());
    } else if ((seen == Const.GOTO) || (seen == Const.GOTO_W) || (seen == Const.IF_ACMPEQ) || (seen == Const.IF_ACMPNE)
            || (seen == Const.IF_ICMPEQ)
            || (seen == Const.IF_ICMPGE) || (seen == Const.IF_ICMPGT) || (seen == Const.IF_ICMPLE) || (seen == Const.IF_ICMPLT)
            || (seen == Const.IF_ICMPNE) || (seen == Const.IFEQ) || (seen == Const.IFGE) || (seen == Const.IFGT) || (seen == Const.IFLE)
            || (seen == Const.IFLT)
            || (seen == Const.IFNE) || (seen == Const.IFNONNULL) || (seen == Const.IFNULL)) {
        System.out.print("   " + getBranchTarget());
    } else if ((seen == Const.NEW) || (seen == Const.INSTANCEOF)) {
        System.out.print("   " + getClassConstantOperand());
    } else if ((seen == Const.TABLESWITCH) || (seen == Const.LOOKUPSWITCH)) {
        System.out.print("    [");
        int switchPC = getPC();
        int[] offsets = getSwitchOffsets();
        for (int offset : offsets) {
            System.out.print((switchPC + offset) + ",");
        }
        System.out.print((switchPC + getDefaultSwitchOffset()) + "]");
    }

    System.out.println();
}
 
Example 3
Source File: IOStreamFactory.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public Stream createStream(Location location, ObjectType type, ConstantPoolGen cpg,
        RepositoryLookupFailureCallback lookupFailureCallback) {

    try {
        Instruction ins = location.getHandle().getInstruction();

        if (ins.getOpcode() != Const.NEW) {
            return null;
        }

        if (Hierarchy.isSubtype(type, baseClassType)) {
            boolean isUninteresting = false;
            for (ObjectType aUninterestingSubclassTypeList : uninterestingSubclassTypeList) {
                if (Hierarchy.isSubtype(type, aUninterestingSubclassTypeList)) {
                    isUninteresting = true;
                    break;
                }
            }
            Stream result = new Stream(location, type.getClassName(), baseClassType.getClassName())
                    .setIgnoreImplicitExceptions(true);
            if (!isUninteresting) {
                result.setInteresting(bugType);
            }
            return result;
        }
    } catch (ClassNotFoundException e) {
        lookupFailureCallback.reportMissingClass(e);
    }

    return null;
}
 
Example 4
Source File: IteratorIdioms.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    if (seen == Const.NEW && "java/util/NoSuchElementException".equals(getClassConstantOperand())) {
        sawNoSuchElement = true;
    } else if (seen == Const.INVOKESPECIAL || seen == Const.INVOKEVIRTUAL || seen == Const.INVOKEINTERFACE) {
        sawCall = true;
        String name = getNameConstantOperand().toLowerCase();
        if (name.indexOf("next") >= 0
                || name.indexOf("previous") >= 0) {
            sawNoSuchElement = true;
        }
    }
}
 
Example 5
Source File: BCELFactory.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
@Override
public void visitAllocationInstruction( final AllocationInstruction i ) {
    Type type;
    if (i instanceof CPInstruction) {
        type = ((CPInstruction) i).getType(_cp);
    } else {
        type = ((NEWARRAY) i).getType();
    }
    final short opcode = ((Instruction) i).getOpcode();
    int dim = 1;
    switch (opcode) {
        case Const.NEW:
            _out.println("il.append(_factory.createNew(\"" + ((ObjectType) type).getClassName()
                    + "\"));");
            break;
        case Const.MULTIANEWARRAY:
            dim = ((MULTIANEWARRAY) i).getDimensions();
            //$FALL-THROUGH$
        case Const.ANEWARRAY:
        case Const.NEWARRAY:
            if (type instanceof ArrayType) {
                type = ((ArrayType) type).getBasicType();
            }
            _out.println("il.append(_factory.createNewArray(" + BCELifier.printType(type)
                    + ", (short) " + dim + "));");
            break;
        default:
            throw new IllegalArgumentException("Unhandled opcode: " + opcode);
    }
}
 
Example 6
Source File: QuestionableBooleanAssignment.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    if (seen == Const.GOTO && getBranchOffset() == 4) {
        state = SEEN_GOTO;
    } else {
        switch (state) {
        case SEEN_NOTHING:
            if ((seen == Const.ICONST_1) || (seen == Const.ICONST_0)) {
                state = SEEN_ICONST_0_OR_1;
            }
            break;

        case SEEN_ICONST_0_OR_1:
            if (seen == Const.DUP) {
                state = SEEN_DUP;
            } else {
                state = SEEN_NOTHING;
            }
            break;

        case SEEN_DUP:
            if (((seen >= Const.ISTORE_0) && (seen <= Const.ISTORE_3)) || (seen == Const.ISTORE)) {
                state = SEEN_ISTORE;
            } else {
                state = SEEN_NOTHING;
            }
            break;

        case SEEN_ISTORE:
            if (seen == Const.IFEQ || seen == Const.IFNE) {
                bug = new BugInstance(this, "QBA_QUESTIONABLE_BOOLEAN_ASSIGNMENT", HIGH_PRIORITY).addClassAndMethod(this)
                        .addSourceLine(this);
                state = SEEN_IF;
            } else {
                state = SEEN_NOTHING;
            }
            break;

        case SEEN_IF:
            state = SEEN_NOTHING;
            if (seen == Const.NEW) {
                String cName = getClassConstantOperand();
                if ("java/lang/AssertionError".equals(cName)) {
                    break;
                }
            }
            bugReporter.reportBug(bug);
            break;
        case SEEN_GOTO:
            state = SEEN_NOTHING;
            break;
        default:
            break;
        }
    }
}
 
Example 7
Source File: FindUninitializedGet.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    if (!inConstructor) {
        return;
    }
    if (uninitializedFieldReadAndCheckedForNonnull != null) {
        if (seen == Const.NEW && getClassConstantOperand().endsWith("Exception")) {
            uninitializedFieldReadAndCheckedForNonnull.raisePriority();
        }
        uninitializedFieldReadAndCheckedForNonnull = null;
    }

    if (seen == Const.ALOAD_0) {
        thisOnTOS = true;
        return;
    }

    if (seen == Const.PUTFIELD && getClassConstantOperand().equals(getClassName())) {
        initializedFields.add(FieldAnnotation.fromReferencedField(this));
    } else if (thisOnTOS && seen == Const.GETFIELD && getClassConstantOperand().equals(getClassName())) {
        UnreadFieldsData unreadFields = AnalysisContext.currentAnalysisContext().getUnreadFieldsData();
        XField xField = XFactory.createReferencedXField(this);
        FieldAnnotation f = FieldAnnotation.fromReferencedField(this);
        int nextOpcode = 0xff & codeBytes[getPC() + 3];
        if (nextOpcode != Const.POP && !initializedFields.contains(f) && declaredFields.contains(f) && !containerFields.contains(f)
                && !unreadFields.isContainerField(xField)) {
            // System.out.println("Next opcode: " +
            // Const.getOpcodeName(nextOpcode));
            LocalVariableAnnotation possibleTarget = LocalVariableAnnotation.findMatchingIgnoredParameter(getClassContext(),
                    getMethod(), getNameConstantOperand(), xField.getSignature());
            if (possibleTarget == null) {
                possibleTarget = LocalVariableAnnotation.findUniqueBestMatchingParameter(getClassContext(), getMethod(),
                        getNameConstantOperand(), getSigConstantOperand());
            }
            int priority = unreadFields.getReadFields().contains(xField) ? NORMAL_PRIORITY : LOW_PRIORITY;
            boolean priorityLoweredBecauseOfIfNonnullTest = false;
            if (possibleTarget != null) {
                priority--;
            } else {
                FieldSummary fieldSummary = AnalysisContext.currentAnalysisContext().getFieldSummary();
                if (fieldSummary.callsOverriddenMethodsFromSuperConstructor(getClassDescriptor())) {
                    priority++;
                } else if (nextOpcode == Const.IFNONNULL) {
                    priority++;
                    priorityLoweredBecauseOfIfNonnullTest = true;
                }
            }

            BugInstance bug = new BugInstance(this, "UR_UNINIT_READ", priority).addClassAndMethod(this).addField(f)
                    .addOptionalAnnotation(possibleTarget).addSourceLine(this);
            pendingBugs.add(bug);
            if (priorityLoweredBecauseOfIfNonnullTest) {
                uninitializedFieldReadAndCheckedForNonnull = bug;
            }
            initializedFields.add(f);
        }
    } else if ((seen == Const.INVOKESPECIAL && !(Const.CONSTRUCTOR_NAME.equals(getNameConstantOperand()) && !getClassConstantOperand().equals(
            getClassName())))
            || (seen == Const.INVOKESTATIC && "doPrivileged".equals(getNameConstantOperand()) && "java/security/AccessController".equals(
                    getClassConstantOperand()))
            || (seen == Const.INVOKEVIRTUAL && getClassConstantOperand().equals(getClassName()))
            || (seen == Const.INVOKEVIRTUAL && "start".equals(getNameConstantOperand()))) {

        inConstructor = false;
    }

    thisOnTOS = false;
}
 
Example 8
Source File: Noise.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    int priority;
    switch (seen) {
    case Const.INVOKEINTERFACE:
    case Const.INVOKEVIRTUAL:
    case Const.INVOKESPECIAL:
    case Const.INVOKESTATIC:
        hq.pushHash(getClassConstantOperand());
        if (getNameConstantOperand().indexOf('$') == -1) {
            hq.pushHash(getNameConstantOperand());
        }
        hq.pushHash(getSigConstantOperand());

        priority = hq.getPriority();
        if (priority <= Priorities.LOW_PRIORITY) {
            accumulator.accumulateBug(new BugInstance(this, "NOISE_METHOD_CALL", priority).addClassAndMethod(this)
                    .addCalledMethod(this), this);
        }
        break;
    case Const.GETFIELD:
    case Const.PUTFIELD:
    case Const.GETSTATIC:
    case Const.PUTSTATIC:
        hq.pushHash(getClassConstantOperand());
        if (getNameConstantOperand().indexOf('$') == -1) {
            hq.pushHash(getNameConstantOperand());
        }
        hq.pushHash(getSigConstantOperand());
        priority = hq.getPriority();
        if (priority <= Priorities.LOW_PRIORITY) {
            accumulator.accumulateBug(new BugInstance(this, "NOISE_FIELD_REFERENCE", priority).addClassAndMethod(this)
                    .addReferencedField(this), this);
        }
        break;
    case Const.CHECKCAST:
    case Const.INSTANCEOF:
    case Const.NEW:
        hq.pushHash(getClassConstantOperand());
        break;
    case Const.IFEQ:
    case Const.IFNE:
    case Const.IFNONNULL:
    case Const.IFNULL:
    case Const.IF_ICMPEQ:
    case Const.IF_ICMPNE:
    case Const.IF_ICMPLE:
    case Const.IF_ICMPGE:
    case Const.IF_ICMPGT:
    case Const.IF_ICMPLT:
    case Const.IF_ACMPEQ:
    case Const.IF_ACMPNE:
    case Const.RETURN:
    case Const.ARETURN:
    case Const.IRETURN:
    case Const.MONITORENTER:
    case Const.MONITOREXIT:
    case Const.IINC:
    case Const.NEWARRAY:
    case Const.TABLESWITCH:
    case Const.LOOKUPSWITCH:
    case Const.LCMP:
    case Const.INEG:
    case Const.IADD:
    case Const.IMUL:
    case Const.ISUB:
    case Const.IDIV:
    case Const.IREM:
    case Const.IXOR:
    case Const.ISHL:
    case Const.ISHR:
    case Const.IUSHR:
    case Const.IAND:
    case Const.IOR:
    case Const.LAND:
    case Const.LOR:
    case Const.LADD:
    case Const.LMUL:
    case Const.LSUB:
    case Const.LDIV:
    case Const.LSHL:
    case Const.LSHR:
    case Const.LUSHR:
    case Const.AALOAD:
    case Const.AASTORE:
    case Const.IALOAD:
    case Const.IASTORE:
    case Const.BALOAD:
    case Const.BASTORE:
        hq.push(seen);
        priority = hq.getPriority();
        if (priority <= Priorities.LOW_PRIORITY) {
            accumulator.accumulateBug(
                    new BugInstance(this, "NOISE_OPERATION", priority).addClassAndMethod(this).addString(Const.getOpcodeName(seen)),
                    this);
        }
        break;
    default:
        break;
    }
}
 
Example 9
Source File: InitializationChain.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    InvocationInfo prev = lastInvocation;
    lastInvocation = null;
    if (Const.CONSTRUCTOR_NAME.equals(getMethodName())) {
        if (seen == Const.GETSTATIC && getClassConstantOperand().equals(getClassName())) {
            staticFieldsReadInAnyConstructor.add(getXFieldOperand());
            fieldsReadInThisConstructor.add(getXFieldOperand());
        }
        return;
    }

    if (seen == Const.INVOKESPECIAL && Const.CONSTRUCTOR_NAME.equals(getNameConstantOperand()) && getClassConstantOperand().equals(
            getClassName())) {

        XMethod m = getXMethodOperand();
        Set<XField> read = staticFieldsRead.get(m);
        if (constructorsInvokedInStaticInitializer.add(m) && read != null && !read.isEmpty()) {
            lastInvocation = new InvocationInfo(m, getPC());
            invocationInfo.add(lastInvocation);

        }

    }
    if (seen == Const.PUTSTATIC && getClassConstantOperand().equals(getClassName())) {
        XField f = getXFieldOperand();
        if (prev != null) {
            prev.field = f;
        }
        if (staticFieldsReadInAnyConstructor.contains(f) && !warningGiven.contains(f)) {
            for (InvocationInfo i : invocationInfo) {
                Set<XField> fields = staticFieldsRead.get(i.constructor);
                if (fields != null && fields.contains(f)) {
                    warningGiven.add(f);
                    BugInstance bug = new BugInstance(this, "SI_INSTANCE_BEFORE_FINALS_ASSIGNED", NORMAL_PRIORITY).addClassAndMethod(this);
                    if (i.field != null) {
                        bug.addField(i.field).describe(FieldAnnotation.STORED_ROLE);
                    }
                    bug.addMethod(i.constructor).describe(MethodAnnotation.METHOD_CONSTRUCTOR);
                    bug.addReferencedField(this).describe(FieldAnnotation.VALUE_OF_ROLE).addSourceLine(this, i.pc);
                    bugReporter.reportBug(bug);
                    break;

                }
            }
        }

    } else if (seen == Const.PUTSTATIC || seen == Const.GETSTATIC || seen == Const.INVOKESTATIC || seen == Const.NEW) {
        if (getPC() + 6 < codeBytes.length) {
            requires.add(getDottedClassConstantOperand());
        }
    }
}