Resolve.Env classEnv = env.dup(); classEnv.outer = env; classEnv.enclosingClass = symbol;
/** * Is class accessible in given environment? */ @VisibleForTesting boolean isAccessible(Env env, JavaSymbol.TypeJavaSymbol c) { final boolean result; switch (c.flags() & Flags.ACCESS_FLAGS) { case Flags.PRIVATE: result = (env.enclosingClass().outermostClass() == c.owner().outermostClass()); break; case 0: result = (env.packge() == c.packge()); break; case Flags.PUBLIC: result = true; break; case Flags.PROTECTED: result = (env.packge() == c.packge()) || isInnerSubClass(env.enclosingClass(), c.owner()); break; default: throw new IllegalStateException(); } // TODO check accessibility of enclosing type: isAccessible(env, c.type.getEnclosingType()) return result; }
@Override public void visitIdentifier(IdentifierTree tree) { if (JavaPunctuator.STAR.getValue().equals(tree.name())) { //star import : we save the current symbol if (isStatic) { env.staticStarImports().enter(currentSymbol); } else { env.starImports().enter(currentSymbol); } //we set current symbol to not found to do not put it in named import scope. currentSymbol = new Resolve.JavaSymbolNotFound(); } else { if (currentSymbol.kind == JavaSymbol.PCK) { currentSymbol = resolve.findIdentInPackage(currentSymbol, tree.name(), JavaSymbol.PCK | JavaSymbol.TYP); resolved = Collections.emptyList(); } else if (currentSymbol.kind == JavaSymbol.TYP) { resolved = ((JavaSymbol.TypeJavaSymbol) currentSymbol).members().lookup(tree.name()); currentSymbol = resolve.findIdentInType(env, (JavaSymbol.TypeJavaSymbol) currentSymbol, tree.name(), JavaSymbol.TYP | JavaSymbol.VAR); } else { //Site symbol is not found so we won't be able to resolve the import. currentSymbol = new Resolve.JavaSymbolNotFound(); resolved = Collections.emptyList(); } } }
Resolve.Env classEnv = env.dup(); classEnv.outer = env; classEnv.enclosingClass = symbol;
for (Env env1 = env; env1 != null; env1 = env1.outer()) { for (JavaSymbol symbol : env1.scope().lookup(name)) { if (symbol.kind == JavaSymbol.TYP) { return symbol; JavaSymbol symbol = findMemberType(env1, env1.enclosingClass(), name, env1.enclosingClass()); if (symbol.kind < JavaSymbol.ERRONEOUS) { for (JavaSymbol symbol : env.namedImports().lookup(name)) { if (symbol.kind == JavaSymbol.TYP) { return symbol; JavaSymbol sym = findIdentInPackage(env.packge(), name, JavaSymbol.TYP); if (sym.kind < bestSoFar.kind) { return sym; for (JavaSymbol symbol : env.starImports().lookup(name)) { if (symbol.kind == JavaSymbol.TYP) { return symbol;
Resolve.Env classEnv = env.dup(); classEnv.outer = env; classEnv.enclosingClass = symbol;
Resolve.Env classEnv = env.dup(); classEnv.outer = env; classEnv.enclosingClass = symbol;
Resolve.Env methodEnv = env.dup(); methodEnv.scope = symbol.parameters; methodEnv.outer = env;
Resolve.Env methodEnv = env.dup(); methodEnv.scope = symbol.parameters; methodEnv.outer = env;
Resolve.Env methodEnv = env.dup(); methodEnv.scope = symbol.parameters; methodEnv.outer = env;
Resolve.Env methodEnv = env.dup(); methodEnv.scope = symbol.parameters; methodEnv.outer = env;
while (env1.outer() != null) { Resolution sym = new Resolution(); for (JavaSymbol symbol : env1.scope().lookup(name)) { if (symbol.kind == JavaSymbol.VAR) { sym.symbol = symbol; sym = findField(env1, env1.enclosingClass(), name, env1.enclosingClass()); bestSoFar = sym; env1 = env1.outer();
/** * Finds method matching given name and types of arguments. */ public Resolution findMethod(Env env, String name, List<JavaType> argTypes, List<JavaType> typeParamTypes) { Resolution bestSoFar = unresolved(); Env env1 = env; while (env1.outer() != null) { Resolution res = findMethod(env1, env1.enclosingClass().getType(), name, argTypes, typeParamTypes); if (res.symbol.kind < JavaSymbol.ERRONEOUS) { // symbol exists return res; } else if (res.symbol.kind < bestSoFar.symbol.kind) { bestSoFar = res; } env1 = env1.outer; } JavaSymbol sym = findInStaticImport(env, name, JavaSymbol.MTH); if (sym.kind < JavaSymbol.ERRONEOUS) { // symbol exists return Resolution.resolution(sym); } else if (sym.kind < bestSoFar.symbol.kind) { bestSoFar = Resolution.resolution(sym); } return bestSoFar; }
/** * Is symbol accessible as a member of given class in given environment? * <p/> * Symbol is accessible only if not overridden by another symbol. If overridden, then strictly speaking it is not a member. */ private boolean isAccessible(Env env, JavaSymbol.TypeJavaSymbol site, JavaSymbol symbol) { switch (symbol.flags() & Flags.ACCESS_FLAGS) { case Flags.PRIVATE: //if enclosing class is null, we are checking accessibility for imports so we return false. // no check of overriding, because private members cannot be overridden return env.enclosingClass != null && (env.enclosingClass().outermostClass() == symbol.owner().outermostClass()) && isInheritedIn(symbol, site); case 0: return (env.packge() == symbol.packge()) && isAccessible(env, site) && isInheritedIn(symbol, site) && notOverriddenIn(site, symbol); case Flags.PUBLIC: return isAccessible(env, site) && notOverriddenIn(site, symbol); case Flags.PROTECTED: return ((env.packge() == symbol.packge()) || isProtectedAccessible(symbol, env.enclosingClass, site)) && isAccessible(env, site) && notOverriddenIn(site, symbol); default: throw new IllegalStateException(); } }
@Override public void visitCompilationUnit(CompilationUnitTree tree) { JavaSymbol.PackageJavaSymbol compilationUnitPackage = symbols.defaultPackage; PackageDeclarationTree packageDeclaration = tree.packageDeclaration(); if (packageDeclaration != null) { ExpressionTree packageName = packageDeclaration.packageName(); PackageResolverVisitor packageResolver = new PackageResolverVisitor(); packageName.accept(packageResolver); compilationUnitPackage = (JavaSymbol.PackageJavaSymbol) resolve.findIdentInPackage(compilationUnitPackage, packageResolver.packageName, JavaSymbol.PCK); semanticModel.associateSymbol(packageName, compilationUnitPackage); } compilationUnitPackage.members = new Scope(compilationUnitPackage); env = new Resolve.Env(); env.packge = compilationUnitPackage; env.scope = compilationUnitPackage.members; env.namedImports = new Scope(compilationUnitPackage); env.starImports = resolve.createStarImportScope(compilationUnitPackage); env.staticStarImports = resolve.createStaticStarImportScope(compilationUnitPackage); semanticModel.associateEnv(tree, env); super.visitCompilationUnit(tree); restoreEnvironment(tree); resolveImports(tree.imports()); completeSymbols(); }
@Override public void visitCompilationUnit(CompilationUnitTree tree) { JavaSymbol.PackageJavaSymbol compilationUnitPackage = symbols.defaultPackage; PackageDeclarationTree packageDeclaration = tree.packageDeclaration(); if (packageDeclaration != null) { ExpressionTree packageName = packageDeclaration.packageName(); PackageResolverVisitor packageResolver = new PackageResolverVisitor(); packageName.accept(packageResolver); compilationUnitPackage = (JavaSymbol.PackageJavaSymbol) resolve.findIdentInPackage(compilationUnitPackage, packageResolver.packageName, JavaSymbol.PCK); semanticModel.associateSymbol(packageName, compilationUnitPackage); } compilationUnitPackage.members = new Scope(compilationUnitPackage); env = new Resolve.Env(); env.packge = compilationUnitPackage; env.scope = compilationUnitPackage.members; env.namedImports = new Scope.ImportScope(compilationUnitPackage); env.starImports = resolve.createStarImportScope(compilationUnitPackage); env.staticStarImports = resolve.createStaticStarImportScope(compilationUnitPackage); semanticModel.associateEnv(tree, env); scan(tree.types()); restoreEnvironment(tree); resolveImports(tree.imports()); completeSymbols(); }
@Override public void visitCompilationUnit(CompilationUnitTree tree) { JavaSymbol.PackageJavaSymbol compilationUnitPackage = symbols.defaultPackage; ExpressionTree packageName = tree.packageName(); if (packageName != null) { PackageResolverVisitor packageResolver = new PackageResolverVisitor(); packageName.accept(packageResolver); compilationUnitPackage = (JavaSymbol.PackageJavaSymbol) resolve.findIdentInPackage(compilationUnitPackage, packageResolver.packageName, JavaSymbol.PCK); semanticModel.associateSymbol(packageName, compilationUnitPackage); } compilationUnitPackage.members = new Scope(compilationUnitPackage); env = new Resolve.Env(); env.packge = compilationUnitPackage; env.scope = compilationUnitPackage.members; env.namedImports = new Scope(compilationUnitPackage); env.starImports = resolve.createStarImportScope(compilationUnitPackage); env.staticStarImports = resolve.createStaticStarImportScope(compilationUnitPackage); semanticModel.associateEnv(tree, env); super.visitCompilationUnit(tree); restoreEnvironment(tree); resolveImports(tree.imports()); completeSymbols(); }
@Override public void visitCompilationUnit(CompilationUnitTree tree) { JavaSymbol.PackageJavaSymbol compilationUnitPackage = symbols.defaultPackage; PackageDeclarationTree packageDeclaration = tree.packageDeclaration(); if (packageDeclaration != null) { ExpressionTree packageName = packageDeclaration.packageName(); PackageResolverVisitor packageResolver = new PackageResolverVisitor(); packageName.accept(packageResolver); compilationUnitPackage = (JavaSymbol.PackageJavaSymbol) resolve.findIdentInPackage(compilationUnitPackage, packageResolver.packageName, JavaSymbol.PCK); semanticModel.associateSymbol(packageName, compilationUnitPackage); } compilationUnitPackage.members = new Scope(compilationUnitPackage); env = new Resolve.Env(); env.packge = compilationUnitPackage; env.scope = compilationUnitPackage.members; env.namedImports = new Scope.ImportScope(compilationUnitPackage); env.starImports = resolve.createStarImportScope(compilationUnitPackage); env.staticStarImports = resolve.createStaticStarImportScope(compilationUnitPackage); semanticModel.associateEnv(tree, env); scan(tree.types()); restoreEnvironment(tree); resolveImports(tree.imports()); completeSymbols(); }
/** * @param kind subset of {@link JavaSymbol#VAR}, {@link JavaSymbol#MTH} */ private JavaSymbol findInStaticImport(Env env, String name, int kind) { JavaSymbol bestSoFar = symbolNotFound; //imports //Ok because clash of name between type and var/method result in compile error: JLS8 7.5.3 for (JavaSymbol symbol : env.namedImports().lookup(name)) { if ((kind & symbol.kind) != 0) { return symbol; } } for (JavaSymbol symbol : env.staticStarImports().lookup(name)) { if ((kind & symbol.kind) != 0) { return symbol; } } return bestSoFar; }
public Env dup() { Env env = new Env(); env.next = this; env.outer = this.outer; env.packge = this.packge; env.enclosingClass = this.enclosingClass; env.scope = this.scope; env.namedImports = this.namedImports; env.starImports = this.starImports; env.staticStarImports = this.staticStarImports; return env; }