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

Thu, 01 Nov 2012 10:48:36 +0100

author
ohrstrom
date
Thu, 01 Nov 2012 10:48:36 +0100
changeset 1384
bf54daa9dcd8
parent 1374
c002fdee76fd
child 1521
71f35e4b93a5
permissions
-rw-r--r--

7153951: Add new lint option -Xlint:auxiliaryclass
Reviewed-by: jjg, mcimadamore, forax

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

mercurial