Java Code Examples for org.apache.bcel.Const#CONSTANT_NameAndType
The following examples show how to use
org.apache.bcel.Const#CONSTANT_NameAndType .
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 | 5 votes |
@Override public void visitConstantNameAndType(final ConstantNameAndType obj) { if (obj.getTag() != Const.CONSTANT_NameAndType) { throw new ClassConstraintException("Wrong constant tag in '"+tostring(obj)+"'."); } checkIndex(obj, obj.getNameIndex(), CONST_Utf8); //checkIndex(obj, obj.getDescriptorIndex(), CONST_Utf8); //inconsistently named in BCEL, see below. checkIndex(obj, obj.getSignatureIndex(), CONST_Utf8); }
Example 2
Source File: ClassModel.java From aparapi with Apache License 2.0 | 4 votes |
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 3
Source File: ConstantPool.java From commons-bcel with Apache License 2.0 | 4 votes |
/** * 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 4
Source File: Constant.java From commons-bcel with Apache License 2.0 | 4 votes |
/** * 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); } }
Example 5
Source File: ConstantNameAndType.java From commons-bcel with Apache License 2.0 | 4 votes |
/** * @param nameIndex Name of field/method * @param signatureIndex and its signature */ public ConstantNameAndType(final int nameIndex, final int signatureIndex) { super(Const.CONSTANT_NameAndType); this.nameIndex = nameIndex; this.signatureIndex = signatureIndex; }