langtools/src/jdk.jdeps/share/classes/com/sun/tools/classfile/Descriptor.java
changeset 30846 2b3f379840f0
parent 25874 83c19f00452c
child 34752 9c262a013456
equal deleted inserted replaced
30845:43ddd58a5a56 30846:2b3f379840f0
       
     1 /*
       
     2  * Copyright (c) 2007, 2013, Oracle and/or its affiliates. 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.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 
       
    26 
       
    27 package com.sun.tools.classfile;
       
    28 
       
    29 import java.io.IOException;
       
    30 
       
    31 /**
       
    32  * See JVMS, section 4.4.
       
    33  *
       
    34  *  <p><b>This is NOT part of any supported API.
       
    35  *  If 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 static class InvalidDescriptor extends DescriptorException {
       
    41         private static final long serialVersionUID = 1L;
       
    42         InvalidDescriptor(String desc) {
       
    43             this.desc = desc;
       
    44             this.index = -1;
       
    45         }
       
    46 
       
    47         InvalidDescriptor(String desc, int index) {
       
    48             this.desc = desc;
       
    49             this.index = index;
       
    50         }
       
    51 
       
    52         @Override
       
    53         public String getMessage() {
       
    54             // i18n
       
    55             if (index == -1)
       
    56                 return "invalid descriptor \"" + desc + "\"";
       
    57             else
       
    58                 return "descriptor is invalid at offset " + index + " in \"" + desc + "\"";
       
    59         }
       
    60 
       
    61         public final String desc;
       
    62         public final int index;
       
    63 
       
    64     }
       
    65 
       
    66     public Descriptor(ClassReader cr) throws IOException {
       
    67         this(cr.readUnsignedShort());
       
    68     }
       
    69 
       
    70     public Descriptor(int index) {
       
    71         this.index = index;
       
    72 
       
    73     }
       
    74 
       
    75     public String getValue(ConstantPool constant_pool) throws ConstantPoolException {
       
    76         return constant_pool.getUTF8Value(index);
       
    77     }
       
    78 
       
    79     public int getParameterCount(ConstantPool constant_pool)
       
    80             throws ConstantPoolException, InvalidDescriptor {
       
    81         String desc = getValue(constant_pool);
       
    82         int end = desc.indexOf(")");
       
    83         if (end == -1)
       
    84             throw new InvalidDescriptor(desc);
       
    85         parse(desc, 0, end + 1);
       
    86         return count;
       
    87 
       
    88     }
       
    89 
       
    90     public String getParameterTypes(ConstantPool constant_pool)
       
    91             throws ConstantPoolException, InvalidDescriptor {
       
    92         String desc = getValue(constant_pool);
       
    93         int end = desc.indexOf(")");
       
    94         if (end == -1)
       
    95             throw new InvalidDescriptor(desc);
       
    96         return parse(desc, 0, end + 1);
       
    97     }
       
    98 
       
    99     public String getReturnType(ConstantPool constant_pool)
       
   100             throws ConstantPoolException, InvalidDescriptor {
       
   101         String desc = getValue(constant_pool);
       
   102         int end = desc.indexOf(")");
       
   103         if (end == -1)
       
   104             throw new InvalidDescriptor(desc);
       
   105         return parse(desc, end + 1, desc.length());
       
   106     }
       
   107 
       
   108     public String getFieldType(ConstantPool constant_pool)
       
   109             throws ConstantPoolException, InvalidDescriptor {
       
   110         String desc = getValue(constant_pool);
       
   111         return parse(desc, 0, desc.length());
       
   112     }
       
   113 
       
   114     private String parse(String desc, int start, int end)
       
   115             throws InvalidDescriptor {
       
   116         int p = start;
       
   117         StringBuilder sb = new StringBuilder();
       
   118         int dims = 0;
       
   119         count = 0;
       
   120 
       
   121         while (p < end) {
       
   122             String type;
       
   123             char ch;
       
   124             switch (ch = desc.charAt(p++)) {
       
   125                 case '(':
       
   126                     sb.append('(');
       
   127                     continue;
       
   128 
       
   129                 case ')':
       
   130                     sb.append(')');
       
   131                     continue;
       
   132 
       
   133                 case '[':
       
   134                     dims++;
       
   135                     continue;
       
   136 
       
   137                 case 'B':
       
   138                     type = "byte";
       
   139                     break;
       
   140 
       
   141                 case 'C':
       
   142                     type = "char";
       
   143                     break;
       
   144 
       
   145                 case 'D':
       
   146                     type = "double";
       
   147                     break;
       
   148 
       
   149                 case 'F':
       
   150                     type = "float";
       
   151                     break;
       
   152 
       
   153                 case 'I':
       
   154                     type = "int";
       
   155                     break;
       
   156 
       
   157                 case 'J':
       
   158                     type = "long";
       
   159                     break;
       
   160 
       
   161                 case 'L':
       
   162                     int sep = desc.indexOf(';', p);
       
   163                     if (sep == -1)
       
   164                         throw new InvalidDescriptor(desc, p - 1);
       
   165                     type = desc.substring(p, sep).replace('/', '.');
       
   166                     p = sep + 1;
       
   167                     break;
       
   168 
       
   169                 case 'S':
       
   170                     type = "short";
       
   171                     break;
       
   172 
       
   173                 case 'Z':
       
   174                     type = "boolean";
       
   175                     break;
       
   176 
       
   177                 case 'V':
       
   178                     type = "void";
       
   179                     break;
       
   180 
       
   181                 default:
       
   182                     throw new InvalidDescriptor(desc, p - 1);
       
   183             }
       
   184 
       
   185             if (sb.length() > 1 && sb.charAt(0) == '(')
       
   186                 sb.append(", ");
       
   187             sb.append(type);
       
   188             for ( ; dims > 0; dims-- )
       
   189                 sb.append("[]");
       
   190 
       
   191             count++;
       
   192         }
       
   193 
       
   194         return sb.toString();
       
   195     }
       
   196 
       
   197     public final int index;
       
   198     private int count;
       
   199 }