src/jdk.compiler/share/classes/com/sun/tools/javac/model/JavacElements.java
author jlahoda
Fri, 09 Mar 2018 09:42:10 +0100
changeset 49197 cc2673fa8c20
parent 48945 6e6c777a37a2
child 49917 1871c5d07caf
permissions -rw-r--r--
8187950: javax.lang.model APIs throws CompletionFailure or a subtype of CompletionFailure. Summary: Catching CompletionFailures that would be thrown to API clients, and re-completing the symbols again when javac itself needs it. Reviewed-by: cushon, jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
48945
6e6c777a37a2 8186688: javax.lang.model.util.Elements.hides does not work correctly with interfaces
jjg
parents: 48085
diff changeset
     2
 * Copyright (c) 2005, 2018, 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: 1264
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: 1264
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: 1264
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
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
43857
37614a3b7193 8173945: Add methods for Elements.getAll{Type, Package, Module}Elements
darcy
parents: 43591
diff changeset
    28
import java.util.Collections;
43149
047a57b0839a 8172262: packages missing from docs build
jlahoda
parents: 43142
diff changeset
    29
import java.util.HashSet;
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
    30
import java.util.LinkedHashSet;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.util.Map;
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
    32
import java.util.Set;
43149
047a57b0839a 8172262: packages missing from docs build
jlahoda
parents: 43142
diff changeset
    33
import java.util.stream.Collectors;
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 10950
diff changeset
    34
42826
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
    35
import javax.lang.model.AnnotatedConstruct;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import javax.lang.model.SourceVersion;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import javax.lang.model.element.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import javax.lang.model.type.DeclaredType;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import javax.lang.model.util.Elements;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
import javax.tools.JavaFileObject;
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 10950
diff changeset
    41
import static javax.lang.model.util.ElementFilter.methodsIn;
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 10950
diff changeset
    42
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
    43
import com.sun.source.util.JavacTask;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
    44
import com.sun.tools.javac.api.JavacTaskImpl;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
import com.sun.tools.javac.code.*;
42826
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
    46
import com.sun.tools.javac.code.Attribute.Compound;
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
    47
import com.sun.tools.javac.code.Directive.ExportsDirective;
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
    48
import com.sun.tools.javac.code.Directive.ExportsFlag;
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
    49
import com.sun.tools.javac.code.Directive.OpensDirective;
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
    50
import com.sun.tools.javac.code.Directive.OpensFlag;
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
    51
import com.sun.tools.javac.code.Directive.RequiresDirective;
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
    52
import com.sun.tools.javac.code.Directive.RequiresFlag;
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 24069
diff changeset
    53
import com.sun.tools.javac.code.Scope.WriteableScope;
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47216
diff changeset
    54
import com.sun.tools.javac.code.Source.Feature;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
import com.sun.tools.javac.code.Symbol.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
import com.sun.tools.javac.comp.AttrContext;
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
import com.sun.tools.javac.comp.Enter;
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
import com.sun.tools.javac.comp.Env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
import com.sun.tools.javac.main.JavaCompiler;
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
import com.sun.tools.javac.processing.PrintingProcessor;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
import com.sun.tools.javac.tree.JCTree;
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
import com.sun.tools.javac.tree.JCTree.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
import com.sun.tools.javac.tree.TreeInfo;
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
import com.sun.tools.javac.tree.TreeScanner;
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
    65
import com.sun.tools.javac.util.*;
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    66
import com.sun.tools.javac.util.DefinedBy.Api;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
import com.sun.tools.javac.util.Name;
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26266
diff changeset
    68
import static com.sun.tools.javac.code.Kinds.Kind.*;
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 24069
diff changeset
    69
import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 13077
diff changeset
    70
import static com.sun.tools.javac.code.TypeTag.CLASS;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
    71
import com.sun.tools.javac.comp.Modules;
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
    72
import com.sun.tools.javac.comp.Resolve;
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
    73
import com.sun.tools.javac.comp.Resolve.RecoveryLoadClass;
43149
047a57b0839a 8172262: packages missing from docs build
jlahoda
parents: 43142
diff changeset
    74
import com.sun.tools.javac.resources.CompilerProperties.Notes;
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7681
diff changeset
    75
import static com.sun.tools.javac.tree.JCTree.Tag.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
 * Utility methods for operating on program elements.
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    80
 * <p><b>This is NOT part of any supported API.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
 * If you write code that depends on this, you do so at your own
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
 * risk.  This code and its internal interfaces are subject to change
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
 * or deletion without notice.</b></p>
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
public class JavacElements implements Elements {
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23122
diff changeset
    87
    private final JavaCompiler javaCompiler;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23122
diff changeset
    88
    private final Symtab syms;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
    89
    private final Modules modules;
23810
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23122
diff changeset
    90
    private final Names names;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23122
diff changeset
    91
    private final Types types;
b92eb80925f0 8038455: Use single Context for all rounds of annotation processing
jlahoda
parents: 23122
diff changeset
    92
    private final Enter enter;
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
    93
    private final Resolve resolve;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
    94
    private final JavacTaskImpl javacTaskImpl;
43149
047a57b0839a 8172262: packages missing from docs build
jlahoda
parents: 43142
diff changeset
    95
    private final Log log;
047a57b0839a 8172262: packages missing from docs build
jlahoda
parents: 43142
diff changeset
    96
    private final boolean allowModules;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    public static JavacElements instance(Context context) {
6924
ef7a9281ad2f 6988836: A new JavacElements is created for each round of annotation processing
jjg
parents: 5847
diff changeset
    99
        JavacElements instance = context.get(JavacElements.class);
ef7a9281ad2f 6988836: A new JavacElements is created for each round of annotation processing
jjg
parents: 5847
diff changeset
   100
        if (instance == null)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
            instance = new JavacElements(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        return instance;
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
6924
ef7a9281ad2f 6988836: A new JavacElements is created for each round of annotation processing
jjg
parents: 5847
diff changeset
   105
    protected JavacElements(Context context) {
ef7a9281ad2f 6988836: A new JavacElements is created for each round of annotation processing
jjg
parents: 5847
diff changeset
   106
        context.put(JavacElements.class, this);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        javaCompiler = JavaCompiler.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
        syms = Symtab.instance(context);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   109
        modules = Modules.instance(context);
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
   110
        names = Names.instance(context);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        types = Types.instance(context);
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
        enter = Enter.instance(context);
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   113
        resolve = Resolve.instance(context);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   114
        JavacTask t = context.get(JavacTask.class);
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   115
        javacTaskImpl = t instanceof JavacTaskImpl ? (JavacTaskImpl) t : null;
43149
047a57b0839a 8172262: packages missing from docs build
jlahoda
parents: 43142
diff changeset
   116
        log = Log.instance(context);
047a57b0839a 8172262: packages missing from docs build
jlahoda
parents: 43142
diff changeset
   117
        Source source = Source.instance(context);
48054
702043a4cdeb 8189749: Devise strategy for making source level checks more uniform
mcimadamore
parents: 47216
diff changeset
   118
        allowModules = Feature.MODULES.allowedInSource(source);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   119
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   120
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   121
    @Override @DefinedBy(Api.LANGUAGE_MODEL)
43857
37614a3b7193 8173945: Add methods for Elements.getAll{Type, Package, Module}Elements
darcy
parents: 43591
diff changeset
   122
    public Set<? extends ModuleElement> getAllModuleElements() {
37614a3b7193 8173945: Add methods for Elements.getAll{Type, Package, Module}Elements
darcy
parents: 43591
diff changeset
   123
        if (allowModules)
37614a3b7193 8173945: Add methods for Elements.getAll{Type, Package, Module}Elements
darcy
parents: 43591
diff changeset
   124
            return Collections.unmodifiableSet(modules.allModules());
37614a3b7193 8173945: Add methods for Elements.getAll{Type, Package, Module}Elements
darcy
parents: 43591
diff changeset
   125
        else
37614a3b7193 8173945: Add methods for Elements.getAll{Type, Package, Module}Elements
darcy
parents: 43591
diff changeset
   126
            return Collections.emptySet();
37614a3b7193 8173945: Add methods for Elements.getAll{Type, Package, Module}Elements
darcy
parents: 43591
diff changeset
   127
    }
37614a3b7193 8173945: Add methods for Elements.getAll{Type, Package, Module}Elements
darcy
parents: 43591
diff changeset
   128
37614a3b7193 8173945: Add methods for Elements.getAll{Type, Package, Module}Elements
darcy
parents: 43591
diff changeset
   129
    @Override @DefinedBy(Api.LANGUAGE_MODEL)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   130
    public ModuleSymbol getModuleElement(CharSequence name) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   131
        ensureEntered("getModuleElement");
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   132
        if (modules.getDefaultModule() == syms.noModule)
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   133
            return null;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   134
        String strName = name.toString();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   135
        if (strName.equals(""))
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   136
            return syms.unnamedModule;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   137
        return modules.getObservableModule(names.fromString(strName));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
37848
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36779
diff changeset
   140
    @Override @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
    public PackageSymbol getPackageElement(CharSequence name) {
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   142
        return doGetPackageElement(null, name);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   143
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   144
37848
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36779
diff changeset
   145
    @Override @DefinedBy(Api.LANGUAGE_MODEL)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   146
    public PackageSymbol getPackageElement(ModuleElement module, CharSequence name) {
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   147
        module.getClass();
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   148
        return doGetPackageElement(module, name);
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   149
    }
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   150
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   151
    private PackageSymbol doGetPackageElement(ModuleElement module, CharSequence name) {
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   152
        ensureEntered("getPackageElement");
43149
047a57b0839a 8172262: packages missing from docs build
jlahoda
parents: 43142
diff changeset
   153
        return doGetElement(module, "getPackageElement", name, PackageSymbol.class);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
37848
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36779
diff changeset
   156
    @Override @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
    public ClassSymbol getTypeElement(CharSequence name) {
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   158
        return doGetTypeElement(null, name);
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   159
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   160
37848
3c8ff4204d2d 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36779
diff changeset
   161
    @Override @DefinedBy(Api.LANGUAGE_MODEL)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   162
    public ClassSymbol getTypeElement(ModuleElement module, CharSequence name) {
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   163
        module.getClass();
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   164
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   165
        return doGetTypeElement(module, name);
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   166
    }
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   167
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   168
    private ClassSymbol doGetTypeElement(ModuleElement module, CharSequence name) {
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   169
        ensureEntered("getTypeElement");
43149
047a57b0839a 8172262: packages missing from docs build
jlahoda
parents: 43142
diff changeset
   170
        return doGetElement(module, "getTypeElement", name, ClassSymbol.class);
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   171
    }
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   172
43149
047a57b0839a 8172262: packages missing from docs build
jlahoda
parents: 43142
diff changeset
   173
    private <S extends Symbol> S doGetElement(ModuleElement module, String methodName,
047a57b0839a 8172262: packages missing from docs build
jlahoda
parents: 43142
diff changeset
   174
                                              CharSequence name, Class<S> clazz) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
        String strName = name.toString();
43369
aafd33c96bac 8173068: ElementUtils getPackageElement does not allow for an unnamed package
jlahoda
parents: 43272
diff changeset
   176
        if (!SourceVersion.isName(strName) && (!strName.isEmpty() || clazz == ClassSymbol.class)) {
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   177
            return null;
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   178
        }
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   179
        if (module == null) {
43149
047a57b0839a 8172262: packages missing from docs build
jlahoda
parents: 43142
diff changeset
   180
            return unboundNameToSymbol(methodName, strName, clazz);
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   181
        } else {
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   182
            return nameToSymbol((ModuleSymbol) module, strName, clazz);
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   183
        }
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   184
    }
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   185
43149
047a57b0839a 8172262: packages missing from docs build
jlahoda
parents: 43142
diff changeset
   186
    private final Set<String> alreadyWarnedDuplicates = new HashSet<>();
047a57b0839a 8172262: packages missing from docs build
jlahoda
parents: 43142
diff changeset
   187
047a57b0839a 8172262: packages missing from docs build
jlahoda
parents: 43142
diff changeset
   188
    private <S extends Symbol> S unboundNameToSymbol(String methodName,
047a57b0839a 8172262: packages missing from docs build
jlahoda
parents: 43142
diff changeset
   189
                                                     String nameStr,
047a57b0839a 8172262: packages missing from docs build
jlahoda
parents: 43142
diff changeset
   190
                                                     Class<S> clazz) {
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   191
        if (modules.getDefaultModule() == syms.noModule) { //not a modular mode:
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   192
            return nameToSymbol(syms.noModule, nameStr, clazz);
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   193
        }
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   194
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43149
diff changeset
   195
        Set<S> found = new LinkedHashSet<>();
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43149
diff changeset
   196
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43149
diff changeset
   197
        for (ModuleSymbol msym : modules.allModules()) {
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43149
diff changeset
   198
            S sym = nameToSymbol(msym, nameStr, clazz);
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   199
48085
8e96f85f2feb 8191078: Wrong "Package not found" warning
ksrini
parents: 48054
diff changeset
   200
            if (sym == null)
8e96f85f2feb 8191078: Wrong "Package not found" warning
ksrini
parents: 48054
diff changeset
   201
                continue;
8e96f85f2feb 8191078: Wrong "Package not found" warning
ksrini
parents: 48054
diff changeset
   202
8e96f85f2feb 8191078: Wrong "Package not found" warning
ksrini
parents: 48054
diff changeset
   203
            if (clazz == ClassSymbol.class) {
8e96f85f2feb 8191078: Wrong "Package not found" warning
ksrini
parents: 48054
diff changeset
   204
                // Always include classes
8e96f85f2feb 8191078: Wrong "Package not found" warning
ksrini
parents: 48054
diff changeset
   205
                found.add(sym);
8e96f85f2feb 8191078: Wrong "Package not found" warning
ksrini
parents: 48054
diff changeset
   206
            } else if (clazz == PackageSymbol.class) {
8e96f85f2feb 8191078: Wrong "Package not found" warning
ksrini
parents: 48054
diff changeset
   207
                // In module mode, ignore the "spurious" empty packages that "enclose" module-specific packages.
8e96f85f2feb 8191078: Wrong "Package not found" warning
ksrini
parents: 48054
diff changeset
   208
                // For example, if a module contains classes or package info in package p.q.r, it will also appear
8e96f85f2feb 8191078: Wrong "Package not found" warning
ksrini
parents: 48054
diff changeset
   209
                // to have additional packages p.q and p, even though these packages have no content other
8e96f85f2feb 8191078: Wrong "Package not found" warning
ksrini
parents: 48054
diff changeset
   210
                // than the subpackage.  We don't want those empty packages showing up in searches for p or p.q.
8e96f85f2feb 8191078: Wrong "Package not found" warning
ksrini
parents: 48054
diff changeset
   211
                if (!sym.members().isEmpty() || ((PackageSymbol) sym).package_info != null) {
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43149
diff changeset
   212
                    found.add(sym);
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   213
                }
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   214
            }
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43149
diff changeset
   215
        }
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   216
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43149
diff changeset
   217
        if (found.size() == 1) {
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43149
diff changeset
   218
            return found.iterator().next();
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43149
diff changeset
   219
        } else if (found.size() > 1) {
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43149
diff changeset
   220
            //more than one element found, produce a note:
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43149
diff changeset
   221
            if (alreadyWarnedDuplicates.add(methodName + ":" + nameStr)) {
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43149
diff changeset
   222
                String moduleNames = found.stream()
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43149
diff changeset
   223
                                          .map(s -> s.packge().modle)
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43149
diff changeset
   224
                                          .map(m -> m.toString())
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43149
diff changeset
   225
                                          .collect(Collectors.joining(", "));
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43149
diff changeset
   226
                log.note(Notes.MultipleElements(methodName, nameStr, moduleNames));
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   227
            }
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43149
diff changeset
   228
            return null;
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43149
diff changeset
   229
        } else {
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43149
diff changeset
   230
            //not found, or more than one element found:
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 43149
diff changeset
   231
            return null;
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   232
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
    /**
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   236
     * Returns a symbol given the type's or package's canonical name,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
     * or null if the name isn't found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
     */
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   239
    private <S extends Symbol> S nameToSymbol(ModuleSymbol module, String nameStr, Class<S> clazz) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
        Name name = names.fromString(nameStr);
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
        // First check cache.
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
        Symbol sym = (clazz == ClassSymbol.class)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   243
                    ? syms.getClass(module, name)
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   244
                    : syms.lookupPackage(module, name);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
            if (sym == null)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   248
                sym = javaCompiler.resolveIdent(module, nameStr);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
            sym.complete();
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26266
diff changeset
   252
            return (sym.kind != ERR &&
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
                    sym.exists() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
                    clazz.isInstance(sym) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
                    name.equals(sym.getQualifiedName()))
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
                ? clazz.cast(sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
                : null;
49197
cc2673fa8c20 8187950: javax.lang.model APIs throws CompletionFailure or a subtype of CompletionFailure.
jlahoda
parents: 48945
diff changeset
   258
        } catch (CompletionFailure cf) {
cc2673fa8c20 8187950: javax.lang.model APIs throws CompletionFailure or a subtype of CompletionFailure.
jlahoda
parents: 48945
diff changeset
   259
            cf.dcfh.handleAPICompletionFailure(cf);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
     * Returns the tree for an annotation given the annotated element
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
     * and the element's own tree.  Returns null if the tree cannot be found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
    private JCTree matchAnnoToTree(AnnotationMirror findme,
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
                                   Element e, JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
        Symbol sym = cast(Symbol.class, e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
        class Vis extends JCTree.Visitor {
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
            List<JCAnnotation> result = null;
24069
dfb8f11542fc 8034245: Refactor TopLevel tree node.
pgovereau
parents: 23810
diff changeset
   273
            public void visitPackageDef(JCPackageDecl tree) {
dfb8f11542fc 8034245: Refactor TopLevel tree node.
pgovereau
parents: 23810
diff changeset
   274
                result = tree.annotations;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
            public void visitClassDef(JCClassDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
                result = tree.mods.annotations;
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
            public void visitMethodDef(JCMethodDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
                result = tree.mods.annotations;
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
            public void visitVarDef(JCVariableDecl tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
                result = tree.mods.annotations;
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
            }
23122
02c931d49ad2 6411385: Trees.getPath does not work for constructors
jlahoda
parents: 22163
diff changeset
   285
            @Override
02c931d49ad2 6411385: Trees.getPath does not work for constructors
jlahoda
parents: 22163
diff changeset
   286
            public void visitTypeParameter(JCTypeParameter tree) {
02c931d49ad2 6411385: Trees.getPath does not work for constructors
jlahoda
parents: 22163
diff changeset
   287
                result = tree.annotations;
02c931d49ad2 6411385: Trees.getPath does not work for constructors
jlahoda
parents: 22163
diff changeset
   288
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
        Vis vis = new Vis();
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
        tree.accept(vis);
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
        if (vis.result == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
            return null;
15355
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   294
23122
02c931d49ad2 6411385: Trees.getPath does not work for constructors
jlahoda
parents: 22163
diff changeset
   295
        List<Attribute.Compound> annos = sym.getAnnotationMirrors();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
        return matchAnnoToTree(cast(Attribute.Compound.class, findme),
15355
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   297
                               annos,
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
                               vis.result);
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
     * Returns the tree for an annotation given a list of annotations
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
     * in which to search (recursively) and their corresponding trees.
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
     * Returns null if the tree cannot be found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
    private JCTree matchAnnoToTree(Attribute.Compound findme,
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
                                   List<Attribute.Compound> annos,
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
                                   List<JCAnnotation> trees) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
        for (Attribute.Compound anno : annos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
            for (JCAnnotation tree : trees) {
43591
62824732af55 6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents: 43369
diff changeset
   311
                if (tree.type.tsym != anno.type.tsym)
62824732af55 6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents: 43369
diff changeset
   312
                    continue;
62824732af55 6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents: 43369
diff changeset
   313
                JCTree match = matchAttributeToTree(findme, anno, tree);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
                if (match != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
                    return match;
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
    /**
43591
62824732af55 6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents: 43369
diff changeset
   322
     * Returns the tree for an attribute given an enclosing attribute to
62824732af55 6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents: 43369
diff changeset
   323
     * search (recursively) and the enclosing attribute's corresponding tree.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
     * Returns null if the tree cannot be found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
     */
43591
62824732af55 6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents: 43369
diff changeset
   326
    private JCTree matchAttributeToTree(final Attribute findme,
62824732af55 6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents: 43369
diff changeset
   327
                                        final Attribute attr,
62824732af55 6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents: 43369
diff changeset
   328
                                        final JCTree tree) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
        if (attr == findme)
43591
62824732af55 6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents: 43369
diff changeset
   330
            return tree;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
        class Vis implements Attribute.Visitor {
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
            JCTree result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
            public void visitConstant(Attribute.Constant value) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
            public void visitClass(Attribute.Class clazz) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
            public void visitCompound(Attribute.Compound anno) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   339
                for (Pair<MethodSymbol, Attribute> pair : anno.values) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   340
                    JCExpression expr = scanForAssign(pair.fst, tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   341
                    if (expr != null) {
43591
62824732af55 6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents: 43369
diff changeset
   342
                        JCTree match = matchAttributeToTree(findme, pair.snd, expr);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   343
                        if (match != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   344
                            result = match;
06bc494ca11e Initial load
duke
parents:
diff changeset
   345
                            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   346
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   348
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   349
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
            public void visitArray(Attribute.Array array) {
43591
62824732af55 6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents: 43369
diff changeset
   351
                if (tree.hasTag(NEWARRAY)) {
62824732af55 6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents: 43369
diff changeset
   352
                    List<JCExpression> elems = ((JCNewArray)tree).elems;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
                    for (Attribute value : array.values) {
43591
62824732af55 6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents: 43369
diff changeset
   354
                        JCTree match = matchAttributeToTree(findme, value, elems.head);
62824732af55 6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents: 43369
diff changeset
   355
                        if (match != null) {
62824732af55 6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents: 43369
diff changeset
   356
                            result = match;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
                            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
                        elems = elems.tail;
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
                    }
43591
62824732af55 6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents: 43369
diff changeset
   361
                } else if (array.values.length == 1) {
62824732af55 6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents: 43369
diff changeset
   362
                    // the tree may not be a NEWARRAY for single-element array initializers
62824732af55 6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents: 43369
diff changeset
   363
                    result = matchAttributeToTree(findme, array.values[0], tree);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   365
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   366
            public void visitEnum(Attribute.Enum e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   368
            public void visitError(Attribute.Error e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   370
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   371
        Vis vis = new Vis();
06bc494ca11e Initial load
duke
parents:
diff changeset
   372
        attr.accept(vis);
06bc494ca11e Initial load
duke
parents:
diff changeset
   373
        return vis.result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   374
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   375
06bc494ca11e Initial load
duke
parents:
diff changeset
   376
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   377
     * Scans for a JCAssign node with a LHS matching a given
06bc494ca11e Initial load
duke
parents:
diff changeset
   378
     * symbol, and returns its RHS.  Does not scan nested JCAnnotations.
06bc494ca11e Initial load
duke
parents:
diff changeset
   379
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   380
    private JCExpression scanForAssign(final MethodSymbol sym,
06bc494ca11e Initial load
duke
parents:
diff changeset
   381
                                       final JCTree tree) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   382
        class TS extends TreeScanner {
06bc494ca11e Initial load
duke
parents:
diff changeset
   383
            JCExpression result = null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   384
            public void scan(JCTree t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   385
                if (t != null && result == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   386
                    t.accept(this);
06bc494ca11e Initial load
duke
parents:
diff changeset
   387
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   388
            public void visitAnnotation(JCAnnotation t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   389
                if (t == tree)
06bc494ca11e Initial load
duke
parents:
diff changeset
   390
                    scan(t.args);
06bc494ca11e Initial load
duke
parents:
diff changeset
   391
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   392
            public void visitAssign(JCAssign t) {
10950
e87b50888909 6921494: provide way to print javac tree tag values
jjg
parents: 7681
diff changeset
   393
                if (t.lhs.hasTag(IDENT)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   394
                    JCIdent ident = (JCIdent) t.lhs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   395
                    if (ident.sym == sym)
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
                        result = t.rhs;
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   398
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
        TS scanner = new TS();
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
        tree.accept(scanner);
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
        return scanner.result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   404
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
     * Returns the tree node corresponding to this element, or null
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
     * if none can be found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
    public JCTree getTree(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
        Pair<JCTree, ?> treeTop = getTreeAndTopLevel(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
        return (treeTop != null) ? treeTop.fst : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   412
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   413
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   414
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   415
    public String getDocComment(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   416
        // Our doc comment is contained in a map in our toplevel,
06bc494ca11e Initial load
duke
parents:
diff changeset
   417
        // indexed by our tree.  Find our enter environment, which gives
06bc494ca11e Initial load
duke
parents:
diff changeset
   418
        // us our toplevel.  It also gives us a tree that contains our
06bc494ca11e Initial load
duke
parents:
diff changeset
   419
        // tree:  walk it to find our tree.  This is painful.
06bc494ca11e Initial load
duke
parents:
diff changeset
   420
        Pair<JCTree, JCCompilationUnit> treeTop = getTreeAndTopLevel(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   421
        if (treeTop == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   422
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   423
        JCTree tree = treeTop.fst;
06bc494ca11e Initial load
duke
parents:
diff changeset
   424
        JCCompilationUnit toplevel = treeTop.snd;
06bc494ca11e Initial load
duke
parents:
diff changeset
   425
        if (toplevel.docComments == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
            return null;
13077
16fb753bb5dc 7174143: encapsulate doc comment table
jjg
parents: 10950
diff changeset
   427
        return toplevel.docComments.getCommentText(tree);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   428
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   429
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   430
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   431
    public PackageElement getPackageOf(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   432
        return cast(Symbol.class, e).packge();
06bc494ca11e Initial load
duke
parents:
diff changeset
   433
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   434
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   435
    @DefinedBy(Api.LANGUAGE_MODEL)
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   436
    public ModuleElement getModuleOf(Element e) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   437
        Symbol sym = cast(Symbol.class, e);
42824
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   438
        if (modules.getDefaultModule() == syms.noModule)
89b14017e8d6 8133896: Update javax.lang.model APIs
jlahoda
parents: 42407
diff changeset
   439
            return null;
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   440
        return (sym.kind == MDL) ? ((ModuleElement) e) : sym.packge().modle;
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   441
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   442
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   443
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   444
    public boolean isDeprecated(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   445
        Symbol sym = cast(Symbol.class, e);
49197
cc2673fa8c20 8187950: javax.lang.model APIs throws CompletionFailure or a subtype of CompletionFailure.
jlahoda
parents: 48945
diff changeset
   446
        sym.apiComplete();
42407
f3702cff2933 8169069: Module system implementation refresh (11/2016)
alanb
parents: 37848
diff changeset
   447
        return sym.isDeprecated();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   448
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   449
42826
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   450
    @Override @DefinedBy(Api.LANGUAGE_MODEL)
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   451
    public Origin getOrigin(Element e) {
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   452
        Symbol sym = cast(Symbol.class, e);
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   453
        if ((sym.flags() & Flags.GENERATEDCONSTR) != 0)
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   454
            return Origin.MANDATED;
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   455
        //TypeElement.getEnclosedElements does not return synthetic elements,
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   456
        //and most synthetic elements are not read from the classfile anyway:
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   457
        return Origin.EXPLICIT;
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   458
    }
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   459
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   460
    @Override @DefinedBy(Api.LANGUAGE_MODEL)
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   461
    public Origin getOrigin(AnnotatedConstruct c, AnnotationMirror a) {
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   462
        Compound ac = cast(Compound.class, a);
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   463
        if (ac.isSynthesized())
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   464
            return Origin.MANDATED;
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   465
        return Origin.EXPLICIT;
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   466
    }
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   467
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   468
    @Override @DefinedBy(Api.LANGUAGE_MODEL)
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   469
    public Origin getOrigin(ModuleElement m, ModuleElement.Directive directive) {
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   470
        switch (directive.getKind()) {
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   471
            case REQUIRES:
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   472
                RequiresDirective rd = cast(RequiresDirective.class, directive);
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   473
                if (rd.flags.contains(RequiresFlag.MANDATED))
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   474
                    return Origin.MANDATED;
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   475
                if (rd.flags.contains(RequiresFlag.SYNTHETIC))
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   476
                    return Origin.SYNTHETIC;
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   477
                return Origin.EXPLICIT;
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   478
            case EXPORTS:
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   479
                ExportsDirective ed = cast(ExportsDirective.class, directive);
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   480
                if (ed.flags.contains(ExportsFlag.MANDATED))
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   481
                    return Origin.MANDATED;
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   482
                if (ed.flags.contains(ExportsFlag.SYNTHETIC))
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   483
                    return Origin.SYNTHETIC;
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   484
                return Origin.EXPLICIT;
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   485
            case OPENS:
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   486
                OpensDirective od = cast(OpensDirective.class, directive);
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   487
                if (od.flags.contains(OpensFlag.MANDATED))
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   488
                    return Origin.MANDATED;
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   489
                if (od.flags.contains(OpensFlag.SYNTHETIC))
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   490
                    return Origin.SYNTHETIC;
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   491
                return Origin.EXPLICIT;
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   492
        }
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   493
        return Origin.EXPLICIT;
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   494
    }
563b42fc70ba 8171355: Implement Elements.getOrigin
jlahoda
parents: 42824
diff changeset
   495
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   496
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   497
    public Name getBinaryName(TypeElement type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   498
        return cast(TypeSymbol.class, type).flatName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   499
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   500
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   501
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   502
    public Map<MethodSymbol, Attribute> getElementValuesWithDefaults(
06bc494ca11e Initial load
duke
parents:
diff changeset
   503
                                                        AnnotationMirror a) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   504
        Attribute.Compound anno = cast(Attribute.Compound.class, a);
06bc494ca11e Initial load
duke
parents:
diff changeset
   505
        DeclaredType annotype = a.getAnnotationType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   506
        Map<MethodSymbol, Attribute> valmap = anno.getElementValues();
06bc494ca11e Initial load
duke
parents:
diff changeset
   507
06bc494ca11e Initial load
duke
parents:
diff changeset
   508
        for (ExecutableElement ex :
06bc494ca11e Initial load
duke
parents:
diff changeset
   509
                 methodsIn(annotype.asElement().getEnclosedElements())) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   510
            MethodSymbol meth = (MethodSymbol) ex;
06bc494ca11e Initial load
duke
parents:
diff changeset
   511
            Attribute defaultValue = meth.getDefaultValue();
06bc494ca11e Initial load
duke
parents:
diff changeset
   512
            if (defaultValue != null && !valmap.containsKey(meth)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   513
                valmap.put(meth, defaultValue);
06bc494ca11e Initial load
duke
parents:
diff changeset
   514
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   515
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   516
        return valmap;
06bc494ca11e Initial load
duke
parents:
diff changeset
   517
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   518
06bc494ca11e Initial load
duke
parents:
diff changeset
   519
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   520
     * {@inheritDoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
   521
     */
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   522
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   523
    public FilteredMemberList getAllMembers(TypeElement element) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   524
        Symbol sym = cast(Symbol.class, element);
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 24069
diff changeset
   525
        WriteableScope scope = sym.members().dupUnshared();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   526
        List<Type> closure = types.closure(sym.asType());
06bc494ca11e Initial load
duke
parents:
diff changeset
   527
        for (Type t : closure)
06bc494ca11e Initial load
duke
parents:
diff changeset
   528
            addMembers(scope, t);
06bc494ca11e Initial load
duke
parents:
diff changeset
   529
        return new FilteredMemberList(scope);
06bc494ca11e Initial load
duke
parents:
diff changeset
   530
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   531
    // where
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 24069
diff changeset
   532
        private void addMembers(WriteableScope scope, Type type) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   533
            members:
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 24069
diff changeset
   534
            for (Symbol e : type.asElement().members().getSymbols(NON_RECURSIVE)) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 24069
diff changeset
   535
                for (Symbol overrider : scope.getSymbolsByName(e.getSimpleName())) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 24069
diff changeset
   536
                    if (overrider.kind == e.kind && (overrider.flags() & Flags.SYNTHETIC) == 0) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 24069
diff changeset
   537
                        if (overrider.getKind() == ElementKind.METHOD &&
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 24069
diff changeset
   538
                                overrides((ExecutableElement)overrider, (ExecutableElement)e, (TypeElement)type.asElement())) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   539
                            continue members;
06bc494ca11e Initial load
duke
parents:
diff changeset
   540
                        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   541
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   542
                }
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 24069
diff changeset
   543
                boolean derived = e.getEnclosingElement() != scope.owner;
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 24069
diff changeset
   544
                ElementKind kind = e.getKind();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   545
                boolean initializer = kind == ElementKind.CONSTRUCTOR
06bc494ca11e Initial load
duke
parents:
diff changeset
   546
                    || kind == ElementKind.INSTANCE_INIT
06bc494ca11e Initial load
duke
parents:
diff changeset
   547
                    || kind == ElementKind.STATIC_INIT;
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 24069
diff changeset
   548
                if (!derived || (!initializer && e.isInheritedIn(scope.owner, types)))
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 24069
diff changeset
   549
                    scope.enter(e);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   550
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   551
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   552
06bc494ca11e Initial load
duke
parents:
diff changeset
   553
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   554
     * Returns all annotations of an element, whether
06bc494ca11e Initial load
duke
parents:
diff changeset
   555
     * inherited or directly present.
06bc494ca11e Initial load
duke
parents:
diff changeset
   556
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   557
     * @param e  the element being examined
06bc494ca11e Initial load
duke
parents:
diff changeset
   558
     * @return all annotations of the element
06bc494ca11e Initial load
duke
parents:
diff changeset
   559
     */
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   560
    @Override @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   561
    public List<Attribute.Compound> getAllAnnotationMirrors(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   562
        Symbol sym = cast(Symbol.class, e);
17278
a48ec76f26e9 8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents: 16557
diff changeset
   563
        List<Attribute.Compound> annos = sym.getAnnotationMirrors();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   564
        while (sym.getKind() == ElementKind.CLASS) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   565
            Type sup = ((ClassSymbol) sym).getSuperclass();
14359
d4099818ab70 7200915: convert TypeTags from a series of small ints to an enum
jjg
parents: 13077
diff changeset
   566
            if (!sup.hasTag(CLASS) || sup.isErroneous() ||
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   567
                    sup.tsym == syms.objectType.tsym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   568
                break;
06bc494ca11e Initial load
duke
parents:
diff changeset
   569
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   570
            sym = sup.tsym;
06bc494ca11e Initial load
duke
parents:
diff changeset
   571
            List<Attribute.Compound> oldAnnos = annos;
17278
a48ec76f26e9 8011027: Type parameter annotations not passed through to javax.lang.model
jfranck
parents: 16557
diff changeset
   572
            List<Attribute.Compound> newAnnos = sym.getAnnotationMirrors();
15355
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   573
            for (Attribute.Compound anno : newAnnos) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   574
                if (isInherited(anno.type) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   575
                        !containsAnnoOfType(oldAnnos, anno.type)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   576
                    annos = annos.prepend(anno);
06bc494ca11e Initial load
duke
parents:
diff changeset
   577
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   578
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   579
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   580
        return annos;
06bc494ca11e Initial load
duke
parents:
diff changeset
   581
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   582
06bc494ca11e Initial load
duke
parents:
diff changeset
   583
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   584
     * Tests whether an annotation type is @Inherited.
06bc494ca11e Initial load
duke
parents:
diff changeset
   585
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   586
    private boolean isInherited(Type annotype) {
15355
a4757c33cae9 7193719: Support repeating annotations in javax.lang.model
jfranck
parents: 14359
diff changeset
   587
        return annotype.tsym.attribute(syms.inheritedType.tsym) != null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   588
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   589
06bc494ca11e Initial load
duke
parents:
diff changeset
   590
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   591
     * Tests whether a list of annotations contains an annotation
06bc494ca11e Initial load
duke
parents:
diff changeset
   592
     * of a given type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   593
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   594
    private static boolean containsAnnoOfType(List<Attribute.Compound> annos,
06bc494ca11e Initial load
duke
parents:
diff changeset
   595
                                              Type type) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   596
        for (Attribute.Compound anno : annos) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   597
            if (anno.type.tsym == type.tsym)
06bc494ca11e Initial load
duke
parents:
diff changeset
   598
                return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   599
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   600
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   601
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   602
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   603
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   604
    public boolean hides(Element hiderEl, Element hideeEl) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   605
        Symbol hider = cast(Symbol.class, hiderEl);
06bc494ca11e Initial load
duke
parents:
diff changeset
   606
        Symbol hidee = cast(Symbol.class, hideeEl);
06bc494ca11e Initial load
duke
parents:
diff changeset
   607
06bc494ca11e Initial load
duke
parents:
diff changeset
   608
        // Fields only hide fields; methods only methods; types only types.
06bc494ca11e Initial load
duke
parents:
diff changeset
   609
        // Names must match.  Nothing hides itself (just try it).
06bc494ca11e Initial load
duke
parents:
diff changeset
   610
        if (hider == hidee ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   611
                hider.kind != hidee.kind ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   612
                hider.name != hidee.name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   613
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   614
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   615
06bc494ca11e Initial load
duke
parents:
diff changeset
   616
        // Only static methods can hide other methods.
06bc494ca11e Initial load
duke
parents:
diff changeset
   617
        // Methods only hide methods with matching signatures.
27224
228abfa87080 8054457: Refactor Symbol kinds from small ints to an enum
emc
parents: 26266
diff changeset
   618
        if (hider.kind == MTH) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   619
            if (!hider.isStatic() ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   620
                        !types.isSubSignature(hider.type, hidee.type)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   621
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   622
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   623
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   624
06bc494ca11e Initial load
duke
parents:
diff changeset
   625
        // Hider must be in a subclass of hidee's class.
06bc494ca11e Initial load
duke
parents:
diff changeset
   626
        // Note that if M1 hides M2, and M2 hides M3, and M3 is accessible
06bc494ca11e Initial load
duke
parents:
diff changeset
   627
        // in M1's class, then M1 and M2 both hide M3.
06bc494ca11e Initial load
duke
parents:
diff changeset
   628
        ClassSymbol hiderClass = hider.owner.enclClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   629
        ClassSymbol hideeClass = hidee.owner.enclClass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   630
        if (hiderClass == null || hideeClass == null ||
06bc494ca11e Initial load
duke
parents:
diff changeset
   631
                !hiderClass.isSubClass(hideeClass, types)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   632
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   633
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   634
06bc494ca11e Initial load
duke
parents:
diff changeset
   635
        // Hidee must be accessible in hider's class.
48945
6e6c777a37a2 8186688: javax.lang.model.util.Elements.hides does not work correctly with interfaces
jjg
parents: 48085
diff changeset
   636
        return hidee.isAccessibleIn(hiderClass, types);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   637
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   638
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   639
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   640
    public boolean overrides(ExecutableElement riderEl,
06bc494ca11e Initial load
duke
parents:
diff changeset
   641
                             ExecutableElement rideeEl, TypeElement typeEl) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   642
        MethodSymbol rider = cast(MethodSymbol.class, riderEl);
06bc494ca11e Initial load
duke
parents:
diff changeset
   643
        MethodSymbol ridee = cast(MethodSymbol.class, rideeEl);
06bc494ca11e Initial load
duke
parents:
diff changeset
   644
        ClassSymbol origin = cast(ClassSymbol.class, typeEl);
06bc494ca11e Initial load
duke
parents:
diff changeset
   645
06bc494ca11e Initial load
duke
parents:
diff changeset
   646
        return rider.name == ridee.name &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   647
06bc494ca11e Initial load
duke
parents:
diff changeset
   648
               // not reflexive as per JLS
06bc494ca11e Initial load
duke
parents:
diff changeset
   649
               rider != ridee &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   650
06bc494ca11e Initial load
duke
parents:
diff changeset
   651
               // we don't care if ridee is static, though that wouldn't
06bc494ca11e Initial load
duke
parents:
diff changeset
   652
               // compile
06bc494ca11e Initial load
duke
parents:
diff changeset
   653
               !rider.isStatic() &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   654
06bc494ca11e Initial load
duke
parents:
diff changeset
   655
               // Symbol.overrides assumes the following
06bc494ca11e Initial load
duke
parents:
diff changeset
   656
               ridee.isMemberOf(origin, types) &&
06bc494ca11e Initial load
duke
parents:
diff changeset
   657
06bc494ca11e Initial load
duke
parents:
diff changeset
   658
               // check access and signatures; don't check return types
06bc494ca11e Initial load
duke
parents:
diff changeset
   659
               rider.overrides(ridee, origin, types, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   660
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   661
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   662
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   663
    public String getConstantExpression(Object value) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   664
        return Constants.format(value);
06bc494ca11e Initial load
duke
parents:
diff changeset
   665
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   666
06bc494ca11e Initial load
duke
parents:
diff changeset
   667
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   668
     * Print a representation of the elements to the given writer in
06bc494ca11e Initial load
duke
parents:
diff changeset
   669
     * the specified order.  The main purpose of this method is for
06bc494ca11e Initial load
duke
parents:
diff changeset
   670
     * diagnostics.  The exact format of the output is <em>not</em>
06bc494ca11e Initial load
duke
parents:
diff changeset
   671
     * specified and is subject to change.
06bc494ca11e Initial load
duke
parents:
diff changeset
   672
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   673
     * @param w the writer to print the output to
06bc494ca11e Initial load
duke
parents:
diff changeset
   674
     * @param elements the elements to print
06bc494ca11e Initial load
duke
parents:
diff changeset
   675
     */
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   676
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   677
    public void printElements(java.io.Writer w, Element... elements) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   678
        for (Element element : elements)
06bc494ca11e Initial load
duke
parents:
diff changeset
   679
            (new PrintingProcessor.PrintingElementVisitor(w, this)).visit(element).flush();
06bc494ca11e Initial load
duke
parents:
diff changeset
   680
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   681
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   682
    @DefinedBy(Api.LANGUAGE_MODEL)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   683
    public Name getName(CharSequence cs) {
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
   684
        return names.fromString(cs.toString());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   685
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   686
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   687
    @Override @DefinedBy(Api.LANGUAGE_MODEL)
15714
d9a83ab62853 8007574: Provide isFunctionalInterface in javax.lang.model
darcy
parents: 15365
diff changeset
   688
    public boolean isFunctionalInterface(TypeElement element) {
d9a83ab62853 8007574: Provide isFunctionalInterface in javax.lang.model
darcy
parents: 15365
diff changeset
   689
        if (element.getKind() != ElementKind.INTERFACE)
d9a83ab62853 8007574: Provide isFunctionalInterface in javax.lang.model
darcy
parents: 15365
diff changeset
   690
            return false;
d9a83ab62853 8007574: Provide isFunctionalInterface in javax.lang.model
darcy
parents: 15365
diff changeset
   691
        else {
d9a83ab62853 8007574: Provide isFunctionalInterface in javax.lang.model
darcy
parents: 15365
diff changeset
   692
            TypeSymbol tsym = cast(TypeSymbol.class, element);
d9a83ab62853 8007574: Provide isFunctionalInterface in javax.lang.model
darcy
parents: 15365
diff changeset
   693
            return types.isFunctionalInterface(tsym);
d9a83ab62853 8007574: Provide isFunctionalInterface in javax.lang.model
darcy
parents: 15365
diff changeset
   694
        }
d9a83ab62853 8007574: Provide isFunctionalInterface in javax.lang.model
darcy
parents: 15365
diff changeset
   695
    }
d9a83ab62853 8007574: Provide isFunctionalInterface in javax.lang.model
darcy
parents: 15365
diff changeset
   696
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   697
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   698
     * Returns the tree node and compilation unit corresponding to this
06bc494ca11e Initial load
duke
parents:
diff changeset
   699
     * element, or null if they can't be found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   700
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   701
    private Pair<JCTree, JCCompilationUnit> getTreeAndTopLevel(Element e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   702
        Symbol sym = cast(Symbol.class, e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   703
        Env<AttrContext> enterEnv = getEnterEnv(sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   704
        if (enterEnv == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   705
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   706
        JCTree tree = TreeInfo.declarationFor(sym, enterEnv.tree);
06bc494ca11e Initial load
duke
parents:
diff changeset
   707
        if (tree == null || enterEnv.toplevel == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   708
            return null;
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 17278
diff changeset
   709
        return new Pair<>(tree, enterEnv.toplevel);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   710
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   711
06bc494ca11e Initial load
duke
parents:
diff changeset
   712
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   713
     * Returns the best approximation for the tree node and compilation unit
06bc494ca11e Initial load
duke
parents:
diff changeset
   714
     * corresponding to the given element, annotation and value.
06bc494ca11e Initial load
duke
parents:
diff changeset
   715
     * If the element is null, null is returned.
06bc494ca11e Initial load
duke
parents:
diff changeset
   716
     * If the annotation is null or cannot be found, the tree node and
06bc494ca11e Initial load
duke
parents:
diff changeset
   717
     * compilation unit for the element is returned.
06bc494ca11e Initial load
duke
parents:
diff changeset
   718
     * If the annotation value is null or cannot be found, the tree node and
06bc494ca11e Initial load
duke
parents:
diff changeset
   719
     * compilation unit for the annotation is returned.
06bc494ca11e Initial load
duke
parents:
diff changeset
   720
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   721
    public Pair<JCTree, JCCompilationUnit> getTreeAndTopLevel(
06bc494ca11e Initial load
duke
parents:
diff changeset
   722
                      Element e, AnnotationMirror a, AnnotationValue v) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   723
        if (e == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   724
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   725
06bc494ca11e Initial load
duke
parents:
diff changeset
   726
        Pair<JCTree, JCCompilationUnit> elemTreeTop = getTreeAndTopLevel(e);
06bc494ca11e Initial load
duke
parents:
diff changeset
   727
        if (elemTreeTop == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   728
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   729
06bc494ca11e Initial load
duke
parents:
diff changeset
   730
        if (a == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   731
            return elemTreeTop;
06bc494ca11e Initial load
duke
parents:
diff changeset
   732
06bc494ca11e Initial load
duke
parents:
diff changeset
   733
        JCTree annoTree = matchAnnoToTree(a, e, elemTreeTop.fst);
06bc494ca11e Initial load
duke
parents:
diff changeset
   734
        if (annoTree == null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   735
            return elemTreeTop;
06bc494ca11e Initial load
duke
parents:
diff changeset
   736
43591
62824732af55 6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents: 43369
diff changeset
   737
        if (v == null)
62824732af55 6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents: 43369
diff changeset
   738
            return new Pair<>(annoTree, elemTreeTop.snd);
62824732af55 6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents: 43369
diff changeset
   739
62824732af55 6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents: 43369
diff changeset
   740
        JCTree valueTree = matchAttributeToTree(
62824732af55 6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents: 43369
diff changeset
   741
                cast(Attribute.class, v), cast(Attribute.class, a), annoTree);
62824732af55 6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents: 43369
diff changeset
   742
        if (valueTree == null)
62824732af55 6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents: 43369
diff changeset
   743
            return new Pair<>(annoTree, elemTreeTop.snd);
62824732af55 6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents: 43369
diff changeset
   744
62824732af55 6388543: improve accuracy of source positions for AnnotationValue param of Messager.printMessage
cushon
parents: 43369
diff changeset
   745
        return new Pair<>(valueTree, elemTreeTop.snd);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   746
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   747
06bc494ca11e Initial load
duke
parents:
diff changeset
   748
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   749
     * Returns a symbol's enter environment, or null if it has none.
06bc494ca11e Initial load
duke
parents:
diff changeset
   750
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   751
    private Env<AttrContext> getEnterEnv(Symbol sym) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   752
        // Get enclosing class of sym, or sym itself if it is a class
36779
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents: 36526
diff changeset
   753
        // package, or module.
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents: 36526
diff changeset
   754
        TypeSymbol ts = null;
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents: 36526
diff changeset
   755
        switch (sym.kind) {
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents: 36526
diff changeset
   756
            case PCK:
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents: 36526
diff changeset
   757
                ts = (PackageSymbol)sym;
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents: 36526
diff changeset
   758
                break;
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents: 36526
diff changeset
   759
            case MDL:
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents: 36526
diff changeset
   760
                ts = (ModuleSymbol)sym;
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents: 36526
diff changeset
   761
                break;
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents: 36526
diff changeset
   762
            default:
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents: 36526
diff changeset
   763
                ts = sym.enclClass();
9a030c4d2591 8152771: NPE accessing comments on module declarations
ksrini
parents: 36526
diff changeset
   764
        }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   765
        return (ts != null)
06bc494ca11e Initial load
duke
parents:
diff changeset
   766
                ? enter.getEnv(ts)
06bc494ca11e Initial load
duke
parents:
diff changeset
   767
                : null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   768
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   769
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   770
    private void ensureEntered(String methodName) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   771
        if (javacTaskImpl != null) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   772
            javacTaskImpl.ensureEntered();
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   773
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   774
        if (!javaCompiler.isEnterDone()) {
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   775
            throw new IllegalStateException("Cannot use Elements." + methodName + " before the TaskEvent.Kind.ENTER finished event.");
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   776
        }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   777
    }
3b41f1c69604 8142968: Module System implementation
alanb
parents: 27224
diff changeset
   778
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   779
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   780
     * Returns an object cast to the specified type.
06bc494ca11e Initial load
duke
parents:
diff changeset
   781
     * @throws NullPointerException if the object is {@code null}
06bc494ca11e Initial load
duke
parents:
diff changeset
   782
     * @throws IllegalArgumentException if the object is of the wrong type
06bc494ca11e Initial load
duke
parents:
diff changeset
   783
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   784
    private static <T> T cast(Class<T> clazz, Object o) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   785
        if (! clazz.isInstance(o))
06bc494ca11e Initial load
duke
parents:
diff changeset
   786
            throw new IllegalArgumentException(o.toString());
06bc494ca11e Initial load
duke
parents:
diff changeset
   787
        return clazz.cast(o);
06bc494ca11e Initial load
duke
parents:
diff changeset
   788
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   789
}