langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/api/JavacScope.java
author jlahoda
Wed, 27 Aug 2014 07:44:00 +0200
changeset 26266 2d24bda701dc
parent 25874 83c19f00452c
child 29291 076c277565f7
permissions -rw-r--r--
8056061: Mark implementations of public interfaces with an annotation Summary: Adding @DefinedBy annotation to mark methods that implement public API methods; annotating the methods; adding a coding rules analyzer to enforce all such methods are annotated. Reviewed-by: jjg, mcimadamore, jfranck Contributed-by: jan.lahoda@oracle.com, jonathan.gibbons@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 5847
diff changeset
     2
 * Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 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.api;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import javax.lang.model.element.Element;
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import javax.lang.model.element.ExecutableElement;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import javax.lang.model.element.TypeElement;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.javac.comp.AttrContext;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import com.sun.tools.javac.comp.Env;
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    35
import com.sun.tools.javac.util.DefinedBy;
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    36
import com.sun.tools.javac.util.DefinedBy.Api;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 * Provides an implementation of Scope.
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    43
 * <p><b>This is NOT part of any supported API.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 * If you write code that depends on this, you do so at your own
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 * risk.  This code and its internal interfaces are subject to change
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 * or deletion without notice.</b></p>
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 * @author Jonathan Gibbons;
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
public class JavacScope implements com.sun.source.tree.Scope {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 5847
diff changeset
    51
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 5847
diff changeset
    52
    static JavacScope create(Env<AttrContext> env) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 5847
diff changeset
    53
        if (env.outer == null || env.outer == env) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 5847
diff changeset
    54
            //the "top-level" scope needs to return both imported and defined elements
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 5847
diff changeset
    55
            //see test CheckLocalElements
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 5847
diff changeset
    56
            return new JavacScope(env) {
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    57
                @Override @DefinedBy(Api.COMPILER_TREE)
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 5847
diff changeset
    58
                public Iterable<? extends Element> getLocalElements() {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 5847
diff changeset
    59
                    return env.toplevel.namedImportScope.getSymbols();
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 5847
diff changeset
    60
                }
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 5847
diff changeset
    61
            };
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 5847
diff changeset
    62
        } else {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 5847
diff changeset
    63
            return new JavacScope(env);
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 5847
diff changeset
    64
        }
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 5847
diff changeset
    65
    }
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 5847
diff changeset
    66
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    protected final Env<AttrContext> env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 5847
diff changeset
    69
    private JavacScope(Env<AttrContext> env) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
        env.getClass(); // null-check
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
        this.env = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    74
    @DefinedBy(Api.COMPILER_TREE)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    public JavacScope getEnclosingScope() {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 5847
diff changeset
    76
        if (env.outer != null && env.outer != env) {
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 5847
diff changeset
    77
            return create(env.outer);
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 5847
diff changeset
    78
        } else {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
            // synthesize an outermost "star-import" scope
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
            return new JavacScope(env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
                public boolean isStarImportScope() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
                    return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
                }
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    84
                @DefinedBy(Api.COMPILER_TREE)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
                public JavacScope getEnclosingScope() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
                    return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
                }
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    88
                @DefinedBy(Api.COMPILER_TREE)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
                public Iterable<? extends Element> getLocalElements() {
25443
9187d77f2c64 8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents: 5847
diff changeset
    90
                    return env.toplevel.starImportScope.getSymbols();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
            };
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
    96
    @DefinedBy(Api.COMPILER_TREE)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
    public TypeElement getEnclosingClass() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        // hide the dummy class that javac uses to enclose the top level declarations
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        return (env.outer == null || env.outer == env ? null : env.enclClass.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   102
    @DefinedBy(Api.COMPILER_TREE)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    public ExecutableElement getEnclosingMethod() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        return (env.enclMethod == null ? null : env.enclMethod.sym);
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
26266
2d24bda701dc 8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents: 25874
diff changeset
   107
    @DefinedBy(Api.COMPILER_TREE)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    public Iterable<? extends Element> getLocalElements() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        return env.info.getLocalElements();
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    public Env<AttrContext> getEnv() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        return env;
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
    public boolean isStarImportScope() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
    public boolean equals(Object other) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        if (other instanceof JavacScope) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
            JavacScope s = (JavacScope) other;
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
            return (env.equals(s.env)
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
                && isStarImportScope() == s.isStarImportScope());
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        } else
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
    public int hashCode() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        return env.hashCode() + (isStarImportScope() ? 1 : 0);
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
    public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        return "JavacScope[env=" + env + ",starImport=" + isStarImportScope() + "]";
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
}