langtools/src/jdk.javadoc/share/classes/com/sun/tools/javadoc/main/WildcardTypeImpl.java
author jjg
Mon, 09 May 2016 16:52:15 -0700
changeset 37938 42baa89d2156
parent 25874 langtools/src/jdk.javadoc/share/classes/com/sun/tools/javadoc/WildcardTypeImpl.java@83c19f00452c
child 38617 d93a7f64e231
permissions -rw-r--r--
8075703: jdk.javadoc module exports com.sun.tools.javadoc package which contains a lot of internal API. Reviewed-by: ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 8631
diff changeset
     2
 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
37938
42baa89d2156 8075703: jdk.javadoc module exports com.sun.tools.javadoc package which contains a lot of internal API.
jjg
parents: 25874
diff changeset
    26
package com.sun.tools.javadoc.main;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import com.sun.javadoc.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.tools.javac.code.Symbol.ClassSymbol;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import com.sun.tools.javac.code.Type;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import com.sun.tools.javac.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
 * Implementation of <code>WildcardType</code>, which
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 * represents a wildcard type.
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 *
14260
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    39
 *  <p><b>This is NOT part of any supported API.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    40
 *  If you write code that depends on this, you do so at your own risk.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    41
 *  This code and its internal interfaces are subject to change or
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    42
 *  deletion without notice.</b>
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    43
 *
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 * @author Scott Seligman
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 * @since 1.5
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
public class WildcardTypeImpl extends AbstractTypeImpl implements WildcardType {
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    WildcardTypeImpl(DocEnv env, Type.WildcardType type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
        super(env, type);
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
     * Return the upper bounds of this wildcard type argument
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
     * as given by the <i>extends</i> clause.
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
     * Return an empty array if no such bounds are explicitly given.
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    public com.sun.javadoc.Type[] extendsBounds() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        return TypeMaker.getTypes(env, getExtendsBounds((Type.WildcardType)type));
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     * Return the lower bounds of this wildcard type argument
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
     * as given by the <i>super</i> clause.
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
     * Return an empty array if no such bounds are explicitly given.
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    public com.sun.javadoc.Type[] superBounds() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        return TypeMaker.getTypes(env, getSuperBounds((Type.WildcardType)type));
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
     * Return the ClassDoc of the erasure of this wildcard type.
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     */
8631
664f84942e74 6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents: 5520
diff changeset
    74
    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    public ClassDoc asClassDoc() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
        return env.getClassDoc((ClassSymbol)env.types.erasure(type).tsym);
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
8631
664f84942e74 6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents: 5520
diff changeset
    79
    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    public WildcardType asWildcardType() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        return this;
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
8631
664f84942e74 6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents: 5520
diff changeset
    84
    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    public String typeName()            { return "?"; }
8631
664f84942e74 6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents: 5520
diff changeset
    86
    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    public String qualifiedTypeName()   { return "?"; }
8631
664f84942e74 6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents: 5520
diff changeset
    88
    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    public String simpleTypeName()      { return "?"; }
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
8631
664f84942e74 6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents: 5520
diff changeset
    91
    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
    public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        return wildcardTypeToString(env, (Type.WildcardType)type, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
     * Return the string form of a wildcard type ("?") along with any
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
     * "extends" or "super" clause.  Delimiting brackets are not
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
     * included.  Class names are qualified if "full" is true.
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
    static String wildcardTypeToString(DocEnv env,
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
                                       Type.WildcardType wildThing, boolean full) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        if (env.legacyDoclet) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
            return TypeMaker.getTypeName(env.types.erasure(wildThing), full);
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        }
8631
664f84942e74 6866185: Util.getPackageSourcePath should use lastIndexOf not indexOf and related cleanup
jjg
parents: 5520
diff changeset
   107
        StringBuilder s = new StringBuilder("?");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        List<Type> bounds = getExtendsBounds(wildThing);
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        if (bounds.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
            s.append(" extends ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
            bounds = getSuperBounds(wildThing);
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
            if (bounds.nonEmpty()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
                s.append(" super ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        boolean first = true;   // currently only one bound is allowed
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        for (Type b : bounds) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
            if (!first) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
                s.append(" & ");
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
            s.append(TypeMaker.getTypeString(env, b, full));
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
            first = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        return s.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
    private static List<Type> getExtendsBounds(Type.WildcardType wild) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        return wild.isSuperBound()
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
                ? List.<Type>nil()
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
                : List.of(wild.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    private static List<Type> getSuperBounds(Type.WildcardType wild) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        return wild.isExtendsBound()
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
                ? List.<Type>nil()
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
                : List.of(wild.type);
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
}