src/share/classes/com/sun/tools/javac/code/Printer.java

Thu, 04 Jul 2013 10:41:08 +0100

author
vromero
date
Thu, 04 Jul 2013 10:41:08 +0100
changeset 1886
79c3146e417b
parent 1853
831467c4c6a7
child 1969
7de231613e4a
permissions
-rw-r--r--

6356530: -Xlint:serial does not flag abstract classes with concrete methods/members
Reviewed-by: mcimadamore

     1 /*
     2  * Copyright (c) 2009, 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  */
    26 package com.sun.tools.javac.code;
    28 import java.util.Locale;
    30 import javax.lang.model.type.TypeKind;
    32 import com.sun.tools.javac.api.Messages;
    33 import com.sun.tools.javac.code.Type.AnnotatedType;
    34 import com.sun.tools.javac.code.Type.ArrayType;
    35 import com.sun.tools.javac.code.Symbol.*;
    36 import com.sun.tools.javac.code.Type.*;
    37 import com.sun.tools.javac.util.List;
    38 import com.sun.tools.javac.util.ListBuffer;
    40 import static com.sun.tools.javac.code.BoundKind.*;
    41 import static com.sun.tools.javac.code.Flags.*;
    42 import static com.sun.tools.javac.code.TypeTag.CLASS;
    43 import static com.sun.tools.javac.code.TypeTag.FORALL;
    45 /**
    46  * A combined type/symbol visitor for generating non-trivial localized string
    47  * representation of types and symbols.
    48  *
    49  * <p><b>This is NOT part of any supported API.
    50  * If you write code that depends on this, you do so at your own risk.
    51  * This code and its internal interfaces are subject to change or
    52  * deletion without notice.</b>
    53  */
    54 public abstract class Printer implements Type.Visitor<String, Locale>, Symbol.Visitor<String, Locale> {
    56     List<Type> seenCaptured = List.nil();
    57     static final int PRIME = 997;  // largest prime less than 1000
    59     protected Printer() { }
    61     /**
    62      * This method should be overriden in order to provide proper i18n support.
    63      *
    64      * @param locale the locale in which the string is to be rendered
    65      * @param key the key corresponding to the message to be displayed
    66      * @param args a list of optional arguments
    67      * @return localized string representation
    68      */
    69     protected abstract String localize(Locale locale, String key, Object... args);
    71     /**
    72      * Maps a captured type into an unique identifier.
    73      *
    74      * @param t the captured type for which an id is to be retrieved
    75      * @param locale locale settings
    76      * @return unique id representing this captured type
    77      */
    78     protected abstract String capturedVarId(CapturedType t, Locale locale);
    80     /**
    81      * Create a printer with default i18n support provided by Messages. By default,
    82      * captured types ids are generated using hashcode.
    83      *
    84      * @param messages Messages class to be used for i18n
    85      * @return printer visitor instance
    86      */
    87     public static Printer createStandardPrinter(final Messages messages) {
    88         return new Printer() {
    89             @Override
    90             protected String localize(Locale locale, String key, Object... args) {
    91                 return messages.getLocalizedString(locale, key, args);
    92             }
    94             @Override
    95             protected String capturedVarId(CapturedType t, Locale locale) {
    96                 return (t.hashCode() & 0xFFFFFFFFL) % PRIME + "";
    97         }};
    98     }
   100     /**
   101      * Get a localized string representation for all the types in the input list.
   102      *
   103      * @param ts types to be displayed
   104      * @param locale the locale in which the string is to be rendered
   105      * @return localized string representation
   106      */
   107     public String visitTypes(List<Type> ts, Locale locale) {
   108         ListBuffer<String> sbuf = ListBuffer.lb();
   109         for (Type t : ts) {
   110             sbuf.append(visit(t, locale));
   111         }
   112         return sbuf.toList().toString();
   113     }
   115     /**
   116      * * Get a localized string representation for all the symbols in the input list.
   117      *
   118      * @param ts symbols to be displayed
   119      * @param locale the locale in which the string is to be rendered
   120      * @return localized string representation
   121      */
   122     public String visitSymbols(List<Symbol> ts, Locale locale) {
   123         ListBuffer<String> sbuf = ListBuffer.lb();
   124         for (Symbol t : ts) {
   125             sbuf.append(visit(t, locale));
   126         }
   127         return sbuf.toList().toString();
   128     }
   130     /**
   131      * Get a localized string representation for a given type.
   132      *
   133      * @param t type to be displayed
   134      * @param locale the locale in which the string is to be rendered
   135      * @return localized string representation
   136      */
   137     public String visit(Type t, Locale locale) {
   138         return t.accept(this, locale);
   139     }
   141     /**
   142      * Get a localized string representation for a given symbol.
   143      *
   144      * @param s symbol to be displayed
   145      * @param locale the locale in which the string is to be rendered
   146      * @return localized string representation
   147      */
   148     public String visit(Symbol s, Locale locale) {
   149         return s.accept(this, locale);
   150     }
   152     @Override
   153     public String visitCapturedType(CapturedType t, Locale locale) {
   154         if (seenCaptured.contains(t))
   155             return localize(locale, "compiler.misc.type.captureof.1",
   156                 capturedVarId(t, locale));
   157         else {
   158             try {
   159                 seenCaptured = seenCaptured.prepend(t);
   160                 return localize(locale, "compiler.misc.type.captureof",
   161                     capturedVarId(t, locale),
   162                     visit(t.wildcard, locale));
   163             }
   164             finally {
   165                 seenCaptured = seenCaptured.tail;
   166             }
   167         }
   168     }
   170     @Override
   171     public String visitForAll(ForAll t, Locale locale) {
   172         return "<" + visitTypes(t.tvars, locale) + ">" + visit(t.qtype, locale);
   173     }
   175     @Override
   176     public String visitUndetVar(UndetVar t, Locale locale) {
   177         if (t.inst != null) {
   178             return visit(t.inst, locale);
   179         } else {
   180             return visit(t.qtype, locale) + "?";
   181         }
   182     }
   184     @Override
   185     public String visitArrayType(ArrayType t, Locale locale) {
   186         StringBuilder res = new StringBuilder();
   187         printBaseElementType(t, res, locale);
   188         printBrackets(t, res, locale);
   189         return res.toString();
   190     }
   192     void printBaseElementType(Type t, StringBuilder sb, Locale locale) {
   193         Type arrel = t;
   194         while (arrel.getKind() == TypeKind.ARRAY) {
   195             arrel = arrel.unannotatedType();
   196             arrel = ((ArrayType) arrel).elemtype;
   197         }
   198         sb.append(visit(arrel, locale));
   199     }
   201     void printBrackets(Type t, StringBuilder sb, Locale locale) {
   202         Type arrel = t;
   203         while (arrel.getKind() == TypeKind.ARRAY) {
   204             if (arrel.isAnnotated()) {
   205                 sb.append(' ');
   206                 sb.append(arrel.getAnnotationMirrors());
   207                 sb.append(' ');
   208             }
   209             sb.append("[]");
   210             arrel = arrel.unannotatedType();
   211             arrel = ((ArrayType) arrel).elemtype;
   212         }
   213     }
   215     @Override
   216     public String visitClassType(ClassType t, Locale locale) {
   217         StringBuilder buf = new StringBuilder();
   218         if (t.getEnclosingType().hasTag(CLASS) && t.tsym.owner.kind == Kinds.TYP) {
   219             buf.append(visit(t.getEnclosingType(), locale));
   220             buf.append('.');
   221             buf.append(className(t, false, locale));
   222         } else {
   223             buf.append(className(t, true, locale));
   224         }
   225         if (t.getTypeArguments().nonEmpty()) {
   226             buf.append('<');
   227             buf.append(visitTypes(t.getTypeArguments(), locale));
   228             buf.append('>');
   229         }
   230         return buf.toString();
   231     }
   233     @Override
   234     public String visitMethodType(MethodType t, Locale locale) {
   235         return "(" + printMethodArgs(t.argtypes, false, locale) + ")" + visit(t.restype, locale);
   236     }
   238     @Override
   239     public String visitPackageType(PackageType t, Locale locale) {
   240         return t.tsym.getQualifiedName().toString();
   241     }
   243     @Override
   244     public String visitWildcardType(WildcardType t, Locale locale) {
   245         StringBuilder s = new StringBuilder();
   246         s.append(t.kind);
   247         if (t.kind != UNBOUND) {
   248             s.append(visit(t.type, locale));
   249         }
   250         return s.toString();
   251     }
   253     @Override
   254     public String visitErrorType(ErrorType t, Locale locale) {
   255         return visitType(t, locale);
   256     }
   258     @Override
   259     public String visitTypeVar(TypeVar t, Locale locale) {
   260         return visitType(t, locale);
   261     }
   263     @Override
   264     public String visitAnnotatedType(AnnotatedType t, Locale locale) {
   265         if (t.typeAnnotations != null &&
   266                 t.typeAnnotations.nonEmpty()) {
   267             if (t.underlyingType.getKind() == TypeKind.ARRAY) {
   268                 StringBuilder res = new StringBuilder();
   269                 printBaseElementType(t, res, locale);
   270                 printBrackets(t, res, locale);
   271                 return res.toString();
   272             } else if (t.underlyingType.getKind() == TypeKind.DECLARED &&
   273                     t.underlyingType.getEnclosingType() != Type.noType) {
   274                 return visit(t.underlyingType.getEnclosingType(), locale) +
   275                         ". " +
   276                         t.typeAnnotations +
   277                         " " + className((ClassType)t.underlyingType, false, locale);
   278             } else {
   279                 return t.typeAnnotations + " " + visit(t.underlyingType, locale);
   280             }
   281         } else {
   282             return visit(t.underlyingType, locale);
   283         }
   284     }
   286     public String visitType(Type t, Locale locale) {
   287         String s = (t.tsym == null || t.tsym.name == null)
   288                 ? localize(locale, "compiler.misc.type.none")
   289                 : t.tsym.name.toString();
   290         return s;
   291     }
   293     /**
   294      * Converts a class name into a (possibly localized) string. Anonymous
   295      * inner classes get converted into a localized string.
   296      *
   297      * @param t the type of the class whose name is to be rendered
   298      * @param longform if set, the class' fullname is displayed - if unset the
   299      * short name is chosen (w/o package)
   300      * @param locale the locale in which the string is to be rendered
   301      * @return localized string representation
   302      */
   303     protected String className(ClassType t, boolean longform, Locale locale) {
   304         Symbol sym = t.tsym;
   305         if (sym.name.length() == 0 && (sym.flags() & COMPOUND) != 0) {
   306             StringBuilder s = new StringBuilder(visit(t.supertype_field, locale));
   307             for (List<Type> is = t.interfaces_field; is.nonEmpty(); is = is.tail) {
   308                 s.append('&');
   309                 s.append(visit(is.head, locale));
   310             }
   311             return s.toString();
   312         } else if (sym.name.length() == 0) {
   313             String s;
   314             ClassType norm = (ClassType) t.tsym.type;
   315             if (norm == null) {
   316                 s = localize(locale, "compiler.misc.anonymous.class", (Object) null);
   317             } else if (norm.interfaces_field != null && norm.interfaces_field.nonEmpty()) {
   318                 s = localize(locale, "compiler.misc.anonymous.class",
   319                         visit(norm.interfaces_field.head, locale));
   320             } else {
   321                 s = localize(locale, "compiler.misc.anonymous.class",
   322                         visit(norm.supertype_field, locale));
   323             }
   324             return s;
   325         } else if (longform) {
   326             return sym.getQualifiedName().toString();
   327         } else {
   328             return sym.name.toString();
   329         }
   330     }
   332     /**
   333      * Converts a set of method argument types into their corresponding
   334      * localized string representation.
   335      *
   336      * @param args arguments to be rendered
   337      * @param varArgs if true, the last method argument is regarded as a vararg
   338      * @param locale the locale in which the string is to be rendered
   339      * @return localized string representation
   340      */
   341     protected String printMethodArgs(List<Type> args, boolean varArgs, Locale locale) {
   342         if (!varArgs) {
   343             return visitTypes(args, locale);
   344         } else {
   345             StringBuilder buf = new StringBuilder();
   346             while (args.tail.nonEmpty()) {
   347                 buf.append(visit(args.head, locale));
   348                 args = args.tail;
   349                 buf.append(',');
   350             }
   351             if (args.head.unannotatedType().getKind() == TypeKind.ARRAY) {
   352                 buf.append(visit(((ArrayType) args.head.unannotatedType()).elemtype, locale));
   353                 if (args.head.getAnnotationMirrors().nonEmpty()) {
   354                     buf.append(' ');
   355                     buf.append(args.head.getAnnotationMirrors());
   356                     buf.append(' ');
   357                 }
   358                 buf.append("...");
   359             } else {
   360                 buf.append(visit(args.head, locale));
   361             }
   362             return buf.toString();
   363         }
   364     }
   366     @Override
   367     public String visitClassSymbol(ClassSymbol sym, Locale locale) {
   368         return sym.name.isEmpty()
   369                 ? localize(locale, "compiler.misc.anonymous.class", sym.flatname)
   370                 : sym.fullname.toString();
   371     }
   373     @Override
   374     public String visitMethodSymbol(MethodSymbol s, Locale locale) {
   375         if (s.isStaticOrInstanceInit()) {
   376             return s.owner.name.toString();
   377         } else {
   378             String ms = (s.name == s.name.table.names.init)
   379                     ? s.owner.name.toString()
   380                     : s.name.toString();
   381             if (s.type != null) {
   382                 if (s.type.hasTag(FORALL)) {
   383                     ms = "<" + visitTypes(s.type.getTypeArguments(), locale) + ">" + ms;
   384                 }
   385                 ms += "(" + printMethodArgs(
   386                         s.type.getParameterTypes(),
   387                         (s.flags() & VARARGS) != 0,
   388                         locale) + ")";
   389             }
   390             return ms;
   391         }
   392     }
   394     @Override
   395     public String visitOperatorSymbol(OperatorSymbol s, Locale locale) {
   396         return visitMethodSymbol(s, locale);
   397     }
   399     @Override
   400     public String visitPackageSymbol(PackageSymbol s, Locale locale) {
   401         return s.isUnnamed()
   402                 ? localize(locale, "compiler.misc.unnamed.package")
   403                 : s.fullname.toString();
   404     }
   406     @Override
   407     public String visitTypeSymbol(TypeSymbol s, Locale locale) {
   408         return visitSymbol(s, locale);
   409     }
   411     @Override
   412     public String visitVarSymbol(VarSymbol s, Locale locale) {
   413         return visitSymbol(s, locale);
   414     }
   416     @Override
   417     public String visitSymbol(Symbol s, Locale locale) {
   418         return s.name.toString();
   419     }
   420 }

mercurial