Package org.apache.bcel.classfile

Examples of org.apache.bcel.classfile.ConstantNameAndType


                String key = u8.getBytes();
                if (!class_table.containsKey(key)) {
                    class_table.put(key, new Index(i));
                }
            } else if (c instanceof ConstantNameAndType) {
                ConstantNameAndType n = (ConstantNameAndType) c;
                ConstantUtf8 u8 = (ConstantUtf8) constants[n.getNameIndex()];
                ConstantUtf8 u8_2 = (ConstantUtf8) constants[n.getSignatureIndex()];
               
                sb.append(u8.getBytes());
                sb.append(NAT_DELIM);
                sb.append(u8_2.getBytes());
                String key = sb.toString();
                sb.delete(0, sb.length());
               
                if (!n_a_t_table.containsKey(key)) {
                    n_a_t_table.put(key, new Index(i));
                }
            } else if (c instanceof ConstantUtf8) {
                ConstantUtf8 u = (ConstantUtf8) c;
                String key = u.getBytes();
                if (!utf8_table.containsKey(key)) {
                    utf8_table.put(key, new Index(i));
                }
            } else if (c instanceof ConstantCP) {
                ConstantCP m = (ConstantCP) c;
                ConstantClass clazz = (ConstantClass) constants[m.getClassIndex()];
                ConstantNameAndType n = (ConstantNameAndType) constants[m.getNameAndTypeIndex()];
                ConstantUtf8 u8 = (ConstantUtf8) constants[clazz.getNameIndex()];
                String class_name = u8.getBytes().replace('/', '.');
                u8 = (ConstantUtf8) constants[n.getNameIndex()];
                String method_name = u8.getBytes();
                u8 = (ConstantUtf8) constants[n.getSignatureIndex()];
                String signature = u8.getBytes();
                String delim = METHODREF_DELIM;
                if (c instanceof ConstantInterfaceMethodref) {
                    delim = IMETHODREF_DELIM;
                } else if (c instanceof ConstantFieldref) {
View Full Code Here


        }
        adjustSize();
        name_index = addUtf8(name);
        signature_index = addUtf8(signature);
        ret = index;
        constants[index++] = new ConstantNameAndType(name_index, signature_index);
        String key = name + NAT_DELIM + signature;
        if (!n_a_t_table.containsKey(key)) {
            n_a_t_table.put(key, new Index(ret));
        }
        return ret;
View Full Code Here

                ConstantClass s = (ConstantClass) c;
                ConstantUtf8 u8 = (ConstantUtf8) constants[s.getNameIndex()];
                return addClass(u8.getBytes());
            }
            case Constants.CONSTANT_NameAndType: {
                ConstantNameAndType n = (ConstantNameAndType) c;
                ConstantUtf8 u8 = (ConstantUtf8) constants[n.getNameIndex()];
                ConstantUtf8 u8_2 = (ConstantUtf8) constants[n.getSignatureIndex()];
                return addNameAndType(u8.getBytes(), u8_2.getBytes());
            }
            case Constants.CONSTANT_Utf8:
                return addUtf8(((ConstantUtf8) c).getBytes());
            case Constants.CONSTANT_Double:
                return addDouble(((ConstantDouble) c).getBytes());
            case Constants.CONSTANT_Float:
                return addFloat(((ConstantFloat) c).getBytes());
            case Constants.CONSTANT_Long:
                return addLong(((ConstantLong) c).getBytes());
            case Constants.CONSTANT_Integer:
                return addInteger(((ConstantInteger) c).getBytes());
            case Constants.CONSTANT_InterfaceMethodref:
            case Constants.CONSTANT_Methodref:
            case Constants.CONSTANT_Fieldref: {
                ConstantCP m = (ConstantCP) c;
                ConstantClass clazz = (ConstantClass) constants[m.getClassIndex()];
                ConstantNameAndType n = (ConstantNameAndType) constants[m.getNameAndTypeIndex()];
                ConstantUtf8 u8 = (ConstantUtf8) constants[clazz.getNameIndex()];
                String class_name = u8.getBytes().replace('/', '.');
                u8 = (ConstantUtf8) constants[n.getNameIndex()];
                String name = u8.getBytes();
                u8 = (ConstantUtf8) constants[n.getSignatureIndex()];
                String signature = u8.getBytes();
                switch (c.getTag()) {
                    case Constants.CONSTANT_InterfaceMethodref:
                        return addInterfaceMethodref(class_name, name, signature);
                    case Constants.CONSTANT_Methodref:
View Full Code Here

        sig = sig.substring(0, sig.indexOf(')') + 1);
        return m.getName() + sig;
    }
   
    public static String getMethodIdentifier(ConstantCP c, ConstantPool cp){
        ConstantNameAndType cnat =
                (ConstantNameAndType)cp.getConstant(c.getNameAndTypeIndex());
        String sig = cnat.getSignature(cp);
        sig = sig.substring(0, sig.indexOf(')') + 1);
        return cnat.getName(cp) + sig;
    }
View Full Code Here

                String method_class = constant_pool.constantToString(class_index, CONSTANT_Class);
                String short_method_class = Utility.compactClassName(method_class); // I.e., remove java.lang.
                short_method_class = Utility.compactClassName(short_method_class, class_package
                        + ".", true); // Remove class package prefix
                // Get method signature
                ConstantNameAndType c2 = (ConstantNameAndType) constant_pool.getConstant(
                        name_index, CONSTANT_NameAndType);
                String signature = constant_pool.constantToString(c2.getSignatureIndex(),
                        CONSTANT_Utf8);
                // Get array of strings containing the argument types
                String[] args = Utility.methodSignatureArgumentTypes(signature, false);
                // Get return type string
                String type = Utility.methodSignatureReturnType(signature, false);
                String ret_type = Class2HTML.referenceType(type);
                StringBuilder buf = new StringBuilder("(");
                for (int i = 0; i < args.length; i++) {
                    buf.append(Class2HTML.referenceType(args[i]));
                    if (i < args.length - 1) {
                        buf.append(",&nbsp;");
                    }
                }
                buf.append(")");
                String arg_types = buf.toString();
                if (method_class.equals(class_name)) {
                    ref = "<A HREF=\"" + class_name + "_code.html#method"
                            + getMethodNumber(method_name + signature) + "\" TARGET=Code>"
                            + html_method_name + "</A>";
                } else {
                    ref = "<A HREF=\"" + method_class + ".html" + "\" TARGET=_top>"
                            + short_method_class + "</A>." + html_method_name;
                }
                constant_ref[index] = ret_type + "&nbsp;<A HREF=\"" + class_name + "_cp.html#cp"
                        + class_index + "\" TARGET=Constants>" + short_method_class
                        + "</A>.<A HREF=\"" + class_name + "_cp.html#cp" + index
                        + "\" TARGET=ConstantPool>" + html_method_name + "</A>&nbsp;" + arg_types;
                file.println("<P><TT>" + ret_type + "&nbsp;" + ref + arg_types
                        + "&nbsp;</TT>\n<UL>" + "<LI><A HREF=\"#cp" + class_index
                        + "\">Class index(" + class_index + ")</A>\n" + "<LI><A HREF=\"#cp"
                        + name_index + "\">NameAndType index(" + name_index + ")</A></UL>");
                break;
            case CONSTANT_Fieldref:
                // Get class_index and name_and_type_index
                ConstantFieldref c3 = (ConstantFieldref) constant_pool.getConstant(index,
                        CONSTANT_Fieldref);
                class_index = c3.getClassIndex();
                name_index = c3.getNameAndTypeIndex();
                // Get method name and its class (compacted)
                String field_class = constant_pool.constantToString(class_index, CONSTANT_Class);
                String short_field_class = Utility.compactClassName(field_class); // I.e., remove java.lang.
                short_field_class = Utility.compactClassName(short_field_class,
                        class_package + ".", true); // Remove class package prefix
                String field_name = constant_pool
                        .constantToString(name_index, CONSTANT_NameAndType);
                if (field_class.equals(class_name)) {
                    ref = "<A HREF=\"" + field_class + "_methods.html#field" + field_name
                            + "\" TARGET=Methods>" + field_name + "</A>";
                } else {
                    ref = "<A HREF=\"" + field_class + ".html\" TARGET=_top>" + short_field_class
                            + "</A>." + field_name + "\n";
                }
                constant_ref[index] = "<A HREF=\"" + class_name + "_cp.html#cp" + class_index
                        + "\" TARGET=Constants>" + short_field_class + "</A>.<A HREF=\""
                        + class_name + "_cp.html#cp" + index + "\" TARGET=ConstantPool>"
                        + field_name + "</A>";
                file.println("<P><TT>" + ref + "</TT><BR>\n" + "<UL>" + "<LI><A HREF=\"#cp"
                        + class_index + "\">Class(" + class_index + ")</A><BR>\n"
                        + "<LI><A HREF=\"#cp" + name_index + "\">NameAndType(" + name_index
                        + ")</A></UL>");
                break;
            case CONSTANT_Class:
                ConstantClass c4 = (ConstantClass) constant_pool.getConstant(index, CONSTANT_Class);
                name_index = c4.getNameIndex();
                String class_name2 = constant_pool.constantToString(index, tag); // / -> .
                String short_class_name = Utility.compactClassName(class_name2); // I.e., remove java.lang.
                short_class_name = Utility.compactClassName(short_class_name, class_package + ".",
                        true); // Remove class package prefix
                ref = "<A HREF=\"" + class_name2 + ".html\" TARGET=_top>" + short_class_name
                        + "</A>";
                constant_ref[index] = "<A HREF=\"" + class_name + "_cp.html#cp" + index
                        + "\" TARGET=ConstantPool>" + short_class_name + "</A>";
                file.println("<P><TT>" + ref + "</TT><UL>" + "<LI><A HREF=\"#cp" + name_index
                        + "\">Name index(" + name_index + ")</A></UL>\n");
                break;
            case CONSTANT_String:
                ConstantString c5 = (ConstantString) constant_pool.getConstant(index,
                        CONSTANT_String);
                name_index = c5.getStringIndex();
                String str = Class2HTML.toHTML(constant_pool.constantToString(index, tag));
                file.println("<P><TT>" + str + "</TT><UL>" + "<LI><A HREF=\"#cp" + name_index
                        + "\">Name index(" + name_index + ")</A></UL>\n");
                break;
            case CONSTANT_NameAndType:
                ConstantNameAndType c6 = (ConstantNameAndType) constant_pool.getConstant(index,
                        CONSTANT_NameAndType);
                name_index = c6.getNameIndex();
                int signature_index = c6.getSignatureIndex();
                file.println("<P><TT>"
                        + Class2HTML.toHTML(constant_pool.constantToString(index, tag))
                        + "</TT><UL>" + "<LI><A HREF=\"#cp" + name_index + "\">Name index("
                        + name_index + ")</A>\n" + "<LI><A HREF=\"#cp" + signature_index
                        + "\">Signature index(" + signature_index + ")</A></UL>\n");
View Full Code Here

                }
                name = Class2HTML.referenceClass(class_index);
                str = Class2HTML.toHTML(constant_pool.constantToString(constant_pool.getConstant(
                        index, CONSTANT_NameAndType)));
                // Get signature, i.e., types
                ConstantNameAndType c2 = (ConstantNameAndType) constant_pool.getConstant(index,
                        CONSTANT_NameAndType);
                signature = constant_pool.constantToString(c2.getSignatureIndex(), CONSTANT_Utf8);
                String[] args = Utility.methodSignatureArgumentTypes(signature, false);
                String type = Utility.methodSignatureReturnType(signature, false);
                buf.append(name).append(".<A HREF=\"").append(class_name).append("_cp.html#cp")
                        .append(m_index).append("\" TARGET=ConstantPool>").append(str).append(
                                "</A>").append("(");
View Full Code Here

        if (! (c instanceof ConstantMethodref)){
          constraintViolated(o, "Indexing a constant that's not a CONSTANT_Methodref but a '"+c+"'.");
        }
        else{
          // Constants are okay due to pass2.
          ConstantNameAndType cnat = (ConstantNameAndType) (cpg.getConstant(((ConstantMethodref) c).getNameAndTypeIndex()));
          ConstantUtf8 cutf8 = (ConstantUtf8) (cpg.getConstant(cnat.getNameIndex()));
          if (cutf8.getBytes().equals(Constants.CONSTRUCTOR_NAME) && (!(o instanceof INVOKESPECIAL)) ){
            constraintViolated(o, "Only INVOKESPECIAL is allowed to invoke instance initialization methods.");
          }
          if ( (! (cutf8.getBytes().equals(Constants.CONSTRUCTOR_NAME)) ) && (cutf8.getBytes().startsWith("<")) ){
            constraintViolated(o, "No method with a name beginning with '<' other than the instance initialization methods may be called by the method invocation instructions.");
          }
        }
      }
      else{ //if (o instanceof INVOKEINTERFACE){
        Constant c = cpg.getConstant(o.getIndex());
        if (! (c instanceof ConstantInterfaceMethodref)){
          constraintViolated(o, "Indexing a constant that's not a CONSTANT_InterfaceMethodref but a '"+c+"'.");
        }
        // TODO: From time to time check if BCEL allows to detect if the
        // 'count' operand is consistent with the information in the
        // CONSTANT_InterfaceMethodref and if the last operand is zero.
        // By now, BCEL hides those two operands because they're superfluous.
       
        // Invoked method must not be <init> or <clinit>
        ConstantNameAndType cnat = (ConstantNameAndType) (cpg.getConstant(((ConstantInterfaceMethodref)c).getNameAndTypeIndex()));
        String name = ((ConstantUtf8) (cpg.getConstant(cnat.getNameIndex()))).getBytes();
        if (name.equals(Constants.CONSTRUCTOR_NAME)){
          constraintViolated(o, "Method to invoke must not be '"+Constants.CONSTRUCTOR_NAME+"'.");
        }
        if (name.equals(Constants.STATIC_INITIALIZER_NAME)){
          constraintViolated(o, "Method to invoke must not be '"+Constants.STATIC_INITIALIZER_NAME+"'.");
View Full Code Here

        public void visitConstantFieldref(ConstantFieldref obj){
      if (obj.getTag() != Constants.CONSTANT_Fieldref){
        throw new ClassConstraintException("ConstantFieldref '"+tostring(obj)+"' has wrong tag!");
      }
      int name_and_type_index = obj.getNameAndTypeIndex();
      ConstantNameAndType cnat = (ConstantNameAndType) (cp.getConstant(name_and_type_index));
      String name = ((ConstantUtf8) (cp.getConstant(cnat.getNameIndex()))).getBytes(); // Field or Method name
      if (!validFieldName(name)){
        throw new ClassConstraintException("Invalid field name '"+name+"' referenced by '"+tostring(obj)+"'.");
      }
     
      int class_index = obj.getClassIndex();
      ConstantClass cc = (ConstantClass) (cp.getConstant(class_index));
      String className = ((ConstantUtf8) (cp.getConstant(cc.getNameIndex()))).getBytes(); // Class Name in internal form
      if (! validClassName(className)){
        throw new ClassConstraintException("Illegal class name '"+className+"' used by '"+tostring(obj)+"'.");
      }

      String sig  = ((ConstantUtf8) (cp.getConstant(cnat.getSignatureIndex()))).getBytes(); // Field or Method signature(=descriptor)
           
      try{
        Type.getType(sig); /* Don't need the return value */
      }
      catch (ClassFormatException cfe){
View Full Code Here

        public void visitConstantMethodref(ConstantMethodref obj){
      if (obj.getTag() != Constants.CONSTANT_Methodref){
        throw new ClassConstraintException("ConstantMethodref '"+tostring(obj)+"' has wrong tag!");
      }
      int name_and_type_index = obj.getNameAndTypeIndex();
      ConstantNameAndType cnat = (ConstantNameAndType) (cp.getConstant(name_and_type_index));
      String name = ((ConstantUtf8) (cp.getConstant(cnat.getNameIndex()))).getBytes(); // Field or Method name
      if (!validClassMethodName(name)){
        throw new ClassConstraintException("Invalid (non-interface) method name '"+name+"' referenced by '"+tostring(obj)+"'.");
      }

      int class_index = obj.getClassIndex();
      ConstantClass cc = (ConstantClass) (cp.getConstant(class_index));
      String className = ((ConstantUtf8) (cp.getConstant(cc.getNameIndex()))).getBytes(); // Class Name in internal form
      if (! validClassName(className)){
        throw new ClassConstraintException("Illegal class name '"+className+"' used by '"+tostring(obj)+"'.");
      }

      String sig  = ((ConstantUtf8) (cp.getConstant(cnat.getSignatureIndex()))).getBytes(); // Field or Method signature(=descriptor)
           
      try{
        Type   t  = Type.getReturnType(sig);
        if ( name.equals(CONSTRUCTOR_NAME) && (t != Type.VOID) ){
          throw new ClassConstraintException("Instance initialization method must have VOID return type.");
View Full Code Here

        public void visitConstantInterfaceMethodref(ConstantInterfaceMethodref obj){
      if (obj.getTag() != Constants.CONSTANT_InterfaceMethodref){
        throw new ClassConstraintException("ConstantInterfaceMethodref '"+tostring(obj)+"' has wrong tag!");
      }
      int name_and_type_index = obj.getNameAndTypeIndex();
      ConstantNameAndType cnat = (ConstantNameAndType) (cp.getConstant(name_and_type_index));
      String name = ((ConstantUtf8) (cp.getConstant(cnat.getNameIndex()))).getBytes(); // Field or Method name
      if (!validInterfaceMethodName(name)){
        throw new ClassConstraintException("Invalid (interface) method name '"+name+"' referenced by '"+tostring(obj)+"'.");
      }

      int class_index = obj.getClassIndex();
      ConstantClass cc = (ConstantClass) (cp.getConstant(class_index));
      String className = ((ConstantUtf8) (cp.getConstant(cc.getNameIndex()))).getBytes(); // Class Name in internal form
      if (! validClassName(className)){
        throw new ClassConstraintException("Illegal class name '"+className+"' used by '"+tostring(obj)+"'.");
      }

      String sig  = ((ConstantUtf8) (cp.getConstant(cnat.getSignatureIndex()))).getBytes(); // Field or Method signature(=descriptor)
           
      try{
        Type   t  = Type.getReturnType(sig);
        if ( name.equals(STATIC_INITIALIZER_NAME) && (t != Type.VOID) ){
          addMessage("Class or interface initialization method '"+STATIC_INITIALIZER_NAME+"' usually has VOID return type instead of '"+t+"'. Note this is really not a requirement of The Java Virtual Machine Specification, Second Edition.");
View Full Code Here

TOP

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

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.