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

The following examples show how to use org.apache.bcel.Const#ANEWARRAY . 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: 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 2
Source File: InefficientToArray.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    if (DEBUG) {
        System.out.println("State: " + state + "  Opcode: " + Const.getOpcodeName(seen));
    }

    switch (state) {
    case SEEN_NOTHING:
        if (seen == Const.ICONST_0) {
            state = SEEN_ICONST_0;
        }
        break;

    case SEEN_ICONST_0:
        if (seen == Const.ANEWARRAY) {
            state = SEEN_ANEWARRAY;
        } else {
            state = SEEN_NOTHING;
        }
        break;

    case SEEN_ANEWARRAY:
        if (((seen == Const.INVOKEVIRTUAL) || (seen == Const.INVOKEINTERFACE)) && ("toArray".equals(getNameConstantOperand()))
                && ("([Ljava/lang/Object;)[Ljava/lang/Object;".equals(getSigConstantOperand()))) {
            try {
                String clsName = getDottedClassConstantOperand();
                JavaClass cls = Repository.lookupClass(clsName);
                if (cls.implementationOf(collectionClass)) {
                    bugAccumulator.accumulateBug(
                            new BugInstance(this, "ITA_INEFFICIENT_TO_ARRAY", LOW_PRIORITY).addClassAndMethod(this), this);
                }

            } catch (ClassNotFoundException cnfe) {
                bugReporter.reportMissingClass(cnfe);
            }
        }
        state = SEEN_NOTHING;
        break;

    default:
        state = SEEN_NOTHING;
        break;
    }
}
 
Example 3
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");

        }
    }
}
 
Example 4
Source File: FormatStringChecker.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(getPC() + " " + Const.getOpcodeName(seen) + " " + state);

    if (stack.getStackDepth() < stackDepth) {
        state = FormatState.NONE;
        stackDepth = 0;
        arguments = null;
    }
    if (seen == Const.ANEWARRAY && stack.getStackDepth() >= 2) {
        Object size = stack.getStackItem(0).getConstant();
        Object formatStr = stack.getStackItem(1).getConstant();
        if (size instanceof Integer && formatStr instanceof String) {
            arguments = new OpcodeStack.Item[(Integer) size];
            this.formatString = (String) formatStr;
            state = FormatState.READY_FOR_FORMAT;
            stackDepth = stack.getStackDepth();
        }
    } else if (state == FormatState.READY_FOR_FORMAT && seen == Const.DUP) {
        state = FormatState.EXPECTING_ASSIGNMENT;
    } else if (state == FormatState.EXPECTING_ASSIGNMENT && stack.getStackDepth() == stackDepth + 3 && seen == Const.AASTORE) {
        Object pos = stack.getStackItem(1).getConstant();
        OpcodeStack.Item value = stack.getStackItem(0);
        if (pos instanceof Integer) {
            int index = (Integer) pos;
            if (index >= 0 && index < arguments.length) {
                arguments[index] = value;
                state = FormatState.READY_FOR_FORMAT;
            } else {
                state = FormatState.NONE;
            }
        } else {
            state = FormatState.NONE;
        }
    } else if (state == FormatState.READY_FOR_FORMAT
            && (seen == Const.INVOKESPECIAL || seen == Const.INVOKEVIRTUAL || seen == Const.INVOKESTATIC || seen == Const.INVOKEINTERFACE)
            && stack.getStackDepth() == stackDepth) {

        String cl = getClassConstantOperand();
        String nm = getNameConstantOperand();
        String sig = getSigConstantOperand();
        XMethod m = getXMethodOperand();
        if ((m == null || m.isVarArgs())
                && sig.indexOf("Ljava/lang/String;[Ljava/lang/Object;)") >= 0
                && ("java/util/Formatter".equals(cl) && "format".equals(nm) || "java/lang/String".equals(cl)
                        && "format".equals(nm) || "java/io/PrintStream".equals(cl) && "format".equals(nm)
                        || "java/io/PrintStream".equals(cl) && "printf".equals(nm) || cl.endsWith("Writer")
                                && "format".equals(nm) || cl.endsWith("Writer") && "printf".equals(nm)) || cl.endsWith("Logger")
                                        && nm.endsWith("fmt")) {

            if (formatString.indexOf('\n') >= 0) {
                bugReporter.reportBug(new BugInstance(this, "VA_FORMAT_STRING_USES_NEWLINE", NORMAL_PRIORITY)
                        .addClassAndMethod(this).addCalledMethod(this).addString(formatString)
                        .describe(StringAnnotation.FORMAT_STRING_ROLE).addSourceLine(this));
            }
        }

    }
}