author | jlahoda |
Wed, 27 Aug 2014 07:44:00 +0200 | |
changeset 26266 | 2d24bda701dc |
parent 25874 | 83c19f00452c |
child 29291 | 076c277565f7 |
permissions | -rw-r--r-- |
10 | 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 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5520 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.javac.api; |
|
27 |
||
28 |
||
29 |
import javax.lang.model.element.Element; |
|
30 |
import javax.lang.model.element.ExecutableElement; |
|
31 |
import javax.lang.model.element.TypeElement; |
|
32 |
||
33 |
import com.sun.tools.javac.comp.AttrContext; |
|
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 | 37 |
|
38 |
||
39 |
||
40 |
/** |
|
41 |
* Provides an implementation of Scope. |
|
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 | 44 |
* If you write code that depends on this, you do so at your own |
45 |
* risk. This code and its internal interfaces are subject to change |
|
46 |
* or deletion without notice.</b></p> |
|
47 |
* |
|
48 |
* @author Jonathan Gibbons; |
|
49 |
*/ |
|
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 | 67 |
protected final Env<AttrContext> env; |
68 |
||
25443
9187d77f2c64
8031569: Refactor javac scope implementation to enable lazy imports
jlahoda
parents:
5847
diff
changeset
|
69 |
private JavacScope(Env<AttrContext> env) { |
10 | 70 |
env.getClass(); // null-check |
71 |
this.env = env; |
|
72 |
} |
|
73 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
74 |
@DefinedBy(Api.COMPILER_TREE) |
10 | 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 | 79 |
// synthesize an outermost "star-import" scope |
80 |
return new JavacScope(env) { |
|
81 |
public boolean isStarImportScope() { |
|
82 |
return true; |
|
83 |
} |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
84 |
@DefinedBy(Api.COMPILER_TREE) |
10 | 85 |
public JavacScope getEnclosingScope() { |
86 |
return null; |
|
87 |
} |
|
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
88 |
@DefinedBy(Api.COMPILER_TREE) |
10 | 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 | 91 |
} |
92 |
}; |
|
93 |
} |
|
94 |
} |
|
95 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
96 |
@DefinedBy(Api.COMPILER_TREE) |
10 | 97 |
public TypeElement getEnclosingClass() { |
98 |
// hide the dummy class that javac uses to enclose the top level declarations |
|
99 |
return (env.outer == null || env.outer == env ? null : env.enclClass.sym); |
|
100 |
} |
|
101 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
102 |
@DefinedBy(Api.COMPILER_TREE) |
10 | 103 |
public ExecutableElement getEnclosingMethod() { |
104 |
return (env.enclMethod == null ? null : env.enclMethod.sym); |
|
105 |
} |
|
106 |
||
26266
2d24bda701dc
8056061: Mark implementations of public interfaces with an annotation
jlahoda
parents:
25874
diff
changeset
|
107 |
@DefinedBy(Api.COMPILER_TREE) |
10 | 108 |
public Iterable<? extends Element> getLocalElements() { |
109 |
return env.info.getLocalElements(); |
|
110 |
} |
|
111 |
||
112 |
public Env<AttrContext> getEnv() { |
|
113 |
return env; |
|
114 |
} |
|
115 |
||
116 |
public boolean isStarImportScope() { |
|
117 |
return false; |
|
118 |
} |
|
119 |
||
120 |
public boolean equals(Object other) { |
|
121 |
if (other instanceof JavacScope) { |
|
122 |
JavacScope s = (JavacScope) other; |
|
123 |
return (env.equals(s.env) |
|
124 |
&& isStarImportScope() == s.isStarImportScope()); |
|
125 |
} else |
|
126 |
return false; |
|
127 |
} |
|
128 |
||
129 |
public int hashCode() { |
|
130 |
return env.hashCode() + (isStarImportScope() ? 1 : 0); |
|
131 |
} |
|
132 |
||
133 |
public String toString() { |
|
134 |
return "JavacScope[env=" + env + ",starImport=" + isStarImportScope() + "]"; |
|
135 |
} |
|
136 |
} |