src/share/classes/com/sun/tools/javadoc/TypeVariableImpl.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

     1 /*
     2  * Copyright (c) 2003, 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.javadoc;
    28 import com.sun.javadoc.*;
    30 import com.sun.tools.javac.code.Attribute;
    31 import com.sun.tools.javac.code.Attribute.TypeCompound;
    32 import com.sun.tools.javac.code.Kinds;
    33 import com.sun.tools.javac.code.Symbol;
    34 import com.sun.tools.javac.code.Symbol.ClassSymbol;
    35 import com.sun.tools.javac.code.Symbol.MethodSymbol;
    36 import com.sun.tools.javac.code.Type;
    37 import com.sun.tools.javac.code.Type.TypeVar;
    38 import com.sun.tools.javac.util.List;
    39 import com.sun.tools.javac.util.Name;
    40 import com.sun.tools.javac.util.Names;
    42 /**
    43  * Implementation of <code>TypeVariable</code>, which
    44  * represents a type variable.
    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  * @author Scott Seligman
    52  * @since 1.5
    53  */
    54 public class TypeVariableImpl extends AbstractTypeImpl implements TypeVariable {
    56     TypeVariableImpl(DocEnv env, TypeVar type) {
    57         super(env, type);
    58     }
    60     /**
    61      * Return the bounds of this type variable.
    62      */
    63     public com.sun.javadoc.Type[] bounds() {
    64         return TypeMaker.getTypes(env, getBounds((TypeVar)type, env));
    65     }
    67     /**
    68      * Return the class, interface, method, or constructor within
    69      * which this type variable is declared.
    70      */
    71     public ProgramElementDoc owner() {
    72         Symbol osym = type.tsym.owner;
    73         if ((osym.kind & Kinds.TYP) != 0) {
    74             return env.getClassDoc((ClassSymbol)osym);
    75         }
    76         Names names = osym.name.table.names;
    77         if (osym.name == names.init) {
    78             return env.getConstructorDoc((MethodSymbol)osym);
    79         } else {
    80             return env.getMethodDoc((MethodSymbol)osym);
    81         }
    82     }
    84     /**
    85      * Return the ClassDoc of the erasure of this type variable.
    86      */
    87     @Override
    88     public ClassDoc asClassDoc() {
    89         return env.getClassDoc((ClassSymbol)env.types.erasure(type).tsym);
    90     }
    92     @Override
    93     public TypeVariable asTypeVariable() {
    94         return this;
    95     }
    97     @Override
    98     public String toString() {
    99         return typeVarToString(env, (TypeVar)type, true);
   100     }
   103     /**
   104      * Return the string form of a type variable along with any
   105      * "extends" clause.  Class names are qualified if "full" is true.
   106      */
   107     static String typeVarToString(DocEnv env, TypeVar v, boolean full) {
   108         StringBuilder s = new StringBuilder(v.toString());
   109         List<Type> bounds = getBounds(v, env);
   110         if (bounds.nonEmpty()) {
   111             boolean first = true;
   112             for (Type b : bounds) {
   113                 s.append(first ? " extends " : " & ");
   114                 s.append(TypeMaker.getTypeString(env, b, full));
   115                 first = false;
   116             }
   117         }
   118         return s.toString();
   119     }
   121     /**
   122      * Get the bounds of a type variable as listed in the "extends" clause.
   123      */
   124     private static List<Type> getBounds(TypeVar v, DocEnv env) {
   125         final Type upperBound = v.getUpperBound();
   126         Name boundname = upperBound.tsym.getQualifiedName();
   127         if (boundname == boundname.table.names.java_lang_Object
   128             && !upperBound.isAnnotated()) {
   129             return List.nil();
   130         } else {
   131             return env.types.getBounds(v);
   132         }
   133     }
   135     /**
   136      * Get the annotations of this program element.
   137      * Return an empty array if there are none.
   138      */
   139     public AnnotationDesc[] annotations() {
   140         if (!type.isAnnotated()) {
   141             return new AnnotationDesc[0];
   142         }
   143         List<? extends TypeCompound> tas = type.getAnnotationMirrors();
   144         AnnotationDesc res[] = new AnnotationDesc[tas.length()];
   145         int i = 0;
   146         for (Attribute.Compound a : tas) {
   147             res[i++] = new AnnotationDescImpl(env, a);
   148         }
   149         return res;
   150     }
   151 }

mercurial