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

The following examples show how to use org.apache.bcel.classfile.ConstantFloat. 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
@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 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. */
// LDC and LDC_W (LDC_W is a subclass of LDC in BCEL's model)
@Override
public void visitLDC(final LDC ldc) {
    indexValid(ldc, ldc.getIndex());
    final Constant c = constantPoolGen.getConstant(ldc.getIndex());
    if (c instanceof ConstantClass) {
      addMessage("Operand of LDC or LDC_W is CONSTANT_Class '"+c+"' - this is only supported in JDK 1.5 and higher.");
    }
    else{
      if (! ( (c instanceof ConstantInteger)    ||
              (c instanceof ConstantFloat)         ||
        (c instanceof ConstantString) ) ) {
    constraintViolated(ldc,
        "Operand of LDC or LDC_W must be one of CONSTANT_Integer, CONSTANT_Float or CONSTANT_String, but is '"+c+"'.");
      }
    }
}
 
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: commons-bcel   Source File: ExecutionVisitor.java    License: Apache License 2.0 6 votes vote down vote up
/** Symbolically executes the corresponding Java Virtual Machine instruction. */
@Override
public void visitLDC(final LDC o) {
    final Constant c = cpg.getConstant(o.getIndex());
    if (c instanceof ConstantInteger) {
        stack().push(Type.INT);
    }
    if (c instanceof ConstantFloat) {
        stack().push(Type.FLOAT);
    }
    if (c instanceof ConstantString) {
        stack().push(Type.STRING);
    }
    if (c instanceof ConstantClass) {
        stack().push(Type.CLASS);
    }
}
 
Example 6
Source Project: commons-bcel   Source File: ExecutionVisitor.java    License: Apache License 2.0 6 votes vote down vote up
/** Symbolically executes the corresponding Java Virtual Machine instruction. */
public void visitLDC_W(final LDC_W o) {
    final Constant c = cpg.getConstant(o.getIndex());
    if (c instanceof ConstantInteger) {
        stack().push(Type.INT);
    }
    if (c instanceof ConstantFloat) {
        stack().push(Type.FLOAT);
    }
    if (c instanceof ConstantString) {
        stack().push(Type.STRING);
    }
    if (c instanceof ConstantClass) {
        stack().push(Type.CLASS);
    }
}
 
Example 7
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 visitLDC(LDC o){
	Constant c = cpg.getConstant(o.getIndex());
	if (c instanceof ConstantInteger){
		stack().push(Type.INT);
	}
	if (c instanceof ConstantFloat){
		stack().push(Type.FLOAT);
	}
	if (c instanceof ConstantString){
		stack().push(Type.STRING);
	}
}
 
Example 8
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 visitLDC_W(LDC_W o){
	Constant c = cpg.getConstant(o.getIndex());
	if (c instanceof ConstantInteger){
		stack().push(Type.INT);
	}
	if (c instanceof ConstantFloat){
		stack().push(Type.FLOAT);
	}
	if (c instanceof ConstantString){
		stack().push(Type.STRING);
	}
}
 
Example 9
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 visitLDC(LDC o){
	// visitCPInstruction is called first.
	
	Constant c = cpg.getConstant(o.getIndex());
	if 	(!	(	( c instanceof ConstantInteger) ||
						( c instanceof ConstantFloat	)	||
						( c instanceof ConstantString )	)	){
		constraintViolated(o, "Referenced constant should be a CONSTANT_Integer, a CONSTANT_Float or a CONSTANT_String, but is '"+c+"'.");
	}
}
 
Example 10
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 visitLDC_W(LDC_W o){
	// visitCPInstruction is called first.
	
	Constant c = cpg.getConstant(o.getIndex());
	if 	(!	(	( c instanceof ConstantInteger) ||
						( c instanceof ConstantFloat	)	||
						( c instanceof ConstantString )	)	){
		constraintViolated(o, "Referenced constant should be a CONSTANT_Integer, a CONSTANT_Float or a CONSTANT_String, but is '"+c+"'.");
	}
}
 
Example 11
@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 12
Source Project: commons-bcel   Source File: ConstantPoolGen.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Look for ConstantFloat in ConstantPool.
 *
 * @param n Float number to look for
 * @return index on success, -1 otherwise
 */
public int lookupFloat( final float n ) {
    final int bits = Float.floatToIntBits(n);
    for (int i = 1; i < index; i++) {
        if (constants[i] instanceof ConstantFloat) {
            final ConstantFloat c = (ConstantFloat) constants[i];
            if (Float.floatToIntBits(c.getBytes()) == bits) {
                return i;
            }
        }
    }
    return -1;
}
 
Example 13
Source Project: commons-bcel   Source File: ConstantPoolGen.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Add a new Float constant to the ConstantPool, if it is not already in there.
 *
 * @param n Float number to add
 * @return index of entry
 */
public int addFloat( final float n ) {
    int ret;
    if ((ret = lookupFloat(n)) != -1) {
        return ret; // Already in CP
    }
    adjustSize();
    ret = index;
    constants[index++] = new ConstantFloat(n);
    return ret;
}
 
Example 14
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 15
Source Project: commons-bcel   Source File: Pass2Verifier.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void visitConstantFloat(final ConstantFloat obj) {
    if (obj.getTag() != Const.CONSTANT_Float) {
        throw new ClassConstraintException("Wrong constant tag in '"+tostring(obj)+"'.");
    }
    //no indices to check
}
 
Example 16
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 visitLDC(final LDC o) {
    // visitCPInstruction is called first.

    final Constant c = cpg.getConstant(o.getIndex());
    if     (!    (    ( c instanceof ConstantInteger) ||
                ( c instanceof ConstantFloat    )    ||
                ( c instanceof ConstantString    )    ||
                ( c instanceof ConstantClass    ) )    ) {
        constraintViolated(o,
            "Referenced constant should be a CONSTANT_Integer, a CONSTANT_Float, a CONSTANT_String or a CONSTANT_Class, but is '"+
             c + "'.");
    }
}
 
Example 17
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.
 */
public void visitLDC_W(final LDC_W o) {
    // visitCPInstruction is called first.

    final Constant c = cpg.getConstant(o.getIndex());
    if     (!    (    ( c instanceof ConstantInteger) ||
                ( c instanceof ConstantFloat    )    ||
                ( c instanceof ConstantString    )    ||
                ( c instanceof ConstantClass    ) )    ) {
        constraintViolated(o,
            "Referenced constant should be a CONSTANT_Integer, a CONSTANT_Float, a CONSTANT_String or a CONSTANT_Class, but is '"+
             c + "'.");
    }
}
 
Example 18
public void visit(ConstantFloat obj) {
    visit((Constant) obj);
}
 
Example 19
@Override
public void visitConstantFloat(ConstantFloat obj) {
    visit(obj);
}
 
Example 20
Source Project: commons-bcel   Source File: StringRepresentation.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void visitConstantFloat(final ConstantFloat obj) {
    tostring = toString(obj);
}
 
Example 21
Source Project: commons-bcel   Source File: CounterVisitor.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void visitConstantFloat(final ConstantFloat obj)
{
    constantFloatCount++;
}