langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/code/Scope.java
changeset 32059 ea04f56aeacd
parent 31506 4e07f827a794
child 32454 b0ac04e0fefe
equal deleted inserted replaced
31945:eeea9adfd1e3 32059:ea04f56aeacd
    25 
    25 
    26 package com.sun.tools.javac.code;
    26 package com.sun.tools.javac.code;
    27 
    27 
    28 import com.sun.tools.javac.code.Kinds.Kind;
    28 import com.sun.tools.javac.code.Kinds.Kind;
    29 import java.util.*;
    29 import java.util.*;
    30 
    30 import java.util.function.BiConsumer;
       
    31 import java.util.stream.Stream;
       
    32 import java.util.stream.StreamSupport;
       
    33 
       
    34 import com.sun.tools.javac.code.Symbol.CompletionFailure;
    31 import com.sun.tools.javac.code.Symbol.TypeSymbol;
    35 import com.sun.tools.javac.code.Symbol.TypeSymbol;
    32 import com.sun.tools.javac.tree.JCTree.JCImport;
    36 import com.sun.tools.javac.tree.JCTree.JCImport;
    33 import com.sun.tools.javac.util.*;
    37 import com.sun.tools.javac.util.*;
    34 import com.sun.tools.javac.util.List;
    38 import com.sun.tools.javac.util.List;
    35 
    39 
    36 import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
    40 import static com.sun.tools.javac.code.Scope.LookupKind.NON_RECURSIVE;
    37 import static com.sun.tools.javac.code.Scope.LookupKind.RECURSIVE;
    41 import static com.sun.tools.javac.code.Scope.LookupKind.RECURSIVE;
    38 import java.util.stream.Stream;
       
    39 import java.util.stream.StreamSupport;
       
    40 
    42 
    41 /** A scope represents an area of visibility in a Java program. The
    43 /** A scope represents an area of visibility in a Java program. The
    42  *  Scope class is a container for symbols which provides
    44  *  Scope class is a container for symbols which provides
    43  *  efficient access to symbols given their names. Scopes are implemented
    45  *  efficient access to symbols given their names. Scopes are implemented
    44  *  as hash tables with "open addressing" and "double hashing".
    46  *  as hash tables with "open addressing" and "double hashing".
   719         public NamedImportScope(Symbol owner, Scope currentFileScope) {
   721         public NamedImportScope(Symbol owner, Scope currentFileScope) {
   720             super(owner);
   722             super(owner);
   721             prependSubScope(currentFileScope);
   723             prependSubScope(currentFileScope);
   722         }
   724         }
   723 
   725 
   724         public Scope importByName(Types types, Scope origin, Name name, ImportFilter filter) {
   726         public Scope importByName(Types types, Scope origin, Name name, ImportFilter filter, JCImport imp, BiConsumer<JCImport, CompletionFailure> cfHandler) {
   725             return appendScope(new FilterImportScope(types, origin, name, filter, true));
   727             return appendScope(new FilterImportScope(types, origin, name, filter, imp, cfHandler));
   726         }
   728         }
   727 
   729 
   728         public Scope importType(Scope delegate, Scope origin, Symbol sym) {
   730         public Scope importType(Scope delegate, Scope origin, Symbol sym) {
   729             return appendScope(new SingleEntryScope(delegate.owner, sym, origin));
   731             return appendScope(new SingleEntryScope(delegate.owner, sym, origin));
   730         }
   732         }
   784             super(owner);
   786             super(owner);
   785         }
   787         }
   786 
   788 
   787         public void importAll(Types types, Scope origin,
   789         public void importAll(Types types, Scope origin,
   788                               ImportFilter filter,
   790                               ImportFilter filter,
   789                               boolean staticImport) {
   791                               JCImport imp,
       
   792                               BiConsumer<JCImport, CompletionFailure> cfHandler) {
   790             for (Scope existing : subScopes) {
   793             for (Scope existing : subScopes) {
   791                 Assert.check(existing instanceof FilterImportScope);
   794                 Assert.check(existing instanceof FilterImportScope);
   792                 FilterImportScope fis = (FilterImportScope) existing;
   795                 FilterImportScope fis = (FilterImportScope) existing;
   793                 if (fis.origin == origin && fis.filter == filter &&
   796                 if (fis.origin == origin && fis.filter == filter &&
   794                     fis.staticImport == staticImport)
   797                     fis.imp.staticImport == imp.staticImport)
   795                     return ; //avoid entering the same scope twice
   798                     return ; //avoid entering the same scope twice
   796             }
   799             }
   797             prependSubScope(new FilterImportScope(types, origin, null, filter, staticImport));
   800             prependSubScope(new FilterImportScope(types, origin, null, filter, imp, cfHandler));
   798         }
   801         }
   799 
   802 
   800         public boolean isFilled() {
   803         public boolean isFilled() {
   801             return subScopes.nonEmpty();
   804             return subScopes.nonEmpty();
   802         }
   805         }
   811 
   814 
   812         private final Types types;
   815         private final Types types;
   813         private final Scope origin;
   816         private final Scope origin;
   814         private final Name  filterName;
   817         private final Name  filterName;
   815         private final ImportFilter filter;
   818         private final ImportFilter filter;
   816         private final boolean staticImport;
   819         private final JCImport imp;
       
   820         private final BiConsumer<JCImport, CompletionFailure> cfHandler;
   817 
   821 
   818         public FilterImportScope(Types types,
   822         public FilterImportScope(Types types,
   819                                  Scope origin,
   823                                  Scope origin,
   820                                  Name  filterName,
   824                                  Name  filterName,
   821                                  ImportFilter filter,
   825                                  ImportFilter filter,
   822                                  boolean staticImport) {
   826                                  JCImport imp,
       
   827                                  BiConsumer<JCImport, CompletionFailure> cfHandler) {
   823             super(origin.owner);
   828             super(origin.owner);
   824             this.types = types;
   829             this.types = types;
   825             this.origin = origin;
   830             this.origin = origin;
   826             this.filterName = filterName;
   831             this.filterName = filterName;
   827             this.filter = filter;
   832             this.filter = filter;
   828             this.staticImport = staticImport;
   833             this.imp = imp;
       
   834             this.cfHandler = cfHandler;
   829         }
   835         }
   830 
   836 
   831         @Override
   837         @Override
   832         public Iterable<Symbol> getSymbols(final Filter<Symbol> sf, final LookupKind lookupKind) {
   838         public Iterable<Symbol> getSymbols(final Filter<Symbol> sf, final LookupKind lookupKind) {
   833             if (filterName != null)
   839             if (filterName != null)
   834                 return getSymbolsByName(filterName, sf, lookupKind);
   840                 return getSymbolsByName(filterName, sf, lookupKind);
   835             SymbolImporter si = new SymbolImporter(staticImport) {
   841             try {
   836                 @Override
   842                 SymbolImporter si = new SymbolImporter(imp.staticImport) {
   837                 Iterable<Symbol> doLookup(TypeSymbol tsym) {
   843                     @Override
   838                     return tsym.members().getSymbols(sf, lookupKind);
   844                     Iterable<Symbol> doLookup(TypeSymbol tsym) {
   839                 }
   845                         return tsym.members().getSymbols(sf, lookupKind);
   840             };
   846                     }
   841             return si.importFrom((TypeSymbol) origin.owner) :: iterator;
   847                 };
       
   848                 return si.importFrom((TypeSymbol) origin.owner) :: iterator;
       
   849             } catch (CompletionFailure cf) {
       
   850                 cfHandler.accept(imp, cf);
       
   851                 return Collections.emptyList();
       
   852             }
   842         }
   853         }
   843 
   854 
   844         @Override
   855         @Override
   845         public Iterable<Symbol> getSymbolsByName(final Name name,
   856         public Iterable<Symbol> getSymbolsByName(final Name name,
   846                                                  final Filter<Symbol> sf,
   857                                                  final Filter<Symbol> sf,
   847                                                  final LookupKind lookupKind) {
   858                                                  final LookupKind lookupKind) {
   848             if (filterName != null && filterName != name)
   859             if (filterName != null && filterName != name)
   849                 return Collections.emptyList();
   860                 return Collections.emptyList();
   850             SymbolImporter si = new SymbolImporter(staticImport) {
   861             try {
   851                 @Override
   862                 SymbolImporter si = new SymbolImporter(imp.staticImport) {
   852                 Iterable<Symbol> doLookup(TypeSymbol tsym) {
   863                     @Override
   853                     return tsym.members().getSymbolsByName(name, sf, lookupKind);
   864                     Iterable<Symbol> doLookup(TypeSymbol tsym) {
   854                 }
   865                         return tsym.members().getSymbolsByName(name, sf, lookupKind);
   855             };
   866                     }
   856             return si.importFrom((TypeSymbol) origin.owner) :: iterator;
   867                 };
       
   868                 return si.importFrom((TypeSymbol) origin.owner) :: iterator;
       
   869             } catch (CompletionFailure cf) {
       
   870                 cfHandler.accept(imp, cf);
       
   871                 return Collections.emptyList();
       
   872             }
   857         }
   873         }
   858 
   874 
   859         @Override
   875         @Override
   860         public Scope getOrigin(Symbol byName) {
   876         public Scope getOrigin(Symbol byName) {
   861             return origin;
   877             return origin;
   862         }
   878         }
   863 
   879 
   864         @Override
   880         @Override
   865         public boolean isStaticallyImported(Symbol byName) {
   881         public boolean isStaticallyImported(Symbol byName) {
   866             return staticImport;
   882             return imp.staticImport;
   867         }
   883         }
   868 
   884 
   869         abstract class SymbolImporter {
   885         abstract class SymbolImporter {
   870             Set<Symbol> processed = new HashSet<>();
   886             Set<Symbol> processed = new HashSet<>();
   871             List<Iterable<Symbol>> delegates = List.nil();
   887             List<Iterable<Symbol>> delegates = List.nil();