Exemplo n.º 1
0
 public void complete(Symbol.VariableSymbol symbol) {
   VariableTree variableTree = (VariableTree) semanticModel.getTree(symbol);
   Resolve.Env env = semanticModel.getEnv(symbol);
   if (variableTree.is(Tree.Kind.ENUM_CONSTANT)) {
     symbol.type = env.enclosingClass().type;
   } else {
     symbol.type = castToTypeIfPossible(resolveType(env, variableTree.type()));
   }
 }
Exemplo n.º 2
0
  /**
   *
   *
   * <pre>
   * public class OutermostClass {
   *   private static class TargetClass {
   *   }
   *   // accessible
   * }
   * class AnotherOutermostClass {
   *   // not accessible
   * }
   * </pre>
   */
  @Test
  public void access_private_class() {
    env.enclosingClass = new JavaSymbol.TypeJavaSymbol(0, "OutermostClass", env.packge);
    JavaSymbol.TypeJavaSymbol targetClassSymbol =
        new JavaSymbol.TypeJavaSymbol(Flags.PRIVATE, "TargetClass", env.enclosingClass);

    assertThat(resolve.isAccessible(env, targetClassSymbol)).isTrue();

    env.enclosingClass = new JavaSymbol.TypeJavaSymbol(0, "AnotherOutermostClass", env.packge);
    assertThat(resolve.isAccessible(env, targetClassSymbol)).isFalse();
  }
Exemplo n.º 3
0
 @Override
 public void visitMemberSelectExpression(MemberSelectExpressionTree tree) {
   scan(tree.expression());
   if (site.kind >= Symbol.ERRONEOUS) {
     return;
   }
   String name = tree.identifier().name();
   if (site.kind == Symbol.PCK) {
     env.packge = (Symbol.PackageSymbol) site;
     site = resolve.findIdentInPackage(env, site, name, Symbol.TYP | Symbol.PCK);
   } else {
     env.enclosingClass = (Symbol.TypeSymbol) site;
     site =
         resolve.findMemberType(env, (Symbol.TypeSymbol) site, name, (Symbol.TypeSymbol) site);
   }
   associateReference(tree.identifier(), site);
 }
Exemplo n.º 4
0
  /**
   *
   *
   * <pre>
   * package p1;
   * class TargetClass {
   * }
   * // accessible
   * package p2;
   * // not accessible
   * </pre>
   */
  @Test
  public void access_package_local_class() {
    JavaSymbol.TypeJavaSymbol targetClassSymbol =
        new JavaSymbol.TypeJavaSymbol(0, "TargetClass", env.packge);

    assertThat(resolve.isAccessible(env, targetClassSymbol)).isTrue();

    env.packge = new JavaSymbol.PackageJavaSymbol("AnotherPackage", null);
    assertThat(resolve.isAccessible(env, targetClassSymbol)).isFalse();
  }
Exemplo n.º 5
0
  private Symbol resolveType(Resolve.Env env, Tree tree) {
    Preconditions.checkArgument(
        tree.is(Tree.Kind.MEMBER_SELECT)
            || tree.is(Tree.Kind.IDENTIFIER)
            || tree.is(Tree.Kind.PARAMETERIZED_TYPE)
            || tree.is(Tree.Kind.ARRAY_TYPE)
            || tree.is(Tree.Kind.UNION_TYPE)
            || tree instanceof PrimitiveTypeTree,
        "Kind of tree unexpected " + ((JavaTree) tree).getKind());
    class FQV extends BaseTreeVisitor {
      private final Resolve.Env env;
      private Symbol site;

      public FQV(Resolve.Env env) {
        this.env = env;
      }

      @Override
      public void visitParameterizedType(ParameterizedTypeTree tree) {
        // Scan only the type, the generic arguments are not yet handled
        scan(tree.type());
      }

      @Override
      public void visitArrayType(ArrayTypeTree tree) {
        super.visitArrayType(tree);
        // TODO handle arrays type (for methods).
      }

      @Override
      public void visitMemberSelectExpression(MemberSelectExpressionTree tree) {
        scan(tree.expression());
        if (site.kind >= Symbol.ERRONEOUS) {
          return;
        }
        String name = tree.identifier().name();
        if (site.kind == Symbol.PCK) {
          env.packge = (Symbol.PackageSymbol) site;
          site = resolve.findIdentInPackage(env, site, name, Symbol.TYP | Symbol.PCK);
        } else {
          env.enclosingClass = (Symbol.TypeSymbol) site;
          site =
              resolve.findMemberType(env, (Symbol.TypeSymbol) site, name, (Symbol.TypeSymbol) site);
        }
        associateReference(tree.identifier(), site);
      }

      @Override
      public void visitIdentifier(IdentifierTree tree) {
        site = resolve.findIdent(env, tree.name(), Symbol.TYP | Symbol.PCK);
        associateReference(tree, site);
      }

      @Override
      public void visitPrimitiveType(PrimitiveTypeTree tree) {
        site =
            resolve.findIdent(
                semanticModel.getEnv(tree),
                ((JavaTree) tree).getAstNode().getLastChild().getTokenValue(),
                Symbol.TYP);
      }
    }
    FQV fqv = new FQV(env.dup());
    tree.accept(fqv);
    return fqv.site;
  }
Exemplo n.º 6
0
 @Before
 public void setUp() {
   env = new Resolve.Env();
   env.packge = new JavaSymbol.PackageJavaSymbol(null, null);
 }