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

The following examples show how to use com.sun.org.apache.bcel.internal.classfile.Utility. 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: BCELifier.java    From Bytecoder with 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 #2
Source File: BCELifier.java    From Bytecoder with 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 #3
Source File: BCELFactory.java    From Bytecoder with 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 #4
Source File: MethodHTML.java    From Bytecoder with 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 File: Type.java    From Bytecoder with 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 #6
Source File: FieldGen.java    From Bytecoder with 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 #7
Source File: MethodGen.java    From Bytecoder with 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 #8
Source File: Type.java    From Bytecoder with 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 #9
Source File: BCELFactory.java    From openjdk-8 with 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 #10
Source File: BCELFactory.java    From openjdk-8-source with 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 File: BCELFactory.java    From hottub with 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 File: BCELFactory.java    From openjdk-jdk9 with 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 #13
Source File: Class2HTML.java    From Bytecoder with 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 #14
Source File: Class2HTML.java    From Bytecoder with 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 #15
Source File: BCELEncode.java    From R9000 with 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 #16
Source File: BCELFactory.java    From openjdk-jdk8u-backup with 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 #17
Source File: BCELFactory.java    From openjdk-jdk8u with 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 #18
Source File: BCELFactory.java    From jdk1.8-source-analysis with 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 #19
Source File: BCELFactory.java    From JDKSourceCode1.8 with 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 #20
Source File: BCELFactory.java    From jdk8u60 with 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 File: BCELFactory.java    From TencentKona-8 with 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 File: InstructionHandle.java    From jdk1.8-source-analysis with 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
Source File: InstructionHandle.java    From Bytecoder with 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 #24
Source File: InstructionHandle.java    From openjdk-8 with 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 File: InstructionHandle.java    From openjdk-8-source with 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 #26
Source File: InstructionHandle.java    From TencentKona-8 with 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 #27
Source File: InstructionHandle.java    From hottub with 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 #28
Source File: InstructionHandle.java    From openjdk-jdk9 with 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 #29
Source File: InstructionHandle.java    From jdk8u60 with 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 #30
Source File: BCELifier.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
@Override
public void visitJavaClass( final JavaClass clazz ) {
    String class_name = clazz.getClassName();
    final String super_name = clazz.getSuperclassName();
    final String package_name = clazz.getPackageName();
    final String inter = Utility.printArray(clazz.getInterfaceNames(), false, true);
    if (!"".equals(package_name)) {
        class_name = class_name.substring(package_name.length() + 1);
        _out.println("package " + package_name + ";");
        _out.println();
    }
    _out.println("import " + BASE_PACKAGE + ".generic.*;");
    _out.println("import " + BASE_PACKAGE + ".classfile.*;");
    _out.println("import " + BASE_PACKAGE + ".*;");
    _out.println("import java.io.*;");
    _out.println();
    _out.println("public class " + class_name + "Creator {");
    _out.println("  private InstructionFactory _factory;");
    _out.println("  private ConstantPoolGen    _cp;");
    _out.println("  private ClassGen           _cg;");
    _out.println();
    _out.println("  public " + class_name + "Creator() {");
    _out.println("    _cg = new ClassGen(\""
            + (("".equals(package_name)) ? class_name : package_name + "." + class_name)
            + "\", \"" + super_name + "\", " + "\"" + clazz.getSourceFileName() + "\", "
            + printFlags(clazz.getAccessFlags(), FLAGS.CLASS) + ", "
            + "new String[] { " + inter + " });");
    _out.println();
    _out.println("    _cp = _cg.getConstantPool();");
    _out.println("    _factory = new InstructionFactory(_cg, _cp);");
    _out.println("  }");
    _out.println();
    printCreate();
    final Field[] fields = clazz.getFields();
    if (fields.length > 0) {
        _out.println("  private void createFields() {");
        _out.println("    FieldGen field;");
        for (final Field field : fields) {
            field.accept(this);
        }
        _out.println("  }");
        _out.println();
    }
    final Method[] methods = clazz.getMethods();
    for (int i = 0; i < methods.length; i++) {
        _out.println("  private void createMethod_" + i + "() {");
        methods[i].accept(this);
        _out.println("  }");
        _out.println();
    }
    printMain();
    _out.println("}");
}