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

Tue, 19 Feb 2013 17:19:01 -0800

author
ksrini
date
Tue, 19 Feb 2013 17:19:01 -0800
changeset 1592
9345394ac8fe
parent 1362
c46e0c9940d6
child 1648
a03c4a86ea2b
permissions
-rw-r--r--

8006948: Update javac for MethodParameters format change
Reviewed-by: ksrini, forax
Contributed-by: eric.mccorkle@oracle.com

     1 /*
     2  * Copyright (c) 2007, 2012, 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  */
    27 package com.sun.tools.classfile;
    29 import java.io.IOException;
    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 class InvalidDescriptor extends DescriptorException {
    41         private static final long serialVersionUID = 1L;
    42         InvalidDescriptor(String desc) {
    43             this.desc = desc;
    44             this.index = -1;
    45         }
    47         InvalidDescriptor(String desc, int index) {
    48             this.desc = desc;
    49             this.index = index;
    50         }
    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         }
    61         public final String desc;
    62         public final int index;
    64     }
    66     public Descriptor(ClassReader cr) throws IOException {
    67         this(cr.readUnsignedShort());
    68     }
    70     public Descriptor(int index) {
    71         this.index = index;
    73     }
    75     public String getValue(ConstantPool constant_pool) throws ConstantPoolException {
    76         return constant_pool.getUTF8Value(index);
    77     }
    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;
    88     }
    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     }
    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     }
   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     }
   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;
   121         while (p < end) {
   122             String type;
   123             char ch;
   124             switch (ch = desc.charAt(p++)) {
   125                 case '(':
   126                     sb.append('(');
   127                     continue;
   129                 case ')':
   130                     sb.append(')');
   131                     continue;
   133                 case '[':
   134                     dims++;
   135                     continue;
   137                 case 'B':
   138                     type = "byte";
   139                     break;
   141                 case 'C':
   142                     type = "char";
   143                     break;
   145                 case 'D':
   146                     type = "double";
   147                     break;
   149                 case 'F':
   150                     type = "float";
   151                     break;
   153                 case 'I':
   154                     type = "int";
   155                     break;
   157                 case 'J':
   158                     type = "long";
   159                     break;
   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;
   169                 case 'S':
   170                     type = "short";
   171                     break;
   173                 case 'Z':
   174                     type = "boolean";
   175                     break;
   177                 case 'V':
   178                     type = "void";
   179                     break;
   181                 default:
   182                     throw new InvalidDescriptor(desc, p - 1);
   183             }
   185             if (sb.length() > 1 && sb.charAt(0) == '(')
   186                 sb.append(", ");
   187             sb.append(type);
   188             for ( ; dims > 0; dims-- )
   189                 sb.append("[]");
   191             count++;
   192         }
   194         return sb.toString();
   195     }
   197     public final int index;
   198     private int count;
   199 }

mercurial