예제 #1
0
  @Test
  public void testArtifactScopeWithImportStatements() {
    final CommonResolvingFilter<EntitySymbol> classResolver =
        new CommonResolvingFilter<>(EntitySymbol.KIND);

    final MutableScope globalScope =
        new GlobalScope(new ModelPath(), new LinkedHashSet<>(), new ResolverConfiguration());
    globalScope.addResolver(classResolver);

    ArtifactScope artifactScope1 =
        new ArtifactScope(
            Optional.of(globalScope),
            "p",
            Arrays.asList(new ImportStatement("q", true), new ImportStatement("r.B", false)));
    artifactScope1.addResolver(classResolver);
    EntitySymbol classA = new EntitySymbol("A");
    artifactScope1.add(classA);

    MutableScope classAScope = classA.getMutableSpannedScope();
    classAScope.addResolver(classResolver);

    ArtifactScope artifactScope2 =
        new ArtifactScope(Optional.of(globalScope), "q", new ArrayList<>());
    artifactScope2.addResolver(classResolver);
    EntitySymbol classQB = new EntitySymbol("B");
    classQB.getMutableSpannedScope().addResolver(classResolver);
    artifactScope2.add(classQB);

    // resolve by qualified name
    assertSame(classQB, classAScope.resolve("q.B", EntitySymbol.KIND).get());

    // q.B is imported, hence, resolving by simple succeeds.
    assertSame(classQB, classAScope.resolve("B", EntitySymbol.KIND).orElse(null));

    ArtifactScope artifactScope3 =
        new ArtifactScope(Optional.of(globalScope), "r", new ArrayList<>());
    artifactScope3.addResolver(classResolver);
    EntitySymbol classRB = new EntitySymbol("B");
    classRB.getMutableSpannedScope().addResolver(classResolver);
    artifactScope3.add(classRB);

    // Now, besides q.B the symbol r.B is defined in the global scope
    // (resp. in the artifact scope scopes)

    // resolve by qualified name
    assertSame(classQB, classAScope.resolve("q.B", EntitySymbol.KIND).get());

    // resolve by simple name
    try {
      classAScope.resolve("B", EntitySymbol.KIND);
      fail();
    } catch (ResolvedSeveralEntriesException e) {
      assertEquals(2, e.getSymbols().size());
      assertTrue(e.getSymbols().contains(classQB));
      assertTrue(e.getSymbols().contains(classRB));
    }
  }
예제 #2
0
  @Test
  public void testLocalScopeDoesNotShadowEnclosingScope() {
    CommonScope enclosingScope = new CommonScope(false);
    CommonScope scope = new CommonScope(Optional.of(enclosingScope), false);

    JTypeReference intReference = new CommonJTypeReference<>("int", JTypeSymbol.KIND, scope);
    JTypeReference stringReference = new CommonJTypeReference<>("String", JTypeSymbol.KIND, scope);

    PropertySymbol v1 = new PropertySymbol("var", intReference);
    enclosingScope.add(v1);
    PropertySymbol x = new PropertySymbol("x", intReference);
    enclosingScope.add(x);

    PropertySymbol v2 = new PropertySymbol("var", stringReference);
    scope.add(v2);
    PropertySymbol y = new PropertySymbol("y", stringReference);
    scope.add(y);

    ResolvingFilter<PropertySymbol> variableResolvingFilter =
        CommonResolvingFilter.create(PropertySymbol.KIND);
    enclosingScope.addResolver(variableResolvingFilter);
    scope.addResolver(variableResolvingFilter);

    assertSame(v1, enclosingScope.resolve("var", PropertySymbol.KIND).get());
    assertSame(x, enclosingScope.resolve("x", PropertySymbol.KIND).get());
    assertSame(x, scope.resolve("x", PropertySymbol.KIND).get());
    assertSame(y, scope.resolve("y", PropertySymbol.KIND).get());

    try {
      scope.resolve("var", PropertySymbol.KIND);
      fail();
    } catch (ResolvedSeveralEntriesException e) {
      assertEquals(2, e.getSymbols().size());
      assertTrue(e.getSymbols().contains(v2));
      assertTrue(e.getSymbols().contains(v1));
    }
  }