langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/model/JavacTypes.java
author jlahoda
Wed, 27 Aug 2014 07:44:00 +0200
changeset 26266 2d24bda701dc
parent 25874 83c19f00452c
child 26781 a786b07c7b91
permissions -rw-r--r--
8056061: Mark implementations of public interfaces with an annotation Summary: Adding @DefinedBy annotation to mark methods that implement public API methods; annotating the methods; adding a coding rules analyzer to enforce all such methods are annotated. Reviewed-by: jjg, mcimadamore, jfranck Contributed-by: jan.lahoda@oracle.com, jonathan.gibbons@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 22163
diff changeset
     2
 * Copyright (c) 2005, 2014, 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
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.model;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
14952
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14725
diff changeset
    28
import java.util.Collections;
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14725
diff changeset
    29
import java.util.EnumSet;
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14725
diff changeset
    30
import java.util.LinkedHashSet;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.util.List;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.util.Set;
14952
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14725
diff changeset
    33
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import javax.lang.model.element.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import javax.lang.model.type.*;
14952
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14725
diff changeset
    36
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import com.sun.tools.javac.code.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import com.sun.tools.javac.code.Symbol.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import com.sun.tools.javac.util.*;
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    40
import com.sun.tools.javac.util.DefinedBy.Api;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * Utility methods for operating on types.
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    45
 * <p><b>This is NOT part of any supported API.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 * If you write code that depends on this, you do so at your own
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 * risk.  This code and its internal interfaces are subject to change
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 * or deletion without notice.</b></p>
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
public class JavacTypes implements javax.lang.model.util.Types {
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 22163
diff changeset
    52
    private final Symtab syms;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 22163
diff changeset
    53
    private final Types types;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    public static JavacTypes instance(Context context) {
6924
ef7a9281ad2f 6988836: A new JavacElements is created for each round of annotation processing
jjg
parents: 6593
diff changeset
    56
        JavacTypes instance = context.get(JavacTypes.class);
ef7a9281ad2f 6988836: A new JavacElements is created for each round of annotation processing
jjg
parents: 6593
diff changeset
    57
        if (instance == null)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
            instance = new JavacTypes(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
6924
ef7a9281ad2f 6988836: A new JavacElements is created for each round of annotation processing
jjg
parents: 6593
diff changeset
    62
    protected JavacTypes(Context context) {
ef7a9281ad2f 6988836: A new JavacElements is created for each round of annotation processing
jjg
parents: 6593
diff changeset
    63
        context.put(JavacTypes.class, this);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
        syms = Symtab.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
        types = Types.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    68
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    public Element asElement(TypeMirror t) {
9599
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 8842
diff changeset
    70
        switch (t.getKind()) {
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 8842
diff changeset
    71
            case DECLARED:
14725
65836e833f59 8002099: Add support for intersection types in cast expression
mcimadamore
parents: 9599
diff changeset
    72
            case INTERSECTION:
9599
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 8842
diff changeset
    73
            case ERROR:
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 8842
diff changeset
    74
            case TYPEVAR:
0996df19ea87 7029150: Project Coin: present union types from the tree API through to javax.lang.model
jjg
parents: 8842
diff changeset
    75
                Type type = cast(Type.class, t);
8842
d532916921b7 7026414: Types.asElement() returns null for ErrorType
jjg
parents: 7681
diff changeset
    76
                return type.asElement();
d532916921b7 7026414: Types.asElement() returns null for ErrorType
jjg
parents: 7681
diff changeset
    77
            default:
d532916921b7 7026414: Types.asElement() returns null for ErrorType
jjg
parents: 7681
diff changeset
    78
                return null;
d532916921b7 7026414: Types.asElement() returns null for ErrorType
jjg
parents: 7681
diff changeset
    79
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    82
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    public boolean isSameType(TypeMirror t1, TypeMirror t2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        return types.isSameType((Type) t1, (Type) t2);
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    87
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
    public boolean isSubtype(TypeMirror t1, TypeMirror t2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        validateTypeNotIn(t1, EXEC_OR_PKG);
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        validateTypeNotIn(t2, EXEC_OR_PKG);
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        return types.isSubtype((Type) t1, (Type) t2);
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    94
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    public boolean isAssignable(TypeMirror t1, TypeMirror t2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        validateTypeNotIn(t1, EXEC_OR_PKG);
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        validateTypeNotIn(t2, EXEC_OR_PKG);
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        return types.isAssignable((Type) t1, (Type) t2);
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   101
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
    public boolean contains(TypeMirror t1, TypeMirror t2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        validateTypeNotIn(t1, EXEC_OR_PKG);
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        validateTypeNotIn(t2, EXEC_OR_PKG);
6593
4d13b5b812ef 6981185: com.sun.tools.model.JavacTypes.contains() calls Type.contains instead of Types.containsType
mcimadamore
parents: 5847
diff changeset
   105
        return types.containsType((Type) t1, (Type) t2);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   108
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
    public boolean isSubsignature(ExecutableType m1, ExecutableType m2) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
        return types.isSubSignature((Type) m1, (Type) m2);
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   113
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    public List<Type> directSupertypes(TypeMirror t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        validateTypeNotIn(t, EXEC_OR_PKG);
20252
fae5c47ba7a4 6499673: Assertion check for TypeVariable.getUpperBound() fails.
emc
parents: 18646
diff changeset
   116
        return types.directSupertypes((Type) t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   119
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
    public TypeMirror erasure(TypeMirror t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        if (t.getKind() == TypeKind.PACKAGE)
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
            throw new IllegalArgumentException(t.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        return types.erasure((Type) t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   126
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
    public TypeElement boxedClass(PrimitiveType p) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        return types.boxedClass((Type) p);
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   131
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
    public PrimitiveType unboxedType(TypeMirror t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
        if (t.getKind() != TypeKind.DECLARED)
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
            throw new IllegalArgumentException(t.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        Type unboxed = types.unboxedType((Type) t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
        if (! unboxed.isPrimitive())    // only true primitives, not void
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
            throw new IllegalArgumentException(t.toString());
18646
e628560a86d1 8017104: javac should have a class for primitive types that inherits from Type
vromero
parents: 16557
diff changeset
   138
        return (PrimitiveType)unboxed;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   141
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
    public TypeMirror capture(TypeMirror t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
        validateTypeNotIn(t, EXEC_OR_PKG);
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        return types.capture((Type) t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   147
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
    public PrimitiveType getPrimitiveType(TypeKind kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
        switch (kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
        case BOOLEAN:   return syms.booleanType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        case BYTE:      return syms.byteType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        case SHORT:     return syms.shortType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        case INT:       return syms.intType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        case LONG:      return syms.longType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
        case CHAR:      return syms.charType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
        case FLOAT:     return syms.floatType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
        case DOUBLE:    return syms.doubleType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
            throw new IllegalArgumentException("Not a primitive type: " + kind);
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   163
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
    public NullType getNullType() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
        return (NullType) syms.botType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   168
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
    public NoType getNoType(TypeKind kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
        switch (kind) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        case VOID:      return syms.voidType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        case NONE:      return Type.noType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
            throw new IllegalArgumentException(kind.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   178
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
    public ArrayType getArrayType(TypeMirror componentType) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
        switch (componentType.getKind()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        case VOID:
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
        case EXECUTABLE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
        case WILDCARD:  // heh!
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
        case PACKAGE:
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
            throw new IllegalArgumentException(componentType.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
        }
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 23810
diff changeset
   187
        return new Type.ArrayType((Type) componentType, syms.arrayClass,
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 23810
diff changeset
   188
                                  Type.noAnnotations);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   191
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
    public WildcardType getWildcardType(TypeMirror extendsBound,
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
                                        TypeMirror superBound) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
        BoundKind bkind;
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
        Type bound;
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
        if (extendsBound == null && superBound == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
            bkind = BoundKind.UNBOUND;
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
            bound = syms.objectType;
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
        } else if (superBound == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
            bkind = BoundKind.EXTENDS;
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
            bound = (Type) extendsBound;
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
        } else if (extendsBound == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
            bkind = BoundKind.SUPER;
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
            bound = (Type) superBound;
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
            throw new IllegalArgumentException(
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
                    "Extends and super bounds cannot both be provided");
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
        switch (bound.getKind()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
        case ARRAY:
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
        case DECLARED:
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
        case ERROR:
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
        case TYPEVAR:
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 23810
diff changeset
   214
            return new Type.WildcardType(bound, bkind, syms.boundClass,
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 23810
diff changeset
   215
                                         Type.noAnnotations);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
        default:
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
            throw new IllegalArgumentException(bound.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   221
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
    public DeclaredType getDeclaredType(TypeElement typeElem,
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
                                        TypeMirror... typeArgs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
        ClassSymbol sym = (ClassSymbol) typeElem;
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
        if (typeArgs.length == 0)
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
            return (DeclaredType) sym.erasure(types);
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
        if (sym.type.getEnclosingType().isParameterized())
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
            throw new IllegalArgumentException(sym.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
        return getDeclaredType0(sym.type.getEnclosingType(), sym, typeArgs);
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   234
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
    public DeclaredType getDeclaredType(DeclaredType enclosing,
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
                                        TypeElement typeElem,
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
                                        TypeMirror... typeArgs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
        if (enclosing == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
            return getDeclaredType(typeElem, typeArgs);
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
        ClassSymbol sym = (ClassSymbol) typeElem;
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
        Type outer = (Type) enclosing;
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
        if (outer.tsym != sym.owner.enclClass())
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
            throw new IllegalArgumentException(enclosing.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
        if (!outer.isParameterized())
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
            return getDeclaredType(typeElem, typeArgs);
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
        return getDeclaredType0(outer, sym, typeArgs);
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
    // where
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
        private DeclaredType getDeclaredType0(Type outer,
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
                                              ClassSymbol sym,
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
                                              TypeMirror... typeArgs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
            if (typeArgs.length != sym.type.getTypeArguments().length())
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
                throw new IllegalArgumentException(
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
                "Incorrect number of type arguments");
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20252
diff changeset
   259
            ListBuffer<Type> targs = new ListBuffer<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
            for (TypeMirror t : typeArgs) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
                if (!(t instanceof ReferenceType || t instanceof WildcardType))
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
                    throw new IllegalArgumentException(t.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
                targs.append((Type) t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
            // TODO: Would like a way to check that type args match formals.
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
24396
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 23810
diff changeset
   267
            return (DeclaredType) new Type.ClassType(outer, targs.toList(), sym,
3c36c6afcbca 8040327: Eliminate AnnotatedType
emc
parents: 23810
diff changeset
   268
                                                     Type.noAnnotations);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
     * Returns the type of an element when that element is viewed as
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
     * a member of, or otherwise directly contained by, a given type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
     * For example,
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
     * when viewed as a member of the parameterized type {@code Set<String>},
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
     * the {@code Set.add} method is an {@code ExecutableType}
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
     * whose parameter is of type {@code String}.
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
     * @param containing  the containing type
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
     * @param element     the element
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
     * @return the type of the element as viewed from the containing type
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
     * @throws IllegalArgumentException if the element is not a valid one
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
     *          for the given type
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
     */
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   285
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
    public TypeMirror asMemberOf(DeclaredType containing, Element element) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
        Type site = (Type)containing;
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
        Symbol sym = (Symbol)element;
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
        if (types.asSuper(site, sym.getEnclosingElement()) == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
            throw new IllegalArgumentException(sym + "@" + site);
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
        return types.memberType(site, sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
    private static final Set<TypeKind> EXEC_OR_PKG =
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
            EnumSet.of(TypeKind.EXECUTABLE, TypeKind.PACKAGE);
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
     * Throws an IllegalArgumentException if a type's kind is one of a set.
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
    private void validateTypeNotIn(TypeMirror t, Set<TypeKind> invalidKinds) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
        if (invalidKinds.contains(t.getKind()))
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
            throw new IllegalArgumentException(t.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
     * Returns an object cast to the specified type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
     * @throws NullPointerException if the object is {@code null}
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
     * @throws IllegalArgumentException if the object is of the wrong type
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
    private static <T> T cast(Class<T> clazz, Object o) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
        if (! clazz.isInstance(o))
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
            throw new IllegalArgumentException(o.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
        return clazz.cast(o);
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
    }
14952
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14725
diff changeset
   316
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14725
diff changeset
   317
    public Set<MethodSymbol> getOverriddenMethods(Element elem) {
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14725
diff changeset
   318
        if (elem.getKind() != ElementKind.METHOD
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14725
diff changeset
   319
                || elem.getModifiers().contains(Modifier.STATIC)
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14725
diff changeset
   320
                || elem.getModifiers().contains(Modifier.PRIVATE))
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14725
diff changeset
   321
            return Collections.emptySet();
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14725
diff changeset
   322
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14725
diff changeset
   323
        if (!(elem instanceof MethodSymbol))
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14725
diff changeset
   324
            throw new IllegalArgumentException();
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14725
diff changeset
   325
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14725
diff changeset
   326
        MethodSymbol m = (MethodSymbol) elem;
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14725
diff changeset
   327
        ClassSymbol origin = (ClassSymbol) m.owner;
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14725
diff changeset
   328
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 20252
diff changeset
   329
        Set<MethodSymbol> results = new LinkedHashSet<>();
14952
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14725
diff changeset
   330
        for (Type t : types.closure(origin.type)) {
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14725
diff changeset
   331
            if (t != origin.type) {
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14725
diff changeset
   332
                ClassSymbol c = (ClassSymbol) t.tsym;
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 24396
diff changeset
   333
                for (Symbol sym : c.members().getSymbolsByName(m.name)) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 24396
diff changeset
   334
                    if (sym.kind == Kinds.MTH && m.overrides(sym, origin, types, true)) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 24396
diff changeset
   335
                        results.add((MethodSymbol) sym);
14952
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14725
diff changeset
   336
                    }
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14725
diff changeset
   337
                }
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14725
diff changeset
   338
            }
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14725
diff changeset
   339
        }
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14725
diff changeset
   340
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14725
diff changeset
   341
        return results;
d0022ae20516 8004832: Add new doclint package
jjg
parents: 14725
diff changeset
   342
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
}