Java Code Examples for org.apache.bcel.classfile.ConstantLong

The following examples show how to use org.apache.bcel.classfile.ConstantLong. These examples are extracted from open source projects. 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 Project: spotbugs   Source File: DumbMethods.java    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
@Override
public void visit(Field field) {
    ConstantValue value = field.getConstantValue();
    if (value == null) {
        return;
    }
    Constant c = getConstantPool().getConstant(value.getConstantValueIndex());

    if (testingEnabled && c instanceof ConstantLong && ((ConstantLong) c).getBytes() == MICROS_PER_DAY_OVERFLOWED_AS_INT) {
        bugReporter.reportBug(new BugInstance(this, "TESTING", HIGH_PRIORITY).addClass(this).addField(this)
                .addString("Did you mean MICROS_PER_DAY")
                .addInt(MICROS_PER_DAY_OVERFLOWED_AS_INT)
                .describe(IntAnnotation.INT_VALUE));

    }
}
 
Example 2
Source Project: spotbugs   Source File: OpcodeStack.java    License: GNU Lesser General Public License v2.1 6 votes vote down vote up
private void pushByConstant(DismantleBytecode dbc, Constant c) {

        if (c instanceof ConstantClass) {
            push(new Item("Ljava/lang/Class;", ((ConstantClass) c).getConstantValue(dbc.getConstantPool())));
        } else if (c instanceof ConstantInteger) {
            push(new Item("I", Integer.valueOf(((ConstantInteger) c).getBytes())));
        } else if (c instanceof ConstantString) {
            int s = ((ConstantString) c).getStringIndex();
            push(new Item("Ljava/lang/String;", getStringFromIndex(dbc, s)));
        } else if (c instanceof ConstantFloat) {
            push(new Item("F", Float.valueOf(((ConstantFloat) c).getBytes())));
        } else if (c instanceof ConstantDouble) {
            push(new Item("D", Double.valueOf(((ConstantDouble) c).getBytes())));
        } else if (c instanceof ConstantLong) {
            push(new Item("J", Long.valueOf(((ConstantLong) c).getBytes())));
        } else {
            throw new UnsupportedOperationException("StaticConstant type not expected");
        }
    }
 
Example 3
Source Project: commons-bcel   Source File: Pass3aVerifier.java    License: Apache License 2.0 6 votes vote down vote up
/** Checks if the constraints of operands of the said instruction(s) are satisfied. */
// LDC2_W
@Override
public void visitLDC2_W(final LDC2_W o) {
    indexValid(o, o.getIndex());
    final Constant c = constantPoolGen.getConstant(o.getIndex());
    if (! ( (c instanceof ConstantLong)    ||
                    (c instanceof ConstantDouble) ) ) {
        constraintViolated(o, "Operand of LDC2_W must be CONSTANT_Long or CONSTANT_Double, but is '"+c+"'.");
    }
    try{
        indexValid(o, o.getIndex()+1);
    }
    catch(final StaticCodeInstructionOperandConstraintException e) {
        throw new AssertionViolatedException("Does not BCEL handle that? LDC2_W operand has a problem.", e);
    }
}
 
Example 4
Source Project: commons-bcel   Source File: Pass2Verifier.java    License: Apache License 2.0 6 votes vote down vote up
private CPESSC_Visitor(final JavaClass _jc) {
    jc = _jc;
    cp = _jc.getConstantPool();
    cplen = cp.getLength();

    CONST_Class = ConstantClass.class;
    /*
    CONST_Fieldref = ConstantFieldref.class;
    CONST_Methodref = ConstantMethodref.class;
    CONST_InterfaceMethodref = ConstantInterfaceMethodref.class;
    */
    CONST_String = ConstantString.class;
    CONST_Integer = ConstantInteger.class;
    CONST_Float = ConstantFloat.class;
    CONST_Long = ConstantLong.class;
    CONST_Double = ConstantDouble.class;
    CONST_NameAndType = ConstantNameAndType.class;
    CONST_Utf8 = ConstantUtf8.class;

    carrier = new DescendingVisitor(_jc, this);
    carrier.visit();
}
 
Example 5
Source Project: ApkToolPlus   Source File: ExecutionVisitor.java    License: Apache License 2.0 5 votes vote down vote up
/** Symbolically executes the corresponding Java Virtual Machine instruction. */ 
public void visitLDC2_W(LDC2_W o){
	Constant c = cpg.getConstant(o.getIndex());
	if (c instanceof ConstantLong){
		stack().push(Type.LONG);
	}
	if (c instanceof ConstantDouble){
		stack().push(Type.DOUBLE);
	}
}
 
Example 6
Source Project: ApkToolPlus   Source File: InstConstraintVisitor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Ensures the specific preconditions of the said instruction.
 */
public void visitLDC2_W(LDC2_W o){
	// visitCPInstruction is called first.
	
	Constant c = cpg.getConstant(o.getIndex());
	if 	(!	(	( c instanceof ConstantLong) ||
						( c instanceof ConstantDouble )	)	){
		constraintViolated(o, "Referenced constant should be a CONSTANT_Integer, a CONSTANT_Float or a CONSTANT_String, but is '"+c+"'.");
	}
}
 
Example 7
@Override
public void sawOpcode(int seen) {
    if (state == SEEN_NOTHING) {
        if ((seen == Const.DCONST_0) || (seen == Const.DCONST_1)) {
            constValue = seen - Const.DCONST_0;
            state = SEEN_DCONST;
        } else if ((seen == Const.LDC2_W) || (seen == Const.LDC_W)) {
            state = SEEN_DCONST;
            Constant c = this.getConstantRefOperand();
            if (c instanceof ConstantDouble) {
                constValue = ((ConstantDouble) c).getBytes();
            } else if (c instanceof ConstantFloat) {
                constValue = ((ConstantFloat) c).getBytes();
            } else if (c instanceof ConstantLong) {
                constValue = ((ConstantLong) c).getBytes();
            } else {
                state = SEEN_NOTHING;
            }
        }
    } else if (state == SEEN_DCONST) {
        if (seen == Const.INVOKESTATIC) {
            state = SEEN_NOTHING;
            if ("java.lang.Math".equals(getDottedClassConstantOperand())) {
                String methodName = getNameConstantOperand();

                if (((constValue == 0.0) && zeroMethods.contains(methodName))
                        || ((constValue == 1.0) && oneMethods.contains(methodName)) || (anyMethods.contains(methodName))) {
                    bugReporter.reportBug(new BugInstance(this, "UM_UNNECESSARY_MATH", LOW_PRIORITY).addClassAndMethod(this)
                            .addSourceLine(this));
                }
            }
        }
        state = SEEN_NOTHING;
    }
}
 
Example 8
Source Project: commons-bcel   Source File: ConstantPoolGen.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Look for ConstantLong in ConstantPool.
 *
 * @param n Long number to look for
 * @return index on success, -1 otherwise
 */
public int lookupLong( final long n ) {
    for (int i = 1; i < index; i++) {
        if (constants[i] instanceof ConstantLong) {
            final ConstantLong c = (ConstantLong) constants[i];
            if (c.getBytes() == n) {
                return i;
            }
        }
    }
    return -1;
}
 
Example 9
Source Project: commons-bcel   Source File: ConstantPoolGen.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Add a new long constant to the ConstantPool, if it is not already in there.
 *
 * @param n Long number to add
 * @return index of entry
 */
public int addLong( final long n ) {
    int ret;
    if ((ret = lookupLong(n)) != -1) {
        return ret; // Already in CP
    }
    adjustSize();
    ret = index;
    constants[index] = new ConstantLong(n);
    index += 2; // Wastes one entry according to spec
    return ret;
}
 
Example 10
Source Project: commons-bcel   Source File: SimpleElementValueGen.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public String stringifyValue()
{
    switch (super.getElementValueType())
    {
    case PRIMITIVE_INT:
        final ConstantInteger c = (ConstantInteger) getConstantPool().getConstant(idx);
        return Integer.toString(c.getBytes());
    case PRIMITIVE_LONG:
        final ConstantLong j = (ConstantLong) getConstantPool().getConstant(idx);
        return Long.toString(j.getBytes());
    case PRIMITIVE_DOUBLE:
        final ConstantDouble d = (ConstantDouble) getConstantPool().getConstant(idx);
        return Double.toString(d.getBytes());
    case PRIMITIVE_FLOAT:
        final ConstantFloat f = (ConstantFloat) getConstantPool().getConstant(idx);
        return Float.toString(f.getBytes());
    case PRIMITIVE_SHORT:
        final ConstantInteger s = (ConstantInteger) getConstantPool().getConstant(idx);
        return Integer.toString(s.getBytes());
    case PRIMITIVE_BYTE:
        final ConstantInteger b = (ConstantInteger) getConstantPool().getConstant(idx);
        return Integer.toString(b.getBytes());
    case PRIMITIVE_CHAR:
        final ConstantInteger ch = (ConstantInteger) getConstantPool().getConstant(idx);
        return Integer.toString(ch.getBytes());
    case PRIMITIVE_BOOLEAN:
        final ConstantInteger bo = (ConstantInteger) getConstantPool().getConstant(idx);
        if (bo.getBytes() == 0) {
            return "false";
        }
        return "true";
    case STRING:
        final ConstantUtf8 cu8 = (ConstantUtf8) getConstantPool().getConstant(idx);
        return cu8.getBytes();
    default:
        throw new IllegalStateException(
            "SimpleElementValueGen class does not know how to stringify type " + super.getElementValueType());
    }
}
 
Example 11
Source Project: commons-bcel   Source File: Pass2Verifier.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void visitConstantLong(final ConstantLong obj) {
    if (obj.getTag() != Const.CONSTANT_Long) {
        throw new ClassConstraintException("Wrong constant tag in '"+tostring(obj)+"'.");
    }
    //no indices to check
}
 
Example 12
Source Project: commons-bcel   Source File: ExecutionVisitor.java    License: Apache License 2.0 5 votes vote down vote up
/** Symbolically executes the corresponding Java Virtual Machine instruction. */
@Override
public void visitLDC2_W(final LDC2_W o) {
    final Constant c = cpg.getConstant(o.getIndex());
    if (c instanceof ConstantLong) {
        stack().push(Type.LONG);
    }
    if (c instanceof ConstantDouble) {
        stack().push(Type.DOUBLE);
    }
}
 
Example 13
Source Project: commons-bcel   Source File: InstConstraintVisitor.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Ensures the specific preconditions of the said instruction.
 */
@Override
public void visitLDC2_W(final LDC2_W o) {
    // visitCPInstruction is called first.

    final Constant c = cpg.getConstant(o.getIndex());
    if     (!    (    ( c instanceof ConstantLong) ||
                        ( c instanceof ConstantDouble )    )    ) {
        constraintViolated(o,
                "Referenced constant should be a CONSTANT_Integer, a CONSTANT_Float or a CONSTANT_String, but is '"+c+"'.");
    }
}
 
Example 14
public void visit(ConstantLong obj) {
    visit((Constant) obj);
}
 
Example 15
@Override
public void visitConstantLong(ConstantLong obj) {
    visit(obj);
}
 
Example 16
@Override
public void visit(JavaClass obj) {
    compute();
    ConstantPool cp = obj.getConstantPool();
    Constant[] constants = cp.getConstantPool();
    checkConstant: for (int i = 0; i < constants.length; i++) {
        Constant co = constants[i];
        if (co instanceof ConstantDouble || co instanceof ConstantLong) {
            i++;
        }
        if (co instanceof ConstantClass) {
            String ref = getClassName(obj, i);
            if ((ref.startsWith("java") || ref.startsWith("org.w3c.dom")) && !defined.contains(ref)) {
                bugReporter.reportBug(new BugInstance(this, "VR_UNRESOLVABLE_REFERENCE", NORMAL_PRIORITY).addClass(obj)
                        .addString(ref));
            }

        } else if (co instanceof ConstantFieldref) {
            // do nothing until we handle static fields defined in
            // interfaces

        } else if (co instanceof ConstantInvokeDynamic) {
            // ignore. BCEL puts garbage data into ConstantInvokeDynamic
        } else if (co instanceof ConstantCP) {
            ConstantCP co2 = (ConstantCP) co;
            String className = getClassName(obj, co2.getClassIndex());

            // System.out.println("checking " + ref);
            if (className.equals(obj.getClassName()) || !defined.contains(className)) {
                // System.out.println("Skipping check of " + ref);
                continue checkConstant;
            }
            ConstantNameAndType nt = (ConstantNameAndType) cp.getConstant(co2.getNameAndTypeIndex());
            String name = ((ConstantUtf8) obj.getConstantPool().getConstant(nt.getNameIndex(), Const.CONSTANT_Utf8)).getBytes();
            String signature = ((ConstantUtf8) obj.getConstantPool().getConstant(nt.getSignatureIndex(), Const.CONSTANT_Utf8))
                    .getBytes();

            try {
                JavaClass target = Repository.lookupClass(className);
                if (!find(target, name, signature)) {
                    bugReporter.reportBug(new BugInstance(this, "VR_UNRESOLVABLE_REFERENCE", NORMAL_PRIORITY).addClass(obj)
                            .addString(getMemberName(target.getClassName(), name, signature)));
                }

            } catch (ClassNotFoundException e) {
                bugReporter.reportMissingClass(e);
            }
        }

    }
}
 
Example 17
Source Project: commons-bcel   Source File: StringRepresentation.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void visitConstantLong(final ConstantLong obj) {
    tostring = toString(obj);
}
 
Example 18
Source Project: commons-bcel   Source File: CounterVisitor.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void visitConstantLong(final ConstantLong obj)
{
    constantLongCount++;
}