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

The following examples show how to use org.apache.bcel.Const#ALOAD_3 . 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: UnconditionalValueDerefAnalysis.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
private static boolean check(InstructionHandle h, int[] opcodes) {
    for (int opcode : opcodes) {
        if (h == null) {
            return false;
        }
        short opcode2 = h.getInstruction().getOpcode();
        if (opcode == Const.LDC) {
            switch (opcode2) {
            case Const.LDC:
            case Const.ALOAD:
            case Const.ALOAD_0:
            case Const.ALOAD_1:
            case Const.ALOAD_2:
            case Const.ALOAD_3:
                break;
            default:
                return false;
            }
        } else if (opcode2 != opcode) {
            return false;
        }
        h = h.getNext();
    }
    return true;
}
 
Example 2
Source File: LocalVariableInstruction.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
/**
 * Read needed data (e.g. index) from file.
 * <pre>
 * (ILOAD &lt;= tag &lt;= ALOAD_3) || (ISTORE &lt;= tag &lt;= ASTORE_3)
 * </pre>
 */
@Override
protected void initFromFile( final ByteSequence bytes, final boolean wide ) throws IOException {
    if (wide) {
        n = bytes.readUnsignedShort();
        super.setLength(4);
    } else {
        final short _opcode = super.getOpcode();
        if (((_opcode >= Const.ILOAD) && (_opcode <= Const.ALOAD))
         || ((_opcode >= Const.ISTORE) && (_opcode <= Const.ASTORE))) {
            n = bytes.readUnsignedByte();
            super.setLength(2);
        } else if (_opcode <= Const.ALOAD_3) { // compact load instruction such as ILOAD_2
            n = (_opcode - Const.ILOAD_0) % 4;
            super.setLength(1);
        } else { // Assert ISTORE_0 <= tag <= ASTORE_3
            n = (_opcode - Const.ISTORE_0) % 4;
            super.setLength(1);
        }
    }
}
 
Example 3
Source File: LocalVariableInstruction.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * Long output format:
 *
 * &lt;name of opcode&gt; "["&lt;opcode number&gt;"]"
 * "("&lt;length of instruction&gt;")" "&lt;"&lt; local variable index&gt;"&gt;"
 *
 * @param verbose long/short format switch
 * @return mnemonic for instruction
 */
@Override
public String toString( final boolean verbose ) {
    final short _opcode = super.getOpcode();
    if (((_opcode >= Const.ILOAD_0) && (_opcode <= Const.ALOAD_3))
     || ((_opcode >= Const.ISTORE_0) && (_opcode <= Const.ASTORE_3))) {
        return super.toString(verbose);
    }
    return super.toString(verbose) + " " + n;
}
 
Example 4
Source File: FindSpinLoop.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {

    // System.out.println("PC: " + PC + ", stage: " + stage1);
    switch (seen) {
    case Const.ALOAD_0:
    case Const.ALOAD_1:
    case Const.ALOAD_2:
    case Const.ALOAD_3:
    case Const.ALOAD:
        if (DEBUG) {
            System.out.println("   ALOAD at PC " + getPC());
        }
        start = getPC();
        stage = 1;
        break;
    case Const.GETSTATIC:
        if (DEBUG) {
            System.out.println("   getfield in stage " + stage);
        }
        lastFieldSeen = FieldAnnotation.fromReferencedField(this);
        start = getPC();
        stage = 2;
        break;
    case Const.GETFIELD:
        if (DEBUG) {
            System.out.println("   getfield in stage " + stage);
        }
        lastFieldSeen = FieldAnnotation.fromReferencedField(this);
        if (stage == 1 || stage == 2) {
            stage = 2;
        } else {
            stage = 0;
        }
        break;
    case Const.GOTO:
    case Const.IFNE:
    case Const.IFEQ:
    case Const.IFNULL:
    case Const.IFNONNULL:
        if (DEBUG) {
            System.out.println("   conditional branch in stage " + stage + " to " + getBranchTarget());
        }
        if (stage == 2 && getBranchTarget() == start) {
            bugReporter.reportBug(new BugInstance(this, "SP_SPIN_ON_FIELD", NORMAL_PRIORITY).addClassAndMethod(this)
                    .addReferencedField(lastFieldSeen).addSourceLine(this, start));
            stage = 0;
        } else if (getBranchTarget() < getPC()) {
            stage = 0;
        }
        break;
    default:
        stage = 0;
        break;
    }

}
 
Example 5
Source File: SuspiciousThreadInterrupted.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {

    if (state == SEEN_POSSIBLE_THREAD) {
        if (seen == Const.POP) {
            state = SEEN_UNKNOWNCONTEXT_POP;
            return;
        } else {
            state = SEEN_NOTHING;
        }
    }
    switch (state) {
    case SEEN_NOTHING:
        if ((seen == Const.INVOKESTATIC) && "java/lang/Thread".equals(getClassConstantOperand())
                && "currentThread".equals(getNameConstantOperand()) && "()Ljava/lang/Thread;".equals(getSigConstantOperand())) {
            state = SEEN_CURRENTTHREAD;
        } else if ((seen == Const.INVOKESTATIC || seen == Const.INVOKEINTERFACE || seen == Const.INVOKEVIRTUAL || seen == Const.INVOKESPECIAL)
                && getSigConstantOperand().endsWith("Ljava/lang/Thread;")) {
            state = SEEN_POSSIBLE_THREAD;
        } else if (seen == Const.ALOAD) {
            if (localsWithCurrentThreadValue.get(getRegisterOperand())) {
                state = SEEN_CURRENTTHREAD;
            } else {
                state = SEEN_POSSIBLE_THREAD;
            }
        } else if ((seen >= Const.ALOAD_0) && (seen <= Const.ALOAD_3)) {
            if (localsWithCurrentThreadValue.get(seen - Const.ALOAD_0)) {
                state = SEEN_CURRENTTHREAD;
            } else {
                state = SEEN_POSSIBLE_THREAD;
            }
        } else if ((seen == Const.GETFIELD || seen == Const.GETSTATIC) && "Ljava/lang/Thread;".equals(getSigConstantOperand())) {
            state = SEEN_POSSIBLE_THREAD;
        }
        break;

    case SEEN_CURRENTTHREAD:
        if (seen == Const.POP) {
            state = SEEN_POP_AFTER_CURRENTTHREAD;
        } else if (seen == Const.ASTORE) {
            localsWithCurrentThreadValue.set(getRegisterOperand());
            state = SEEN_NOTHING;
        } else if ((seen >= Const.ASTORE_0) && (seen <= Const.ASTORE_3)) {
            localsWithCurrentThreadValue.set(seen - Const.ASTORE_0);
            state = SEEN_NOTHING;
        } else {
            state = SEEN_NOTHING;
        }
        break;

    default:
        if ((seen == Const.INVOKESTATIC) && "java/lang/Thread".equals(getClassConstantOperand())
                && "interrupted".equals(getNameConstantOperand()) && "()Z".equals(getSigConstantOperand())) {
            if (state == SEEN_POP_AFTER_CURRENTTHREAD) {
                bugReporter.reportBug(new BugInstance(this, "STI_INTERRUPTED_ON_CURRENTTHREAD", LOW_PRIORITY)
                        .addClassAndMethod(this).addSourceLine(this));
            } else if (state == SEEN_UNKNOWNCONTEXT_POP) {
                bugReporter.reportBug(new BugInstance(this, "STI_INTERRUPTED_ON_UNKNOWNTHREAD", NORMAL_PRIORITY)
                        .addClassAndMethod(this).addSourceLine(this));
            }
        }
        state = SEEN_NOTHING;
        break;
    }
}
 
Example 6
Source File: SuperfluousInstanceOf.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    switch (state) {
    case SEEN_NOTHING:
        if (seen == Const.ALOAD) {
            register = getRegisterOperand();
        } else if ((seen >= Const.ALOAD_0) && (seen <= Const.ALOAD_3)) {
            register = seen - Const.ALOAD_0;
        } else {
            return;
        }
        state = SEEN_ALOAD;
        break;

    case SEEN_ALOAD:
        try {
            if (seen == Const.INSTANCEOF) {
                LocalVariable lv = LVTHelper.getLocalVariableAtPC(varTable, register, getPC());
                if (lv != null) {
                    String objSignature = lv.getSignature();
                    if (objSignature.charAt(0) == 'L') {
                        objSignature = objSignature.substring(1, objSignature.length() - 1).replace('/', '.');
                        String clsSignature = getDottedClassConstantOperand();

                        if (clsSignature.charAt(0) != '[') {
                            if (org.apache.bcel.Repository.instanceOf(objSignature, clsSignature)) {
                                bugReporter.reportBug(new BugInstance(this, "SIO_SUPERFLUOUS_INSTANCEOF", LOW_PRIORITY)
                                        .addClassAndMethod(this).addSourceLine(this));
                            }
                        }
                    }
                }
            }
        } catch (ClassNotFoundException cnfe) {
            bugReporter.reportMissingClass(cnfe);
        }

        state = SEEN_NOTHING;
        break;
    default:
        break;
    }

}
 
Example 7
Source File: VarArgsProblems.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    // System.out.println("State:" + state);
    if (seen == Const.GOTO && getBranchOffset() == 4) {
        state = SEEN_GOTO;
    } else {
        switch (state) {
        case SEEN_NOTHING:
            if ((seen == Const.ICONST_1)) {
                state = SEEN_ICONST_1;
            }
            break;

        case SEEN_ICONST_1:
            if (seen == Const.ANEWARRAY && primitiveArray.matcher(getClassConstantOperand()).matches()) {
                // System.out.println("Allocation of array of type " +
                // getClassConstantOperand());
                primitiveArraySig = getClassConstantOperand();
                state = SEEN_ANEWARRAY;
            } else {
                state = SEEN_NOTHING;
            }
            break;

        case SEEN_ANEWARRAY:
            if (seen == Const.DUP) {
                state = SEEN_DUP;
            } else {
                state = SEEN_NOTHING;
            }
            break;
        case SEEN_DUP:
            if (seen == Const.ICONST_0) {
                state = SEEN_ICONST_0;
            } else {
                state = SEEN_NOTHING;
            }
            break;
        case SEEN_ICONST_0:
            if (((seen >= Const.ALOAD_0) && (seen < Const.ALOAD_3)) || (seen == Const.ALOAD)) {
                state = SEEN_ALOAD;
            } else {
                state = SEEN_NOTHING;
            }
            break;

        case SEEN_ALOAD:
            if (seen == Const.AASTORE) {
                state = SEEN_AASTORE;
            } else {
                state = SEEN_NOTHING;
            }
            break;

        case SEEN_AASTORE:
            if (seen == Const.INVOKESTATIC || seen == Const.INVOKEINTERFACE || seen == Const.INVOKESPECIAL || seen == Const.INVOKEVIRTUAL) {
                // System.out.println(getClassConstantOperand());
                // System.out.println(getNameConstantOperand());
                // System.out.println(getSigConstantOperand());
                if (getSigConstantOperand().indexOf("Ljava/lang/Object;)") == -1) {
                    break;
                }
                int priority = NORMAL_PRIORITY;
                if ("asList".equals(getNameConstantOperand()) && "java/util/Arrays".equals(getClassConstantOperand())) {
                    priority = HIGH_PRIORITY;
                }
                bugReporter.reportBug(new BugInstance(this, "VA_PRIMITIVE_ARRAY_PASSED_TO_OBJECT_VARARG", priority)
                        .addClassAndMethod(this).addType(primitiveArraySig).describe(TypeAnnotation.FOUND_ROLE)
                        .addCalledMethod(this).addSourceLine(this));
            }
            state = SEEN_NOTHING;
            break;

        case SEEN_GOTO:
            state = SEEN_NOTHING;
            break;
        default:
            throw new IllegalStateException("State " + state + " not expected");

        }
    }
}