org.apache.bcel.classfile.ConstantDouble Java Examples

The following examples show how to use org.apache.bcel.classfile.ConstantDouble. 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: Pass2Verifier.java    From commons-bcel with 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 #2
Source File: FindRoughConstants.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.LDC || seen == Const.LDC_W || seen == Const.LDC2_W) {
        Constant c = getConstantRefOperand();
        if (c instanceof ConstantFloat) {
            checkConst(((ConstantFloat) c).getBytes());
        } else if (c instanceof ConstantDouble) {
            checkConst(((ConstantDouble) c).getBytes());
        }
        return;
    }
    // Lower priority if the constant is put into array immediately or after the boxing:
    // this is likely to be just similar number in some predefined dataset (like lookup table)
    if (seen == Const.INVOKESTATIC && lastBug != null) {
        if (getNextOpcode() == Const.AASTORE
                && getNameConstantOperand().equals("valueOf")
                && (getClassConstantOperand().equals("java/lang/Double") || getClassConstantOperand().equals(
                        "java/lang/Float"))) {
            lastBug = ((BugInstance) lastBug.clone());
            lastBug.setPriority(lastPriority + 1);
            bugAccumulator.forgetLastBug();
            bugAccumulator.accumulateBug(lastBug, this);
        }
    }
    lastBug = null;
}
 
Example #3
Source File: Pass3aVerifier.java    From commons-bcel with 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 File: OpcodeStack.java    From spotbugs with 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 #5
Source File: InstConstraintVisitor.java    From ApkToolPlus with 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 #6
Source File: InstConstraintVisitor.java    From commons-bcel with 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 #7
Source File: ExecutionVisitor.java    From commons-bcel with 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 #8
Source File: Pass2Verifier.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
@Override
public void visitConstantDouble(final ConstantDouble obj) {
    if (obj.getTag() != Const.CONSTANT_Double) {
        throw new ClassConstraintException("Wrong constant tag in '"+tostring(obj)+"'.");
    }
    //no indices to check
}
 
Example #9
Source File: SimpleElementValueGen.java    From commons-bcel with 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 #10
Source File: ConstantPoolGen.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * Add a new double constant to the ConstantPool, if it is not already in there.
 *
 * @param n Double number to add
 * @return index of entry
 */
public int addDouble( final double n ) {
    int ret;
    if ((ret = lookupDouble(n)) != -1) {
        return ret; // Already in CP
    }
    adjustSize();
    ret = index;
    constants[index] = new ConstantDouble(n);
    index += 2; // Wastes one entry according to spec
    return ret;
}
 
Example #11
Source File: ConstantPoolGen.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * Look for ConstantDouble in ConstantPool.
 *
 * @param n Double number to look for
 * @return index on success, -1 otherwise
 */
public int lookupDouble( final double n ) {
    final long bits = Double.doubleToLongBits(n);
    for (int i = 1; i < index; i++) {
        if (constants[i] instanceof ConstantDouble) {
            final ConstantDouble c = (ConstantDouble) constants[i];
            if (Double.doubleToLongBits(c.getBytes()) == bits) {
                return i;
            }
        }
    }
    return -1;
}
 
Example #12
Source File: UnnecessaryMath.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@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 #13
Source File: ExecutionVisitor.java    From ApkToolPlus with 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 #14
Source File: ResolveAllReferences.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@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 #15
Source File: BetterVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void visitConstantDouble(ConstantDouble obj) {
    visit(obj);
}
 
Example #16
Source File: StringRepresentation.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
@Override
public void visitConstantDouble(final ConstantDouble obj) {
    tostring = toString(obj);
}
 
Example #17
Source File: BetterVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void visit(ConstantDouble obj) {
    visit((Constant) obj);
}
 
Example #18
Source File: CounterVisitor.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
@Override
public void visitConstantDouble(final ConstantDouble obj)
{
    constantDoubleCount++;
}