Package org.apache.bcel.classfile

Examples of org.apache.bcel.classfile.ConstantUtf8


   * "<em>replacement</em>" in all Utf8 constants
   */
  private static void patchIt(String old, String replacement,
            Constant[] constant_pool)
  {
    ConstantUtf8 c;
    String       str;
    int          index, old_index;
    StringBuffer buf;

    /* Loop through constant pool
     */
    for(short i=0; i < constant_pool.length; i++) {
      if(constant_pool[i] instanceof ConstantUtf8) { // Utf8 string found
  try {
    c   = (ConstantUtf8)constant_pool[i]; // Get the string
    str = c.getBytes();
   
    if((index = str.indexOf(old)) != -1) { // `old' found in str
      buf       = new StringBuffer();      // target buffer
      old_index = 0;                       // String start offset
     
      // While we have something to replace
      while((index = str.indexOf(old, old_index)) != -1) {
        buf.append(str.substring(old_index, index)); // append prefix
        buf.append(replacement);               // append `replacement'
       
        old_index = index + old.length(); // Skip `old'.length chars
      }

      buf.append(str.substring(old_index)); // append rest of string
      str = buf.toString();

      // Finally push the new string back to the constant pool
      c = new ConstantUtf8(str);
      constant_pool[i] = c;
    }
  } catch(StringIndexOutOfBoundsException e) { // Should not occur
    System.err.println(e);
  }
View Full Code Here


    StringBuffer buf = new StringBuffer();

    for(int idx = 0; idx < pool.getLength(); idx++) {
      Constant c = pool.getConstant(idx);
      if(c != null && c.getTag() == Constants.CONSTANT_Class) {
        ConstantUtf8 c1 = (ConstantUtf8) pool.getConstant(((ConstantClass)c).getNameIndex());
        buf.setLength(0);
        buf.append(c1.getBytes());
        for(int n = 0; n < buf.length(); n++) {
          if(buf.charAt(n) == '/') {
            buf.setCharAt(n, '.');
        }
  }
View Full Code Here

  private static void helloifyClassName(JavaClass java_class ) {
    class_name = java_class.getClassName() + "_hello";
    int index = java_class.getClassNameIndex();
   
    index = ((ConstantClass)cp.getConstant(index)).getNameIndex();
    cp.setConstant(index, new ConstantUtf8(class_name.replace('.', '/')));
  }
View Full Code Here

                FieldInstruction field = (FieldInstruction) i;
                Constant c = m.getConstantPool().getConstant(field.getIndex());
                ConstantFieldref fieldRef = (ConstantFieldref) c;

                ConstantClass cclass = (ConstantClass) m.getConstantPool().getConstant(fieldRef.getClassIndex());
                ConstantUtf8 utfClassName = (ConstantUtf8) m.getConstantPool().getConstant(cclass.getNameIndex());
                String utfClassNameString = StringUtils.replaceAll(utfClassName.getBytes().toString(), "/", ".");
                JavaClass fieldJavaClass = null;
                try
                {
                    fieldJavaClass = Repository.lookupClass(utfClassNameString);
                }
View Full Code Here

        Constant c = cp.getConstant(((CPInstruction) ih.getInstruction()).getIndex());
        if (c instanceof ConstantFieldref)
        {
            ConstantFieldref fieldRef = (ConstantFieldref) c;
            ConstantClass cclass = (ConstantClass) cp.getConstant(fieldRef.getClassIndex());
            ConstantUtf8 className = (ConstantUtf8) cp.getConstant(cclass.getNameIndex());
            return StringUtils.replaceAll(className.getBytes().toString(), "/", ".");
        }
        return null;
    }
View Full Code Here

      {
         Constant oConstant = oConstantPool.getConstant( i );

         if ( oConstant instanceof ConstantClass )
         {
            ConstantUtf8 oConstantUtf8
               =  ( ConstantUtf8 )
                     oConstantPool
                        .getConstant( ( ( ConstantClass ) oConstant ).getNameIndex() );

            String sReferencedClassname = oConstantUtf8.getBytes().replace( '/', '.' );

            // only consider the class if it is not an array, since classloaders do not
            // handle arrays, the JVM does this directly
            if ( ! sReferencedClassname.startsWith( "[" ))
            {
View Full Code Here

    {
        int index = cg.getClassNameIndex();

        ConstantPoolGen cp = cg.getConstantPool();
        index = ((ConstantClass)cp.getConstant(index)).getNameIndex();
        cp.setConstant(index, new ConstantUtf8(className.replace('.', '/')));
    }
View Full Code Here

        int length = constantPool.getLength();
        for (int i = 0; i < length; i++) {
            Constant constant = constantPool.getConstant(i);
            if (constant instanceof ConstantClass) {
                ConstantClass cc = (ConstantClass) constant;
                ConstantUtf8 constant2 = (ConstantUtf8) constantPool.getConstant(cc.getNameIndex());

                // In case a subclass fails, skip, but print warning.
                try {
                    String toLoad = constant2.getBytes().replace('/', '.');
                    if (toLoad.contains("[")) continue;
                    Class<?> forName = Class.forName(toLoad);
                    rval.add(forName);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
View Full Code Here

        {
            Constant constant = cpool.getConstant(i);
            switch (constant.getTag())
            {
            case Constants.CONSTANT_Utf8:
                ConstantUtf8 cstring = (ConstantUtf8)constant;
                String bytes = cstring.getBytes();
                String newbytes = (String) strings.get(bytes);
                if (newbytes != null)
                    cstring.setBytes(newbytes);
                break;
            case Constants.CONSTANT_Double:
            case Constants.CONSTANT_Long:
                i++;
                break;
View Full Code Here

                result.append(i);
                result.append(" = ");

                // Append a constant specific string.
                if (constant instanceof ConstantUtf8) {
                    ConstantUtf8 c = (ConstantUtf8) constant;
                    result.append("Asciz\t");
                    result.append(constPool.constantToString(c));
                    result.append(';');
                } else if (constant instanceof ConstantFieldref) {
                    ConstantFieldref c = (ConstantFieldref) constant;
                    result.append("Field\t#");
                    result.append(c.getClassIndex());
                    result.append(".#");
                    result.append(c.getNameAndTypeIndex());
                    result.append(";\t//  ");
                    result.append(Utility.replace(
                            constPool.constantToString(c), " ", ":"));
                } else if (constant instanceof ConstantNameAndType) {
                    ConstantNameAndType c = (ConstantNameAndType) constant;
                    result.append("NameAndType\t#");
                    result.append(c.getNameIndex());
                    result.append(":#");
                    result.append(c.getSignatureIndex());
                    result.append(";\t//  ");
                    result.append(Utility.replace(
                            constPool.constantToString(c), " ", ":"));
                } else if (constant instanceof ConstantMethodref) {
                    ConstantMethodref c = (ConstantMethodref) constant;
                    result.append("Method\t#");
                    result.append(c.getClassIndex());
                    result.append(".#");
                    result.append(c.getNameAndTypeIndex());
                    result.append(";\t//  ");
                    result.append(Utility.replace(
                            constPool.constantToString(c), " ", ":"));
                } else if (constant instanceof ConstantInterfaceMethodref) {
                    ConstantInterfaceMethodref c = (ConstantInterfaceMethodref) constant;
                    result.append("InterfaceMethod\t#");
                    result.append(c.getClassIndex());
                    result.append(".#");
                    result.append(c.getNameAndTypeIndex());
                    result.append(";\t//  ");
                    result.append(Utility.replace(
                            constPool.constantToString(c), " ", ":"));
                } else if (constant instanceof ConstantDouble) {
                    ConstantDouble c = (ConstantDouble) constant;
                    result.append("double\t");
                    result.append(constPool.constantToString(c));
                    result.append(';');
                } else if (constant instanceof ConstantFloat) {
                    ConstantFloat c = (ConstantFloat) constant;
                    result.append("float\t");
                    result.append(constPool.constantToString(c));
                    result.append(';');
                } else if (constant instanceof ConstantInteger) {
                    ConstantInteger c = (ConstantInteger) constant;
                    result.append("int\t");
                    result.append(constPool.constantToString(c));
                    result.append(';');
                } else if (constant instanceof ConstantLong) {
                    ConstantLong c = (ConstantLong) constant;
                    result.append("long\t");
                    result.append(constPool.constantToString(c));
                    result.append(';');
                } else if (constant instanceof ConstantClass) {
                    ConstantClass c = (ConstantClass) constant;
                    result.append("class\t#");
                    result.append(c.getNameIndex());
                    result.append(";\t//  ");
                    result.append(constPool.constantToString(c));
                } else if (constant instanceof ConstantString) {
                    ConstantString c = (ConstantString) constant;
                    result.append("String\t#");
                    result.append(c.getStringIndex());
                    result.append(";\t//  ");
                    result.append(constPool.constantToString(c));
                }

                result.append(n);
View Full Code Here

TOP

Related Classes of org.apache.bcel.classfile.ConstantUtf8

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.