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

The following examples show how to use org.apache.bcel.Const#CONSTANT_Long . 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: ConstantPool.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
/**
 * Reads constants from given input stream.
 *
 * @param input Input stream
 * @throws IOException
 * @throws ClassFormatException
 */
public ConstantPool(final DataInput input) throws IOException, ClassFormatException {
    byte tag;
    final int constant_pool_count = input.readUnsignedShort();
    constantPool = new Constant[constant_pool_count];
    /* constantPool[0] is unused by the compiler and may be used freely
     * by the implementation.
     */
    for (int i = 1; i < constant_pool_count; i++) {
        constantPool[i] = Constant.readConstant(input);
        /* Quote from the JVM specification:
         * "All eight byte constants take up two spots in the constant pool.
         * If this is the n'th byte in the constant pool, then the next item
         * will be numbered n+2"
         *
         * Thus we have to increment the index counter.
         */
        tag = constantPool[i].getTag();
        if ((tag == Const.CONSTANT_Double) || (tag == Const.CONSTANT_Long)) {
            i++;
        }
    }
}
 
Example 2
Source File: PreorderVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void visitConstantPool(ConstantPool obj) {
    super.visitConstantPool(obj);
    Constant[] constant_pool = obj.getConstantPool();
    for (int i = 1; i < constant_pool.length; i++) {
        constant_pool[i].accept(this);
        byte tag = constant_pool[i].getTag();
        if ((tag == Const.CONSTANT_Double) || (tag == Const.CONSTANT_Long)) {
            i++;
        }
    }
}
 
Example 3
Source File: ClassDumper.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * Processes constant pool entries.
 * @throws  IOException
 * @throws  ClassFormatException
 */
private final void processConstantPool () throws IOException, ClassFormatException {
    byte tag;
    final int constant_pool_count = file.readUnsignedShort();
    constant_items = new Constant[constant_pool_count];
    constant_pool = new ConstantPool(constant_items);

    // constant_pool[0] is unused by the compiler
    System.out.printf("%nConstant pool(%d):%n", constant_pool_count - 1);

    for (int i = 1; i < constant_pool_count; i++) {
        constant_items[i] = Constant.readConstant(file);
        // i'm sure there is a better way to do this
        if (i < 10) {
            System.out.printf("    #%1d = ", i);
        } else if (i <100) {
            System.out.printf("   #%2d = ", i);
        } else {
            System.out.printf("  #%d = ", i);
        }
        System.out.println(constant_items[i]);

        // All eight byte constants take up two spots in the constant pool
        tag = constant_items[i].getTag();
        if ((tag == Const.CONSTANT_Double) ||
                (tag == Const.CONSTANT_Long)) {
            i++;
        }
    }
}
 
Example 4
Source File: ConstantValue.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * @return String representation of constant value.
 */
@Override
public String toString() {
    Constant c = super.getConstantPool().getConstant(constantValueIndex);
    String buf;
    int i;
    // Print constant to string depending on its type
    switch (c.getTag()) {
        case Const.CONSTANT_Long:
            buf = String.valueOf(((ConstantLong) c).getBytes());
            break;
        case Const.CONSTANT_Float:
            buf = String.valueOf(((ConstantFloat) c).getBytes());
            break;
        case Const.CONSTANT_Double:
            buf = String.valueOf(((ConstantDouble) c).getBytes());
            break;
        case Const.CONSTANT_Integer:
            buf = String.valueOf(((ConstantInteger) c).getBytes());
            break;
        case Const.CONSTANT_String:
            i = ((ConstantString) c).getStringIndex();
            c = super.getConstantPool().getConstant(i, Const.CONSTANT_Utf8);
            buf = "\"" + Utility.convertString(((ConstantUtf8) c).getBytes()) + "\"";
            break;
        default:
            throw new IllegalStateException("Type of ConstValue invalid: " + c);
    }
    return buf;
}
 
Example 5
Source File: Pass2Verifier.java    From commons-bcel with 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 6
Source File: ClassModel.java    From aparapi with Apache License 2.0 4 votes vote down vote up
public ConstantPool(JavaClass cls) {
    org.apache.bcel.classfile.ConstantPool cp = cls.getConstantPool();
   final int size = cp.getLength();
   add(new EmptyEntry(0)); // slot 0

   for (int i = 1; i < size; i++) {
      final Constant constant = cp.getConstant(i);
      if (constant != null) {
          final byte tag = constant.getTag();
          switch (tag) {
              case Const.CONSTANT_Class:
                  final int ni = ((ConstantClass) constant).getNameIndex();
                  add(new ClassEntry(ni, i));
                  break;
              case Const.CONSTANT_String:
                  final int si = ((ConstantString) constant).getStringIndex();
                  add(new StringEntry(si, i));
                  break;
              case Const.CONSTANT_Utf8:
                  add(new UTF8Entry(((ConstantUtf8) constant).getBytes(), i));
                  break;
              case Const.CONSTANT_Double:
                  add(new DoubleEntry(((ConstantDouble) constant).getBytes(), i));
                  break;
              case Const.CONSTANT_Float:
                  add(new FloatEntry(((ConstantFloat) constant).getBytes(), i));
                  break;
              case Const.CONSTANT_Long:
                  add(new LongEntry(((ConstantLong) constant).getBytes(), i));
                  break;
              case Const.CONSTANT_Integer:
                  add(new IntegerEntry(((ConstantInteger) constant).getBytes(), i));
                  break;
              case Const.CONSTANT_NameAndType:
                  final int ntInd = ((ConstantNameAndType) constant).getNameIndex();
                  final int siNT = ((ConstantNameAndType) constant).getSignatureIndex();
                  add(new NameAndTypeEntry(ntInd, siNT, i));
                  break;
              case Const.CONSTANT_InterfaceMethodref:
                  final int refClassIndexIMR = ((ConstantCP) constant).getClassIndex();
                  final int nameAndTypeIndIMR = ((ConstantCP) constant).getNameAndTypeIndex();
                  add(new InterfaceMethodEntry(refClassIndexIMR, nameAndTypeIndIMR, i));
                  break;
              case Const.CONSTANT_Methodref:
                  final int refClassIndexM = ((ConstantCP) constant).getClassIndex();
                  final int nameAndTypeIndM = ((ConstantCP) constant).getNameAndTypeIndex();
                  add(new MethodEntry(refClassIndexM, nameAndTypeIndM, i));
                  break;
              case Const.CONSTANT_Fieldref:
                  final int refClassIndex = ((ConstantCP) constant).getClassIndex();
                  final int nameAndTypeInd = ((ConstantCP) constant).getNameAndTypeIndex();
                  add(new FieldEntry(refClassIndex, nameAndTypeInd, i));
                  break;
              case Const.CONSTANT_MethodHandle:
                  final ConstantMethodHandle cmh = (ConstantMethodHandle) constant;
                  final int refKind = cmh.getReferenceKind();
                  final int refInd = cmh.getReferenceIndex();
                  add(new MethodHandleEntry(refKind, refInd, i));
                  break;
              case Const.CONSTANT_MethodType:
                  final ConstantMethodType cmt = (ConstantMethodType) constant;
                  final int descInd = cmt.getDescriptorIndex();
                  add(new MethodTypeEntry(descInd, i));
                  break;
              case Const.CONSTANT_InvokeDynamic:
                  final ConstantInvokeDynamic cid = (ConstantInvokeDynamic) constant;
                  final int bsMethAttInd = cid.getBootstrapMethodAttrIndex();
                  final int ntIndID = cid.getNameAndTypeIndex();
                  add(new InvokeDynamicEntry(bsMethAttInd, ntIndID, i));
                  break;
              default:
                  System.out.printf("slot %04x unexpected Constant constantPoolType = %s\n", i, constant);
          }
      } else {
          add(new EmptyEntry(i));
      }
   }
}
 
Example 7
Source File: ConstantLong.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
/**
 * @param bytes Data
 */
public ConstantLong(final long bytes) {
    super(Const.CONSTANT_Long);
    this.bytes = bytes;
}
 
Example 8
Source File: ConstantPool.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
/**
 * Resolves constant to a string representation.
 *
 * @param  c Constant to be printed
 * @return String representation
 */
public String constantToString( Constant c ) throws ClassFormatException {
    String str;
    int i;
    final byte tag = c.getTag();
    switch (tag) {
        case Const.CONSTANT_Class:
            i = ((ConstantClass) c).getNameIndex();
            c = getConstant(i, Const.CONSTANT_Utf8);
            str = Utility.compactClassName(((ConstantUtf8) c).getBytes(), false);
            break;
        case Const.CONSTANT_String:
            i = ((ConstantString) c).getStringIndex();
            c = getConstant(i, Const.CONSTANT_Utf8);
            str = "\"" + escape(((ConstantUtf8) c).getBytes()) + "\"";
            break;
        case Const.CONSTANT_Utf8:
            str = ((ConstantUtf8) c).getBytes();
            break;
        case Const.CONSTANT_Double:
            str = String.valueOf(((ConstantDouble) c).getBytes());
            break;
        case Const.CONSTANT_Float:
            str = String.valueOf(((ConstantFloat) c).getBytes());
            break;
        case Const.CONSTANT_Long:
            str = String.valueOf(((ConstantLong) c).getBytes());
            break;
        case Const.CONSTANT_Integer:
            str = String.valueOf(((ConstantInteger) c).getBytes());
            break;
        case Const.CONSTANT_NameAndType:
            str = constantToString(((ConstantNameAndType) c).getNameIndex(),
                    Const.CONSTANT_Utf8)
                    + " " + constantToString(((ConstantNameAndType) c).getSignatureIndex(),
                    Const.CONSTANT_Utf8);
            break;
        case Const.CONSTANT_InterfaceMethodref:
        case Const.CONSTANT_Methodref:
        case Const.CONSTANT_Fieldref:
            str = constantToString(((ConstantCP) c).getClassIndex(), Const.CONSTANT_Class)
                    + "." + constantToString(((ConstantCP) c).getNameAndTypeIndex(),
                    Const.CONSTANT_NameAndType);
            break;
        case Const.CONSTANT_MethodHandle:
            // Note that the ReferenceIndex may point to a Fieldref, Methodref or
            // InterfaceMethodref - so we need to peek ahead to get the actual type.
            final ConstantMethodHandle cmh = (ConstantMethodHandle) c;
            str = Const.getMethodHandleName(cmh.getReferenceKind())
                    + " " + constantToString(cmh.getReferenceIndex(),
                    getConstant(cmh.getReferenceIndex()).getTag());
            break;
        case Const.CONSTANT_MethodType:
            final ConstantMethodType cmt = (ConstantMethodType) c;
            str = constantToString(cmt.getDescriptorIndex(), Const.CONSTANT_Utf8);
            break;
        case Const.CONSTANT_InvokeDynamic:
            final ConstantInvokeDynamic cid = (ConstantInvokeDynamic) c;
            str = cid.getBootstrapMethodAttrIndex()
                    + ":" + constantToString(cid.getNameAndTypeIndex(),
                    Const.CONSTANT_NameAndType);
            break;
        case Const.CONSTANT_Module:
            i = ((ConstantModule) c).getNameIndex();
            c = getConstant(i, Const.CONSTANT_Utf8);
            str = Utility.compactClassName(((ConstantUtf8) c).getBytes(), false);
            break;
        case Const.CONSTANT_Package:
            i = ((ConstantPackage) c).getNameIndex();
            c = getConstant(i, Const.CONSTANT_Utf8);
            str = Utility.compactClassName(((ConstantUtf8) c).getBytes(), false);
            break;
        default: // Never reached
            throw new IllegalArgumentException("Unknown constant type " + tag);
    }
    return str;
}
 
Example 9
Source File: Constant.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
/**
 * Reads one constant from the given input, the type depends on a tag byte.
 *
 * @param dataInput Input stream
 * @return Constant object
 * @throws IOException if an I/O error occurs reading from the given {@code dataInput}.
 * @throws ClassFormatException if the next byte is not recognized
 * @since 6.0 made public
 */
public static Constant readConstant(final DataInput dataInput) throws IOException, ClassFormatException {
    final byte b = dataInput.readByte(); // Read tag byte
    switch (b) {
    case Const.CONSTANT_Class:
        return new ConstantClass(dataInput);
    case Const.CONSTANT_Fieldref:
        return new ConstantFieldref(dataInput);
    case Const.CONSTANT_Methodref:
        return new ConstantMethodref(dataInput);
    case Const.CONSTANT_InterfaceMethodref:
        return new ConstantInterfaceMethodref(dataInput);
    case Const.CONSTANT_String:
        return new ConstantString(dataInput);
    case Const.CONSTANT_Integer:
        return new ConstantInteger(dataInput);
    case Const.CONSTANT_Float:
        return new ConstantFloat(dataInput);
    case Const.CONSTANT_Long:
        return new ConstantLong(dataInput);
    case Const.CONSTANT_Double:
        return new ConstantDouble(dataInput);
    case Const.CONSTANT_NameAndType:
        return new ConstantNameAndType(dataInput);
    case Const.CONSTANT_Utf8:
        return ConstantUtf8.getInstance(dataInput);
    case Const.CONSTANT_MethodHandle:
        return new ConstantMethodHandle(dataInput);
    case Const.CONSTANT_MethodType:
        return new ConstantMethodType(dataInput);
    case Const.CONSTANT_Dynamic:
        return new ConstantDynamic(dataInput);
    case Const.CONSTANT_InvokeDynamic:
        return new ConstantInvokeDynamic(dataInput);
    case Const.CONSTANT_Module:
        return new ConstantModule(dataInput);
    case Const.CONSTANT_Package:
        return new ConstantPackage(dataInput);
    default:
        throw new ClassFormatException("Invalid byte tag in constant pool: " + b);
    }
}