src/share/classes/com/sun/tools/classfile/Descriptor.java

Tue, 03 Jun 2008 13:26:47 -0700

author
jjg
date
Tue, 03 Jun 2008 13:26:47 -0700
changeset 46
7708bd6d800d
child 54
eaf608c64fec
permissions
-rw-r--r--

4075303: Use javap to enquire aboput a specific inner class
4348375: Javap is not internationalized
4459541: "javap -l" shows line numbers as signed short; they should be unsigned
4501660: change diagnostic of -help as 'print this help message and exit'
4776241: unused source file in javap...
4870651: javap should recognize generics, varargs, enum
4876942: javap invoked without args does not print help screen
4880663: javap could output whitespace between class name and opening brace
4975569: javap doesn't print new flag bits
6271787: javap dumps LocalVariableTypeTable attribute in hex, needs to print a table
6305779: javap: support annotations
6439940: Clean up javap implementation
6469569: wrong check of searchpath in JavapEnvironment
6474890: javap does not open .zip files in -classpath
6587786: Javap throws error : "ERROR:Could not find <classname>" for JRE classes
6622215: javap ignores certain relevant access flags
6622216: javap names some attributes incorrectly
6622232: javap gets whitespace confused
6622260: javap prints negative bytes incorrectly in hex
Reviewed-by: ksrini

     1 /*
     2  * Copyright 2007 Sun Microsystems, Inc.  All Rights Reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    27 package com.sun.tools.classfile;
    29 import java.io.IOException;
    31 /**
    32  * See JVMS3, section 4.4.
    33  *
    34  *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    35  *  you write code that depends on this, you do so at your own risk.
    36  *  This code and its internal interfaces are subject to change or
    37  *  deletion without notice.</b>
    38  */
    39 public class Descriptor {
    40     public class InvalidDescriptor extends DescriptorException {
    41         InvalidDescriptor(String desc) {
    42             this.desc = desc;
    43             this.index = -1;
    44         }
    46         InvalidDescriptor(String desc, int index) {
    47             this.desc = desc;
    48             this.index = index;
    49         }
    51         @Override
    52         public String getMessage() {
    53             // i18n
    54             if (index == -1)
    55                 return "invalid descriptor \"" + desc + "\"";
    56             else
    57                 return "descriptor is invalid at offset " + index + " in \"" + desc + "\"";
    58         }
    60         public final String desc;
    61         public final int index;
    63     }
    65     public Descriptor(ClassReader cr) throws IOException {
    66         this(cr.readUnsignedShort());
    67     }
    69     public Descriptor(int index) {
    70         this.index = index;
    72     }
    74     public String getValue(ConstantPool constant_pool) throws ConstantPoolException {
    75         return constant_pool.getUTF8Value(index);
    76     }
    78     public int getParameterCount(ConstantPool constant_pool)
    79             throws ConstantPoolException, InvalidDescriptor {
    80         String desc = getValue(constant_pool);
    81         int end = desc.indexOf(")");
    82         if (end == -1)
    83             throw new InvalidDescriptor(desc);
    84         parse(desc, 0, end + 1);
    85         return count;
    87     }
    89     public String getParameterTypes(ConstantPool constant_pool)
    90             throws ConstantPoolException, InvalidDescriptor {
    91         String desc = getValue(constant_pool);
    92         int end = desc.indexOf(")");
    93         if (end == -1)
    94             throw new InvalidDescriptor(desc);
    95         return parse(desc, 0, end + 1);
    96     }
    98     public String getReturnType(ConstantPool constant_pool)
    99             throws ConstantPoolException, InvalidDescriptor {
   100         String desc = getValue(constant_pool);
   101         int end = desc.indexOf(")");
   102         if (end == -1)
   103             throw new InvalidDescriptor(desc);
   104         return parse(desc, end + 1, desc.length());
   105     }
   107     public String getFieldType(ConstantPool constant_pool)
   108             throws ConstantPoolException, InvalidDescriptor {
   109         String desc = getValue(constant_pool);
   110         return parse(desc, 0, desc.length());
   111     }
   113     private String parse(String desc, int start, int end)
   114             throws InvalidDescriptor {
   115         int p = start;
   116         StringBuffer sb = new StringBuffer();
   117         int dims = 0;
   118         count = 0;
   120         while (p < end) {
   121             String type;
   122             char ch;
   123             switch (ch = desc.charAt(p++)) {
   124                 case '(':
   125                     sb.append('(');
   126                     continue;
   128                 case ')':
   129                     sb.append(')');
   130                     continue;
   132                 case '[':
   133                     dims++;
   134                     continue;
   136                 case 'B':
   137                     type = "byte";
   138                     break;
   140                 case 'C':
   141                     type = "char";
   142                     break;
   144                 case 'D':
   145                     type = "double";
   146                     break;
   148                 case 'F':
   149                     type = "float";
   150                     break;
   152                 case 'I':
   153                     type = "int";
   154                     break;
   156                 case 'J':
   157                     type = "long";
   158                     break;
   160                 case 'L':
   161                     int sep = desc.indexOf(';', p);
   162                     if (sep == -1)
   163                         throw new InvalidDescriptor(desc, p - 1);
   164                     type = desc.substring(p, sep).replace('/', '.');
   165                     p = sep + 1;
   166                     break;
   168                 case 'S':
   169                     type = "short";
   170                     break;
   172                 case 'Z':
   173                     type = "boolean";
   174                     break;
   176                 case 'V':
   177                     type = "void";
   178                     break;
   180                 default:
   181                     throw new InvalidDescriptor(desc, p - 1);
   182             }
   184             if (sb.length() > 1 && sb.charAt(0) == '(')
   185                 sb.append(", ");
   186             sb.append(type);
   187             for ( ; dims > 0; dims-- )
   188                 sb.append("[]");
   190             count++;
   191         }
   193         return sb.toString();
   194     }
   196     public final int index;
   197     private int count;
   198 }

mercurial