Java Code Examples for com.sun.org.apache.bcel.internal.classfile.Utility

The following examples show how to use com.sun.org.apache.bcel.internal.classfile.Utility. 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: Bytecoder   Source File: FieldGen.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Return string representation close to declaration format,
 * `public static final short MAX = 100', e.g..
 *
 * @return String representation of field
 */
@Override
public final String toString() {
    String name;
    String signature;
    String access; // Short cuts to constant pool
    access = Utility.accessToString(super.getAccessFlags());
    access = access.isEmpty() ? "" : (access + " ");
    signature = super.getType().toString();
    name = getName();
    final StringBuilder buf = new StringBuilder(32); // CHECKSTYLE IGNORE MagicNumber
    buf.append(access).append(signature).append(" ").append(name);
    final String value = getInitValue();
    if (value != null) {
        buf.append(" = ").append(value);
    }
    return buf.toString();
}
 
Example 2
Source Project: Bytecoder   Source File: MethodGen.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Return string representation close to declaration format,
 * `public static void main(String[]) throws IOException', e.g.
 *
 * @return String representation of the method.
 */
@Override
public final String toString() {
    final String access = Utility.accessToString(super.getAccessFlags());
    String signature = Type.getMethodSignature(super.getType(), arg_types);
    signature = Utility.methodSignatureToString(signature, super.getName(), access, true,
            getLocalVariableTable(super.getConstantPool()));
    final StringBuilder buf = new StringBuilder(signature);
    for (final Attribute a : getAttributes()) {
        if (!((a instanceof Code) || (a instanceof ExceptionTable))) {
            buf.append(" [").append(a).append("]");
        }
    }

    if (throws_vec.size() > 0) {
        for (final String throwsDescriptor : throws_vec) {
            buf.append("\n\t\tthrows ").append(throwsDescriptor);
        }
    }
    return buf.toString();
}
 
Example 3
Source Project: Bytecoder   Source File: Type.java    License: Apache License 2.0 6 votes vote down vote up
static int getTypeSize( final String signature ) throws StringIndexOutOfBoundsException {
    final byte type = Utility.typeOfSignature(signature);
    if (type <= Const.T_VOID) {
        return encode(BasicType.getType(type).getSize(), 1);
    } else if (type == Const.T_ARRAY) {
        int dim = 0;
        do { // Count dimensions
            dim++;
        } while (signature.charAt(dim) == '[');
        // Recurse, but just once, if the signature is ok
        final int consumed = consumed(getTypeSize(signature.substring(dim)));
        return encode(1, dim + consumed);
    } else { // type == T_REFERENCE
        final int index = signature.indexOf(';'); // Look for closing `;'
        if (index < 0) {
            throw new ClassFormatException("Invalid signature: " + signature);
        }
        return encode(1, index + 1);
    }
}
 
Example 4
Source Project: Bytecoder   Source File: MethodHTML.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Print field of class.
 *
 * @param field field to print
 * @throws java.io.IOException
 */
private void writeField( final Field field ) throws IOException {
    final String type = Utility.signatureToString(field.getSignature());
    final String name = field.getName();
    String access = Utility.accessToString(field.getAccessFlags());
    Attribute[] attributes;
    access = Utility.replace(access, " ", "&nbsp;");
    file.print("<TR><TD><FONT COLOR=\"#FF0000\">" + access + "</FONT></TD>\n<TD>"
            + Class2HTML.referenceType(type) + "</TD><TD><A NAME=\"field" + name + "\">" + name
            + "</A></TD>");
    attributes = field.getAttributes();
    // Write them to the Attributes.html file with anchor "<name>[<i>]"
    for (int i = 0; i < attributes.length; i++) {
        attribute_html.writeAttribute(attributes[i], name + "@" + i);
    }
    for (int i = 0; i < attributes.length; i++) {
        if (attributes[i].getTag() == Const.ATTR_CONSTANT_VALUE) { // Default value
            final String str = ((ConstantValue) attributes[i]).toString();
            // Reference attribute in _attributes.html
            file.print("<TD>= <A HREF=\"" + class_name + "_attributes.html#" + name + "@" + i
                    + "\" TARGET=\"Attributes\">" + str + "</TD>\n");
            break;
        }
    }
    file.println("</TR>");
}
 
Example 5
Source Project: Bytecoder   Source File: BCELFactory.java    License: Apache License 2.0 6 votes vote down vote up
private void createConstant( final Object value ) {
    String embed = value.toString();
    if (value instanceof String) {
        embed = '"' + Utility.convertString(embed) + '"';
    } else if (value instanceof Character) {
        embed = "(char)0x" + Integer.toHexString(((Character) value).charValue());
    } else if (value instanceof Float) {
        embed += "f";
    } else if (value instanceof Long) {
        embed += "L";
    } else if (value instanceof ObjectType) {
        final ObjectType ot = (ObjectType) value;
        embed = "new ObjectType(\""+ot.getClassName()+"\")";
    }

    _out.println("il.append(new PUSH(_cp, " + embed + "));");
}
 
Example 6
Source Project: Bytecoder   Source File: BCELifier.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void visitMethod( final Method method ) {
    final MethodGen mg = new MethodGen(method, _clazz.getClassName(), _cp);
    _out.println("    InstructionList il = new InstructionList();");
    _out.println("    MethodGen method = new MethodGen("
            + printFlags(method.getAccessFlags(), FLAGS.METHOD) + ", "
            + printType(mg.getReturnType()) + ", "
            + printArgumentTypes(mg.getArgumentTypes()) + ", "
            + "new String[] { " + Utility.printArray(mg.getArgumentNames(), false, true)
            + " }, \"" + method.getName() + "\", \"" + _clazz.getClassName() + "\", il, _cp);");
    _out.println();
    final BCELFactory factory = new BCELFactory(mg, _out);
    factory.start();
    _out.println("    method.setMaxStack();");
    _out.println("    method.setMaxLocals();");
    _out.println("    _cg.addMethod(method.getMethod());");
    _out.println("    il.dispose();");
}
 
Example 7
Source Project: Bytecoder   Source File: BCELifier.java    License: Apache License 2.0 6 votes vote down vote up
static String printType( final String signature ) {
    final Type type = Type.getType(signature);
    final byte t = type.getType();
    if (t <= Const.T_VOID) {
        return "Type." + Const.getTypeName(t).toUpperCase(Locale.ENGLISH);
    } else if (type.toString().equals("java.lang.String")) {
        return "Type.STRING";
    } else if (type.toString().equals("java.lang.Object")) {
        return "Type.OBJECT";
    } else if (type.toString().equals("java.lang.StringBuffer")) {
        return "Type.STRINGBUFFER";
    } else if (type instanceof ArrayType) {
        final ArrayType at = (ArrayType) type;
        return "new ArrayType(" + printType(at.getBasicType()) + ", " + at.getDimensions()
                + ")";
    } else {
        return "new ObjectType(\"" + Utility.signatureToString(signature, false) + "\")";
    }
}
 
Example 8
Source Project: R9000   Source File: BCELEncode.java    License: Eclipse Public License 2.0 5 votes vote down vote up
public static void main(String []args) throws Exception{
    //There also should be compiled class file,not java file
    Path path = Paths.get( "target/classes/MSFPayload.class");
    byte[] data = Files.readAllBytes( path);
    String s =  Utility.encode( data, true);
    System.out.println(s);
    testBCELEncode("$$BCEL$$"+ s );
}
 
Example 9
Source Project: jdk1.8-source-analysis   Source File: BCELFactory.java    License: Apache License 2.0 5 votes vote down vote up
private void createConstant(Object value) {
  String embed = value.toString();

  if(value instanceof String)
    embed = '"' + Utility.convertString(value.toString()) + '"';
  else if(value instanceof Character)
    embed = "(char)0x" + Integer.toHexString(((Character)value).charValue());

  _out.println("il.append(new PUSH(_cp, " + embed + "));");
}
 
Example 10
Source Project: TencentKona-8   Source File: BCELFactory.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void createConstant(Object value) {
  String embed = value.toString();

  if(value instanceof String)
    embed = '"' + Utility.convertString(value.toString()) + '"';
  else if(value instanceof Character)
    embed = "(char)0x" + Integer.toHexString(((Character)value).charValue());

  _out.println("il.append(new PUSH(_cp, " + embed + "));");
}
 
Example 11
Source Project: jdk8u60   Source File: BCELFactory.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void createConstant(Object value) {
  String embed = value.toString();

  if(value instanceof String)
    embed = '"' + Utility.convertString(value.toString()) + '"';
  else if(value instanceof Character)
    embed = "(char)0x" + Integer.toHexString(((Character)value).charValue());

  _out.println("il.append(new PUSH(_cp, " + embed + "));");
}
 
Example 12
Source Project: JDKSourceCode1.8   Source File: BCELFactory.java    License: MIT License 5 votes vote down vote up
private void createConstant(Object value) {
  String embed = value.toString();

  if(value instanceof String)
    embed = '"' + Utility.convertString(value.toString()) + '"';
  else if(value instanceof Character)
    embed = "(char)0x" + Integer.toHexString(((Character)value).charValue());

  _out.println("il.append(new PUSH(_cp, " + embed + "));");
}
 
Example 13
Source Project: openjdk-jdk8u   Source File: BCELFactory.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void createConstant(Object value) {
  String embed = value.toString();

  if(value instanceof String)
    embed = '"' + Utility.convertString(value.toString()) + '"';
  else if(value instanceof Character)
    embed = "(char)0x" + Integer.toHexString(((Character)value).charValue());

  _out.println("il.append(new PUSH(_cp, " + embed + "));");
}
 
Example 14
private void createConstant(Object value) {
  String embed = value.toString();

  if(value instanceof String)
    embed = '"' + Utility.convertString(value.toString()) + '"';
  else if(value instanceof Character)
    embed = "(char)0x" + Integer.toHexString(((Character)value).charValue());

  _out.println("il.append(new PUSH(_cp, " + embed + "));");
}
 
Example 15
Source Project: Bytecoder   Source File: Type.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Convert signature to a Type object.
 * @param signature signature string such as Ljava/lang/String;
 * @return type object
 */
// @since 6.0 no longer final
public static Type getType( final String signature ) throws StringIndexOutOfBoundsException {
    final byte type = Utility.typeOfSignature(signature);
    if (type <= Const.T_VOID) {
        //corrected concurrent private static field acess
        wrap(consumed_chars, 1);
        return BasicType.getType(type);
    } else if (type == Const.T_ARRAY) {
        int dim = 0;
        do { // Count dimensions
            dim++;
        } while (signature.charAt(dim) == '[');
        // Recurse, but just once, if the signature is ok
        final Type t = getType(signature.substring(dim));
        //corrected concurrent private static field acess
        //  consumed_chars += dim; // update counter - is replaced by
        final int _temp = unwrap(consumed_chars) + dim;
        wrap(consumed_chars, _temp);
        return new ArrayType(t, dim);
    } else { // type == T_REFERENCE
        // Utility.signatureToString understands how to parse
        // generic types.
        final String parsedSignature = Utility.signatureToString(signature, false);
        wrap(consumed_chars, parsedSignature.length() + 2); // "Lblabla;" `L' and `;' are removed
        return ObjectType.getInstance(parsedSignature.replace('/', '.'));
    }
}
 
Example 16
Source Project: Bytecoder   Source File: Class2HTML.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Utility method that converts a class reference in the constant pool,
 * i.e., an index to a string.
 */
static String referenceClass(final int index) {
    String str = constant_pool.getConstantString(index, Const.CONSTANT_Class);
    str = Utility.compactClassName(str);
    str = Utility.compactClassName(str, class_package + ".", true);
    return "<A HREF=\"" + class_name + "_cp.html#cp" + index + "\" TARGET=ConstantPool>" + str
            + "</A>";
}
 
Example 17
Source Project: Bytecoder   Source File: Class2HTML.java    License: Apache License 2.0 5 votes vote down vote up
static String referenceType( final String type ) {
    String short_type = Utility.compactClassName(type);
    short_type = Utility.compactClassName(short_type, class_package + ".", true);
    final int index = type.indexOf('['); // Type is an array?
    String base_type = type;
    if (index > -1) {
        base_type = type.substring(0, index); // Tack of the `['
    }
    // test for basic type
    if (basic_types.contains(base_type)) {
        return "<FONT COLOR=\"#00FF00\">" + type + "</FONT>";
    }
    return "<A HREF=\"" + base_type + ".html\" TARGET=_top>" + short_type + "</A>";
}
 
Example 18
Source Project: openjdk-jdk9   Source File: BCELFactory.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void createConstant(Object value) {
  String embed = value.toString();

  if(value instanceof String)
    embed = '"' + Utility.convertString(value.toString()) + '"';
  else if(value instanceof Character)
    embed = "(char)0x" + Integer.toHexString(((Character)value).charValue());

  _out.println("il.append(new PUSH(_cp, " + embed + "));");
}
 
Example 19
Source Project: hottub   Source File: BCELFactory.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void createConstant(Object value) {
  String embed = value.toString();

  if(value instanceof String)
    embed = '"' + Utility.convertString(value.toString()) + '"';
  else if(value instanceof Character)
    embed = "(char)0x" + Integer.toHexString(((Character)value).charValue());

  _out.println("il.append(new PUSH(_cp, " + embed + "));");
}
 
Example 20
Source Project: openjdk-8-source   Source File: BCELFactory.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void createConstant(Object value) {
  String embed = value.toString();

  if(value instanceof String)
    embed = '"' + Utility.convertString(value.toString()) + '"';
  else if(value instanceof Character)
    embed = "(char)0x" + Integer.toHexString(((Character)value).charValue());

  _out.println("il.append(new PUSH(_cp, " + embed + "));");
}
 
Example 21
Source Project: openjdk-8   Source File: BCELFactory.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void createConstant(Object value) {
  String embed = value.toString();

  if(value instanceof String)
    embed = '"' + Utility.convertString(value.toString()) + '"';
  else if(value instanceof Character)
    embed = "(char)0x" + Integer.toHexString(((Character)value).charValue());

  _out.println("il.append(new PUSH(_cp, " + embed + "));");
}
 
Example 22
Source Project: jdk1.8-source-analysis   Source File: InstructionHandle.java    License: Apache License 2.0 4 votes vote down vote up
/** @return a (verbose) string representation of the contained instruction.
 */
public String toString(boolean verbose) {
  return Utility.format(i_position, 4, false, ' ') + ": " + instruction.toString(verbose);
}
 
Example 23
/** @return a (verbose) string representation of the contained instruction.
 */
public String toString(boolean verbose) {
  return Utility.format(i_position, 4, false, ' ') + ": " + instruction.toString(verbose);
}
 
Example 24
Source Project: jdk8u60   Source File: InstructionHandle.java    License: GNU General Public License v2.0 4 votes vote down vote up
/** @return a (verbose) string representation of the contained instruction.
 */
public String toString(boolean verbose) {
  return Utility.format(i_position, 4, false, ' ') + ": " + instruction.toString(verbose);
}
 
Example 25
Source Project: JDKSourceCode1.8   Source File: InstructionHandle.java    License: MIT License 4 votes vote down vote up
/** @return a (verbose) string representation of the contained instruction.
 */
public String toString(boolean verbose) {
  return Utility.format(i_position, 4, false, ' ') + ": " + instruction.toString(verbose);
}
 
Example 26
/** @return a (verbose) string representation of the contained instruction.
 */
public String toString(boolean verbose) {
  return Utility.format(i_position, 4, false, ' ') + ": " + instruction.toString(verbose);
}
 
Example 27
/** @return a (verbose) string representation of the contained instruction.
 */
public String toString(boolean verbose) {
  return Utility.format(i_position, 4, false, ' ') + ": " + instruction.toString(verbose);
}
 
Example 28
Source Project: Bytecoder   Source File: InstructionHandle.java    License: Apache License 2.0 4 votes vote down vote up
/** @return a (verbose) string representation of the contained instruction.
 */
public String toString( final boolean verbose ) {
    return Utility.format(i_position, 4, false, ' ') + ": " + instruction.toString(verbose);
}
 
Example 29
Source Project: Bytecoder   Source File: Type.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * @return Type string, e.g. `int[]'
 */
@Override
public String toString() {
    return ((this.equals(Type.NULL) || (type >= Const.T_UNKNOWN))) ? signature : Utility
            .signatureToString(signature, false);
}
 
Example 30
Source Project: Bytecoder   Source File: MethodHTML.java    License: Apache License 2.0 4 votes vote down vote up
private void writeMethod( final Method method, final int method_number ) {
    // Get raw signature
    final String signature = method.getSignature();
    // Get array of strings containing the argument types
    final String[] args = Utility.methodSignatureArgumentTypes(signature, false);
    // Get return type string
    final String type = Utility.methodSignatureReturnType(signature, false);
    // Get method name
    final String name = method.getName();
    String html_name;
    // Get method's access flags
    String access = Utility.accessToString(method.getAccessFlags());
    // Get the method's attributes, the Code Attribute in particular
    final Attribute[] attributes = method.getAttributes();
    /* HTML doesn't like names like <clinit> and spaces are places to break
     * lines. Both we don't want...
     */
    access = Utility.replace(access, " ", "&nbsp;");
    html_name = Class2HTML.toHTML(name);
    file.print("<TR VALIGN=TOP><TD><FONT COLOR=\"#FF0000\"><A NAME=method" + method_number
            + ">" + access + "</A></FONT></TD>");
    file.print("<TD>" + Class2HTML.referenceType(type) + "</TD><TD>" + "<A HREF=" + class_name
            + "_code.html#method" + method_number + " TARGET=Code>" + html_name
            + "</A></TD>\n<TD>(");
    for (int i = 0; i < args.length; i++) {
        file.print(Class2HTML.referenceType(args[i]));
        if (i < args.length - 1) {
            file.print(", ");
        }
    }
    file.print(")</TD></TR>");
    // Check for thrown exceptions
    for (int i = 0; i < attributes.length; i++) {
        attribute_html.writeAttribute(attributes[i], "method" + method_number + "@" + i,
                method_number);
        final byte tag = attributes[i].getTag();
        if (tag == Const.ATTR_EXCEPTIONS) {
            file.print("<TR VALIGN=TOP><TD COLSPAN=2></TD><TH ALIGN=LEFT>throws</TH><TD>");
            final int[] exceptions = ((ExceptionTable) attributes[i]).getExceptionIndexTable();
            for (int j = 0; j < exceptions.length; j++) {
                file.print(constant_html.referenceConstant(exceptions[j]));
                if (j < exceptions.length - 1) {
                    file.print(", ");
                }
            }
            file.println("</TD></TR>");
        } else if (tag == Const.ATTR_CODE) {
            final Attribute[] c_a = ((Code) attributes[i]).getAttributes();
            for (int j = 0; j < c_a.length; j++) {
                attribute_html.writeAttribute(c_a[j], "method" + method_number + "@" + i + "@"
                        + j, method_number);
            }
        }
    }
}