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

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

author
jjg
date
Tue, 03 Jun 2008 13:26:47 -0700
changeset 46
7708bd6d800d
child 65
0d4aa3c00af5
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 2008 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  */
    26 package com.sun.tools.classfile;
    28 import java.util.List;
    30 /*
    31  *  <p><b>This is NOT part of any API supported by Sun Microsystems.  If
    32  *  you write code that depends on this, you do so at your own risk.
    33  *  This code and its internal interfaces are subject to change or
    34  *  deletion without notice.</b>
    35  */
    36 public class Type {
    37     protected Type() { }
    39     public boolean isObject() {
    40         return false;
    41     }
    43     protected static void append(StringBuilder sb, String prefix, List<? extends Type> types, String suffix) {
    44         sb.append(prefix);
    45         String sep = "";
    46         for (Type t: types) {
    47             sb.append(sep);
    48             sb.append(t);
    49             sep = ", ";
    50         }
    51         sb.append(suffix);
    52     }
    54     protected static void appendIfNotEmpty(StringBuilder sb, String prefix, List<? extends Type> types, String suffix) {
    55         if (types != null && types.size() > 0)
    56             append(sb, prefix, types, suffix);
    57     }
    59     public static class SimpleType extends Type {
    60         public SimpleType(String name) {
    61             this.name = name;
    62         }
    64         @Override
    65         public String toString() {
    66             return name;
    67         }
    69         @Override
    70         public boolean isObject() {
    71             return name.equals("java.lang.Object");
    72         }
    74         public final String name;
    75     }
    77     public static class ArrayType extends Type {
    78         public ArrayType(Type elemType) {
    79             this.elemType = elemType;
    80         }
    82         @Override
    83         public String toString() {
    84             return elemType + "[]";
    85         }
    87         public final Type elemType;
    88     }
    90     public static class MethodType extends Type {
    91         public MethodType(List<? extends Type> argTypes, Type resultType) {
    92             this(null, argTypes, resultType, null);
    93         }
    95         public MethodType(List<? extends Type> typeArgTypes,
    96                 List<? extends Type> argTypes,
    97                 Type returnType,
    98                 List<? extends Type> throwsTypes) {
    99             this.typeArgTypes = typeArgTypes;
   100             this.argTypes = argTypes;
   101             this.returnType = returnType;
   102             this.throwsTypes = throwsTypes;
   103         }
   105         @Override
   106         public String toString() {
   107             StringBuilder sb = new StringBuilder();
   108             appendIfNotEmpty(sb, "<", typeArgTypes, "> ");
   109             sb.append(returnType);
   110             append(sb, " (", argTypes, ")");
   111             appendIfNotEmpty(sb, " throws ", throwsTypes, "");
   112             return sb.toString();
   113         }
   115         public final List<? extends Type> typeArgTypes;
   116         public final List<? extends Type> argTypes;
   117         public final Type returnType;
   118         public final List<? extends Type> throwsTypes;
   119     }
   121     public static class ClassSigType extends Type {
   122         public ClassSigType(List<Type> typeArgTypes, Type superclassType, List<Type> superinterfaceTypes) {
   123             this.typeArgTypes = typeArgTypes;
   124             this.superclassType = superclassType;
   125             this.superinterfaceTypes = superinterfaceTypes;
   126         }
   128         @Override
   129         public String toString() {
   130             StringBuilder sb = new StringBuilder();
   131             appendIfNotEmpty(sb, "<", typeArgTypes, ">");
   132             if (superclassType != null && !superclassType.isObject()) {
   133                 sb.append(" extends ");
   134                 sb.append(superclassType);
   135             }
   136             appendIfNotEmpty(sb, " implements ", superinterfaceTypes, "");
   137             return sb.toString();
   138         }
   140         public final List<Type> typeArgTypes;
   141         public final Type superclassType;
   142         public final List<Type> superinterfaceTypes;
   143     }
   145     public static class ClassType extends Type {
   146         public ClassType(String name, List<Type> typeArgs) {
   147             this.name = name;
   148             this.typeArgs = typeArgs;
   149         }
   151         @Override
   152         public String toString() {
   153             StringBuilder sb = new StringBuilder();
   154             sb.append(name);
   155             appendIfNotEmpty(sb, "<", typeArgs, ">");
   156             return sb.toString();
   157         }
   159         public final String name;
   160         public final List<Type> typeArgs;
   161     }
   164     public static class InnerClassType extends Type {
   165         public InnerClassType(Type outerType, Type innerType) {
   166             this.outerType = outerType;
   167             this.innerType = innerType;
   168         }
   170         @Override
   171         public String toString() {
   172             return outerType + "." + innerType;
   173         }
   175         public final Type outerType;
   176         public final Type innerType;
   177     }
   179     public static class TypeArgType extends Type {
   180         public TypeArgType(String name, Type classBound, List<Type> interfaceBounds) {
   181             this.name = name;
   182             this.classBound = classBound;
   183             this.interfaceBounds = interfaceBounds;
   184         }
   186         @Override
   187         public String toString() {
   188             StringBuilder sb = new StringBuilder();
   189             sb.append(name);
   190             String sep = " extends ";
   191             if (classBound != null && !classBound.isObject()) {
   192                 sb.append(sep);
   193                 sb.append(classBound);
   194                 sep = " & ";
   195             }
   196             if (interfaceBounds != null) {
   197                 for (Type bound: interfaceBounds) {
   198                     sb.append(sep);
   199                     sb.append(bound);
   200                     sep = " & ";
   201                 }
   202             }
   203             return sb.toString();
   204         }
   206         public final String name;
   207         public final Type classBound;
   208         public final List<Type> interfaceBounds;
   209     }
   211     public static class WildcardType extends Type {
   212         public WildcardType() {
   213             this(null, null);
   214         }
   216         public WildcardType(String kind, Type boundType) {
   217             this.kind = kind;
   218             this.boundType = boundType;
   219         }
   221         @Override
   222         public String toString() {
   223             if (kind == null)
   224                 return "?";
   225             else
   226                 return "? " + kind + " " + boundType;
   227         }
   229         public final String kind;
   230         public final Type boundType;
   231     }
   232 }

mercurial