langtools/src/share/classes/com/sun/tools/javap/ClassWriter.java
changeset 1038 fbe81cf1123d
parent 859 16b1ea00598f
child 1039 ca7b8a8c9268
equal deleted inserted replaced
1037:c6b315a34dc6 1038:fbe81cf1123d
    33 import com.sun.tools.classfile.Attributes;
    33 import com.sun.tools.classfile.Attributes;
    34 import com.sun.tools.classfile.ClassFile;
    34 import com.sun.tools.classfile.ClassFile;
    35 import com.sun.tools.classfile.Code_attribute;
    35 import com.sun.tools.classfile.Code_attribute;
    36 import com.sun.tools.classfile.ConstantPool;
    36 import com.sun.tools.classfile.ConstantPool;
    37 import com.sun.tools.classfile.ConstantPoolException;
    37 import com.sun.tools.classfile.ConstantPoolException;
       
    38 import com.sun.tools.classfile.ConstantValue_attribute;
    38 import com.sun.tools.classfile.Descriptor;
    39 import com.sun.tools.classfile.Descriptor;
    39 import com.sun.tools.classfile.DescriptorException;
    40 import com.sun.tools.classfile.DescriptorException;
    40 import com.sun.tools.classfile.Exceptions_attribute;
    41 import com.sun.tools.classfile.Exceptions_attribute;
    41 import com.sun.tools.classfile.Field;
    42 import com.sun.tools.classfile.Field;
    42 import com.sun.tools.classfile.Method;
    43 import com.sun.tools.classfile.Method;
   183                 print(getFieldType(f.descriptor));
   184                 print(getFieldType(f.descriptor));
   184             }
   185             }
   185         }
   186         }
   186         print(" ");
   187         print(" ");
   187         print(getFieldName(f));
   188         print(getFieldName(f));
       
   189         if (options.showConstants && !options.compat) { // BUG 4111861 print static final field contents
       
   190             Attribute a = f.attributes.get(Attribute.ConstantValue);
       
   191             if (a instanceof ConstantValue_attribute) {
       
   192                 print(" = ");
       
   193                 ConstantValue_attribute cv = (ConstantValue_attribute) a;
       
   194                 print(getConstantValue(f.descriptor, cv.constantvalue_index));
       
   195             }
       
   196         }
   188         print(";");
   197         print(";");
   189         println();
   198         println();
   190 
   199 
   191         if (options.showInternalSignatures)
   200         if (options.showInternalSignatures)
   192             println("  Signature: " + getValue(f.descriptor));
   201             println("  Signature: " + getValue(f.descriptor));
   479         } catch (ConstantPoolException e) {
   488         } catch (ConstantPoolException e) {
   480             return report(e);
   489             return report(e);
   481         }
   490         }
   482     }
   491     }
   483 
   492 
       
   493     /**
       
   494      * Get the value of an entry in the constant pool as a Java constant.
       
   495      * Characters and booleans are represented by CONSTANT_Intgere entries.
       
   496      * Character and string values are processed to escape characters outside
       
   497      * the basic printable ASCII set.
       
   498      * @param d the descriptor, giving the expected type of the constant
       
   499      * @param index the index of the value in the constant pool
       
   500      * @return a printable string containing the value of the constant.
       
   501      */
       
   502     String getConstantValue(Descriptor d, int index) {
       
   503         try {
       
   504             ConstantPool.CPInfo cpInfo = constant_pool.get(index);
       
   505 
       
   506             switch (cpInfo.getTag()) {
       
   507                 case ConstantPool.CONSTANT_Integer: {
       
   508                     ConstantPool.CONSTANT_Integer_info info =
       
   509                             (ConstantPool.CONSTANT_Integer_info) cpInfo;
       
   510                     String t = d.getValue(constant_pool);
       
   511                     if (t.equals("C")) { // character
       
   512                         return getConstantCharValue((char) info.value);
       
   513                     } else if (t.equals("Z")) { // boolean
       
   514                         return String.valueOf(info.value == 1);
       
   515                     } else { // other: assume integer
       
   516                         return String.valueOf(info.value);
       
   517                     }
       
   518                 }
       
   519 
       
   520                 case ConstantPool.CONSTANT_String: {
       
   521                     ConstantPool.CONSTANT_String_info info =
       
   522                             (ConstantPool.CONSTANT_String_info) cpInfo;
       
   523                     return getConstantStringValue(info.getString());
       
   524                 }
       
   525 
       
   526                 default:
       
   527                     return constantWriter.stringValue(cpInfo);
       
   528             }
       
   529         } catch (ConstantPoolException e) {
       
   530             return "#" + index;
       
   531         }
       
   532     }
       
   533 
       
   534     private String getConstantCharValue(char c) {
       
   535         StringBuilder sb = new StringBuilder();
       
   536         sb.append('\'');
       
   537         sb.append(esc(c, '\''));
       
   538         sb.append('\'');
       
   539         return sb.toString();
       
   540     }
       
   541 
       
   542     private String getConstantStringValue(String s) {
       
   543         StringBuilder sb = new StringBuilder();
       
   544         sb.append("\"");
       
   545         for (int i = 0; i < s.length(); i++) {
       
   546             sb.append(esc(s.charAt(i), '"'));
       
   547         }
       
   548         sb.append("\"");
       
   549         return sb.toString();
       
   550     }
       
   551 
       
   552     private String esc(char c, char quote) {
       
   553         if (32 <= c && c <= 126 && c != quote)
       
   554             return String.valueOf(c);
       
   555         else switch (c) {
       
   556             case '\b': return "\\b";
       
   557             case '\n': return "\\n";
       
   558             case '\t': return "\\t";
       
   559             case '\f': return "\\f";
       
   560             case '\r': return "\\r";
       
   561             case '\\': return "\\\\";
       
   562             case '\'': return "\\'";
       
   563             case '\"': return "\\\"";
       
   564             default:   return String.format("\\u%04x", (int) c);
       
   565         }
       
   566     }
       
   567 
   484     private Options options;
   568     private Options options;
   485     private AttributeWriter attrWriter;
   569     private AttributeWriter attrWriter;
   486     private CodeWriter codeWriter;
   570     private CodeWriter codeWriter;
   487     private ConstantWriter constantWriter;
   571     private ConstantWriter constantWriter;
   488     private ClassFile classFile;
   572     private ClassFile classFile;