8031569: Refactor javac scope implementation to enable lazy imports

Introducing an internal API for Scope; rewriting ImportScopes to extend CompoundScopes.

Co-authored-by: Maurizio Cimadamore <maurizio.cimadamore@oracle.com>
Reviewed-by: mcimadamore, jjg, jfranck
This commit is contained in:
Jan Lahoda 2014-07-09 16:32:05 +02:00
parent c2f98fba15
commit 0067b70725
50 changed files with 1614 additions and 1383 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2006, 2008, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -46,18 +46,33 @@ import com.sun.tools.javac.comp.Env;
* @author Jonathan Gibbons;
*/
public class JavacScope implements com.sun.source.tree.Scope {
static JavacScope create(Env<AttrContext> env) {
if (env.outer == null || env.outer == env) {
//the "top-level" scope needs to return both imported and defined elements
//see test CheckLocalElements
return new JavacScope(env) {
@Override
public Iterable<? extends Element> getLocalElements() {
return env.toplevel.namedImportScope.getSymbols();
}
};
} else {
return new JavacScope(env);
}
}
protected final Env<AttrContext> env;
/** Creates a new instance of JavacScope */
JavacScope(Env<AttrContext> env) {
private JavacScope(Env<AttrContext> env) {
env.getClass(); // null-check
this.env = env;
}
public JavacScope getEnclosingScope() {
if (env.outer != null && env.outer != env)
return new JavacScope(env.outer);
else {
if (env.outer != null && env.outer != env) {
return create(env.outer);
} else {
// synthesize an outermost "star-import" scope
return new JavacScope(env) {
public boolean isStarImportScope() {
@ -67,7 +82,7 @@ public class JavacScope implements com.sun.source.tree.Scope {
return null;
}
public Iterable<? extends Element> getLocalElements() {
return env.toplevel.starImportScope.getElements();
return env.toplevel.starImportScope.getSymbols();
}
};
}

View File

@ -456,10 +456,9 @@ public class JavacTrees extends DocTrees {
}
searched.add(tsym);
for (com.sun.tools.javac.code.Scope.Entry e = tsym.members().lookup(fieldName);
e.scope != null; e = e.next()) {
if (e.sym.kind == Kinds.VAR) {
return (VarSymbol)e.sym;
for (Symbol sym : tsym.members().getSymbolsByName(fieldName)) {
if (sym.kind == Kinds.VAR) {
return (VarSymbol)sym;
}
}
@ -499,11 +498,10 @@ public class JavacTrees extends DocTrees {
/** @see com.sun.tools.javadoc.ClassDocImpl#findConstructor */
MethodSymbol findConstructor(ClassSymbol tsym, List<Type> paramTypes) {
for (com.sun.tools.javac.code.Scope.Entry e = tsym.members().lookup(names.init);
e.scope != null; e = e.next()) {
if (e.sym.kind == Kinds.MTH) {
if (hasParameterTypes((MethodSymbol) e.sym, paramTypes)) {
return (MethodSymbol) e.sym;
for (Symbol sym : tsym.members().getSymbolsByName(names.init)) {
if (sym.kind == Kinds.MTH) {
if (hasParameterTypes((MethodSymbol) sym, paramTypes)) {
return (MethodSymbol) sym;
}
}
}
@ -529,7 +527,6 @@ public class JavacTrees extends DocTrees {
searched.add(tsym);
// search current class
com.sun.tools.javac.code.Scope.Entry e = tsym.members().lookup(methodName);
//### Using modifier filter here isn't really correct,
//### but emulates the old behavior. Instead, we should
@ -542,10 +539,10 @@ public class JavacTrees extends DocTrees {
// In order to provide textually identical results, we
// attempt to emulate the old behavior.
MethodSymbol lastFound = null;
for (; e.scope != null; e = e.next()) {
if (e.sym.kind == Kinds.MTH) {
if (e.sym.name == methodName) {
lastFound = (MethodSymbol)e.sym;
for (Symbol sym : tsym.members().getSymbolsByName(methodName)) {
if (sym.kind == Kinds.MTH) {
if (sym.name == methodName) {
lastFound = (MethodSymbol)sym;
}
}
}
@ -553,11 +550,11 @@ public class JavacTrees extends DocTrees {
return lastFound;
}
} else {
for (; e.scope != null; e = e.next()) {
if (e.sym != null &&
e.sym.kind == Kinds.MTH) {
if (hasParameterTypes((MethodSymbol) e.sym, paramTypes)) {
return (MethodSymbol) e.sym;
for (Symbol sym : tsym.members().getSymbolsByName(methodName)) {
if (sym != null &&
sym.kind == Kinds.MTH) {
if (hasParameterTypes((MethodSymbol) sym, paramTypes)) {
return (MethodSymbol) sym;
}
}
}
@ -684,7 +681,7 @@ public class JavacTrees extends DocTrees {
}
public JavacScope getScope(TreePath path) {
return new JavacScope(getAttrContext(path));
return JavacScope.create(getAttrContext(path));
}
public String getDocComment(TreePath path) {

View File

@ -37,6 +37,7 @@ import javax.tools.StandardJavaFileManager;
import static javax.tools.StandardLocation.*;
import com.sun.tools.javac.comp.Annotate;
import com.sun.tools.javac.code.Scope.WriteableScope;
import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.jvm.ClassReader;
import com.sun.tools.javac.util.*;
@ -218,7 +219,7 @@ public class ClassFinder {
if (c.owner.kind == PCK) {
Symbol owner = c.owner;
for (Name name : Convert.enclosingCandidates(Convert.shortName(c.name))) {
Symbol encl = owner.members().lookup(name).sym;
Symbol encl = owner.members().findFirst(name);
if (encl == null)
encl = syms.classes.get(TypeSymbol.formFlatName(name, owner));
if (encl != null)
@ -335,7 +336,7 @@ public class ClassFinder {
boolean isPkgInfo = classname == names.package_info;
ClassSymbol c = isPkgInfo
? p.package_info
: (ClassSymbol) p.members_field.lookup(classname).sym;
: (ClassSymbol) p.members_field.findFirst(classname);
if (c == null) {
c = syms.enterClass(classname, p);
if (c.classfile == null) // only update the file if's it's newly created
@ -399,7 +400,7 @@ public class ClassFinder {
*/
private void fillIn(PackageSymbol p) throws IOException {
if (p.members_field == null)
p.members_field = new Scope(p);
p.members_field = WriteableScope.create(p);
preferCurrent = false;
if (userPathsFirst) {

File diff suppressed because it is too large Load Diff

View File

@ -33,19 +33,21 @@ import java.util.concurrent.Callable;
import javax.lang.model.element.*;
import javax.tools.JavaFileObject;
import com.sun.tools.javac.code.Scope.WriteableScope;
import com.sun.tools.javac.code.Type.*;
import com.sun.tools.javac.comp.Attr;
import com.sun.tools.javac.comp.AttrContext;
import com.sun.tools.javac.comp.Env;
import com.sun.tools.javac.jvm.*;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
import com.sun.tools.javac.util.*;
import com.sun.tools.javac.util.Name;
import static com.sun.tools.javac.code.Flags.*;
import static com.sun.tools.javac.code.Kinds.*;
import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
import static com.sun.tools.javac.code.TypeTag.CLASS;
import static com.sun.tools.javac.code.TypeTag.FORALL;
import static com.sun.tools.javac.code.TypeTag.TYPEVAR;
import com.sun.tools.javac.tree.JCTree.JCVariableDecl;
/** Root class for Java symbols. It contains subclasses
* for specific sorts of symbols, such as variables, methods and operators,
@ -376,7 +378,7 @@ public abstract class Symbol extends AnnoConstruct implements Element {
/** If this is a class or package, its members, otherwise null.
*/
public Scope members() {
public WriteableScope members() {
return null;
}
@ -475,15 +477,13 @@ public abstract class Symbol extends AnnoConstruct implements Element {
if (currentClass == owner) {
return this;
}
Scope.Entry e = currentClass.members().lookup(name);
while (e.scope != null) {
if (e.sym.kind == kind &&
for (Symbol sym : currentClass.members().getSymbolsByName(name)) {
if (sym.kind == kind &&
(kind != MTH ||
(e.sym.flags() & STATIC) != 0 &&
types.isSubSignature(e.sym.type, type))) {
return e.sym;
(sym.flags() & STATIC) != 0 &&
types.isSubSignature(sym.type, type))) {
return sym;
}
e = e.next();
}
Symbol hiddenSym = null;
for (Type st : types.interfaces(currentClass.type)
@ -632,7 +632,7 @@ public abstract class Symbol extends AnnoConstruct implements Element {
public boolean isConstructor() { return other.isConstructor(); }
public Name getQualifiedName() { return other.getQualifiedName(); }
public Name flatName() { return other.flatName(); }
public Scope members() { return other.members(); }
public WriteableScope members() { return other.members(); }
public boolean isInner() { return other.isInner(); }
public boolean hasOuterInstance() { return other.hasOuterInstance(); }
public ClassSymbol enclClass() { return other.enclClass(); }
@ -721,9 +721,9 @@ public abstract class Symbol extends AnnoConstruct implements Element {
if (kind == TYP && type.hasTag(TYPEVAR)) {
return list;
}
for (Scope.Entry e = members().elems; e != null; e = e.sibling) {
if (e.sym != null && (e.sym.flags() & SYNTHETIC) == 0 && e.sym.owner == this)
list = list.prepend(e.sym);
for (Symbol sym : members().getSymbols(NON_RECURSIVE)) {
if (sym != null && (sym.flags() & SYNTHETIC) == 0 && sym.owner == this)
list = list.prepend(sym);
}
return list;
}
@ -818,7 +818,7 @@ public abstract class Symbol extends AnnoConstruct implements Element {
public static class PackageSymbol extends TypeSymbol
implements PackageElement {
public Scope members_field;
public WriteableScope members_field;
public Name fullname;
public ClassSymbol package_info; // see bug 6443073
@ -845,7 +845,7 @@ public abstract class Symbol extends AnnoConstruct implements Element {
return name.isEmpty() && owner != null;
}
public Scope members() {
public WriteableScope members() {
if (completer != null) complete();
return members_field;
}
@ -910,7 +910,7 @@ public abstract class Symbol extends AnnoConstruct implements Element {
/** a scope for all class members; variables, methods and inner classes
* type parameters are not part of this scope
*/
public Scope members_field;
public WriteableScope members_field;
/** the fully qualified name of the class, i.e. pck.outer.inner.
* null for anonymous classes
@ -971,7 +971,7 @@ public abstract class Symbol extends AnnoConstruct implements Element {
return flags_field;
}
public Scope members() {
public WriteableScope members() {
if (completer != null) complete();
return members_field;
}
@ -1397,15 +1397,13 @@ public abstract class Symbol extends AnnoConstruct implements Element {
public Symbol implementedIn(TypeSymbol c, Types types) {
Symbol impl = null;
for (Scope.Entry e = c.members().lookup(name);
impl == null && e.scope != null;
e = e.next()) {
if (this.overrides(e.sym, (TypeSymbol)owner, types, true) &&
for (Symbol sym : c.members().getSymbolsByName(name)) {
if (this.overrides(sym, (TypeSymbol)owner, types, true) &&
// FIXME: I suspect the following requires a
// subst() for a parametric return type.
types.isSameType(type.getReturnType(),
types.memberType(owner.type, e.sym).getReturnType())) {
impl = e.sym;
types.memberType(owner.type, sym).getReturnType())) {
impl = sym;
}
}
return impl;
@ -1441,12 +1439,10 @@ public abstract class Symbol extends AnnoConstruct implements Element {
*/
public MethodSymbol binaryImplementation(ClassSymbol origin, Types types) {
for (TypeSymbol c = origin; c != null; c = types.supertype(c.type).tsym) {
for (Scope.Entry e = c.members().lookup(name);
e.scope != null;
e = e.next()) {
if (e.sym.kind == MTH &&
((MethodSymbol)e.sym).binaryOverrides(this, origin, types))
return (MethodSymbol)e.sym;
for (Symbol sym : c.members().getSymbolsByName(name)) {
if (sym.kind == MTH &&
((MethodSymbol)sym).binaryOverrides(this, origin, types))
return (MethodSymbol)sym;
}
}
return null;

View File

@ -34,6 +34,7 @@ import javax.lang.model.element.ElementVisitor;
import javax.tools.JavaFileObject;
import com.sun.tools.javac.code.Scope.WriteableScope;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.code.Symbol.Completer;
import com.sun.tools.javac.code.Symbol.CompletionFailure;
@ -396,7 +397,7 @@ public class Symtab {
sym.completer = null;
sym.flags_field = PUBLIC|ACYCLIC|ANNOTATION|INTERFACE;
sym.erasure_field = type;
sym.members_field = new Scope(sym);
sym.members_field = WriteableScope.create(sym);
type.typarams_field = List.nil();
type.allparams_field = List.nil();
type.supertype_field = annotationType;
@ -466,7 +467,7 @@ public class Symtab {
// Create class to hold all predefined constants and operations.
predefClass = new ClassSymbol(PUBLIC|ACYCLIC, names.empty, rootPackage);
Scope scope = new Scope(predefClass);
WriteableScope scope = WriteableScope.create(predefClass);
predefClass.members_field = scope;
// Get the initial completer for Symbols from the ClassFinder
@ -578,7 +579,7 @@ public class Symtab {
ClassType arrayClassType = (ClassType)arrayClass.type;
arrayClassType.supertype_field = objectType;
arrayClassType.interfaces_field = List.of(cloneableType, serializableType);
arrayClass.members_field = new Scope(arrayClass);
arrayClass.members_field = WriteableScope.create(arrayClass);
lengthVar = new VarSymbol(
PUBLIC | FINAL,
names.length,

View File

@ -403,7 +403,7 @@ public class Types {
}
final ListBuffer<Symbol> abstracts = new ListBuffer<>();
for (Symbol sym : membersCache.getElements(new DescriptorFilter(origin))) {
for (Symbol sym : membersCache.getSymbols(new DescriptorFilter(origin))) {
Type mtype = memberType(origin.type, sym);
if (abstracts.isEmpty() ||
(sym.name == abstracts.first().name &&
@ -633,7 +633,7 @@ public class Types {
Type descType = findDescriptorType(targets.head);
ClassSymbol csym = new ClassSymbol(cflags, name, env.enclClass.sym.outermostClass());
csym.completer = null;
csym.members_field = new Scope(csym);
csym.members_field = WriteableScope.create(csym);
MethodSymbol instDescSym = new MethodSymbol(descSym.flags(), descSym.name, descType, csym);
csym.members_field.enter(instDescSym);
Type.ClassType ctype = new Type.ClassType(Type.noType, List.<Type>nil(), csym,
@ -655,7 +655,7 @@ public class Types {
Symbol descSym = findDescriptorSymbol(origin);
CompoundScope members = membersClosure(origin.type, false);
ListBuffer<Symbol> overridden = new ListBuffer<>();
outer: for (Symbol m2 : members.getElementsByName(descSym.name, bridgeFilter)) {
outer: for (Symbol m2 : members.getSymbolsByName(descSym.name, bridgeFilter)) {
if (m2 == descSym) continue;
else if (descSym.overrides(m2, origin, Types.this, false)) {
for (Symbol m3 : overridden) {
@ -2290,7 +2290,7 @@ public class Types {
bc.erasure_field = (bounds.head.hasTag(TYPEVAR)) ?
syms.objectType : // error condition, recover
erasure(firstExplicitBound);
bc.members_field = new Scope(bc);
bc.members_field = WriteableScope.create(bc);
return bc.type;
}
@ -2619,8 +2619,8 @@ public class Types {
}
public boolean overridesObjectMethod(TypeSymbol origin, Symbol msym) {
for (Scope.Entry e = syms.objectType.tsym.members().lookup(msym.name) ; e.scope != null ; e = e.next()) {
if (msym.overrides(e.sym, origin, Types.this, true)) {
for (Symbol sym : syms.objectType.tsym.members().getSymbolsByName(msym.name)) {
if (msym.overrides(sym, origin, Types.this, true)) {
return true;
}
}
@ -2680,12 +2680,10 @@ public class Types {
while (t.hasTag(TYPEVAR))
t = t.getUpperBound();
TypeSymbol c = t.tsym;
for (Scope.Entry e = c.members().lookup(ms.name, implFilter);
e.scope != null;
e = e.next(implFilter)) {
if (e.sym != null &&
e.sym.overrides(ms, origin, Types.this, checkResult))
return (MethodSymbol)e.sym;
for (Symbol sym : c.members().getSymbolsByName(ms.name, implFilter)) {
if (sym != null &&
sym.overrides(ms, origin, Types.this, checkResult))
return (MethodSymbol)sym;
}
}
return null;
@ -2742,11 +2740,11 @@ public class Types {
CompoundScope membersClosure = new CompoundScope(csym);
if (!skipInterface) {
for (Type i : interfaces(t)) {
membersClosure.addSubScope(visit(i, skipInterface));
membersClosure.prependSubScope(visit(i, skipInterface));
}
}
membersClosure.addSubScope(visit(supertype(t), skipInterface));
membersClosure.addSubScope(csym.members());
membersClosure.prependSubScope(visit(supertype(t), skipInterface));
membersClosure.prependSubScope(csym.members());
e = new Entry(skipInterface, membersClosure);
_map.put(csym, e);
}
@ -2775,7 +2773,7 @@ public class Types {
public List<MethodSymbol> interfaceCandidates(Type site, MethodSymbol ms) {
Filter<Symbol> filter = new MethodFilter(ms, site);
List<MethodSymbol> candidates = List.nil();
for (Symbol s : membersClosure(site, false).getElements(filter)) {
for (Symbol s : membersClosure(site, false).getSymbols(filter)) {
if (!site.tsym.isInterface() && !s.owner.isInterface()) {
return List.of((MethodSymbol)s);
} else if (!candidates.contains(s)) {

View File

@ -690,7 +690,7 @@ public class Annotate {
Scope scope = targetContainerType.tsym.members();
int nr_value_elems = 0;
boolean error = false;
for(Symbol elm : scope.getElementsByName(names.value)) {
for(Symbol elm : scope.getSymbolsByName(names.value)) {
nr_value_elems++;
if (nr_value_elems == 1 &&

View File

@ -37,6 +37,7 @@ import com.sun.source.tree.TreeVisitor;
import com.sun.source.util.SimpleTreeVisitor;
import com.sun.tools.javac.code.*;
import com.sun.tools.javac.code.Lint.LintCategory;
import com.sun.tools.javac.code.Scope.WriteableScope;
import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.code.Type.*;
import com.sun.tools.javac.comp.Check.CheckContext;
@ -618,14 +619,11 @@ public class Attr extends JCTree.Visitor {
return newEnv;
}
Scope copyScope(Scope sc) {
Scope newScope = new Scope(sc.owner);
WriteableScope copyScope(WriteableScope sc) {
WriteableScope newScope = WriteableScope.create(sc.owner);
List<Symbol> elemsList = List.nil();
while (sc != null) {
for (Scope.Entry e = sc.elems ; e != null ; e = e.sibling) {
elemsList = elemsList.prepend(e.sym);
}
sc = sc.next;
for (Symbol sym : sc.getSymbols()) {
elemsList = elemsList.prepend(sym);
}
for (Symbol s : elemsList) {
newScope.enter(s);
@ -1140,12 +1138,12 @@ public class Attr extends JCTree.Visitor {
// Block is a static or instance initializer;
// let the owner of the environment be a freshly
// created BLOCK-method.
final Env<AttrContext> localEnv =
env.dup(tree, env.info.dup(env.info.scope.dupUnshared()));
localEnv.info.scope.owner =
Symbol fakeOwner =
new MethodSymbol(tree.flags | BLOCK |
env.info.scope.owner.flags() & STRICTFP, names.empty, null,
env.info.scope.owner);
final Env<AttrContext> localEnv =
env.dup(tree, env.info.dup(env.info.scope.dupUnshared(fakeOwner)));
if ((tree.flags & STATIC) != 0) localEnv.info.staticLevel++;
attribStats(tree.stats, localEnv);
@ -1328,7 +1326,7 @@ public class Attr extends JCTree.Visitor {
}
// where
/** Add any variables defined in stats to the switch scope. */
private static void addVars(List<JCStatement> stats, Scope switchScope) {
private static void addVars(List<JCStatement> stats, WriteableScope switchScope) {
for (;stats.nonEmpty(); stats = stats.tail) {
JCTree stat = stats.head;
if (stat.hasTag(VARDEF))
@ -1344,10 +1342,9 @@ public class Attr extends JCTree.Visitor {
}
JCIdent ident = (JCIdent)tree;
Name name = ident.name;
for (Scope.Entry e = enumType.tsym.members().lookup(name);
e.scope != null; e = e.next()) {
if (e.sym.kind == VAR) {
Symbol s = ident.sym = e.sym;
for (Symbol sym : enumType.tsym.members().getSymbolsByName(name)) {
if (sym.kind == VAR) {
Symbol s = ident.sym = sym;
((VarSymbol)s).getConstValue(); // ensure initializer is evaluated
ident.type = s.type;
return ((s.flags_field & Flags.ENUM) == 0)
@ -2343,7 +2340,7 @@ public class Attr extends JCTree.Visitor {
Symbol descriptor = types.findDescriptorSymbol(clazztype.tsym);
int count = 0;
boolean found = false;
for (Symbol sym : csym.members().getElements()) {
for (Symbol sym : csym.members().getSymbols()) {
if ((sym.flags() & SYNTHETIC) != 0 ||
sym.isConstructor()) continue;
count++;
@ -2774,15 +2771,15 @@ public class Attr extends JCTree.Visitor {
Symbol owner = env.info.scope.owner;
if (owner.kind == VAR && owner.owner.kind == TYP) {
//field initializer
lambdaEnv = env.dup(that, env.info.dup(env.info.scope.dupUnshared()));
ClassSymbol enclClass = owner.enclClass();
Symbol newScopeOwner = env.info.scope.owner;
/* if the field isn't static, then we can get the first constructor
* and use it as the owner of the environment. This is what
* LTM code is doing to look for type annotations so we are fine.
*/
if ((owner.flags() & STATIC) == 0) {
for (Symbol s : enclClass.members_field.getElementsByName(names.init)) {
lambdaEnv.info.scope.owner = s;
for (Symbol s : enclClass.members_field.getSymbolsByName(names.init)) {
newScopeOwner = s;
break;
}
} else {
@ -2798,8 +2795,9 @@ public class Attr extends JCTree.Visitor {
clinit.params = List.<VarSymbol>nil();
clinits.put(enclClass, clinit);
}
lambdaEnv.info.scope.owner = clinit;
newScopeOwner = clinit;
}
lambdaEnv = env.dup(that, env.info.dup(env.info.scope.dupUnshared(newScopeOwner)));
} else {
lambdaEnv = env.dup(that, env.info.dup(env.info.scope.dup()));
}
@ -4515,7 +4513,7 @@ public class Attr extends JCTree.Visitor {
for (List<JCTypeParameter> l = tree.typarams;
l.nonEmpty(); l = l.tail) {
Assert.checkNonNull(env.info.scope.lookup(l.head.name).scope);
Assert.checkNonNull(env.info.scope.findFirst(l.head.name));
}
// Check that a generic class doesn't extend Throwable
@ -4602,16 +4600,21 @@ public class Attr extends JCTree.Visitor {
private void checkSerialVersionUID(JCClassDecl tree, ClassSymbol c) {
// check for presence of serialVersionUID
Scope.Entry e = c.members().lookup(names.serialVersionUID);
while (e.scope != null && e.sym.kind != VAR) e = e.next();
if (e.scope == null) {
VarSymbol svuid = null;
for (Symbol sym : c.members().getSymbolsByName(names.serialVersionUID)) {
if (sym.kind == VAR) {
svuid = (VarSymbol)sym;
break;
}
}
if (svuid == null) {
log.warning(LintCategory.SERIAL,
tree.pos(), "missing.SVUID", c);
return;
}
// check that it is static final
VarSymbol svuid = (VarSymbol)e.sym;
if ((svuid.flags() & (STATIC | FINAL)) !=
(STATIC | FINAL))
log.warning(LintCategory.SERIAL,

View File

@ -27,6 +27,7 @@ package com.sun.tools.javac.comp;
import com.sun.tools.javac.util.*;
import com.sun.tools.javac.code.*;
import com.sun.tools.javac.code.Scope.WriteableScope;
/** Contains information specific to the attribute and enter
* passes, to be used in place of the generic field in environments.
@ -40,7 +41,7 @@ public class AttrContext {
/** The scope of local symbols.
*/
Scope scope = null;
WriteableScope scope = null;
/** The number of enclosing `static' modifiers.
*/
@ -87,7 +88,7 @@ public class AttrContext {
/** Duplicate this context, replacing scope field and copying all others.
*/
AttrContext dup(Scope scope) {
AttrContext dup(WriteableScope scope) {
AttrContext info = new AttrContext();
info.scope = scope;
info.staticLevel = staticLevel;
@ -112,7 +113,7 @@ public class AttrContext {
public Iterable<Symbol> getLocalElements() {
if (scope == null)
return List.nil();
return scope.getElements();
return scope.getSymbols();
}
boolean lastResolveVarargs() {

View File

@ -28,6 +28,7 @@ package com.sun.tools.javac.comp;
import java.util.*;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import com.sun.tools.javac.code.*;
import com.sun.tools.javac.code.Attribute.Compound;
@ -39,6 +40,8 @@ import com.sun.tools.javac.util.List;
import com.sun.tools.javac.code.Lint;
import com.sun.tools.javac.code.Lint.LintCategory;
import com.sun.tools.javac.code.Scope.NamedImportScope;
import com.sun.tools.javac.code.Scope.WriteableScope;
import com.sun.tools.javac.code.Type.*;
import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.comp.DeferredAttr.DeferredAttrContext;
@ -51,6 +54,7 @@ import static com.sun.tools.javac.code.Flags.*;
import static com.sun.tools.javac.code.Flags.ANNOTATION;
import static com.sun.tools.javac.code.Flags.SYNCHRONIZED;
import static com.sun.tools.javac.code.Kinds.*;
import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
import static com.sun.tools.javac.code.TypeTag.*;
import static com.sun.tools.javac.code.TypeTag.WILDCARD;
@ -362,16 +366,13 @@ public class Check {
* @param s The scope.
*/
void checkTransparentVar(DiagnosticPosition pos, VarSymbol v, Scope s) {
if (s.next != null) {
for (Scope.Entry e = s.next.lookup(v.name);
e.scope != null && e.sym.owner == v.owner;
e = e.next()) {
if (e.sym.kind == VAR &&
(e.sym.owner.kind & (VAR | MTH)) != 0 &&
v.name != names.error) {
duplicateError(pos, e.sym);
return;
}
for (Symbol sym : s.getSymbolsByName(v.name)) {
if (sym.owner != v.owner) break;
if (sym.kind == VAR &&
(sym.owner.kind & (VAR | MTH)) != 0 &&
v.name != names.error) {
duplicateError(pos, sym);
return;
}
}
}
@ -383,16 +384,13 @@ public class Check {
* @param s The scope.
*/
void checkTransparentClass(DiagnosticPosition pos, ClassSymbol c, Scope s) {
if (s.next != null) {
for (Scope.Entry e = s.next.lookup(c.name);
e.scope != null && e.sym.owner == c.owner;
e = e.next()) {
if (e.sym.kind == TYP && !e.sym.type.hasTag(TYPEVAR) &&
(e.sym.owner.kind & (VAR | MTH)) != 0 &&
c.name != names.error) {
duplicateError(pos, e.sym);
return;
}
for (Symbol sym : s.getSymbolsByName(c.name)) {
if (sym.owner != c.owner) break;
if (sym.kind == TYP && !sym.type.hasTag(TYPEVAR) &&
(sym.owner.kind & (VAR | MTH)) != 0 &&
c.name != names.error) {
duplicateError(pos, sym);
return;
}
}
}
@ -405,9 +403,9 @@ public class Check {
* @param s The enclosing scope.
*/
boolean checkUniqueClassName(DiagnosticPosition pos, Name name, Scope s) {
for (Scope.Entry e = s.lookup(name); e.scope == s; e = e.next()) {
if (e.sym.kind == TYP && e.sym.name != names.error) {
duplicateError(pos, e.sym);
for (Symbol sym : s.getSymbolsByName(name, NON_RECURSIVE)) {
if (sym.kind == TYP && sym.name != names.error) {
duplicateError(pos, sym);
return false;
}
}
@ -1778,10 +1776,7 @@ public class Check {
for (Type t1 = sup;
t1.hasTag(CLASS) && t1.tsym.type.isParameterized();
t1 = types.supertype(t1)) {
for (Scope.Entry e1 = t1.tsym.members().elems;
e1 != null;
e1 = e1.sibling) {
Symbol s1 = e1.sym;
for (Symbol s1 : t1.tsym.members().getSymbols(NON_RECURSIVE)) {
if (s1.kind != MTH ||
(s1.flags() & (STATIC|SYNTHETIC|BRIDGE)) != 0 ||
!s1.isInheritedIn(site.tsym, types) ||
@ -1796,10 +1791,7 @@ public class Check {
for (Type t2 = sup;
t2.hasTag(CLASS);
t2 = types.supertype(t2)) {
for (Scope.Entry e2 = t2.tsym.members().lookup(s1.name);
e2.scope != null;
e2 = e2.next()) {
Symbol s2 = e2.sym;
for (Symbol s2 : t2.tsym.members().getSymbolsByName(s1.name)) {
if (s2 == s1 ||
s2.kind != MTH ||
(s2.flags() & (STATIC|SYNTHETIC|BRIDGE)) != 0 ||
@ -1893,15 +1885,13 @@ public class Check {
/** Return the first method in t2 that conflicts with a method from t1. */
private Symbol firstDirectIncompatibility(DiagnosticPosition pos, Type t1, Type t2, Type site) {
for (Scope.Entry e1 = t1.tsym.members().elems; e1 != null; e1 = e1.sibling) {
Symbol s1 = e1.sym;
for (Symbol s1 : t1.tsym.members().getSymbols(NON_RECURSIVE)) {
Type st1 = null;
if (s1.kind != MTH || !s1.isInheritedIn(site.tsym, types) ||
(s1.flags() & SYNTHETIC) != 0) continue;
Symbol impl = ((MethodSymbol)s1).implementation(site.tsym, types, false);
if (impl != null && (impl.flags() & ABSTRACT) == 0) continue;
for (Scope.Entry e2 = t2.tsym.members().lookup(s1.name); e2.scope != null; e2 = e2.next()) {
Symbol s2 = e2.sym;
for (Symbol s2 : t2.tsym.members().getSymbolsByName(s1.name)) {
if (s1 == s2) continue;
if (s2.kind != MTH || !s2.isInheritedIn(site.tsym, types) ||
(s2.flags() & SYNTHETIC) != 0) continue;
@ -1944,8 +1934,7 @@ public class Check {
Type st2 = types.memberType(site, s2);
closure(site, supertypes);
for (Type t : supertypes.values()) {
for (Scope.Entry e = t.tsym.members().lookup(s1.name); e.scope != null; e = e.next()) {
Symbol s3 = e.sym;
for (Symbol s3 : t.tsym.members().getSymbolsByName(s1.name)) {
if (s3 == s1 || s3 == s2 || s3.kind != MTH || (s3.flags() & (BRIDGE|SYNTHETIC)) != 0) continue;
Type st3 = types.memberType(site,s3);
if (types.overrideEquivalent(st3, st1) &&
@ -1995,14 +1984,12 @@ public class Check {
void checkOverride(JCTree tree, Type site, ClassSymbol origin, MethodSymbol m) {
TypeSymbol c = site.tsym;
Scope.Entry e = c.members().lookup(m.name);
while (e.scope != null) {
if (m.overrides(e.sym, origin, types, false)) {
if ((e.sym.flags() & ABSTRACT) == 0) {
checkOverride(tree, m, (MethodSymbol)e.sym, origin);
for (Symbol sym : c.members().getSymbolsByName(m.name)) {
if (m.overrides(sym, origin, types, false)) {
if ((sym.flags() & ABSTRACT) == 0) {
checkOverride(tree, m, (MethodSymbol)sym, origin);
}
}
e = e.next();
}
}
@ -2037,9 +2024,9 @@ public class Check {
ClassSymbol someClass) {
if (lint.isEnabled(LintCategory.OVERRIDES)) {
MethodSymbol equalsAtObject = (MethodSymbol)syms.objectType
.tsym.members().lookup(names.equals).sym;
.tsym.members().findFirst(names.equals);
MethodSymbol hashCodeAtObject = (MethodSymbol)syms.objectType
.tsym.members().lookup(names.hashCode).sym;
.tsym.members().findFirst(names.hashCode);
boolean overridesEquals = types.implementation(equalsAtObject,
someClass, false, equalsHasCodeFilter).owner == someClass;
boolean overridesHashCode = types.implementation(hashCodeAtObject,
@ -2095,12 +2082,10 @@ public class Check {
// since they cannot have abstract members.
if (c == impl || (c.flags() & (ABSTRACT | INTERFACE)) != 0) {
Scope s = c.members();
for (Scope.Entry e = s.elems;
undef == null && e != null;
e = e.sibling) {
if (e.sym.kind == MTH &&
(e.sym.flags() & (ABSTRACT|IPROXY|DEFAULT)) == ABSTRACT) {
MethodSymbol absmeth = (MethodSymbol)e.sym;
for (Symbol sym : s.getSymbols(NON_RECURSIVE)) {
if (sym.kind == MTH &&
(sym.flags() & (ABSTRACT|IPROXY|DEFAULT)) == ABSTRACT) {
MethodSymbol absmeth = (MethodSymbol)sym;
MethodSymbol implmeth = absmeth.implementation(impl, types, true);
if (implmeth == null || implmeth == absmeth) {
//look for default implementations
@ -2113,6 +2098,7 @@ public class Check {
}
if (implmeth == null || implmeth == absmeth) {
undef = absmeth;
break;
}
}
}
@ -2336,10 +2322,10 @@ public class Check {
for (List<Type> l = types.closure(ic.type); l.nonEmpty(); l = l.tail) {
ClassSymbol lc = (ClassSymbol)l.head.tsym;
if ((allowGenerics || origin != lc) && (lc.flags() & ABSTRACT) != 0) {
for (Scope.Entry e=lc.members().elems; e != null; e=e.sibling) {
if (e.sym.kind == MTH &&
(e.sym.flags() & (STATIC|ABSTRACT)) == ABSTRACT) {
MethodSymbol absmeth = (MethodSymbol)e.sym;
for (Symbol sym : lc.members().getSymbols(NON_RECURSIVE)) {
if (sym.kind == MTH &&
(sym.flags() & (STATIC|ABSTRACT)) == ABSTRACT) {
MethodSymbol absmeth = (MethodSymbol)sym;
MethodSymbol implmeth = absmeth.implementation(origin, types, false);
if (implmeth != null && implmeth != absmeth &&
(implmeth.owner.flags() & INTERFACE) ==
@ -2382,15 +2368,15 @@ public class Check {
void checkConflicts(DiagnosticPosition pos, Symbol sym, TypeSymbol c) {
for (Type ct = c.type; ct != Type.noType ; ct = types.supertype(ct)) {
for (Scope.Entry e = ct.tsym.members().lookup(sym.name); e.scope == ct.tsym.members(); e = e.next()) {
for (Symbol sym2 : ct.tsym.members().getSymbolsByName(sym.name, NON_RECURSIVE)) {
// VM allows methods and variables with differing types
if (sym.kind == e.sym.kind &&
types.isSameType(types.erasure(sym.type), types.erasure(e.sym.type)) &&
sym != e.sym &&
(sym.flags() & Flags.SYNTHETIC) != (e.sym.flags() & Flags.SYNTHETIC) &&
(sym.flags() & IPROXY) == 0 && (e.sym.flags() & IPROXY) == 0 &&
(sym.flags() & BRIDGE) == 0 && (e.sym.flags() & BRIDGE) == 0) {
syntheticError(pos, (e.sym.flags() & SYNTHETIC) == 0 ? e.sym : sym);
if (sym.kind == sym2.kind &&
types.isSameType(types.erasure(sym.type), types.erasure(sym2.type)) &&
sym != sym2 &&
(sym.flags() & Flags.SYNTHETIC) != (sym2.flags() & Flags.SYNTHETIC) &&
(sym.flags() & IPROXY) == 0 && (sym2.flags() & IPROXY) == 0 &&
(sym.flags() & BRIDGE) == 0 && (sym2.flags() & BRIDGE) == 0) {
syntheticError(pos, (sym2.flags() & SYNTHETIC) == 0 ? sym2 : sym);
return;
}
}
@ -2411,7 +2397,7 @@ public class Check {
List<MethodSymbol> potentiallyAmbiguousList = List.nil();
boolean overridesAny = false;
for (Symbol m1 : types.membersClosure(site, false).getElementsByName(sym.name, cf)) {
for (Symbol m1 : types.membersClosure(site, false).getSymbolsByName(sym.name, cf)) {
if (!sym.overrides(m1, site.tsym, types, false)) {
if (m1 == sym) {
continue;
@ -2429,7 +2415,7 @@ public class Check {
}
//...check each method m2 that is a member of 'site'
for (Symbol m2 : types.membersClosure(site, false).getElementsByName(sym.name, cf)) {
for (Symbol m2 : types.membersClosure(site, false).getSymbolsByName(sym.name, cf)) {
if (m2 == m1) continue;
//if (i) the signature of 'sym' is not a subsignature of m1 (seen as
//a member of 'site') and (ii) m1 has the same erasure as m2, issue an error
@ -2466,7 +2452,7 @@ public class Check {
void checkHideClashes(DiagnosticPosition pos, Type site, MethodSymbol sym) {
ClashFilter cf = new ClashFilter(site);
//for each method m1 that is a member of 'site'...
for (Symbol s : types.membersClosure(site, true).getElementsByName(sym.name, cf)) {
for (Symbol s : types.membersClosure(site, true).getSymbolsByName(sym.name, cf)) {
//if (i) the signature of 'sym' is not a subsignature of m1 (seen as
//a member of 'site') and (ii) 'sym' has the same erasure as m1, issue an error
if (!types.isSubSignature(sym.type, types.memberType(site, s), allowStrictMethodClashCheck)) {
@ -2508,7 +2494,7 @@ public class Check {
void checkDefaultMethodClashes(DiagnosticPosition pos, Type site) {
DefaultMethodClashFilter dcf = new DefaultMethodClashFilter(site);
for (Symbol m : types.membersClosure(site, false).getElements(dcf)) {
for (Symbol m : types.membersClosure(site, false).getSymbols(dcf)) {
Assert.check(m.kind == MTH);
List<MethodSymbol> prov = types.interfaceCandidates(site, (MethodSymbol)m);
if (prov.size() > 1) {
@ -2772,11 +2758,11 @@ public class Check {
void validateAnnotationMethod(DiagnosticPosition pos, MethodSymbol m) {
for (Type sup = syms.annotationType; sup.hasTag(CLASS); sup = types.supertype(sup)) {
Scope s = sup.tsym.members();
for (Scope.Entry e = s.lookup(m.name); e.scope != null; e = e.next()) {
if (e.sym.kind == MTH &&
(e.sym.flags() & (PUBLIC | PROTECTED)) != 0 &&
types.overrideEquivalent(m.type, e.sym.type))
log.error(pos, "intf.annotation.member.clash", e.sym, sup);
for (Symbol sym : s.getSymbolsByName(m.name)) {
if (sym.kind == MTH &&
(sym.flags() & (PUBLIC | PROTECTED)) != 0 &&
types.overrideEquivalent(m.type, sym.type))
log.error(pos, "intf.annotation.member.clash", sym, sup);
}
}
}
@ -2856,9 +2842,9 @@ public class Check {
}
private void validateValue(TypeSymbol container, TypeSymbol contained, DiagnosticPosition pos) {
Scope.Entry e = container.members().lookup(names.value);
if (e.scope != null && e.sym.kind == MTH) {
MethodSymbol m = (MethodSymbol) e.sym;
Symbol sym = container.members().findFirst(names.value);
if (sym != null && sym.kind == MTH) {
MethodSymbol m = (MethodSymbol) sym;
Type ret = m.getReturnType();
if (!(ret.hasTag(ARRAY) && types.isSameType(((ArrayType)ret).elemtype, contained.type))) {
log.error(pos, "invalid.repeatable.annotation.value.return",
@ -3003,7 +2989,7 @@ public class Check {
private void validateDefault(Symbol container, DiagnosticPosition pos) {
// validate that all other elements of containing type has defaults
Scope scope = container.members();
for(Symbol elm : scope.getElements()) {
for(Symbol elm : scope.getSymbols()) {
if (elm.name != names.value &&
elm.kind == Kinds.MTH &&
((MethodSymbol)elm).defaultValue == null) {
@ -3025,8 +3011,8 @@ public class Check {
if (sup == owner.type)
continue; // skip "this"
Scope scope = sup.tsym.members();
for (Scope.Entry e = scope.lookup(m.name); e.scope != null; e = e.next()) {
if (!e.sym.isStatic() && m.overrides(e.sym, owner, types, true))
for (Symbol sym : scope.getSymbolsByName(m.name)) {
if (!sym.isStatic() && m.overrides(sym, owner, types, true))
return true;
}
}
@ -3160,12 +3146,10 @@ public class Check {
boolean isValid = true;
// collect an inventory of the annotation elements
Set<MethodSymbol> members = new LinkedHashSet<>();
for (Scope.Entry e = a.annotationType.type.tsym.members().elems;
e != null;
e = e.sibling)
if (e.sym.kind == MTH && e.sym.name != names.clinit &&
(e.sym.flags() & SYNTHETIC) == 0)
members.add((MethodSymbol) e.sym);
for (Symbol sym : a.annotationType.type.tsym.members().getSymbols(NON_RECURSIVE))
if (sym.kind == MTH && sym.name != names.clinit &&
(sym.flags() & SYNTHETIC) == 0)
members.add((MethodSymbol) sym);
// remove the ones that are assigned values
for (JCTree arg : a.args) {
@ -3293,8 +3277,7 @@ public class Check {
}
try {
tsym.flags_field |= LOCKED;
for (Scope.Entry e = tsym.members().elems; e != null; e = e.sibling) {
Symbol s = e.sym;
for (Symbol s : tsym.members().getSymbols(NON_RECURSIVE)) {
if (s.kind != Kinds.MTH)
continue;
checkAnnotationResType(pos, ((MethodSymbol)s).type.getReturnType());
@ -3436,23 +3419,23 @@ public class Check {
if (sym.type.isErroneous())
return true;
if (sym.owner.name == names.any) return false;
for (Scope.Entry e = s.lookup(sym.name); e.scope == s; e = e.next()) {
if (sym != e.sym &&
(e.sym.flags() & CLASH) == 0 &&
sym.kind == e.sym.kind &&
for (Symbol byName : s.getSymbolsByName(sym.name, NON_RECURSIVE)) {
if (sym != byName &&
(byName.flags() & CLASH) == 0 &&
sym.kind == byName.kind &&
sym.name != names.error &&
(sym.kind != MTH ||
types.hasSameArgs(sym.type, e.sym.type) ||
types.hasSameArgs(types.erasure(sym.type), types.erasure(e.sym.type)))) {
if ((sym.flags() & VARARGS) != (e.sym.flags() & VARARGS)) {
varargsDuplicateError(pos, sym, e.sym);
types.hasSameArgs(sym.type, byName.type) ||
types.hasSameArgs(types.erasure(sym.type), types.erasure(byName.type)))) {
if ((sym.flags() & VARARGS) != (byName.flags() & VARARGS)) {
varargsDuplicateError(pos, sym, byName);
return true;
} else if (sym.kind == MTH && !types.hasSameArgs(sym.type, e.sym.type, false)) {
duplicateErasureError(pos, sym, e.sym);
} else if (sym.kind == MTH && !types.hasSameArgs(sym.type, byName.type, false)) {
duplicateErasureError(pos, sym, byName);
sym.flags_field |= CLASH;
return true;
} else {
duplicateError(pos, e.sym);
duplicateError(pos, byName);
return false;
}
}
@ -3471,47 +3454,50 @@ public class Check {
/** Check that single-type import is not already imported or top-level defined,
* but make an exception for two single-type imports which denote the same type.
* @param pos Position for error reporting.
* @param toplevel The file in which in the check is performed.
* @param sym The symbol.
* @param s The scope
*/
boolean checkUniqueImport(DiagnosticPosition pos, Symbol sym, Scope s) {
return checkUniqueImport(pos, sym, s, false);
boolean checkUniqueImport(DiagnosticPosition pos, JCCompilationUnit toplevel, Symbol sym) {
return checkUniqueImport(pos, toplevel, sym, false);
}
/** Check that static single-type import is not already imported or top-level defined,
* but make an exception for two single-type imports which denote the same type.
* @param pos Position for error reporting.
* @param toplevel The file in which in the check is performed.
* @param sym The symbol.
* @param s The scope
*/
boolean checkUniqueStaticImport(DiagnosticPosition pos, Symbol sym, Scope s) {
return checkUniqueImport(pos, sym, s, true);
boolean checkUniqueStaticImport(DiagnosticPosition pos, JCCompilationUnit toplevel, Symbol sym) {
return checkUniqueImport(pos, toplevel, sym, true);
}
/** Check that single-type import is not already imported or top-level defined,
* but make an exception for two single-type imports which denote the same type.
* @param pos Position for error reporting.
* @param toplevel The file in which in the check is performed.
* @param sym The symbol.
* @param s The scope.
* @param staticImport Whether or not this was a static import
*/
private boolean checkUniqueImport(DiagnosticPosition pos, Symbol sym, Scope s, boolean staticImport) {
for (Scope.Entry e = s.lookup(sym.name); e.scope != null; e = e.next()) {
private boolean checkUniqueImport(DiagnosticPosition pos, JCCompilationUnit toplevel, Symbol sym, boolean staticImport) {
NamedImportScope namedImportScope = toplevel.namedImportScope;
WriteableScope topLevelScope = toplevel.toplevelScope;
for (Symbol byName : namedImportScope.getSymbolsByName(sym.name)) {
// is encountered class entered via a class declaration?
boolean isClassDecl = e.scope == s;
if ((isClassDecl || sym != e.sym) &&
sym.kind == e.sym.kind &&
boolean isClassDecl = namedImportScope.getOrigin(byName) == topLevelScope;
if ((isClassDecl || sym != byName) &&
sym.kind == byName.kind &&
sym.name != names.error &&
(!staticImport || !e.isStaticallyImported())) {
if (!e.sym.type.isErroneous()) {
(!staticImport || !namedImportScope.isStaticallyImported(byName))) {
if (!byName.type.isErroneous()) {
if (!isClassDecl) {
if (staticImport)
log.error(pos, "already.defined.static.single.import", e.sym);
log.error(pos, "already.defined.static.single.import", byName);
else
log.error(pos, "already.defined.single.import", e.sym);
log.error(pos, "already.defined.single.import", byName);
}
else if (sym != e.sym)
log.error(pos, "already.defined.this.unit", e.sym);
else if (sym != byName)
log.error(pos, "already.defined.this.unit", byName);
}
return false;
}
@ -3610,4 +3596,68 @@ public class Check {
}
}
}
public void checkImportsResolvable(final JCCompilationUnit toplevel) {
for (final JCImport imp : toplevel.getImports()) {
if (!imp.staticImport || !imp.qualid.hasTag(SELECT))
continue;
final JCFieldAccess select = (JCFieldAccess) imp.qualid;
final Symbol origin;
if (select.name == names.asterisk || (origin = TreeInfo.symbol(select.selected)) == null || origin.kind != TYP)
continue;
JavaFileObject prev = log.useSource(toplevel.sourcefile);
try {
TypeSymbol site = (TypeSymbol) TreeInfo.symbol(select.selected);
if (!checkTypeContainsImportableElement(site, site, toplevel.packge, select.name, new HashSet<Symbol>())) {
log.error(imp.pos(), "cant.resolve.location",
KindName.STATIC,
select.name, List.<Type>nil(), List.<Type>nil(),
Kinds.typeKindName(TreeInfo.symbol(select.selected).type),
TreeInfo.symbol(select.selected).type);
}
} finally {
log.useSource(prev);
}
}
}
private boolean checkTypeContainsImportableElement(TypeSymbol tsym, TypeSymbol origin, PackageSymbol packge, Name name, Set<Symbol> processed) {
if (tsym == null || !processed.add(tsym))
return false;
// also search through inherited names
if (checkTypeContainsImportableElement(types.supertype(tsym.type).tsym, origin, packge, name, processed))
return true;
for (Type t : types.interfaces(tsym.type))
if (checkTypeContainsImportableElement(t.tsym, origin, packge, name, processed))
return true;
for (Symbol sym : tsym.members().getSymbolsByName(name)) {
if (sym.isStatic() &&
staticImportAccessible(sym, packge) &&
sym.isMemberOf(origin, types)) {
return true;
}
}
return false;
}
// is the sym accessible everywhere in packge?
public boolean staticImportAccessible(Symbol sym, PackageSymbol packge) {
int flags = (int)(sym.flags() & AccessFlags);
switch (flags) {
default:
case PUBLIC:
return true;
case PRIVATE:
return false;
case 0:
case PROTECTED:
return sym.packge() == packge;
}
}
}

View File

@ -376,9 +376,8 @@ public class DeferredAttr extends JCTree.Visitor {
ResultInfo resultInfo,
Annotate.PositionCreator creator) {
final JCTree newTree = new TreeCopier<>(make).copy(tree);
Env<AttrContext> speculativeEnv = env.dup(newTree, env.info.dup(env.info.scope.dupUnshared()));
Env<AttrContext> speculativeEnv = env.dup(newTree, env.info.dup(env.info.scope.dupUnshared(env.info.scope.owner)));
speculativeEnv.info.isSpeculative = true;
speculativeEnv.info.scope.owner = env.info.scope.owner;
Log.DeferredDiagnosticHandler deferredDiagnosticHandler =
new Log.DeferredDiagnosticHandler(log, new Filter<JCDiagnostic>() {
public boolean accepts(final JCDiagnostic d) {

View File

@ -191,7 +191,7 @@ public class Enter extends JCTree.Visitor {
*/
public Env<AttrContext> classEnv(JCClassDecl tree, Env<AttrContext> env) {
Env<AttrContext> localEnv =
env.dup(tree, env.info.dup(new Scope(tree.sym)));
env.dup(tree, env.info.dup(WriteableScope.create(tree.sym)));
localEnv.enclClass = tree;
localEnv.outer = env;
localEnv.info.isSelfCall = false;
@ -207,9 +207,10 @@ public class Enter extends JCTree.Visitor {
Env<AttrContext> localEnv = new Env<>(tree, new AttrContext());
localEnv.toplevel = tree;
localEnv.enclClass = predefClassDef;
tree.namedImportScope = new ImportScope(tree.packge);
tree.toplevelScope = WriteableScope.create(tree.packge);
tree.namedImportScope = new NamedImportScope(tree.packge, tree.toplevelScope);
tree.starImportScope = new StarImportScope(tree.packge);
localEnv.info.scope = tree.namedImportScope;
localEnv.info.scope = tree.toplevelScope;
localEnv.info.lint = lint;
return localEnv;
}
@ -218,7 +219,7 @@ public class Enter extends JCTree.Visitor {
Env<AttrContext> localEnv = new Env<>(tree, new AttrContext());
localEnv.toplevel = tree;
localEnv.enclClass = predefClassDef;
localEnv.info.scope = tree.namedImportScope;
localEnv.info.scope = tree.toplevelScope;
localEnv.info.lint = lint;
return localEnv;
}
@ -228,7 +229,7 @@ public class Enter extends JCTree.Visitor {
* where the local scope is for type variables, and the this and super symbol
* only, and members go into the class member scope.
*/
Scope enterScope(Env<AttrContext> env) {
WriteableScope enterScope(Env<AttrContext> env) {
return (env.tree.hasTag(JCTree.Tag.CLASSDEF))
? ((JCClassDecl) env.tree).sym.members_field
: env.info.scope;
@ -324,7 +325,7 @@ public class Enter extends JCTree.Visitor {
c.flatname = names.fromString(tree.packge + "." + name);
c.sourcefile = tree.sourcefile;
c.completer = null;
c.members_field = new Scope(c);
c.members_field = WriteableScope.create(c);
tree.packge.package_info = c;
}
classEnter(tree.defs, topEnv);
@ -338,7 +339,7 @@ public class Enter extends JCTree.Visitor {
@Override
public void visitClassDef(JCClassDecl tree) {
Symbol owner = env.info.scope.owner;
Scope enclScope = enterScope(env);
WriteableScope enclScope = enterScope(env);
ClassSymbol c;
if (owner.kind == PCK) {
// We are seeing a toplevel class.
@ -392,7 +393,7 @@ public class Enter extends JCTree.Visitor {
c.completer = memberEnter;
c.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, c, tree);
c.sourcefile = env.toplevel.sourcefile;
c.members_field = new Scope(c);
c.members_field = WriteableScope.create(c);
ClassType ct = (ClassType)c.type;
if (owner.kind != PCK && (c.flags_field & STATIC) == 0) {
@ -495,7 +496,7 @@ public class Enter extends JCTree.Visitor {
// if there remain any unimported toplevels (these must have
// no classes at all), process their import statements as well.
for (JCCompilationUnit tree : trees) {
if (tree.starImportScope.elems == null) {
if (tree.starImportScope.isEmpty()) {
JavaFileObject prev = log.useSource(tree.sourcefile);
Env<AttrContext> topEnv = topLevelEnv(tree);
memberEnter.memberEnter(tree, topEnv);

View File

@ -30,6 +30,7 @@ package com.sun.tools.javac.comp;
import java.util.HashMap;
import com.sun.tools.javac.code.*;
import com.sun.tools.javac.code.Scope.WriteableScope;
import com.sun.tools.javac.tree.*;
import com.sun.tools.javac.util.*;
import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
@ -1419,7 +1420,7 @@ public class Flow {
/** The list of unreferenced automatic resources.
*/
Scope unrefdResources;
WriteableScope unrefdResources;
/** Set when processing a loop body the second time for DU analysis. */
FlowKind flowKind = FlowKind.NORMAL;
@ -2410,7 +2411,7 @@ public class Flow {
nextadr = 0;
pendingExits = new ListBuffer<>();
this.classDef = null;
unrefdResources = new Scope(env.enclClass.sym);
unrefdResources = WriteableScope.create(env.enclClass.sym);
scan(tree);
} finally {
// note that recursive invocations of this method fail hard

View File

@ -31,7 +31,7 @@ import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.tree.TreeTranslator;
import com.sun.tools.javac.code.Attribute;
import com.sun.tools.javac.code.Kinds;
import com.sun.tools.javac.code.Scope;
import com.sun.tools.javac.code.Scope.WriteableScope;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.code.Symbol.DynamicMethodSymbol;
@ -448,7 +448,7 @@ public class LambdaToMethod extends TreeTranslator {
make.at(prevPos);
}
// Replace the entered symbol for this variable
Scope sc = tree.sym.owner.members();
WriteableScope sc = tree.sym.owner.members();
if (sc != null) {
sc.remove(tree.sym);
sc.enter(xsym);
@ -1475,7 +1475,7 @@ public class LambdaToMethod extends TreeTranslator {
return clinit;
} else {
//get the first constructor and treat it as the instance init sym
for (Symbol s : csym.members_field.getElementsByName(names.init)) {
for (Symbol s : csym.members_field.getSymbolsByName(names.init)) {
return s;
}
}

View File

@ -28,6 +28,7 @@ package com.sun.tools.javac.comp;
import java.util.*;
import com.sun.tools.javac.code.*;
import com.sun.tools.javac.code.Scope.WriteableScope;
import com.sun.tools.javac.jvm.*;
import com.sun.tools.javac.main.Option.PkgInfo;
import com.sun.tools.javac.tree.*;
@ -45,6 +46,7 @@ import com.sun.tools.javac.tree.EndPosTable;
import static com.sun.tools.javac.code.Flags.*;
import static com.sun.tools.javac.code.Flags.BLOCK;
import static com.sun.tools.javac.code.Kinds.*;
import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
import static com.sun.tools.javac.code.TypeTag.*;
import static com.sun.tools.javac.jvm.ByteCodes.*;
import static com.sun.tools.javac.tree.JCTree.Tag.*;
@ -299,7 +301,7 @@ public class Lower extends TreeTranslator {
Symbol sym = _sym;
if (sym.kind == VAR || sym.kind == MTH) {
while (sym != null && sym.owner != owner)
sym = proxies.lookup(proxyName(sym.name)).sym;
sym = proxies.findFirst(proxyName(sym.name));
if (sym != null && sym.owner == owner) {
VarSymbol v = (VarSymbol)sym;
if (v.getConstValue() == null) {
@ -644,7 +646,7 @@ public class Lower extends TreeTranslator {
}
c.sourcefile = owner.sourcefile;
c.completer = null;
c.members_field = new Scope(c);
c.members_field = WriteableScope.create(c);
c.flags_field = flags;
ClassType ctype = (ClassType) c.type;
ctype.supertype_field = syms.objectType;
@ -678,7 +680,7 @@ public class Lower extends TreeTranslator {
* @param sym The symbol.
* @param s The scope.
*/
private void enterSynthetic(DiagnosticPosition pos, Symbol sym, Scope s) {
private void enterSynthetic(DiagnosticPosition pos, Symbol sym, WriteableScope s) {
s.enter(sym);
}
@ -746,7 +748,7 @@ public class Lower extends TreeTranslator {
* @param name The name.
*/
private Symbol lookupSynthetic(Name name, Scope s) {
Symbol sym = s.lookup(name).sym;
Symbol sym = s.findFirst(name);
return (sym==null || (sym.flags()&SYNTHETIC)==0) ? null : sym;
}
@ -901,11 +903,9 @@ public class Lower extends TreeTranslator {
/** Return binary operator that corresponds to given access code.
*/
private OperatorSymbol binaryAccessOperator(int acode) {
for (Scope.Entry e = syms.predefClass.members().elems;
e != null;
e = e.sibling) {
if (e.sym instanceof OperatorSymbol) {
OperatorSymbol op = (OperatorSymbol)e.sym;
for (Symbol sym : syms.predefClass.members().getSymbols(NON_RECURSIVE)) {
if (sym instanceof OperatorSymbol) {
OperatorSymbol op = (OperatorSymbol)sym;
if (accessCode(op.opcode) == acode) return op;
}
}
@ -1143,7 +1143,7 @@ public class Lower extends TreeTranslator {
return makeLit(sym.type, cv);
}
// Otherwise replace the variable by its proxy.
sym = proxies.lookup(proxyName(sym.name)).sym;
sym = proxies.findFirst(proxyName(sym.name));
Assert.check(sym != null && (sym.flags_field & FINAL) != 0);
tree = make.at(tree.pos).Ident(sym);
}
@ -1459,12 +1459,12 @@ public class Lower extends TreeTranslator {
* in an additional innermost scope, where they represent the constructor
* parameters.
*/
Scope proxies;
WriteableScope proxies;
/** A scope containing all unnamed resource variables/saved
* exception variables for translated TWR blocks
*/
Scope twrVars;
WriteableScope twrVars;
/** A stack containing the this$n field of the currently translated
* classes (if needed) in innermost first order.
@ -1519,7 +1519,7 @@ public class Lower extends TreeTranslator {
nestingLevel++;
}
Name result = names.fromString("this" + target.syntheticNameChar() + nestingLevel);
while (owner.kind == TYP && ((ClassSymbol)owner).members().lookup(result).scope != null)
while (owner.kind == TYP && ((ClassSymbol)owner).members().findFirst(result) != null)
result = names.fromString(result.toString() + target.syntheticNameChar());
return result;
}
@ -1859,10 +1859,10 @@ public class Lower extends TreeTranslator {
* name is the name of a free variable.
*/
JCStatement initField(int pos, Name name) {
Scope.Entry e = proxies.lookup(name);
Symbol rhs = e.sym;
Iterator<Symbol> it = proxies.getSymbolsByName(name).iterator();
Symbol rhs = it.next();
Assert.check(rhs.owner.kind == MTH);
Symbol lhs = e.next().sym;
Symbol lhs = it.next();
Assert.check(rhs.owner.owner == lhs.owner);
make.at(pos);
return
@ -1903,10 +1903,10 @@ public class Lower extends TreeTranslator {
if ((clazz.flags() & INTERFACE) == 0 &&
!target.useInnerCacheClass()) return clazz;
Scope s = clazz.members();
for (Scope.Entry e = s.elems; e != null; e = e.sibling)
if (e.sym.kind == TYP &&
e.sym.name == names.empty &&
(e.sym.flags() & INTERFACE) == 0) return (ClassSymbol) e.sym;
for (Symbol sym : s.getSymbols(NON_RECURSIVE))
if (sym.kind == TYP &&
sym.name == names.empty &&
(sym.flags() & INTERFACE) == 0) return (ClassSymbol) sym;
return makeEmptyClass(STATIC | SYNTHETIC, clazz).sym;
}
@ -2574,7 +2574,7 @@ public class Lower extends TreeTranslator {
// private static final T[] #VALUES = { a, b, c };
Name valuesName = names.fromString(target.syntheticNameChar() + "VALUES");
while (tree.sym.members().lookup(valuesName).scope != null) // avoid name clash
while (tree.sym.members().findFirst(valuesName) != null) // avoid name clash
valuesName = names.fromString(valuesName + "" + target.syntheticNameChar());
Type arrayType = new ArrayType(types.erasure(tree.type),
syms.arrayClass, Type.noAnnotations);
@ -2602,7 +2602,7 @@ public class Lower extends TreeTranslator {
} else {
// template: T[] $result = new T[$values.length];
Name resultName = names.fromString(target.syntheticNameChar() + "result");
while (tree.sym.members().lookup(resultName).scope != null) // avoid name clash
while (tree.sym.members().findFirst(resultName) != null) // avoid name clash
resultName = names.fromString(resultName + "" + target.syntheticNameChar());
VarSymbol resultVar = new VarSymbol(FINAL|SYNTHETIC,
resultName,
@ -2683,8 +2683,7 @@ public class Lower extends TreeTranslator {
private MethodSymbol systemArraycopyMethod;
private boolean useClone() {
try {
Scope.Entry e = syms.objectType.tsym.members().lookup(names.clone);
return (e.sym != null);
return syms.objectType.tsym.members().findFirst(names.clone) != null;
}
catch (CompletionFailure e) {
return false;
@ -2786,7 +2785,7 @@ public class Lower extends TreeTranslator {
final Name pName = proxyName(l.head.name);
m.capturedLocals =
m.capturedLocals.append((VarSymbol)
(proxies.lookup(pName).sym));
(proxies.findFirst(pName)));
added = added.prepend(
initField(tree.body.pos, pName));
}
@ -3969,8 +3968,8 @@ public class Lower extends TreeTranslator {
classdefs = new HashMap<>();
actualSymbols = new HashMap<>();
freevarCache = new HashMap<>();
proxies = new Scope(syms.noSymbol);
twrVars = new Scope(syms.noSymbol);
proxies = WriteableScope.create(syms.noSymbol);
twrVars = WriteableScope.create(syms.noSymbol);
outerThisStack = List.nil();
accessNums = new HashMap<>();
accessSyms = new HashMap<>();

View File

@ -25,12 +25,18 @@
package com.sun.tools.javac.comp;
import java.util.Collections;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Set;
import javax.tools.JavaFileObject;
import com.sun.tools.javac.code.*;
import com.sun.tools.javac.code.Scope.ImportFilter;
import com.sun.tools.javac.code.Scope.NamedImportScope;
import com.sun.tools.javac.code.Scope.StarImportScope;
import com.sun.tools.javac.code.Scope.WriteableScope;
import com.sun.tools.javac.jvm.*;
import com.sun.tools.javac.tree.*;
import com.sun.tools.javac.util.*;
@ -43,6 +49,7 @@ import com.sun.tools.javac.tree.JCTree.*;
import static com.sun.tools.javac.code.Flags.*;
import static com.sun.tools.javac.code.Flags.ANNOTATION;
import static com.sun.tools.javac.code.Kinds.*;
import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
import static com.sun.tools.javac.code.TypeTag.CLASS;
import static com.sun.tools.javac.code.TypeTag.ERROR;
import static com.sun.tools.javac.code.TypeTag.TYPEVAR;
@ -151,7 +158,7 @@ public class MemberEnter extends JCTree.Visitor implements Completer {
final TypeSymbol tsym,
Env<AttrContext> env) {
// Check that packages imported from exist (JLS ???).
if (tsym.kind == PCK && tsym.members().elems == null && !tsym.exists()) {
if (tsym.kind == PCK && tsym.members().isEmpty() && !tsym.exists()) {
// If we can't find java.lang, exit immediately.
if (((PackageSymbol)tsym).fullname.equals(names.java_lang)) {
JCDiagnostic msg = diags.fragment("fatal.err.no.java.lang");
@ -160,7 +167,7 @@ public class MemberEnter extends JCTree.Visitor implements Completer {
log.error(DiagnosticFlag.RESOLVE_ERROR, pos, "doesnt.exist", tsym);
}
}
env.toplevel.starImportScope.importAll(tsym.members());
env.toplevel.starImportScope.importAll(tsym.members(), tsym.members(), typeImportFilter, false);
}
/** Import all static members of a class or package on demand.
@ -171,82 +178,16 @@ public class MemberEnter extends JCTree.Visitor implements Completer {
private void importStaticAll(int pos,
final TypeSymbol tsym,
Env<AttrContext> env) {
final JavaFileObject sourcefile = env.toplevel.sourcefile;
final Scope toScope = env.toplevel.starImportScope;
final StarImportScope toScope = env.toplevel.starImportScope;
final PackageSymbol packge = env.toplevel.packge;
final TypeSymbol origin = tsym;
// enter imported types immediately
new Object() {
Set<Symbol> processed = new HashSet<>();
void importFrom(TypeSymbol tsym) {
if (tsym == null || !processed.add(tsym))
return;
// also import inherited names
importFrom(types.supertype(tsym.type).tsym);
for (Type t : types.interfaces(tsym.type))
importFrom(t.tsym);
final Scope fromScope = tsym.members();
for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) {
Symbol sym = e.sym;
if (sym.kind == TYP &&
(sym.flags() & STATIC) != 0 &&
staticImportAccessible(sym, packge) &&
sym.isMemberOf(origin, types) &&
!toScope.includes(sym))
toScope.enter(sym, fromScope, origin.members(), true);
}
new SymbolImporter() {
void doImport(TypeSymbol tsym) {
toScope.importAll(tsym.members(), origin.members(), staticImportFilter, true);
}
}.importFrom(tsym);
// enter non-types before annotations that might use them
annotate.earlier(new Annotate.Worker() {
Set<Symbol> processed = new HashSet<>();
public String toString() {
return "import static " + tsym + ".*" + " in " + sourcefile;
}
void importFrom(TypeSymbol tsym) {
if (tsym == null || !processed.add(tsym))
return;
// also import inherited names
importFrom(types.supertype(tsym.type).tsym);
for (Type t : types.interfaces(tsym.type))
importFrom(t.tsym);
final Scope fromScope = tsym.members();
for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) {
Symbol sym = e.sym;
if (sym.isStatic() && sym.kind != TYP &&
staticImportAccessible(sym, packge) &&
!toScope.includes(sym) &&
sym.isMemberOf(origin, types)) {
toScope.enter(sym, fromScope, origin.members(), true);
}
}
}
public void run() {
importFrom(tsym);
}
});
}
// is the sym accessible everywhere in packge?
boolean staticImportAccessible(Symbol sym, PackageSymbol packge) {
int flags = (int)(sym.flags() & AccessFlags);
switch (flags) {
default:
case PUBLIC:
return true;
case PRIVATE:
return false;
case 0:
case PROTECTED:
return sym.packge() == packge;
}
}
/** Import statics types of a given name. Non-types are handled in Attr.
@ -265,44 +206,47 @@ public class MemberEnter extends JCTree.Visitor implements Completer {
return;
}
final Scope toScope = env.toplevel.namedImportScope;
final PackageSymbol packge = env.toplevel.packge;
final TypeSymbol origin = tsym;
final NamedImportScope toScope = env.toplevel.namedImportScope;
final Scope originMembers = tsym.members();
// enter imported types immediately
new Object() {
Set<Symbol> processed = new HashSet<>();
void importFrom(TypeSymbol tsym) {
if (tsym == null || !processed.add(tsym))
return;
// also import inherited names
importFrom(types.supertype(tsym.type).tsym);
for (Type t : types.interfaces(tsym.type))
importFrom(t.tsym);
for (Scope.Entry e = tsym.members().lookup(name);
e.scope != null;
e = e.next()) {
Symbol sym = e.sym;
if (sym.isStatic() &&
sym.kind == TYP &&
staticImportAccessible(sym, packge) &&
sym.isMemberOf(origin, types) &&
chk.checkUniqueStaticImport(pos, sym, toScope))
toScope.enter(sym, sym.owner.members(), origin.members(), true);
new SymbolImporter() {
void doImport(TypeSymbol tsym) {
Set<Symbol> maskedOut = null;
for (Symbol sym : tsym.members().getSymbolsByName(name)) {
if (sym.kind == TYP &&
staticImportFilter.accepts(originMembers, sym) &&
!chk.checkUniqueStaticImport(pos, env.toplevel, sym)) {
if (maskedOut == null)
maskedOut = Collections.newSetFromMap(new IdentityHashMap<Symbol, Boolean>());
maskedOut.add(sym);
}
}
ImportFilter importFilter = maskedOut != null ?
new MaskedImportFilter(staticImportFilter, maskedOut) :
staticImportFilter;
toScope.importByName(tsym.members(), originMembers, name, importFilter);
}
}.importFrom(tsym);
}
//where:
class MaskedImportFilter implements ImportFilter {
// enter non-types before annotations that might use them
annotate.earlier(new Annotate.Worker() {
Set<Symbol> processed = new HashSet<>();
boolean found = false;
private final ImportFilter delegate;
private final Set<Symbol> maskedOut;
public String toString() {
return "import static " + tsym + "." + name;
public MaskedImportFilter(ImportFilter delegate, Set<Symbol> maskedOut) {
this.delegate = delegate;
this.maskedOut = maskedOut;
}
@Override
public boolean accepts(Scope origin, Symbol sym) {
return !maskedOut.contains(sym) && delegate.accepts(origin, sym);
}
}
abstract class SymbolImporter {
Set<Symbol> processed = new HashSet<>();
void importFrom(TypeSymbol tsym) {
if (tsym == null || !processed.add(tsym))
return;
@ -312,47 +256,21 @@ public class MemberEnter extends JCTree.Visitor implements Completer {
for (Type t : types.interfaces(tsym.type))
importFrom(t.tsym);
for (Scope.Entry e = tsym.members().lookup(name);
e.scope != null;
e = e.next()) {
Symbol sym = e.sym;
if (sym.isStatic() &&
staticImportAccessible(sym, packge) &&
sym.isMemberOf(origin, types)) {
found = true;
if (sym.kind != TYP) {
toScope.enter(sym, sym.owner.members(), origin.members(), true);
}
}
}
doImport(tsym);
}
public void run() {
JavaFileObject prev = log.useSource(env.toplevel.sourcefile);
try {
importFrom(tsym);
if (!found) {
log.error(pos, "cant.resolve.location",
KindName.STATIC,
name, List.<Type>nil(), List.<Type>nil(),
Kinds.typeKindName(tsym.type),
tsym.type);
}
} finally {
log.useSource(prev);
}
}
});
}
abstract void doImport(TypeSymbol tsym);
}
/** Import given class.
* @param pos Position to be used for error reporting.
* @param tsym The class to be imported.
* @param env The environment containing the named import
* scope to add to.
*/
private void importNamed(DiagnosticPosition pos, Symbol tsym, Env<AttrContext> env) {
private void importNamed(DiagnosticPosition pos, final Symbol tsym, Env<AttrContext> env) {
if (tsym.kind == TYP &&
chk.checkUniqueImport(pos, tsym, env.toplevel.namedImportScope))
env.toplevel.namedImportScope.enter(tsym, tsym.owner.members());
chk.checkUniqueImport(pos, env.toplevel, tsym))
env.toplevel.namedImportScope.importType(tsym.owner.members(), tsym.owner.members(), tsym);
}
/** Construct method type from method signature.
@ -482,6 +400,32 @@ public class MemberEnter extends JCTree.Visitor implements Completer {
* Visitor methods for member enter
*********************************************************************/
ImportFilter staticImportFilter;
ImportFilter typeImportFilter = new ImportFilter() {
@Override
public boolean accepts(Scope origin, Symbol t) {
return t.kind == Kinds.TYP;
}
};
protected void memberEnter(JCCompilationUnit tree, Env<AttrContext> env) {
ImportFilter prevStaticImportFilter = staticImportFilter;
try {
final PackageSymbol packge = env.toplevel.packge;
this.staticImportFilter = new ImportFilter() {
@Override
public boolean accepts(Scope origin, Symbol sym) {
return sym.isStatic() &&
chk.staticImportAccessible(sym, packge) &&
sym.isMemberOf((TypeSymbol) origin.owner, types);
}
};
memberEnter((JCTree) tree, env);
} finally {
this.staticImportFilter = prevStaticImportFilter;
}
}
/** Visitor argument: the current environment
*/
protected Env<AttrContext> env;
@ -570,7 +514,7 @@ public class MemberEnter extends JCTree.Visitor implements Completer {
}
public void visitTopLevel(JCCompilationUnit tree) {
if (tree.starImportScope.elems != null) {
if (!tree.starImportScope.isEmpty()) {
// we must have already processed this toplevel
return;
}
@ -640,7 +584,7 @@ public class MemberEnter extends JCTree.Visitor implements Completer {
}
public void visitMethodDef(JCMethodDecl tree) {
Scope enclScope = enter.enterScope(env);
WriteableScope enclScope = enter.enterScope(env);
MethodSymbol m = new MethodSymbol(0, tree.name, null, enclScope.owner);
m.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, m, tree);
tree.sym = m;
@ -696,9 +640,8 @@ public class MemberEnter extends JCTree.Visitor implements Completer {
*/
Env<AttrContext> methodEnv(JCMethodDecl tree, Env<AttrContext> env) {
Env<AttrContext> localEnv =
env.dup(tree, env.info.dup(env.info.scope.dupUnshared()));
env.dup(tree, env.info.dup(env.info.scope.dupUnshared(tree.sym)));
localEnv.enclMethod = tree;
localEnv.info.scope.owner = tree.sym;
if (tree.sym.type != null) {
//when this is called in the enter stage, there's no type to be set
localEnv.info.returnResult = attr.new ResultInfo(VAL, tree.sym.type.getReturnType());
@ -739,7 +682,7 @@ public class MemberEnter extends JCTree.Visitor implements Completer {
ArrayType atype = (ArrayType)tree.vartype.type;
tree.vartype.type = atype.makeVarargs();
}
Scope enclScope = enter.enterScope(env);
WriteableScope enclScope = enter.enterScope(env);
VarSymbol v =
new VarSymbol(0, tree.name, tree.vartype.type, enclScope.owner);
v.flags_field = chk.checkFlags(tree.pos(), tree.mods.flags, v, tree);
@ -875,8 +818,7 @@ public class MemberEnter extends JCTree.Visitor implements Completer {
Env<AttrContext> initEnv(JCVariableDecl tree, Env<AttrContext> env) {
Env<AttrContext> localEnv = env.dupto(new AttrContextEnv(tree, env.info.dup()));
if (tree.sym.owner.kind == TYP) {
localEnv.info.scope = env.info.scope.dupUnshared();
localEnv.info.scope.owner = tree.sym;
localEnv.info.scope = env.info.scope.dupUnshared(tree.sym);
}
if ((tree.mods.flags & STATIC) != 0 ||
((env.enclClass.sym.flags() & INTERFACE) != 0 && env.enclMethod == null))
@ -1220,23 +1162,30 @@ public class MemberEnter extends JCTree.Visitor implements Completer {
// Enter all member fields and methods of a set of half completed
// classes in a second phase.
if (wasFirst) {
Set<JCCompilationUnit> topLevels = new HashSet<>();
try {
while (halfcompleted.nonEmpty()) {
Env<AttrContext> toFinish = halfcompleted.next();
topLevels.add(toFinish.toplevel);
finish(toFinish);
}
} finally {
isFirst = true;
}
for (JCCompilationUnit toplevel : topLevels) {
chk.checkImportsResolvable(toplevel);
}
}
}
private Env<AttrContext> baseEnv(JCClassDecl tree, Env<AttrContext> env) {
Scope baseScope = new Scope(tree.sym);
WriteableScope baseScope = WriteableScope.create(tree.sym);
//import already entered local classes into base scope
for (Scope.Entry e = env.outer.info.scope.elems ; e != null ; e = e.sibling) {
if (e.sym.isLocal()) {
baseScope.enter(e.sym);
for (Symbol sym : env.outer.info.scope.getSymbols(NON_RECURSIVE)) {
if (sym.isLocal()) {
baseScope.enter(sym);
}
}
//import current type-parameters into base scope

View File

@ -28,6 +28,7 @@ package com.sun.tools.javac.comp;
import com.sun.source.tree.MemberReferenceTree.ReferenceMode;
import com.sun.tools.javac.api.Formattable.LocalizedString;
import com.sun.tools.javac.code.*;
import com.sun.tools.javac.code.Scope.WriteableScope;
import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.code.Type.*;
import com.sun.tools.javac.comp.Attr.ResultInfo;
@ -100,7 +101,7 @@ public class Resolve {
private final boolean compactMethodDiags;
final EnumSet<VerboseResolutionMode> verboseResolutionMode;
Scope polymorphicSignatureScope;
WriteableScope polymorphicSignatureScope;
protected Resolve(Context context) {
context.put(resolveKey, this);
@ -139,7 +140,7 @@ public class Resolve {
allowFunctionalInterfaceMostSpecific = source.allowFunctionalInterfaceMostSpecific();
checkVarargsAccessAfterResolution =
source.allowPostApplicabilityVarargsAccessCheck();
polymorphicSignatureScope = new Scope(syms.noSymbol);
polymorphicSignatureScope = WriteableScope.create(syms.noSymbol);
inapplicableMethodException = new InapplicableMethodException(diags);
}
@ -1299,13 +1300,11 @@ public class Resolve {
c = c.type.getUpperBound().tsym;
Symbol bestSoFar = varNotFound;
Symbol sym;
Scope.Entry e = c.members().lookup(name);
while (e.scope != null) {
if (e.sym.kind == VAR && (e.sym.flags_field & SYNTHETIC) == 0) {
return isAccessible(env, site, e.sym)
? e.sym : new AccessError(env, site, e.sym);
for (Symbol s : c.members().getSymbolsByName(name)) {
if (s.kind == VAR && (s.flags_field & SYNTHETIC) == 0) {
return isAccessible(env, site, s)
? s : new AccessError(env, site, s);
}
e = e.next();
}
Type st = types.supertype(c.type);
if (st != null && (st.hasTag(CLASS) || st.hasTag(TYPEVAR))) {
@ -1348,20 +1347,20 @@ public class Resolve {
*/
Symbol findVar(Env<AttrContext> env, Name name) {
Symbol bestSoFar = varNotFound;
Symbol sym;
Env<AttrContext> env1 = env;
boolean staticOnly = false;
while (env1.outer != null) {
Symbol sym = null;
if (isStatic(env1)) staticOnly = true;
Scope.Entry e = env1.info.scope.lookup(name);
while (e.scope != null &&
(e.sym.kind != VAR ||
(e.sym.flags_field & SYNTHETIC) != 0))
e = e.next();
sym = (e.scope != null)
? e.sym
: findField(
env1, env1.enclClass.sym.type, name, env1.enclClass.sym);
for (Symbol s : env1.info.scope.getSymbolsByName(name)) {
if (s.kind == VAR && (s.flags_field & SYNTHETIC) == 0) {
sym = s;
break;
}
}
if (sym == null) {
sym = findField(env1, env1.enclClass.sym.type, name, env1.enclClass.sym);
}
if (sym.exists()) {
if (staticOnly &&
sym.kind == VAR &&
@ -1378,7 +1377,7 @@ public class Resolve {
env1 = env1.outer;
}
sym = findField(env, syms.predefClass.type, name, syms.predefClass);
Symbol sym = findField(env, syms.predefClass.type, name, syms.predefClass);
if (sym.exists())
return sym;
if (bestSoFar.exists())
@ -1386,18 +1385,16 @@ public class Resolve {
Symbol origin = null;
for (Scope sc : new Scope[] { env.toplevel.namedImportScope, env.toplevel.starImportScope }) {
Scope.Entry e = sc.lookup(name);
for (; e.scope != null; e = e.next()) {
sym = e.sym;
if (sym.kind != VAR)
for (Symbol currentSymbol : sc.getSymbolsByName(name)) {
if (currentSymbol.kind != VAR)
continue;
// invariant: sym.kind == VAR
if (bestSoFar.kind < AMBIGUOUS && sym.owner != bestSoFar.owner)
return new AmbiguityError(bestSoFar, sym);
if (bestSoFar.kind < AMBIGUOUS && currentSymbol.owner != bestSoFar.owner)
return new AmbiguityError(bestSoFar, currentSymbol);
else if (bestSoFar.kind >= VAR) {
origin = e.getOrigin().owner;
bestSoFar = isAccessible(env, origin.type, sym)
? sym : new AccessError(env, origin.type, sym);
origin = sc.getOrigin(currentSymbol).owner;
bestSoFar = isAccessible(env, origin.type, currentSymbol)
? currentSymbol : new AccessError(env, origin.type, currentSymbol);
}
}
if (bestSoFar.exists()) break;
@ -1627,7 +1624,7 @@ public class Resolve {
boolean useVarargs,
boolean operator,
boolean abstractok) {
for (Symbol s : sc.getElementsByName(name, new LookupFilter(abstractok))) {
for (Symbol s : sc.getSymbolsByName(name, new LookupFilter(abstractok))) {
bestSoFar = selectBest(env, site, argtypes, typeargtypes, s,
bestSoFar, allowBoxing, useVarargs, operator);
}
@ -1827,12 +1824,11 @@ public class Resolve {
List<Type> argtypes, List<Type> typeargtypes,
boolean allowBoxing, boolean useVarargs) {
Symbol bestSoFar = methodNotFound;
Symbol sym;
Env<AttrContext> env1 = env;
boolean staticOnly = false;
while (env1.outer != null) {
if (isStatic(env1)) staticOnly = true;
sym = findMethod(
Symbol sym = findMethod(
env1, env1.enclClass.sym.type, name, argtypes, typeargtypes,
allowBoxing, useVarargs, false);
if (sym.exists()) {
@ -1848,41 +1844,37 @@ public class Resolve {
env1 = env1.outer;
}
sym = findMethod(env, syms.predefClass.type, name, argtypes,
typeargtypes, allowBoxing, useVarargs, false);
Symbol sym = findMethod(env, syms.predefClass.type, name, argtypes,
typeargtypes, allowBoxing, useVarargs, false);
if (sym.exists())
return sym;
Scope.Entry e = env.toplevel.namedImportScope.lookup(name);
for (; e.scope != null; e = e.next()) {
sym = e.sym;
Type origin = e.getOrigin().owner.type;
if (sym.kind == MTH) {
if (e.sym.owner.type != origin)
sym = sym.clone(e.getOrigin().owner);
if (!isAccessible(env, origin, sym))
sym = new AccessError(env, origin, sym);
bestSoFar = selectBest(env, origin,
for (Symbol currentSym : env.toplevel.namedImportScope.getSymbolsByName(name)) {
Symbol origin = env.toplevel.namedImportScope.getOrigin(currentSym).owner;
if (currentSym.kind == MTH) {
if (currentSym.owner.type != origin.type)
currentSym = currentSym.clone(origin);
if (!isAccessible(env, origin.type, currentSym))
currentSym = new AccessError(env, origin.type, currentSym);
bestSoFar = selectBest(env, origin.type,
argtypes, typeargtypes,
sym, bestSoFar,
currentSym, bestSoFar,
allowBoxing, useVarargs, false);
}
}
if (bestSoFar.exists())
return bestSoFar;
e = env.toplevel.starImportScope.lookup(name);
for (; e.scope != null; e = e.next()) {
sym = e.sym;
Type origin = e.getOrigin().owner.type;
if (sym.kind == MTH) {
if (e.sym.owner.type != origin)
sym = sym.clone(e.getOrigin().owner);
if (!isAccessible(env, origin, sym))
sym = new AccessError(env, origin, sym);
bestSoFar = selectBest(env, origin,
for (Symbol currentSym : env.toplevel.starImportScope.getSymbolsByName(name)) {
Symbol origin = env.toplevel.starImportScope.getOrigin(currentSym).owner;
if (currentSym.kind == MTH) {
if (currentSym.owner.type != origin.type)
currentSym = currentSym.clone(origin);
if (!isAccessible(env, origin.type, currentSym))
currentSym = new AccessError(env, origin.type, currentSym);
bestSoFar = selectBest(env, origin.type,
argtypes, typeargtypes,
sym, bestSoFar,
currentSym, bestSoFar,
allowBoxing, useVarargs, false);
}
}
@ -1921,14 +1913,12 @@ public class Resolve {
Type site,
Name name,
TypeSymbol c) {
Scope.Entry e = c.members().lookup(name);
while (e.scope != null) {
if (e.sym.kind == TYP) {
return isAccessible(env, site, e.sym)
? e.sym
: new AccessError(env, site, e.sym);
for (Symbol sym : c.members().getSymbolsByName(name)) {
if (sym.kind == TYP) {
return isAccessible(env, site, sym)
? sym
: new AccessError(env, site, sym);
}
e = e.next();
}
return typeNotFound;
}
@ -1995,8 +1985,8 @@ public class Resolve {
*/
Symbol findGlobalType(Env<AttrContext> env, Scope scope, Name name) {
Symbol bestSoFar = typeNotFound;
for (Scope.Entry e = scope.lookup(name); e.scope != null; e = e.next()) {
Symbol sym = loadClass(env, e.sym.flatName());
for (Symbol s : scope.getSymbolsByName(name)) {
Symbol sym = loadClass(env, s.flatName());
if (bestSoFar.kind == TYP && sym.kind == TYP &&
bestSoFar != sym)
return new AmbiguityError(bestSoFar, sym);
@ -2007,15 +1997,13 @@ public class Resolve {
}
Symbol findTypeVar(Env<AttrContext> env, Name name, boolean staticOnly) {
for (Scope.Entry e = env.info.scope.lookup(name);
e.scope != null;
e = e.next()) {
if (e.sym.kind == TYP) {
for (Symbol sym : env.info.scope.getSymbolsByName(name)) {
if (sym.kind == TYP) {
if (staticOnly &&
e.sym.type.hasTag(TYPEVAR) &&
e.sym.owner.kind == TYP)
return new StaticError(e.sym);
return e.sym;
sym.type.hasTag(TYPEVAR) &&
sym.owner.kind == TYP)
return new StaticError(sym);
return sym;
}
}
return typeNotFound;
@ -2322,42 +2310,6 @@ public class Resolve {
kindName(sym), sym, sym.location());
}
/* ***************************************************************************
* Debugging
****************************************************************************/
/** print all scopes starting with scope s and proceeding outwards.
* used for debugging.
*/
public void printscopes(Scope s) {
while (s != null) {
if (s.owner != null)
System.err.print(s.owner + ": ");
for (Scope.Entry e = s.elems; e != null; e = e.sibling) {
if ((e.sym.flags() & ABSTRACT) != 0)
System.err.print("abstract ");
System.err.print(e.sym + " ");
}
System.err.println();
s = s.next;
}
}
void printscopes(Env<AttrContext> env) {
while (env.outer != null) {
System.err.println("------------------------------");
printscopes(env.info.scope);
env = env.outer;
}
}
public void printscopes(Type t) {
while (t.hasTag(CLASS)) {
printscopes(t.tsym.members());
t = types.supertype(t);
}
}
/* ***************************************************************************
* Name resolution
* Naming conventions are as for symbol lookup
@ -2456,7 +2408,7 @@ public class Resolve {
List<Type> argtypes) {
Type mtype = infer.instantiatePolymorphicSignatureInstance(env,
(MethodSymbol)spMethod, currentResolutionContext, argtypes);
for (Symbol sym : polymorphicSignatureScope.getElementsByName(spMethod.name)) {
for (Symbol sym : polymorphicSignatureScope.getSymbolsByName(spMethod.name)) {
if (types.isSameType(mtype, sym.type)) {
return sym;
}
@ -2629,14 +2581,11 @@ public class Resolve {
boolean allowBoxing,
boolean useVarargs) {
Symbol bestSoFar = methodNotFound;
for (Scope.Entry e = site.tsym.members().lookup(names.init);
e.scope != null;
e = e.next()) {
final Symbol sym = e.sym;
for (final Symbol sym : site.tsym.members().getSymbolsByName(names.init)) {
//- System.out.println(" e " + e.sym);
if (sym.kind == MTH &&
(sym.flags_field & SYNTHETIC) == 0) {
List<Type> oldParams = e.sym.type.hasTag(FORALL) ?
List<Type> oldParams = sym.type.hasTag(FORALL) ?
((ForAll)sym.type).tvars :
List.<Type>nil();
Type constrType = new ForAll(site.tsym.type.getTypeArguments().appendList(oldParams),
@ -3252,7 +3201,7 @@ public class Resolve {
@Override
protected Symbol lookup(Env<AttrContext> env, MethodResolutionPhase phase) {
Scope sc = new Scope(syms.arrayClass);
WriteableScope sc = WriteableScope.create(syms.arrayClass);
MethodSymbol arrayConstr = new MethodSymbol(PUBLIC, name, null, site.tsym);
arrayConstr.type = new MethodType(List.<Type>of(syms.intType), site, List.<Type>nil(), syms.methodClass);
sc.enter(arrayConstr);
@ -3362,7 +3311,7 @@ public class Resolve {
while (env1.outer != null) {
if (isStatic(env1)) staticOnly = true;
if (env1.enclClass.sym == c) {
Symbol sym = env1.info.scope.lookup(name).sym;
Symbol sym = env1.info.scope.findFirst(name);
if (sym != null) {
if (staticOnly) sym = new StaticError(sym);
return accessBase(sym, pos, env.enclClass.sym.type,
@ -3449,7 +3398,7 @@ public class Resolve {
while (env1 != null && env1.outer != null) {
if (isStatic(env1)) staticOnly = true;
if (env1.enclClass.sym.isSubClass(member.owner, types)) {
Symbol sym = env1.info.scope.lookup(name).sym;
Symbol sym = env1.info.scope.findFirst(name);
if (sym != null) {
if (staticOnly) sym = new StaticError(sym);
return sym;

View File

@ -37,6 +37,7 @@ import com.sun.tools.javac.util.List;
import static com.sun.tools.javac.code.Flags.*;
import static com.sun.tools.javac.code.Kinds.*;
import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
import static com.sun.tools.javac.code.TypeTag.CLASS;
import static com.sun.tools.javac.code.TypeTag.TYPEVAR;
import static com.sun.tools.javac.code.TypeTag.VOID;
@ -449,8 +450,8 @@ public class TransTypes extends TreeTranslator {
TypeSymbol i,
ClassSymbol origin,
ListBuffer<JCTree> bridges) {
for (Scope.Entry e = i.members().elems; e != null; e = e.sibling)
addBridgeIfNeeded(pos, e.sym, origin, bridges);
for (Symbol sym : i.members().getSymbols(NON_RECURSIVE))
addBridgeIfNeeded(pos, sym, origin, bridges);
for (List<Type> l = types.interfaces(i.type); l.nonEmpty(); l = l.tail)
addBridges(pos, l.head.tsym, origin, bridges);
}
@ -529,14 +530,12 @@ public class TransTypes extends TreeTranslator {
}
// Check that we do not introduce a name clash by erasing types.
for (Scope.Entry e = tree.sym.owner.members().lookup(tree.name);
e.sym != null;
e = e.next()) {
if (e.sym != tree.sym &&
types.isSameType(erasure(e.sym.type), tree.type)) {
for (Symbol sym : tree.sym.owner.members().getSymbolsByName(tree.name)) {
if (sym != tree.sym &&
types.isSameType(erasure(sym.type), tree.type)) {
log.error(tree.pos(),
"name.clash.same.erasure", tree.sym,
e.sym);
sym);
return;
}
}

View File

@ -42,6 +42,7 @@ import com.sun.tools.javac.comp.Annotate;
import com.sun.tools.javac.code.*;
import com.sun.tools.javac.code.Lint.LintCategory;
import com.sun.tools.javac.code.Type.*;
import com.sun.tools.javac.code.Scope.WriteableScope;
import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.code.Symtab;
import com.sun.tools.javac.file.BaseFileObject;
@ -141,7 +142,7 @@ public class ClassReader {
/** The current scope where type variables are entered.
*/
protected Scope typevars;
protected WriteableScope typevars;
/** The path name of the class file currently being read.
*/
@ -231,7 +232,7 @@ public class ClassReader {
profile = Profile.instance(context);
typevars = new Scope(syms.noSymbol);
typevars = WriteableScope.create(syms.noSymbol);
lintClassfile = Lint.instance(context).isEnabled(LintCategory.CLASSFILE);
@ -832,9 +833,9 @@ public class ClassReader {
/** Find type variable with given name in `typevars' scope.
*/
Type findTypeVar(Name name) {
Scope.Entry e = typevars.lookup(name);
if (e.scope != null) {
return e.sym.type;
Symbol s = typevars.findFirst(name);
if (s != null) {
return s.type;
} else {
if (readingClassAttr) {
// While reading the class attribute, the supertypes
@ -1228,9 +1229,10 @@ public class ClassReader {
MethodType type = nt.uniqueType.type.asMethodType();
for (Scope.Entry e = scope.lookup(nt.name); e.scope != null; e = e.next())
if (e.sym.kind == MTH && isSameBinaryType(e.sym.type.asMethodType(), type))
return (MethodSymbol)e.sym;
for (Symbol sym : scope.getSymbolsByName(nt.name)) {
if (sym.kind == MTH && isSameBinaryType(sym.type.asMethodType(), type))
return (MethodSymbol)sym;
}
if (nt.name != names.init)
// not a constructor
@ -1769,10 +1771,7 @@ public class ClassReader {
MethodSymbol findAccessMethod(Type container, Name name) {
CompletionFailure failure = null;
try {
for (Scope.Entry e = container.tsym.members().lookup(name);
e.scope != null;
e = e.next()) {
Symbol sym = e.sym;
for (Symbol sym : container.tsym.members().getSymbolsByName(name)) {
if (sym.kind == MTH && sym.type.getParameterTypes().length() == 0)
return (MethodSymbol) sym;
}
@ -1852,11 +1851,9 @@ public class ClassReader {
VarSymbol enumerator = null;
CompletionFailure failure = null;
try {
for (Scope.Entry e = enumTypeSym.members().lookup(proxy.enumerator);
e.scope != null;
e = e.next()) {
if (e.sym.kind == VAR) {
enumerator = (VarSymbol)e.sym;
for (Symbol sym : enumTypeSym.members().getSymbolsByName(proxy.enumerator)) {
if (sym.kind == VAR) {
enumerator = (VarSymbol)sym;
break;
}
}
@ -2197,7 +2194,7 @@ public class ClassReader {
ClassType ct = (ClassType)c.type;
// allocate scope for members
c.members_field = new Scope(c);
c.members_field = WriteableScope.create(c);
// prepare type variable table
typevars = typevars.dup(currentOwner);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -49,6 +49,7 @@ import com.sun.tools.javac.util.*;
import static com.sun.tools.javac.code.Flags.*;
import static com.sun.tools.javac.code.Kinds.*;
import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
import static com.sun.tools.javac.code.TypeTag.*;
import static com.sun.tools.javac.jvm.UninitializedType.*;
import static com.sun.tools.javac.main.Option.*;
@ -1563,12 +1564,12 @@ public class ClassWriter extends ClassFile {
}
}
void writeFields(Scope.Entry e) {
void writeFields(Scope s) {
// process them in reverse sibling order;
// i.e., process them in declaration order.
List<VarSymbol> vars = List.nil();
for (Scope.Entry i = e; i != null; i = i.sibling) {
if (i.sym.kind == VAR) vars = vars.prepend((VarSymbol)i.sym);
for (Symbol sym : s.getSymbols(NON_RECURSIVE)) {
if (sym.kind == VAR) vars = vars.prepend((VarSymbol)sym);
}
while (vars.nonEmpty()) {
writeField(vars.head);
@ -1576,11 +1577,11 @@ public class ClassWriter extends ClassFile {
}
}
void writeMethods(Scope.Entry e) {
void writeMethods(Scope s) {
List<MethodSymbol> methods = List.nil();
for (Scope.Entry i = e; i != null; i = i.sibling) {
if (i.sym.kind == MTH && (i.sym.flags() & HYPOTHETICAL) == 0)
methods = methods.prepend((MethodSymbol)i.sym);
for (Symbol sym : s.getSymbols(NON_RECURSIVE)) {
if (sym.kind == MTH && (sym.flags() & HYPOTHETICAL) == 0)
methods = methods.prepend((MethodSymbol)sym);
}
while (methods.nonEmpty()) {
writeMethod(methods.head);
@ -1654,12 +1655,12 @@ public class ClassWriter extends ClassFile {
databuf.appendChar(pool.put(l.head.tsym));
int fieldsCount = 0;
int methodsCount = 0;
for (Scope.Entry e = c.members().elems; e != null; e = e.sibling) {
switch (e.sym.kind) {
for (Symbol sym : c.members().getSymbols(NON_RECURSIVE)) {
switch (sym.kind) {
case VAR: fieldsCount++; break;
case MTH: if ((e.sym.flags() & HYPOTHETICAL) == 0) methodsCount++;
case MTH: if ((sym.flags() & HYPOTHETICAL) == 0) methodsCount++;
break;
case TYP: enterInner((ClassSymbol)e.sym); break;
case TYP: enterInner((ClassSymbol)sym); break;
default : Assert.error();
}
}
@ -1671,9 +1672,9 @@ public class ClassWriter extends ClassFile {
}
databuf.appendChar(fieldsCount);
writeFields(c.members().elems);
writeFields(c.members());
databuf.appendChar(methodsCount);
writeMethods(c.members().elems);
writeMethods(c.members());
int acountIdx = beginAttrs();
int acount = 0;

View File

@ -45,6 +45,7 @@ import com.sun.tools.javac.tree.JCTree.*;
import static com.sun.tools.javac.code.Flags.*;
import static com.sun.tools.javac.code.Kinds.*;
import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
import static com.sun.tools.javac.code.TypeTag.*;
import static com.sun.tools.javac.jvm.ByteCodes.*;
import static com.sun.tools.javac.jvm.CRTFlags.*;
@ -302,7 +303,7 @@ public class Gen extends JCTree.Visitor {
if (!target.interfaceObjectOverridesBinaryCompatibility()) {
if ((sym.owner.flags() & INTERFACE) != 0 &&
syms.objectType.tsym.members().lookup(sym.name).scope != null)
syms.objectType.tsym.members().findFirst(sym.name) != null)
return sym;
}
@ -651,13 +652,10 @@ public class Gen extends JCTree.Visitor {
void implementInterfaceMethods(ClassSymbol c, ClassSymbol site) {
for (List<Type> l = types.interfaces(c.type); l.nonEmpty(); l = l.tail) {
ClassSymbol i = (ClassSymbol)l.head.tsym;
for (Scope.Entry e = i.members().elems;
e != null;
e = e.sibling)
{
if (e.sym.kind == MTH && (e.sym.flags() & STATIC) == 0)
for (Symbol sym : i.members().getSymbols(NON_RECURSIVE)) {
if (sym.kind == MTH && (sym.flags() & STATIC) == 0)
{
MethodSymbol absMeth = (MethodSymbol)e.sym;
MethodSymbol absMeth = (MethodSymbol)sym;
MethodSymbol implMeth = absMeth.binaryImplementation(site, types);
if (implMeth == null)
addAbstractMethod(site, absMeth);

View File

@ -37,7 +37,6 @@ import javax.tools.StandardLocation;
import com.sun.tools.javac.code.Attribute;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Scope;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.code.Symbol.VarSymbol;
@ -53,6 +52,7 @@ import com.sun.tools.javac.util.Pair;
import static com.sun.tools.javac.main.Option.*;
import static com.sun.tools.javac.code.Kinds.*;
import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
/** This class provides operations to write native header files for classes.
*
@ -146,8 +146,7 @@ public class JNIWriter {
if (c.isLocal() || isSynthetic(c))
return false;
for (Scope.Entry i = c.members_field.elems; i != null; i = i.sibling) {
Symbol sym = i.sym;
for (Symbol sym : c.members_field.getSymbols(NON_RECURSIVE)) {
if (sym.kind == MTH && isNative(sym))
return true;
for (Attribute.Compound a: sym.getDeclarationAttributes()) {
@ -156,8 +155,7 @@ public class JNIWriter {
}
}
if (checkNestedClasses) {
for (Scope.Entry i = c.members_field.elems; i != null; i = i.sibling) {
Symbol sym = i.sym;
for (Symbol sym : c.members_field.getSymbols(NON_RECURSIVE)) {
if ((sym.kind == TYP) && needsHeader(((ClassSymbol) sym), true))
return true;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -43,6 +43,7 @@ import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.code.Type.ArrayType;
import com.sun.tools.javac.util.*;
import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
/**
* A generator of dynamic proxy implementations of
@ -119,9 +120,9 @@ public class AnnotationProxyMaker {
// First find the default values.
ClassSymbol sym = (ClassSymbol) anno.type.tsym;
for (Scope.Entry e = sym.members().elems; e != null; e = e.sibling) {
if (e.sym.kind == Kinds.MTH) {
MethodSymbol m = (MethodSymbol) e.sym;
for (Symbol s : sym.members().getSymbols(NON_RECURSIVE)) {
if (s.kind == Kinds.MTH) {
MethodSymbol m = (MethodSymbol) s;
Attribute def = m.getDefaultValue();
if (def != null)
res.put(m, def);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2005, 2008, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2005, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -27,11 +27,12 @@ package com.sun.tools.javac.model;
import java.util.AbstractList;
import java.util.Iterator;
import java.util.NoSuchElementException;
import com.sun.tools.javac.code.Scope;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.util.Filter;
import static com.sun.tools.javac.code.Flags.*;
import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
/**
* Utility to construct a view of a symbol's members,
@ -53,56 +54,28 @@ public class FilteredMemberList extends AbstractList<Symbol> {
public int size() {
int cnt = 0;
for (Scope.Entry e = scope.elems; e != null; e = e.sibling) {
if (!unwanted(e.sym))
for (Symbol sym : scope.getSymbols(NON_RECURSIVE)) {
if (!unwanted(sym))
cnt++;
}
return cnt;
}
public Symbol get(int index) {
for (Scope.Entry e = scope.elems; e != null; e = e.sibling) {
if (!unwanted(e.sym) && (index-- == 0))
return e.sym;
for (Symbol sym : scope.getSymbols(NON_RECURSIVE)) {
if (!unwanted(sym) && (index-- == 0))
return sym;
}
throw new IndexOutOfBoundsException();
}
// A more efficient implementation than AbstractList's.
public Iterator<Symbol> iterator() {
return new Iterator<Symbol>() {
/** The next entry to examine, or null if none. */
private Scope.Entry nextEntry = scope.elems;
private boolean hasNextForSure = false;
public boolean hasNext() {
if (hasNextForSure) {
return true;
}
while (nextEntry != null && unwanted(nextEntry.sym)) {
nextEntry = nextEntry.sibling;
}
hasNextForSure = (nextEntry != null);
return hasNextForSure;
return scope.getSymbols(new Filter<Symbol>() {
public boolean accepts(Symbol t) {
return !unwanted(t);
}
public Symbol next() {
if (hasNext()) {
Symbol result = nextEntry.sym;
nextEntry = nextEntry.sibling;
hasNextForSure = false;
return result;
} else {
throw new NoSuchElementException();
}
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}, NON_RECURSIVE).iterator();
}
/**

View File

@ -35,6 +35,7 @@ import javax.tools.JavaFileObject;
import static javax.lang.model.util.ElementFilter.methodsIn;
import com.sun.tools.javac.code.*;
import com.sun.tools.javac.code.Scope.WriteableScope;
import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.comp.AttrContext;
import com.sun.tools.javac.comp.Enter;
@ -47,6 +48,7 @@ import com.sun.tools.javac.tree.TreeInfo;
import com.sun.tools.javac.tree.TreeScanner;
import com.sun.tools.javac.util.*;
import com.sun.tools.javac.util.Name;
import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
import static com.sun.tools.javac.code.TypeTag.CLASS;
import static com.sun.tools.javac.tree.JCTree.Tag.*;
@ -356,35 +358,31 @@ public class JavacElements implements Elements {
*/
public FilteredMemberList getAllMembers(TypeElement element) {
Symbol sym = cast(Symbol.class, element);
Scope scope = sym.members().dupUnshared();
WriteableScope scope = sym.members().dupUnshared();
List<Type> closure = types.closure(sym.asType());
for (Type t : closure)
addMembers(scope, t);
return new FilteredMemberList(scope);
}
// where
private void addMembers(Scope scope, Type type) {
private void addMembers(WriteableScope scope, Type type) {
members:
for (Scope.Entry e = type.asElement().members().elems; e != null; e = e.sibling) {
Scope.Entry overrider = scope.lookup(e.sym.getSimpleName());
while (overrider.scope != null) {
if (overrider.sym.kind == e.sym.kind
&& (overrider.sym.flags() & Flags.SYNTHETIC) == 0)
{
if (overrider.sym.getKind() == ElementKind.METHOD
&& overrides((ExecutableElement)overrider.sym, (ExecutableElement)e.sym, (TypeElement)type.asElement())) {
for (Symbol e : type.asElement().members().getSymbols(NON_RECURSIVE)) {
for (Symbol overrider : scope.getSymbolsByName(e.getSimpleName())) {
if (overrider.kind == e.kind && (overrider.flags() & Flags.SYNTHETIC) == 0) {
if (overrider.getKind() == ElementKind.METHOD &&
overrides((ExecutableElement)overrider, (ExecutableElement)e, (TypeElement)type.asElement())) {
continue members;
}
}
overrider = overrider.next();
}
boolean derived = e.sym.getEnclosingElement() != scope.owner;
ElementKind kind = e.sym.getKind();
boolean derived = e.getEnclosingElement() != scope.owner;
ElementKind kind = e.getKind();
boolean initializer = kind == ElementKind.CONSTRUCTOR
|| kind == ElementKind.INSTANCE_INIT
|| kind == ElementKind.STATIC_INIT;
if (!derived || (!initializer && e.sym.isInheritedIn(scope.owner, types)))
scope.enter(e.sym);
if (!derived || (!initializer && e.isInheritedIn(scope.owner, types)))
scope.enter(e);
}
}

View File

@ -310,9 +310,9 @@ public class JavacTypes implements javax.lang.model.util.Types {
for (Type t : types.closure(origin.type)) {
if (t != origin.type) {
ClassSymbol c = (ClassSymbol) t.tsym;
for (Scope.Entry e = c.members().lookup(m.name); e.scope != null; e = e.next()) {
if (e.sym.kind == Kinds.MTH && m.overrides(e.sym, origin, types, true)) {
results.add((MethodSymbol) e.sym);
for (Symbol sym : c.members().getSymbolsByName(m.name)) {
if (sym.kind == Kinds.MTH && m.overrides(sym, origin, types, true)) {
results.add((MethodSymbol) sym);
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2006, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -27,7 +27,7 @@ package com.sun.tools.javac.sym;
import com.sun.tools.javac.api.JavacTaskImpl;
import com.sun.tools.javac.code.Kinds;
import com.sun.tools.javac.code.Scope;
import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Attribute;
@ -189,7 +189,7 @@ public class CreateSymbols extends AbstractProcessor {
new Attribute.Compound(syms.proprietaryType,
List.<Pair<Symbol.MethodSymbol,Attribute>>nil());
Attribute.Compound[] profileAnnos = new Attribute.Compound[profiles.getProfileCount() + 1];
Symbol.MethodSymbol profileValue = (MethodSymbol) syms.profileType.tsym.members().lookup(names.value).sym;
Symbol.MethodSymbol profileValue = (MethodSymbol) syms.profileType.tsym.members().findFirst(names.value);
for (int i = 1; i < profileAnnos.length; i++) {
profileAnnos[i] = new Attribute.Compound(syms.profileType,
List.<Pair<Symbol.MethodSymbol, Attribute>>of(
@ -259,9 +259,9 @@ public class CreateSymbols extends AbstractProcessor {
pool.reset();
cs.pool = pool;
writer.writeClass(cs);
for (Scope.Entry e = cs.members().elems; e != null; e = e.sibling) {
if (e.sym.kind == Kinds.TYP) {
ClassSymbol nestedClass = (ClassSymbol)e.sym;
for (Symbol sym : cs.members().getSymbols(NON_RECURSIVE)) {
if (sym.kind == Kinds.TYP) {
ClassSymbol nestedClass = (ClassSymbol)sym;
nestedClass.complete();
writeClass(pool, nestedClass, writer);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1999, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -488,8 +488,10 @@ public abstract class JCTree implements Tree, Cloneable, DiagnosticPosition {
public JavaFileObject sourcefile;
/** The package to which this compilation unit belongs. */
public PackageSymbol packge;
/** A scope containing top level classes. */
public WriteableScope toplevelScope;
/** A scope for all named imports. */
public ImportScope namedImportScope;
public NamedImportScope namedImportScope;
/** A scope for all import-on-demands. */
public StarImportScope starImportScope;
/** Line starting positions, defined only if option -g is set. */

View File

@ -25,6 +25,8 @@
package com.sun.tools.javac.tree;
import java.util.Iterator;
import com.sun.tools.javac.code.*;
import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.code.Type.*;
@ -978,24 +980,26 @@ public class TreeMaker implements JCTree.Factory {
sym.owner.kind == MTH || sym.owner.kind == VAR) {
return true;
} else if (sym.kind == TYP && toplevel != null) {
Scope.Entry e;
e = toplevel.namedImportScope.lookup(sym.name);
if (e.scope != null) {
Iterator<Symbol> it = toplevel.namedImportScope.getSymbolsByName(sym.name).iterator();
if (it.hasNext()) {
Symbol s = it.next();
return
e.sym == sym &&
e.next().scope == null;
s == sym &&
!it.hasNext();
}
e = toplevel.packge.members().lookup(sym.name);
if (e.scope != null) {
it = toplevel.packge.members().getSymbolsByName(sym.name).iterator();
if (it.hasNext()) {
Symbol s = it.next();
return
e.sym == sym &&
e.next().scope == null;
s == sym &&
!it.hasNext();
}
e = toplevel.starImportScope.lookup(sym.name);
if (e.scope != null) {
it = toplevel.starImportScope.getSymbolsByName(sym.name).iterator();
if (it.hasNext()) {
Symbol s = it.next();
return
e.sym == sym &&
e.next().scope == null;
s == sym &&
!it.hasNext();
}
}
return false;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -29,10 +29,12 @@ import com.sun.javadoc.*;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.code.Kinds;
import com.sun.tools.javac.code.Scope;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.util.List;
import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
/**
* Represents an annotation type.
*
@ -91,9 +93,9 @@ public class AnnotationTypeDocImpl
*/
public AnnotationTypeElementDoc[] elements() {
List<AnnotationTypeElementDoc> elements = List.nil();
for (Scope.Entry e = tsym.members().elems; e != null; e = e.sibling) {
if (e.sym != null && e.sym.kind == Kinds.MTH) {
MethodSymbol s = (MethodSymbol)e.sym;
for (Symbol sym : tsym.members().getSymbols(NON_RECURSIVE)) {
if (sym != null && sym.kind == Kinds.MTH) {
MethodSymbol s = (MethodSymbol)sym;
elements = elements.prepend(env.getAnnotationTypeElementDoc(s));
}
}

View File

@ -59,6 +59,7 @@ import com.sun.tools.javac.util.Name;
import com.sun.tools.javac.util.Names;
import com.sun.tools.javac.util.Position;
import static com.sun.tools.javac.code.Kinds.*;
import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
import static com.sun.tools.javac.code.TypeTag.CLASS;
import static com.sun.tools.javac.tree.JCTree.Tag.*;
@ -589,9 +590,9 @@ public class ClassDocImpl extends ProgramElementDocImpl implements ClassDoc {
*/
private FieldDoc[] fields(boolean filter, boolean enumConstants) {
List<FieldDocImpl> fields = List.nil();
for (Scope.Entry e = tsym.members().elems; e != null; e = e.sibling) {
if (e.sym != null && e.sym.kind == VAR) {
VarSymbol s = (VarSymbol)e.sym;
for (Symbol sym : tsym.members().getSymbols(NON_RECURSIVE)) {
if (sym != null && sym.kind == VAR) {
VarSymbol s = (VarSymbol)sym;
boolean isEnum = ((s.flags() & Flags.ENUM) != 0) &&
!env.legacyDoclet;
if (isEnum == enumConstants &&
@ -614,12 +615,12 @@ public class ClassDocImpl extends ProgramElementDocImpl implements ClassDoc {
public MethodDoc[] methods(boolean filter) {
Names names = tsym.name.table.names;
List<MethodDocImpl> methods = List.nil();
for (Scope.Entry e = tsym.members().elems; e != null; e = e.sibling) {
if (e.sym != null
&& e.sym.kind == Kinds.MTH
&& e.sym.name != names.init
&& e.sym.name != names.clinit) {
MethodSymbol s = (MethodSymbol)e.sym;
for (Symbol sym :tsym.members().getSymbols(NON_RECURSIVE)) {
if (sym != null
&& sym.kind == Kinds.MTH
&& sym.name != names.init
&& sym.name != names.clinit) {
MethodSymbol s = (MethodSymbol)sym;
if (!filter || env.shouldDocument(s)) {
methods = methods.prepend(env.getMethodDoc(s));
}
@ -649,10 +650,10 @@ public class ClassDocImpl extends ProgramElementDocImpl implements ClassDoc {
public ConstructorDoc[] constructors(boolean filter) {
Names names = tsym.name.table.names;
List<ConstructorDocImpl> constructors = List.nil();
for (Scope.Entry e = tsym.members().elems; e != null; e = e.sibling) {
if (e.sym != null &&
e.sym.kind == Kinds.MTH && e.sym.name == names.init) {
MethodSymbol s = (MethodSymbol)e.sym;
for (Symbol sym : tsym.members().getSymbols(NON_RECURSIVE)) {
if (sym != null &&
sym.kind == Kinds.MTH && sym.name == names.init) {
MethodSymbol s = (MethodSymbol)sym;
if (!filter || env.shouldDocument(s)) {
constructors = constructors.prepend(env.getConstructorDoc(s));
}
@ -685,10 +686,9 @@ public class ClassDocImpl extends ProgramElementDocImpl implements ClassDoc {
if (l.contains(this)) return;
l.append(this);
List<ClassDocImpl> more = List.nil();
for (Scope.Entry e = tsym.members().elems; e != null;
e = e.sibling) {
if (e.sym != null && e.sym.kind == Kinds.TYP) {
ClassSymbol s = (ClassSymbol)e.sym;
for (Symbol sym : tsym.members().getSymbols(NON_RECURSIVE)) {
if (sym != null && sym.kind == Kinds.TYP) {
ClassSymbol s = (ClassSymbol)sym;
ClassDocImpl c = env.getClassDoc(s);
if (c.isSynthetic()) continue;
if (c != null) more = more.prepend(c);
@ -713,9 +713,9 @@ public class ClassDocImpl extends ProgramElementDocImpl implements ClassDoc {
*/
public ClassDoc[] innerClasses(boolean filter) {
ListBuffer<ClassDocImpl> innerClasses = new ListBuffer<>();
for (Scope.Entry e = tsym.members().elems; e != null; e = e.sibling) {
if (e.sym != null && e.sym.kind == Kinds.TYP) {
ClassSymbol s = (ClassSymbol)e.sym;
for (Symbol sym : tsym.members().getSymbols(NON_RECURSIVE)) {
if (sym != null && sym.kind == Kinds.TYP) {
ClassSymbol s = (ClassSymbol)sym;
if ((s.flags_field & Flags.SYNTHETIC) != 0) continue;
if (!filter || env.isVisible(s)) {
innerClasses.prepend(env.getClassDoc(s));
@ -809,17 +809,17 @@ public class ClassDocImpl extends ProgramElementDocImpl implements ClassDoc {
if (compenv == null) return null;
Scope s = compenv.toplevel.namedImportScope;
for (Scope.Entry e = s.lookup(names.fromString(className)); e.scope != null; e = e.next()) {
if (e.sym.kind == Kinds.TYP) {
ClassDoc c = env.getClassDoc((ClassSymbol)e.sym);
for (Symbol sym : s.getSymbolsByName(names.fromString(className))) {
if (sym.kind == Kinds.TYP) {
ClassDoc c = env.getClassDoc((ClassSymbol)sym);
return c;
}
}
s = compenv.toplevel.starImportScope;
for (Scope.Entry e = s.lookup(names.fromString(className)); e.scope != null; e = e.next()) {
if (e.sym.kind == Kinds.TYP) {
ClassDoc c = env.getClassDoc((ClassSymbol)e.sym);
for (Symbol sym : s.getSymbolsByName(names.fromString(className))) {
if (sym.kind == Kinds.TYP) {
ClassDoc c = env.getClassDoc((ClassSymbol)sym);
return c;
}
}
@ -918,7 +918,6 @@ public class ClassDocImpl extends ProgramElementDocImpl implements ClassDoc {
*---------------------------------*/
// search current class
Scope.Entry e = tsym.members().lookup(names.fromString(methodName));
//### Using modifier filter here isn't really correct,
//### but emulates the old behavior. Instead, we should
@ -931,11 +930,11 @@ public class ClassDocImpl extends ProgramElementDocImpl implements ClassDoc {
// In order to provide textually identical results, we
// attempt to emulate the old behavior.
MethodSymbol lastFound = null;
for (; e.scope != null; e = e.next()) {
if (e.sym.kind == Kinds.MTH) {
for (Symbol sym : tsym.members().getSymbolsByName(names.fromString(methodName))) {
if (sym.kind == Kinds.MTH) {
//### Should intern methodName as Name.
if (e.sym.name.toString().equals(methodName)) {
lastFound = (MethodSymbol)e.sym;
if (sym.name.toString().equals(methodName)) {
lastFound = (MethodSymbol)sym;
}
}
}
@ -943,12 +942,12 @@ public class ClassDocImpl extends ProgramElementDocImpl implements ClassDoc {
return env.getMethodDoc(lastFound);
}
} else {
for (; e.scope != null; e = e.next()) {
if (e.sym != null &&
e.sym.kind == Kinds.MTH) {
for (Symbol sym : tsym.members().getSymbolsByName(names.fromString(methodName))) {
if (sym != null &&
sym.kind == Kinds.MTH) {
//### Should intern methodName as Name.
if (hasParameterTypes((MethodSymbol)e.sym, paramTypes)) {
return env.getMethodDoc((MethodSymbol)e.sym);
if (hasParameterTypes((MethodSymbol)sym, paramTypes)) {
return env.getMethodDoc((MethodSymbol)sym);
}
}
}
@ -1005,10 +1004,10 @@ public class ClassDocImpl extends ProgramElementDocImpl implements ClassDoc {
public ConstructorDoc findConstructor(String constrName,
String[] paramTypes) {
Names names = tsym.name.table.names;
for (Scope.Entry e = tsym.members().lookup(names.fromString("<init>")); e.scope != null; e = e.next()) {
if (e.sym.kind == Kinds.MTH) {
if (hasParameterTypes((MethodSymbol)e.sym, paramTypes)) {
return env.getConstructorDoc((MethodSymbol)e.sym);
for (Symbol sym : tsym.members().getSymbolsByName(names.fromString("<init>"))) {
if (sym.kind == Kinds.MTH) {
if (hasParameterTypes((MethodSymbol)sym, paramTypes)) {
return env.getConstructorDoc((MethodSymbol)sym);
}
}
}
@ -1047,10 +1046,10 @@ public class ClassDocImpl extends ProgramElementDocImpl implements ClassDoc {
}
searched.add(this);
for (Scope.Entry e = tsym.members().lookup(names.fromString(fieldName)); e.scope != null; e = e.next()) {
if (e.sym.kind == Kinds.VAR) {
for (Symbol sym : tsym.members().getSymbolsByName(names.fromString(fieldName))) {
if (sym.kind == Kinds.VAR) {
//### Should intern fieldName as Name.
return env.getFieldDoc((VarSymbol)e.sym);
return env.getFieldDoc((VarSymbol)sym);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -128,8 +128,8 @@ public class MethodDocImpl
t.hasTag(CLASS);
t = env.types.supertype(t)) {
ClassSymbol c = (ClassSymbol)t.tsym;
for (Scope.Entry e = c.members().lookup(sym.name); e.scope != null; e = e.next()) {
if (sym.overrides(e.sym, origin, env.types, true)) {
for (Symbol sym2 : c.members().getSymbolsByName(sym.name)) {
if (sym.overrides(sym2, origin, env.types, true)) {
return TypeMaker.getType(env, t);
}
}
@ -160,9 +160,9 @@ public class MethodDocImpl
t.hasTag(CLASS);
t = env.types.supertype(t)) {
ClassSymbol c = (ClassSymbol)t.tsym;
for (Scope.Entry e = c.members().lookup(sym.name); e.scope != null; e = e.next()) {
if (sym.overrides(e.sym, origin, env.types, true)) {
return env.getMethodDoc((MethodSymbol)e.sym);
for (Symbol sym2 : c.members().getSymbolsByName(sym.name)) {
if (sym.overrides(sym2, origin, env.types, true)) {
return env.getMethodDoc((MethodSymbol)sym2);
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -33,7 +33,7 @@ import javax.tools.FileObject;
import com.sun.javadoc.*;
import com.sun.source.util.TreePath;
import com.sun.tools.javac.code.Attribute;
import com.sun.tools.javac.code.Scope;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.code.Symbol.PackageSymbol;
import com.sun.tools.javac.tree.JCTree;
@ -43,6 +43,8 @@ import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Name;
import com.sun.tools.javac.util.Position;
import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
/**
* Represents a java package. Provides access to information
* about the package, the package's comment and tags, and the
@ -146,9 +148,9 @@ public class PackageDocImpl extends DocImpl implements PackageDoc {
return allClassesFiltered;
}
ListBuffer<ClassDocImpl> classes = new ListBuffer<>();
for (Scope.Entry e = sym.members().elems; e != null; e = e.sibling) {
if (e.sym != null) {
ClassSymbol s = (ClassSymbol)e.sym;
for (Symbol enumerated : sym.members().getSymbols(NON_RECURSIVE)) {
if (enumerated != null) {
ClassSymbol s = (ClassSymbol)enumerated;
ClassDocImpl c = env.getClassDoc(s);
if (c != null && !c.isSynthetic())
c.addAllClasses(classes, filtered);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -26,15 +26,13 @@
package com.sun.tools.javadoc;
import com.sun.javadoc.*;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Kinds;
import com.sun.tools.javac.code.Scope;
import com.sun.tools.javac.code.*;
import com.sun.tools.javac.code.Symbol.ClassSymbol;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.code.Symbol.VarSymbol;
import com.sun.tools.javac.util.ListBuffer;
import com.sun.tools.javac.util.Name;
import com.sun.tools.javac.util.Names;
import com.sun.tools.javac.util.*;
import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
/**
* The serialized form is the specification of a class' serialization
@ -159,9 +157,9 @@ class SerializedForm {
/* SERIALIZABLE_FIELDS can be private,
* so must lookup by ClassSymbol, not by ClassDocImpl.
*/
for (Scope.Entry e = def.members().lookup(names.fromString(SERIALIZABLE_FIELDS)); e.scope != null; e = e.next()) {
if (e.sym.kind == Kinds.VAR) {
VarSymbol f = (VarSymbol)e.sym;
for (Symbol sym : def.members().getSymbolsByName(names.fromString(SERIALIZABLE_FIELDS))) {
if (sym.kind == Kinds.VAR) {
VarSymbol f = (VarSymbol)sym;
if ((f.flags() & Flags.STATIC) != 0 &&
(f.flags() & Flags.PRIVATE) != 0) {
return f;
@ -180,9 +178,9 @@ class SerializedForm {
private void computeDefaultSerializableFields(DocEnv env,
ClassSymbol def,
ClassDocImpl cd) {
for (Scope.Entry e = def.members().elems; e != null; e = e.sibling) {
if (e.sym != null && e.sym.kind == Kinds.VAR) {
VarSymbol f = (VarSymbol)e.sym;
for (Symbol sym : def.members().getSymbols(NON_RECURSIVE)) {
if (sym != null && sym.kind == Kinds.VAR) {
VarSymbol f = (VarSymbol)sym;
if ((f.flags() & Flags.STATIC) == 0 &&
(f.flags() & Flags.TRANSIENT) == 0) {
//### No modifier filtering applied here.
@ -209,9 +207,9 @@ class SerializedForm {
private void addMethodIfExist(DocEnv env, ClassSymbol def, String methodName) {
Names names = def.name.table.names;
for (Scope.Entry e = def.members().lookup(names.fromString(methodName)); e.scope != null; e = e.next()) {
if (e.sym.kind == Kinds.MTH) {
MethodSymbol md = (MethodSymbol)e.sym;
for (Symbol sym : def.members().getSymbolsByName(names.fromString(methodName))) {
if (sym.kind == Kinds.MTH) {
MethodSymbol md = (MethodSymbol)sym;
if ((md.flags() & Flags.STATIC) == 0) {
/*
* WARNING: not robust if unqualifiedMethodName is overloaded
@ -241,10 +239,9 @@ class SerializedForm {
Name fieldName = names.fromString(tag.fieldName());
// Look for a FieldDocImpl that is documented by serialFieldTagImpl.
for (Scope.Entry e = def.members().lookup(fieldName);
e.scope != null; e = e.next()) {
if (e.sym.kind == Kinds.VAR) {
VarSymbol f = (VarSymbol) e.sym;
for (Symbol sym : def.members().getSymbolsByName(fieldName)) {
if (sym.kind == Kinds.VAR) {
VarSymbol f = (VarSymbol) sym;
FieldDocImpl fdi = env.getFieldDoc(f);
((SerialFieldTagImpl) (tag)).mapToFieldDocImpl(fdi);
break;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,19 +23,24 @@
/*
* @test
* @bug 6402516
* @bug 6402516 8031569
* @summary need Trees.getScope(TreePath)
* @build Checker CheckLocalElements
* @run main CheckLocalElements
*/
import java.io.IOException;
import java.util.*;
import com.sun.source.tree.*;
import java.util.regex.*;
import javax.lang.model.element.*;
import javax.lang.model.util.*;
import com.sun.source.tree.*;
import com.sun.source.util.*;
/*
* Check the local elements of a scope against the contents of string literals.
* Check the local elements of a scope against the contents of string literals and top-level comment.
*/
public class CheckLocalElements extends Checker {
public static void main(String... args) throws Exception {
@ -90,6 +95,16 @@ public class CheckLocalElements extends Checker {
return true;
}
@Override
void additionalChecks(Trees trees, CompilationUnitTree topLevel) throws IOException {
Matcher m = TOPLEVEL_SCOPE_DEF.matcher(topLevel.getSourceFile().getCharContent(false));
if (!m.find())
throw new AssertionError("Should have top-level scope def!");
check(trees.getScope(new TreePath(topLevel)), m.group(1));
}
//where:
Pattern TOPLEVEL_SCOPE_DEF = Pattern.compile("TOPLEVEL_SCOPE:(.*)");
private String getEnclosingName(Element e) {
Element encl = e.getEnclosingElement();
return encl == null ? "" : encl.accept(qualNameVisitor, null);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -67,6 +67,7 @@ abstract class Checker {
for (CompilationUnitTree unit: units) {
TreePath p = new TreePath(unit);
s.scan(p, getTrees());
additionalChecks(getTrees(), unit);
}
task = null;
@ -111,6 +112,9 @@ abstract class Checker {
throw new IllegalStateException();
}
void additionalChecks(Trees trees, CompilationUnitTree topLevel) throws IOException {
}
void error(Scope s, String ref, String msg) {
System.err.println("Error: " + msg);
System.err.println("Scope: " + (s == null ? null : asList(s.getLocalElements())));

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2006, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,7 +23,7 @@
import java.util.List;
import java.io.*;
//TOPLEVEL_SCOPE:List, Test2, Test; java.io.*, java.lang.*
class Test {
void m1(int m1_arg) {
String x = "x, m1_arg, super, this; List, Test2, Test; java.io.*, java.lang.*";

View File

@ -32,7 +32,7 @@ import java.util.*;
import javax.tools.StandardLocation;
import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.Kinds;
import com.sun.tools.javac.code.Scope;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.code.Symtab;
import com.sun.tools.javac.code.Type;
@ -43,6 +43,8 @@ import com.sun.tools.javac.jvm.ClassReader;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.Names;
import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
public class T6889255 {
boolean testInterfaces = true;
boolean testSyntheticMethods = true;
@ -380,11 +382,11 @@ public class T6889255 {
if ((sym.flags() & Flags.INTERFACE) != 0 && !testInterfaces)
continue;
for (Scope.Entry e = sym.members_field.elems; e != null; e = e.sibling) {
System.err.println("Checking member " + e.sym);
switch (e.sym.kind) {
for (Symbol s : sym.members_field.getSymbols(NON_RECURSIVE)) {
System.err.println("Checking member " + s);
switch (s.kind) {
case Kinds.TYP: {
String name = e.sym.flatName().toString();
String name = s.flatName().toString();
if (!classes.contains(name)) {
classes.add(name);
work.add(name);
@ -392,7 +394,7 @@ public class T6889255 {
break;
}
case Kinds.MTH:
verify((MethodSymbol) e.sym, expectNames);
verify((MethodSymbol) s, expectNames);
break;
}

View File

@ -0,0 +1,3 @@
/* @test /nodynamiccopyright/
* @compile/fail/ref=ImportOnDemandConflicts.out -XDrawDiagnostics p1/Object.java p1/String.java p2/Boolean.java
*/

View File

@ -0,0 +1,2 @@
String.java:13:9: compiler.err.ref.ambiguous: Boolean, kindname.class, p2.Boolean, p2, kindname.class, java.lang.Boolean, java.lang
1 error

View File

@ -0,0 +1,5 @@
package p1;
public class Object {
public static void test() { }
}

View File

@ -0,0 +1,15 @@
package p1;
import p2.*;
public class String {
public static void test() { }
}
class Test1 {
private void test() {
String.test();
Object.test();
Boolean.valueOf(true);
}
}

View File

@ -0,0 +1,7 @@
package p2;
public class Boolean {
public static Boolean valueOf(boolean b) {
return null;
}
}

View File

@ -31,6 +31,7 @@ import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
@ -337,6 +338,8 @@ public class DPrinter {
printList(label, (List) item);
} else if (item instanceof Name) {
printName(label, (Name) item);
} else if (item instanceof Scope) {
printScope(label, (Scope) item);
} else {
printString(label, String.valueOf(item));
}
@ -356,7 +359,7 @@ public class DPrinter {
out.print(label);
out.print(": [");
String sep = "";
for (Symbol sym: scope.getElements()) {
for (Symbol sym: scope.getSymbols()) {
out.print(sep);
out.print(sym.name);
sep = ",";
@ -370,19 +373,7 @@ public class DPrinter {
out.println(label);
indent(+1);
printImplClass(scope, Scope.class);
printSymbol("owner", scope.owner, Details.SUMMARY);
printScope("next", scope.next, Details.SUMMARY);
printObject("shared", getField(scope, Scope.class, "shared"), Details.SUMMARY);
if (scope instanceof CompoundScope) {
printObject("subScopes",
getField(scope, CompoundScope.class, "subScopes"),
Details.FULL);
} else {
for (Symbol sym : scope.getElements()) {
printSymbol(sym.name.toString(), sym, Details.SUMMARY);
}
}
printFullScopeImpl(scope);
indent(-1);
break;
}
@ -390,6 +381,72 @@ public class DPrinter {
}
}
void printFullScopeImpl(Scope scope) {
indent();
out.println(scope.getClass().getName());
printSymbol("owner", scope.owner, Details.SUMMARY);
if (SCOPE_IMPL_CLASS.equals(scope.getClass().getName())) {
printScope("next", (Scope) getField(scope, scope.getClass(), "next"), Details.SUMMARY);
printObject("shared", getField(scope, scope.getClass(), "shared"), Details.SUMMARY);
Object[] table = (Object[]) getField(scope, scope.getClass(), "table");
for (int i = 0; i < table.length; i++) {
if (i > 0)
out.print(", ");
else
indent();
out.print(i + ":" + entryToString(table[i], table, false));
}
out.println();
} else if (FILTER_SCOPE_CLASS.equals(scope.getClass().getName())) {
printScope("delegate",
(Scope) getField(scope, scope.getClass(), "delegate"), Details.FULL);
} else if (scope instanceof CompoundScope) {
printList("delegates", (List<?>) getField(scope, CompoundScope.class, "subScopes"));
} else {
for (Symbol sym : scope.getSymbols()) {
printSymbol(sym.name.toString(), sym, Details.SUMMARY);
}
}
}
//where:
static final String SCOPE_IMPL_CLASS = "com.sun.tools.javac.code.Scope$ScopeImpl";
static final String FILTER_SCOPE_CLASS = "com.sun.tools.javac.code.Scope$FilterImportScope";
/**
* Create a string showing the contents of an entry, using the table
* to help identify cross-references to other entries in the table.
* @param e the entry to be shown
* @param table the table containing the other entries
*/
String entryToString(Object e, Object[] table, boolean ref) {
if (e == null)
return "null";
Symbol sym = (Symbol) getField(e, e.getClass(), "sym");
if (sym == null)
return "sent"; // sentinel
if (ref) {
int index = indexOf(table, e);
if (index != -1)
return String.valueOf(index);
}
Scope scope = (Scope) getField(e, e.getClass(), "scope");
return "(" + sym.name + ":" + sym
+ ",shdw:" + entryToString(callMethod(e, e.getClass(), "next"), table, true)
+ ",sibl:" + entryToString(getField(e, e.getClass(), "sibling"), table, true)
+ ((sym.owner != scope.owner)
? (",BOGUS[" + sym.owner + "," + scope.owner + "]")
: "")
+ ")";
}
<T> int indexOf(T[] array, T item) {
for (int i = 0; i < array.length; i++) {
if (array[i] == item)
return i;
}
return -1;
}
public void printSource(String label, JCTree tree) {
printString(label, Pretty.toSimpleString(tree, maxSrcLength));
}
@ -552,6 +609,23 @@ public class DPrinter {
}
}
protected Object callMethod(Object o, Class<?> clazz, String name) {
try {
Method m = clazz.getDeclaredMethod(name);
boolean prev = m.isAccessible();
m.setAccessible(true);
try {
return m.invoke(o);
} finally {
m.setAccessible(prev);
}
} catch (ReflectiveOperationException e) {
return e;
} catch (SecurityException e) {
return e;
}
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="JCTree visitor methods">

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -117,13 +117,13 @@ public class CompoundScopeTest {
boolean subAdded = false;
for (int sc = 0 ; sc < 3 ; sc ++) {
if (scopeNesting[sc][i]) {
sub.addSubScope(scopes[sc]);
sub.prependSubScope(scopes[sc]);
if (!subAdded) {
root.addSubScope(sub);
root.prependSubScope(sub);
subAdded = true;
}
} else {
root.addSubScope(scopes[sc]);
root.prependSubScope(scopes[sc]);
}
}
log("testing scope: " + root);
@ -145,7 +145,7 @@ public class CompoundScopeTest {
* Create a scope containing a given number of synthetic symbols
*/
Scope createScope(int nelems) {
Scope s = new Scope(symtab.noSymbol);
WriteableScope s = WriteableScope.create(symtab.noSymbol);
for (int i = 0 ; i < nelems ; i++) {
Symbol sym = new TypeVariableSymbol(0, names.fromString("s" + i), null, null);
s.enter(sym);
@ -181,7 +181,7 @@ public class CompoundScopeTest {
elems :
filter(elems, sf);
int expectedCount = allSymbols.length();
for (Symbol s : sf == null ? cs.getElements() : cs.getElements(sf)) {
for (Symbol s : sf == null ? cs.getSymbols() : cs.getSymbols(sf)) {
checkSameSymbols(s, allSymbols.head);
allSymbols = allSymbols.tail;
found.append(s);
@ -204,7 +204,7 @@ public class CompoundScopeTest {
filter(shadowedEntry.getValue(), sf);
int expectedCount = shadowed.length();
Name name = shadowedEntry.getKey();
for (Symbol s : sf == null ? cs.getElementsByName(name) : cs.getElementsByName(name, sf)) {
for (Symbol s : sf == null ? cs.getSymbolsByName(name) : cs.getSymbolsByName(name, sf)) {
checkSameSymbols(s, shadowed.head);
shadowed = shadowed.tail;
count++;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -98,7 +98,7 @@ public class ImplementationCacheTest {
MethodSymbol I_m = null;
for (Symbol sym : i.members().getElements()) {
for (Symbol sym : i.members().getSymbols()) {
if (sym.name.contentEquals("m")) {
I_m = (MethodSymbol)sym;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,16 +25,21 @@
* @test
* @bug 7004029
* @summary Ensure Scope impl can cope with hash collisions
* @library /tools/javac/lib
* @build DPrinter HashCollisionTest
* @run main HashCollisionTest
*/
import java.lang.reflect.*;
import java.io.*;
import com.sun.source.util.Trees;
import com.sun.tools.javac.api.JavacTrees;
import com.sun.tools.javac.util.*;
import com.sun.tools.javac.code.*;
import com.sun.tools.javac.code.Scope.*;
import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.file.JavacFileManager;
import static com.sun.tools.javac.code.Kinds.*;
public class HashCollisionTest {
public static void main(String... args) throws Exception {
@ -47,12 +52,13 @@ public class HashCollisionTest {
JavacFileManager.preRegister(context); // required by ClassReader which is required by Symtab
names = Names.instance(context); // Name.Table impls tied to an instance of Names
symtab = Symtab.instance(context);
trees = JavacTrees.instance(context);
// determine hashMask for an empty scope
Scope emptyScope = new Scope(symtab.unnamedPackage); // any owner will do
Field sHashMask = Scope.class.getDeclaredField("hashMask");
sHashMask.setAccessible(true);
scopeHashMask = sHashMask.getInt(emptyScope);
Scope emptyScope = WriteableScope.create(symtab.unnamedPackage); // any owner will do
Field field = emptyScope.getClass().getDeclaredField("hashMask");
field.setAccessible(true);
scopeHashMask = field.getInt(emptyScope);
log("scopeHashMask: " + scopeHashMask);
// 1. determine the Name.hashCode of "Entry", and therefore the index of
@ -92,7 +98,7 @@ public class HashCollisionTest {
// 4. Create a package containing a nested class using the name from 2
PackageSymbol p = new PackageSymbol(names.fromString("p"), symtab.rootPackage);
p.members_field = new Scope(p);
p.members_field = WriteableScope.create(p);
ClassSymbol inner = createClass(innerName, p);
// we'll need this later when we "rename" cn
ClassSymbol outer = createClass(outerName, p);
@ -100,42 +106,25 @@ public class HashCollisionTest {
// 5. Create a star-import scope
log ("createStarImportScope");
// if StarImportScope exists, use it, otherwise, for testing legacy code,
// fall back on ImportScope
Scope starImportScope;
Method importAll;
PackageSymbol pkg = new PackageSymbol(names.fromString("pkg"), symtab.rootPackage);
try {
Class<?> c = Class.forName("com.sun.tools.javac.code.Scope$StarImportScope");
Constructor ctor = c.getDeclaredConstructor(new Class[] { Symbol.class });
importAll = c.getDeclaredMethod("importAll", new Class[] { Scope.class });
starImportScope = (Scope) ctor.newInstance(new Object[] { pkg });
} catch (ClassNotFoundException e) {
starImportScope = new ImportScope(pkg);
importAll = null;
}
StarImportScope starImportScope = new StarImportScope(pkg);
dump("initial", starImportScope);
// 6. Insert the contents of the package from 4.
Scope p_members = p.members();
if (importAll != null) {
importAll.invoke(starImportScope, p_members);
} else {
Scope fromScope = p_members;
Scope toScope = starImportScope;
// The following lines are taken from MemberEnter.importAll,
// before the use of StarImportScope.importAll.
for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) {
if (e.sym.kind == TYP && !toScope.includes(e.sym))
toScope.enter(e.sym, fromScope);
Scope fromScope = p.members();
ImportFilter typeFilter = new ImportFilter() {
@Override
public boolean accepts(Scope origin, Symbol sym) {
return sym.kind == Kinds.TYP;
}
}
};
starImportScope.importAll(fromScope, fromScope, typeFilter, false);
dump("imported p", starImportScope);
// 7. Insert the class from 3.
starImportScope.enter(ce, cc.members_field);
starImportScope.importAll(cc.members_field, cc.members_field, typeFilter, false);
dump("imported ce", starImportScope);
/*
@ -149,11 +138,11 @@ public class HashCollisionTest {
outer.members_field.enter(inner);
// 9. Lookup Entry
Scope.Entry e = starImportScope.lookup(entry);
dump("final", starImportScope);
Symbol found = starImportScope.findFirst(entry);
if (found != ce)
throw new Exception("correct symbol not found: " + entry + "; found=" + found);
if (e.sym == null)
throw new Exception("symbol not found: " + entry);
dump("final", starImportScope);
}
/*
@ -170,7 +159,7 @@ public class HashCollisionTest {
*/
ClassSymbol createClass(Name name, Symbol owner) {
ClassSymbol sym = new ClassSymbol(0, name, owner);
sym.members_field = new Scope(sym);
sym.members_field = WriteableScope.create(sym);
if (owner != symtab.unnamedPackage)
owner.members().enter(sym);
return sym;
@ -180,58 +169,16 @@ public class HashCollisionTest {
* Dump the contents of a scope to System.err.
*/
void dump(String label, Scope s) throws Exception {
dump(label, s, System.err);
PrintWriter pw = new PrintWriter(System.err);
new DPrinter(pw, trees).printScope(label, s);
pw.flush();
}
/**
* Dump the contents of a scope to a stream.
*/
void dump(String label, Scope s, PrintStream out) throws Exception {
out.println(label);
Field sTable = Scope.class.getDeclaredField("table");
sTable.setAccessible(true);
Object readField(Object scope, String fieldName) throws Exception {
Field field = scope.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
out.println("owner:" + s.owner);
Scope.Entry[] table = (Scope.Entry[]) sTable.get(s);
for (int i = 0; i < table.length; i++) {
if (i > 0)
out.print(", ");
out.print(i + ":" + toString(table[i], table, false));
}
out.println();
}
/**
* Create a string showing the contents of an entry, using the table
* to help identify cross-references to other entries in the table.
* @param e the entry to be shown
* @param table the table containing the other entries
*/
String toString(Scope.Entry e, Scope.Entry[] table, boolean ref) {
if (e == null)
return "null";
if (e.sym == null)
return "sent"; // sentinel
if (ref) {
int index = indexOf(table, e);
if (index != -1)
return String.valueOf(index);
}
return "(" + e.sym.name + ":" + e.sym
+ ",shdw:" + toString(e.next(), table, true)
+ ",sibl:" + toString(e.sibling, table, true)
+ ((e.sym.owner != e.scope.owner)
? (",BOGUS[" + e.sym.owner + "," + e.scope.owner + "]")
: "")
+ ")";
}
<T> int indexOf(T[] array, T item) {
for (int i = 0; i < array.length; i++) {
if (array[i] == item)
return i;
}
return -1;
return field.get(scope);
}
/**
@ -246,4 +193,5 @@ public class HashCollisionTest {
Names names;
Symtab symtab;
Trees trees;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -27,14 +27,17 @@
* @summary Basher for star-import scopes
*/
import java.lang.reflect.*;
import java.util.*;
import java.util.List;
import com.sun.tools.javac.util.*;
import com.sun.tools.javac.code.*;
import com.sun.tools.javac.code.Scope.*;
import com.sun.tools.javac.code.Scope.ImportFilter;
import com.sun.tools.javac.code.Scope.StarImportScope;
import com.sun.tools.javac.code.Scope.WriteableScope;
import com.sun.tools.javac.code.Symbol.*;
import com.sun.tools.javac.file.JavacFileManager;
import com.sun.tools.javac.util.*;
import static com.sun.tools.javac.code.Kinds.*;
public class StarImportTest {
@ -87,13 +90,9 @@ public class StarImportTest {
System.err.print(msg);
System.err.print(": ");
String sep = "(";
for (Scope.Entry se = s.elems; se != null; se = se.sibling) {
for (Scope.Entry e = se; e.sym != null; e = e.next()) {
System.err.print(sep + e.sym.name + ":" + e.sym);
sep = ",";
}
System.err.print(")");
sep = ", (";
for (Symbol sym : s.getSymbols()) {
System.err.print(sep + sym.name + ":" + sym);
sep = ",";
}
System.err.println();
}
@ -171,7 +170,7 @@ public class StarImportTest {
int count = rgen.nextInt(MAX_SETUP_PACKAGE_COUNT);
log("setup: creating package " + name + " with " + count + " entries");
PackageSymbol p = new PackageSymbol(name, symtab.rootPackage);
p.members_field = new Scope(p);
p.members_field = WriteableScope.create(p);
for (int i = 0; i < count; i++) {
String outer = name + "c" + i;
String suffix = random(null, "$Entry", "$Entry2");
@ -213,38 +212,21 @@ public class StarImportTest {
log ("createStarImportScope");
PackageSymbol pkg = new PackageSymbol(names.fromString("pkg"), symtab.rootPackage);
// if StarImportScope exists, use it, otherwise, for testing legacy code,
// fall back on ImportScope
Method importAll;
try {
Class<?> c = Class.forName("com.sun.tools.javac.code.Scope$StarImportScope");
Constructor ctor = c.getDeclaredConstructor(new Class[] { Symbol.class });
importAll = c.getDeclaredMethod("importAll", new Class[] { Scope.class });
starImportScope = (Scope) ctor.newInstance(new Object[] { pkg });
} catch (ClassNotFoundException e) {
starImportScope = new ImportScope(pkg);
importAll = null;
}
starImportScope = new StarImportScope(pkg);
starImportModel = new Model();
for (Symbol imp: imports) {
Scope members = imp.members();
if (importAll != null) {
// log("importAll", members);
importAll.invoke(starImportScope, members);
} else {
Scope fromScope = members;
Scope toScope = starImportScope;
// The following lines are taken from MemberEnter.importAll,
// before the use of StarImportScope.importAll.
for (Scope.Entry e = fromScope.elems; e != null; e = e.sibling) {
if (e.sym.kind == TYP && !toScope.includes(e.sym))
toScope.enter(e.sym, fromScope);
starImportScope.importAll(members, members, new ImportFilter() {
@Override
public boolean accepts(Scope origin, Symbol t) {
return t.kind == TYP;
}
}
}, false);
for (Scope.Entry e = members.elems; e != null; e = e.sibling) {
starImportModel.enter(e.sym);
for (Symbol sym : members.getSymbols()) {
starImportModel.enter(sym);
}
}
@ -260,9 +242,9 @@ public class StarImportTest {
log ("test");
List<ClassSymbol> nestedClasses = new LinkedList<ClassSymbol>();
for (PackageSymbol p: packages) {
for (Scope.Entry se = p.members_field.elems; se != null; se = se.sibling) {
if (se.sym.name.toString().contains("$"))
nestedClasses.add((ClassSymbol) se.sym);
for (Symbol sym : p.members_field.getSymbols()) {
if (sym.name.toString().contains("$"))
nestedClasses.add((ClassSymbol) sym);
}
}
@ -283,8 +265,7 @@ public class StarImportTest {
// determine new owner
Name outerName = names.fromString(s.substring(0, dollar));
// log(sym + " owner: " + sym.owner, sym.owner.members());
Scope.Entry outerEntry = sym.owner.members().lookup(outerName);
ClassSymbol outer = (ClassSymbol) outerEntry.sym;
ClassSymbol outer = (ClassSymbol)sym.owner.members().findFirst(outerName);
// log("outer: " + outerName + " " + outer);
// remove from package
@ -302,7 +283,7 @@ public class StarImportTest {
ClassSymbol createClass(Name name, Symbol owner) {
ClassSymbol sym = new ClassSymbol(0, name, owner);
sym.members_field = new Scope(sym);
sym.members_field = WriteableScope.create(sym);
if (owner != symtab.unnamedPackage)
owner.members().enter(sym);
return sym;
@ -318,7 +299,7 @@ public class StarImportTest {
List<Symbol> imports = new ArrayList<Symbol>();
int nextClassSerial;
Scope starImportScope;
StarImportScope starImportScope;
Model starImportModel;
}
@ -355,9 +336,8 @@ public class StarImportTest {
void check(Scope scope) {
// First, check all entries in scope are in map
int bogusCount = 0;
for (Scope.Entry se = scope.elems; se != null; se = se.sibling) {
Symbol sym = se.sym;
if (sym.owner != se.scope.owner) {
for (Symbol sym : scope.getSymbols()) {
if (sym.owner != scope.getOrigin(sym).owner) {
if (bogus.contains(sym)) {
bogusCount++;
} else {
@ -380,16 +360,14 @@ public class StarImportTest {
// Second, check all entries in map are in scope
for (Map.Entry<Name,Set<Symbol>> me: map.entrySet()) {
Name name = me.getKey();
Scope.Entry se = scope.lookup(name);
assert (se != null);
if (se.sym == null) {
if (scope.findFirst(name) == null) {
error("check: no entries found for " + name + " in scope");
continue;
}
nextSym:
for (Symbol sym: me.getValue()) {
for (Scope.Entry e = se; e.sym != null; e = e.next()) {
if (sym == e.sym)
for (Symbol s : scope.getSymbolsByName(name)) {
if (sym == s)
continue nextSym;
}
error("check: symbol " + sym + " not found in scope");

View File

@ -1,2 +1,2 @@
T6537020.java:25:16: compiler.err.ref.ambiguous: s, kindname.variable, s, p.T6537020.B, kindname.variable, s, p.T6537020.A
T6537020.java:25:16: compiler.err.ref.ambiguous: s, kindname.variable, s, p.T6537020.A, kindname.variable, s, p.T6537020.B
1 error