com.sun.org.apache.bcel.internal.classfile.ConstantUtf8 Java Examples

The following examples show how to use com.sun.org.apache.bcel.internal.classfile.ConstantUtf8. 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: ConstantPoolGen.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Add a new Utf8 constant to the ConstantPool, if it is not already in there.
 *
 * @param n Utf8 string to add
 * @return index of entry
 */
public int addUtf8( final String n ) {
    int ret;
    if ((ret = lookupUtf8(n)) != -1) {
        return ret; // Already in CP
    }
    adjustSize();
    ret = index;
    constants[index++] = new ConstantUtf8(n);
    if (!utf8_table.containsKey(n)) {
        utf8_table.put(n, new Index(ret));
    }
    return ret;
}
 
Example #2
Source File: ClassElementValueGen.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public String getClassString()
{
    final ConstantUtf8 cu8 = (ConstantUtf8) getConstantPool().getConstant(idx);
    return cu8.getBytes();
    // ConstantClass c = (ConstantClass)getConstantPool().getConstant(idx);
    // ConstantUtf8 utf8 =
    // (ConstantUtf8)getConstantPool().getConstant(c.getNameIndex());
    // return utf8.getBytes();
}
 
Example #3
Source File: SimpleElementValueGen.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public String getValueString()
{
    if (super.getElementValueType() != STRING) {
        throw new RuntimeException(
                "Dont call getValueString() on a non STRING ElementValue");
    }
    final ConstantUtf8 c = (ConstantUtf8) getConstantPool().getConstant(idx);
    return c.getBytes();
}
 
Example #4
Source File: SimpleElementValueGen.java    From Bytecoder 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 RuntimeException(
            "SimpleElementValueGen class does not know how to stringify type " + super.getElementValueType());
    }
}
 
Example #5
Source File: EnumElementValueGen.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Override
public String stringifyValue()
{
    final ConstantUtf8 cu8 = (ConstantUtf8) getConstantPool().getConstant(valueIdx);
    return cu8.getBytes();
    // ConstantString cu8 =
    // (ConstantString)getConstantPool().getConstant(valueIdx);
    // return
    // ((ConstantUtf8)getConstantPool().getConstant(cu8.getStringIndex())).getBytes();
}
 
Example #6
Source File: EnumElementValueGen.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public String getEnumTypeString()
{
    // Constant cc = getConstantPool().getConstant(typeIdx);
    // ConstantClass cu8 =
    // (ConstantClass)getConstantPool().getConstant(typeIdx);
    // return
    // ((ConstantUtf8)getConstantPool().getConstant(cu8.getNameIndex())).getBytes();
    return ((ConstantUtf8) getConstantPool().getConstant(typeIdx))
            .getBytes();
    // return Utility.signatureToString(cu8.getBytes());
}
 
Example #7
Source File: EnumElementValueGen.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public String getEnumValueString()
{
    return ((ConstantUtf8) getConstantPool().getConstant(valueIdx)).getBytes();
    // ConstantString cu8 =
    // (ConstantString)getConstantPool().getConstant(valueIdx);
    // return
    // ((ConstantUtf8)getConstantPool().getConstant(cu8.getStringIndex())).getBytes();
}
 
Example #8
Source File: FieldOrMethod.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/** @return signature of referenced method/field.
 */
public String getSignature( final ConstantPoolGen cpg ) {
    final ConstantPool cp = cpg.getConstantPool();
    final ConstantCP cmr = (ConstantCP) cp.getConstant(super.getIndex());
    final ConstantNameAndType cnat = (ConstantNameAndType) cp.getConstant(cmr.getNameAndTypeIndex());
    return ((ConstantUtf8) cp.getConstant(cnat.getSignatureIndex())).getBytes();
}
 
Example #9
Source File: FieldOrMethod.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/** @return name of referenced method/field.
 */
public String getName( final ConstantPoolGen cpg ) {
    final ConstantPool cp = cpg.getConstantPool();
    final ConstantCP cmr = (ConstantCP) cp.getConstant(super.getIndex());
    final ConstantNameAndType cnat = (ConstantNameAndType) cp.getConstant(cmr.getNameAndTypeIndex());
    return ((ConstantUtf8) cp.getConstant(cnat.getNameIndex())).getBytes();
}
 
Example #10
Source File: Bug8003147Test.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void test() throws Exception {
    // Note: com.sun.org.apache.bcel.internal.classfile.JavaClass doesn't
    // support InvokeDynamic, so can't use lambda, also can't use string1 +
    // string2, because javac will generate a dynamic call where invoking
    // string1.concat(string2), so create a separate Bug8003147TestClass
    JAXPTestUtilities.tryRunWithTmpPermission(() -> {
        String classfile = getSystemProperty("test.classes") + "/parsers/Bug8003147TestClass.class";
        JavaClass jc = new ClassParser(classfile).parse();

        // rename class
        ConstantPool cp = jc.getConstantPool();
        int cpIndex = ((ConstantClass) cp.getConstant(jc.getClassNameIndex())).getNameIndex();
        cp.setConstant(cpIndex, new ConstantUtf8("parsers/Bug8003147TestClassPrime"));
        ClassGen gen = new ClassGen(jc);
        Method[] methods = jc.getMethods();
        int index;
        for (index = 0; index < methods.length; index++) {
            if (methods[index].getName().equals("doSomething")) {
                break;
            }
        }
        Method m = methods[index];
        MethodGen mg = new MethodGen(m, gen.getClassName(), gen.getConstantPool());
        gen.replaceMethod(m, mg.getMethod());
        String path = classfile.replace("Bug8003147TestClass", "Bug8003147TestClassPrime");
        gen.getJavaClass().dump(new FileOutputStream(path));

        try {
            Class.forName("parsers.Bug8003147TestClassPrime");
        } catch (ClassFormatError cfe) {
            cfe.printStackTrace();
            Assert.fail("modified version of class does not pass verification");
        }
    }, new FilePermission(getSystemProperty("test.classes") + "/-", "read,write"));
}
 
Example #11
Source File: ElementValuePairGen.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
public final String getNameString()
{
    // ConstantString cu8 = (ConstantString)cpool.getConstant(nameIdx);
    return ((ConstantUtf8) cpool.getConstant(nameIdx)).getBytes();
}
 
Example #12
Source File: NameSignatureInstruction.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/** @return signature of referenced method/field.
 */
public String getSignature(final ConstantPoolGen cpg) {
    final ConstantPool cp = cpg.getConstantPool();
    final ConstantNameAndType cnat = getNameAndType(cpg);
    return ((ConstantUtf8) cp.getConstant(cnat.getSignatureIndex())).getBytes();
}
 
Example #13
Source File: NameSignatureInstruction.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/** @return name of referenced method/field.
 */
public String getName(final ConstantPoolGen cpg) {
    final ConstantPool cp = cpg.getConstantPool();
    final ConstantNameAndType cnat = getNameAndType(cpg);
    return ((ConstantUtf8) cp.getConstant(cnat.getNameIndex())).getBytes();
}
 
Example #14
Source File: AnnotationEntryGen.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
public final String getTypeSignature() {
    // ConstantClass c = (ConstantClass)cpool.getConstant(typeIndex);
    final ConstantUtf8 utf8 = (ConstantUtf8) cpool
            .getConstant(typeIndex/* c.getNameIndex() */);
    return utf8.getBytes();
}