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

The following examples show how to use org.apache.bcel.classfile.ConstantInteger. 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   Author: spotbugs   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 #2
Source Project: commons-bcel   Author: apache   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 #3
Source Project: commons-bcel   Author: apache   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 #4
Source Project: commons-bcel   Author: apache   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 #5
Source Project: commons-bcel   Author: apache   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 #6
Source Project: ApkToolPlus   Author: linchaolong   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 #7
Source Project: ApkToolPlus   Author: linchaolong   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 #8
Source Project: ApkToolPlus   Author: linchaolong   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 #9
Source Project: ApkToolPlus   Author: linchaolong   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 #10
Source Project: commons-bcel   Author: apache   File: ConstantPoolGen.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Look for ConstantInteger in ConstantPool.
 *
 * @param n integer number to look for
 * @return index on success, -1 otherwise
 */
public int lookupInteger( final int n ) {
    for (int i = 1; i < index; i++) {
        if (constants[i] instanceof ConstantInteger) {
            final ConstantInteger c = (ConstantInteger) constants[i];
            if (c.getBytes() == n) {
                return i;
            }
        }
    }
    return -1;
}
 
Example #11
Source Project: commons-bcel   Author: apache   File: ConstantPoolGen.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Add a new Integer constant to the ConstantPool, if it is not already in there.
 *
 * @param n integer number to add
 * @return index of entry
 */
public int addInteger( final int n ) {
    int ret;
    if ((ret = lookupInteger(n)) != -1) {
        return ret; // Already in CP
    }
    adjustSize();
    ret = index;
    constants[index++] = new ConstantInteger(n);
    return ret;
}
 
Example #12
Source Project: commons-bcel   Author: apache   File: SimpleElementValueGen.java    License: Apache License 2.0 5 votes vote down vote up
public int getValueInt()
{
    if (super.getElementValueType() != PRIMITIVE_INT) {
        throw new IllegalStateException(
                "Dont call getValueString() on a non STRING ElementValue");
    }
    final ConstantInteger c = (ConstantInteger) getConstantPool().getConstant(idx);
    return c.getBytes();
}
 
Example #13
Source Project: commons-bcel   Author: apache   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 #14
Source Project: commons-bcel   Author: apache   File: Pass2Verifier.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void visitConstantInteger(final ConstantInteger obj) {
    if (obj.getTag() != Const.CONSTANT_Integer) {
        throw new ClassConstraintException("Wrong constant tag in '"+tostring(obj)+"'.");
    }
    // no indices to check
}
 
Example #15
Source Project: commons-bcel   Author: apache   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 #16
Source Project: commons-bcel   Author: apache   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 #17
Source Project: spotbugs   Author: spotbugs   File: BetterVisitor.java    License: GNU Lesser General Public License v2.1 4 votes vote down vote up
public void visit(ConstantInteger obj) {
    visit((Constant) obj);
}
 
Example #18
Source Project: spotbugs   Author: spotbugs   File: BetterVisitor.java    License: GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void visitConstantInteger(ConstantInteger obj) {
    visit(obj);
}
 
Example #19
Source Project: commons-bcel   Author: apache   File: StringRepresentation.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void visitConstantInteger(final ConstantInteger obj) {
    tostring = toString(obj);
}
 
Example #20
Source Project: commons-bcel   Author: apache   File: CounterVisitor.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void visitConstantInteger(final ConstantInteger obj)
{
    constantIntegerCount++;
}