public void test_argumentResolution_setter_static_propertyAccess() throws Exception {
   Source source =
       addSource(
           createSource( //
               "main() {",
               "  A a = new A();",
               "  a.b.sss = 0;",
               "}",
               "class A {",
               "  B b = new B();",
               "}",
               "class B {",
               "  set sss(x) {}",
               "}"));
   LibraryElement library = resolve(source);
   CompilationUnitElement unit = library.getDefiningCompilationUnit();
   // find "a.b.sss = 0"
   AssignmentExpression assignment;
   {
     FunctionElement mainElement = unit.getFunctions()[0];
     FunctionBody mainBody = mainElement.getNode().getFunctionExpression().getBody();
     Statement statement = ((BlockFunctionBody) mainBody).getBlock().getStatements().get(1);
     ExpressionStatement expressionStatement = (ExpressionStatement) statement;
     assignment = (AssignmentExpression) expressionStatement.getExpression();
   }
   // get parameter
   Expression rhs = assignment.getRightHandSide();
   ParameterElement parameter = rhs.getStaticParameterElement();
   assertNotNull(parameter);
   assertEquals("x", parameter.getDisplayName());
   // validate
   ClassElement classB = unit.getTypes()[1];
   PropertyAccessorElement setter = classB.getAccessors()[0];
   assertSame(parameter, setter.getParameters()[0]);
 }
  /**
   * Opens the editor currently associated with the given element (DartElement, IFile, IStorage...)
   *
   * @return an open editor or <code>null</code> if an external editor was opened
   * @throws PartInitException if the editor could not be opened or the input element is not valid
   */
  public static IEditorPart openInEditor(Object inputElement, boolean activate)
      throws DartModelException, PartInitException {

    if (inputElement instanceof IFile) {
      return openInEditor((IFile) inputElement, activate);
    }
    DartX.todo();
    if (inputElement instanceof ImportElement) {
      inputElement = ((ImportElement) inputElement).getImportedLibrary();
    }
    if (inputElement instanceof Element) {
      CompilationUnitElement cu = getCompilationUnit((Element) inputElement);
      IWorkbenchPage page = DartToolsPlugin.getActivePage();
      if (cu != null && page != null) {
        IEditorPart editor = page.getActiveEditor();
        if (editor != null) {
          Element editorCU = EditorUtility.getEditorInputDartElement2(editor, false);
          if (cu.equals(editorCU)) {
            if (activate && page.getActivePart() != editor) {
              page.activate(editor);
            }
            return editor;
          }
        }
      }
    }

    IEditorInput input = getEditorInput(inputElement);

    if (input == null) {
      return null;
    }

    return openInEditor(input, getEditorID(input), activate);
  }
 public void test_metadata_typedef() throws Exception {
   Source source =
       addSource(
           createSource( //
               "const A = null;", "@A typedef F<A>();"));
   LibraryElement library = resolve(source);
   assertNotNull(library);
   CompilationUnitElement unitElement = library.getDefiningCompilationUnit();
   assertNotNull(unitElement);
   FunctionTypeAliasElement[] aliases = unitElement.getFunctionTypeAliases();
   assertLength(1, aliases);
   ElementAnnotation[] annotations = aliases[0].getMetadata();
   assertLength(1, annotations);
   assertNoErrors(source);
   verify(source);
   CompilationUnit unit = resolveCompilationUnit(source, library);
   NodeList<CompilationUnitMember> declarations = unit.getDeclarations();
   assertSizeOfList(2, declarations);
   Element expectedElement =
       ((TopLevelVariableDeclaration) declarations.get(0))
           .getVariables()
           .getVariables()
           .get(0)
           .getName()
           .getStaticElement();
   assertInstanceOf(PropertyInducingElement.class, expectedElement);
   expectedElement = ((PropertyInducingElement) expectedElement).getGetter();
   Element actualElement =
       ((FunctionTypeAlias) declarations.get(1)).getMetadata().get(0).getName().getStaticElement();
   assertSame(expectedElement, actualElement);
 }
 private void validate(int scriptIndex, EmbeddedHtmlScriptElementImpl script) {
   LibraryElement library = script.getScriptLibrary();
   assertNotNull("script " + scriptIndex, library);
   assertSame("script " + scriptIndex, context, script.getContext());
   CompilationUnitElement unit = library.getDefiningCompilationUnit();
   assertNotNull("script " + scriptIndex, unit);
   TopLevelVariableElement[] variables = unit.getTopLevelVariables();
   assertLength(expectedVariables.length, variables);
   for (int index = 0; index < variables.length; index++) {
     expectedVariables[index].validate(scriptIndex, variables[index]);
   }
   assertSame("script " + scriptIndex, script, library.getEnclosingElement());
 }
 public void test_isValidMixin_valid() throws Exception {
   Source source =
       addSource(
           createSource( //
               "class A {}"));
   LibraryElement library = resolve(source);
   assertNotNull(library);
   CompilationUnitElement unit = library.getDefiningCompilationUnit();
   assertNotNull(unit);
   ClassElement[] classes = unit.getTypes();
   assertLength(1, classes);
   assertTrue(classes[0].isValidMixin());
   assertNoErrors(source);
   verify(source);
 }
 @Override
 protected void setUp() throws Exception {
   super.setUp();
   when(contextA.toString()).thenReturn("contextA");
   when(contextB.toString()).thenReturn("contextB");
   when(contextC.toString()).thenReturn("contextC");
   when(sourceA.toString()).thenReturn("sourceA");
   when(sourceB.toString()).thenReturn("sourceB");
   when(sourceC.toString()).thenReturn("sourceC");
   when(sourceD.toString()).thenReturn("sourceD");
   when(elementA.toString()).thenReturn("elementA");
   when(elementB.toString()).thenReturn("elementB");
   when(elementC.toString()).thenReturn("elementC");
   when(elementD.toString()).thenReturn("elementD");
   when(elementA.getContext()).thenReturn(contextA);
   when(elementB.getContext()).thenReturn(contextA);
   when(elementC.getContext()).thenReturn(contextA);
   when(elementD.getContext()).thenReturn(contextA);
   when(elementA.getLocation()).thenReturn(elementLocationA);
   when(elementB.getLocation()).thenReturn(elementLocationB);
   when(elementC.getLocation()).thenReturn(elementLocationC);
   when(elementD.getLocation()).thenReturn(elementLocationD);
   when(elementA.getEnclosingElement()).thenReturn(unitElementA);
   when(elementB.getEnclosingElement()).thenReturn(unitElementB);
   when(elementC.getEnclosingElement()).thenReturn(unitElementC);
   when(elementD.getEnclosingElement()).thenReturn(unitElementD);
   when(elementA.getSource()).thenReturn(sourceA);
   when(elementB.getSource()).thenReturn(sourceB);
   when(elementC.getSource()).thenReturn(sourceC);
   when(elementD.getSource()).thenReturn(sourceD);
   when(elementA.getLibrary()).thenReturn(libraryElement);
   when(elementB.getLibrary()).thenReturn(libraryElement);
   when(elementC.getLibrary()).thenReturn(libraryElement);
   when(elementD.getLibrary()).thenReturn(libraryElement);
   when(unitElementA.getSource()).thenReturn(sourceA);
   when(unitElementB.getSource()).thenReturn(sourceB);
   when(unitElementC.getSource()).thenReturn(sourceC);
   when(unitElementD.getSource()).thenReturn(sourceD);
   when(unitElementA.getLibrary()).thenReturn(libraryElement);
   when(unitElementB.getLibrary()).thenReturn(libraryElement);
   when(unitElementC.getLibrary()).thenReturn(libraryElement);
   when(unitElementD.getLibrary()).thenReturn(libraryElement);
   // library
   when(librarySource.toString()).thenReturn("libSource");
   when(libraryUnitElement.getSource()).thenReturn(librarySource);
   when(libraryElement.getSource()).thenReturn(librarySource);
   when(libraryElement.getDefiningCompilationUnit()).thenReturn(libraryUnitElement);
   // by default index all units
   store.aboutToIndex(contextA, unitElementA);
   store.aboutToIndex(contextA, unitElementB);
   store.aboutToIndex(contextA, unitElementC);
   store.aboutToIndex(contextA, unitElementD);
 }
 public void test_hasReferenceToSuper() throws Exception {
   Source source =
       addSource(
           createSource( //
               "class A {}", "class B {toString() => super.toString();}"));
   LibraryElement library = resolve(source);
   assertNotNull(library);
   CompilationUnitElement unit = library.getDefiningCompilationUnit();
   assertNotNull(unit);
   ClassElement[] classes = unit.getTypes();
   assertLength(2, classes);
   assertFalse(classes[0].hasReferenceToSuper());
   assertTrue(classes[1].hasReferenceToSuper());
   assertNoErrors(source);
   verify(source);
 }
 public void test_metadata_function() throws Exception {
   Source source =
       addSource(
           createSource( //
               "const A = null;", "@A f() {}"));
   LibraryElement library = resolve(source);
   assertNotNull(library);
   CompilationUnitElement unit = library.getDefiningCompilationUnit();
   assertNotNull(unit);
   FunctionElement[] functions = unit.getFunctions();
   assertLength(1, functions);
   ElementAnnotation[] annotations = functions[0].getMetadata();
   assertLength(1, annotations);
   assertNoErrors(source);
   verify(source);
 }
  private static IEditorInput getEditorInput(Element element) {
    CompilationUnitElement cu = getCompilationUnit(element);

    if (cu == null) {
      return null;
    }

    IResource resource = DartCore.getProjectManager().getResource(cu.getSource());

    if (resource instanceof IFile) {
      return new FileEditorInput((IFile) resource);
    }

    Source source = cu.getSource();
    return getEditorInput(source);
  }
 public void test_metadata_method() throws Exception {
   Source source =
       addSource(
           createSource( //
               "const A = null;", "class C {", "  @A void m() {}", "}"));
   LibraryElement library = resolve(source);
   assertNotNull(library);
   CompilationUnitElement unit = library.getDefiningCompilationUnit();
   assertNotNull(unit);
   ClassElement[] classes = unit.getTypes();
   assertLength(1, classes);
   MethodElement method = classes[0].getMethods()[0];
   ElementAnnotation[] annotations = method.getMetadata();
   assertLength(1, annotations);
   assertNoErrors(source);
   verify(source);
 }
 @Override
 protected void setUp() throws Exception {
   super.setUp();
   when(store.aboutToIndexHtml(context, htmlElement)).thenReturn(true);
   when(unit.getElement()).thenReturn(htmlElement);
   when(htmlElement.getSource()).thenReturn(unitSource);
   when(htmlElement.getAngularCompilationUnit()).thenReturn(unitElement);
   when(unitElement.getSource()).thenReturn(unitSource);
   operation = new IndexHtmlUnitOperation(store, context, unit);
 }
 public void test_getRelationships_twoContexts_oneSource() throws Exception {
   when(unitElementB.getSource()).thenReturn(sourceB);
   when(unitElementC.getSource()).thenReturn(sourceB);
   when(elementA.getContext()).thenReturn(contextA);
   when(elementB.getContext()).thenReturn(contextB);
   Location locationA = mockLocation(elementA);
   Location locationB = mockLocation(elementB);
   store.recordRelationship(elementA, relationship, locationA);
   store.recordRelationship(elementB, relationship, locationB);
   // "elementA"
   {
     Location[] locations = store.getRelationships(elementA, relationship);
     assertLocations(locations, locationA);
   }
   // "elementB"
   {
     Location[] locations = store.getRelationships(elementB, relationship);
     assertLocations(locations, locationB);
   }
 }
 public void test_metadata_simpleParameter() throws Exception {
   Source source =
       addSource(
           createSource( //
               "const A = null;", "f(@A p1, @A int p2) {}"));
   LibraryElement library = resolve(source);
   assertNotNull(library);
   CompilationUnitElement unit = library.getDefiningCompilationUnit();
   assertNotNull(unit);
   FunctionElement[] functions = unit.getFunctions();
   assertLength(1, functions);
   ParameterElement[] parameters = functions[0].getParameters();
   assertLength(2, parameters);
   ElementAnnotation[] annotations1 = parameters[0].getMetadata();
   assertLength(1, annotations1);
   ElementAnnotation[] annotations2 = parameters[1].getMetadata();
   assertLength(1, annotations2);
   assertNoErrors(source);
   verify(source);
 }
 public void test_metadata_fieldFormalParameter() throws Exception {
   Source source =
       addSource(
           createSource( //
               "const A = null;", "class C {", "  int f;", "  C(@A this.f);", "}"));
   LibraryElement library = resolve(source);
   assertNotNull(library);
   CompilationUnitElement unit = library.getDefiningCompilationUnit();
   assertNotNull(unit);
   ClassElement[] classes = unit.getTypes();
   assertLength(1, classes);
   ConstructorElement[] constructors = classes[0].getConstructors();
   assertLength(1, constructors);
   ParameterElement[] parameters = constructors[0].getParameters();
   assertLength(1, parameters);
   ElementAnnotation[] annotations = parameters[0].getMetadata();
   assertLength(1, annotations);
   assertNoErrors(source);
   verify(source);
 }
 /**
  * Add all of the non-synthetic getters and setters defined in the given compilation unit that
  * have no corresponding accessor to one of the given collections.
  *
  * @param getters the map to which getters are to be added
  * @param setters the list to which setters are to be added
  * @param unit the compilation unit defining the accessors that are potentially being added
  */
 private void collectAccessors(
     HashMap<String, PropertyAccessorElement> getters,
     ArrayList<PropertyAccessorElement> setters,
     CompilationUnitElement unit) {
   for (PropertyAccessorElement accessor : unit.getAccessors()) {
     if (accessor.isGetter()) {
       if (!accessor.isSynthetic() && accessor.getCorrespondingSetter() == null) {
         getters.put(accessor.getDisplayName(), accessor);
       }
     } else {
       if (!accessor.isSynthetic() && accessor.getCorrespondingGetter() == null) {
         setters.add(accessor);
       }
     }
   }
 }
Esempio n. 16
0
 private void assertTypes(CompilationUnitElement unit, String... typeNames) {
   assertNotNull(unit);
   ClassElement[] types = unit.getTypes();
   assertLength(typeNames.length, types);
   for (ClassElement type : types) {
     assertNotNull(type);
     String actualTypeName = type.getName();
     boolean wasExpected = false;
     for (String expectedTypeName : typeNames) {
       if (expectedTypeName.equals(actualTypeName)) {
         wasExpected = true;
       }
     }
     if (!wasExpected) {
       fail("Found unexpected type " + actualTypeName);
     }
   }
 }
 public void test_removeSource_withRelationship_twoContexts_oneSource() throws Exception {
   when(elementB.getContext()).thenReturn(contextB);
   when(elementC.getContext()).thenReturn(contextC);
   when(elementC.getSource()).thenReturn(sourceB);
   when(unitElementC.getSource()).thenReturn(sourceB);
   // configure B and C
   Location locationB = mockLocation(elementB);
   Location locationC = mockLocation(elementC);
   // record: [B -> A] and [C -> A]
   {
     store.aboutToIndex(contextB, unitElementB);
     store.aboutToIndex(contextC, unitElementC);
     store.recordRelationship(elementA, relationship, locationB);
     store.recordRelationship(elementA, relationship, locationC);
     assertEquals(2, store.internalGetLocationCount());
     assertEquals(1, store.internalGetLocationCount(contextB));
     assertEquals(1, store.internalGetLocationCount(contextC));
     // we get locations from all contexts
     Location[] locations = store.getRelationships(elementA, relationship);
     assertLocations(locations, locationB, locationC);
   }
   // remove "B" in B, 1 relation and 1 location left
   store.removeSource(contextB, sourceB);
   assertEquals(1, store.internalGetLocationCount());
   assertEquals(0, store.internalGetLocationCount(contextB));
   assertEquals(1, store.internalGetLocationCount(contextC));
   {
     Location[] locations = store.getRelationships(elementA, relationship);
     assertLocations(locations, locationC);
   }
   // now remove "B" in C, empty
   store.removeSource(contextC, sourceB);
   assertEquals(0, store.internalGetLocationCount());
   assertEquals(0, store.internalGetLocationCount(contextB));
   assertEquals(0, store.internalGetLocationCount(contextC));
   {
     Location[] locations = store.getRelationships(elementA, relationship);
     assertThat(locations).isEmpty();
   }
 }
Esempio n. 18
0
  public void test_empty() throws Exception {
    Source librarySource = addSource("/lib.dart", "library lib;");

    LibraryElement element = buildLibrary(librarySource);
    assertNotNull(element);
    assertEquals("lib", element.getName());
    assertNull(element.getEntryPoint());
    assertLength(0, element.getImportedLibraries());
    assertLength(0, element.getImports());
    assertNull(element.getLibrary());
    assertLength(0, element.getPrefixes());
    assertLength(0, element.getParts());

    CompilationUnitElement unit = element.getDefiningCompilationUnit();
    assertNotNull(unit);
    assertEquals("lib.dart", unit.getName());
    assertEquals(element, unit.getLibrary());
    assertLength(0, unit.getAccessors());
    assertLength(0, unit.getFields());
    assertLength(0, unit.getFunctions());
    assertLength(0, unit.getTypeAliases());
    assertLength(0, unit.getTypes());
  }
 public void test_aboutToIndex_sharedSource_inTwoLibraries() throws Exception {
   Source librarySourceA = mock(Source.class);
   Source librarySourceB = mock(Source.class);
   LibraryElement libraryA = mock(LibraryElement.class);
   LibraryElement libraryB = mock(LibraryElement.class);
   CompilationUnitElement libraryAUnit = mock(CompilationUnitElement.class);
   CompilationUnitElement libraryBUnit = mock(CompilationUnitElement.class);
   when(libraryA.getDefiningCompilationUnit()).thenReturn(libraryAUnit);
   when(libraryB.getDefiningCompilationUnit()).thenReturn(libraryBUnit);
   when(libraryA.getSource()).thenReturn(librarySourceA);
   when(libraryB.getSource()).thenReturn(librarySourceB);
   when(libraryAUnit.getSource()).thenReturn(librarySourceA);
   when(libraryBUnit.getSource()).thenReturn(librarySourceB);
   when(libraryAUnit.getLibrary()).thenReturn(libraryA);
   when(libraryBUnit.getLibrary()).thenReturn(libraryB);
   // build 2 units in different libraries
   CompilationUnitElement unitA = mock(CompilationUnitElement.class);
   CompilationUnitElement unitB = mock(CompilationUnitElement.class);
   when(unitA.getContext()).thenReturn(contextA);
   when(unitB.getContext()).thenReturn(contextA);
   when(unitA.getSource()).thenReturn(sourceA);
   when(unitB.getSource()).thenReturn(sourceA);
   when(unitA.getLibrary()).thenReturn(libraryA);
   when(unitB.getLibrary()).thenReturn(libraryB);
   when(libraryA.getParts()).thenReturn(new CompilationUnitElement[] {unitA});
   when(libraryB.getParts()).thenReturn(new CompilationUnitElement[] {unitB});
   // record relationships in both A and B
   Location locationA = mockLocation(unitA);
   Location locationB = mockLocation(unitB);
   store.aboutToIndex(contextA, libraryAUnit);
   store.aboutToIndex(contextA, libraryBUnit);
   store.aboutToIndex(contextA, unitA);
   store.aboutToIndex(contextA, unitB);
   store.recordRelationship(elementA, relationship, locationA);
   store.recordRelationship(elementA, relationship, locationB);
   {
     Location[] locations = store.getRelationships(elementA, relationship);
     assertLocations(locations, locationA, locationB);
   }
 }
 public void test_aboutToIndex_unitExcluded() throws Exception {
   // build library with defining unit
   Source librarySource = mock(Source.class);
   LibraryElement library = mock(LibraryElement.class);
   CompilationUnitElement libraryUnit = mock(CompilationUnitElement.class);
   when(library.getContext()).thenReturn(contextA);
   when(library.getDefiningCompilationUnit()).thenReturn(libraryUnit);
   when(library.getSource()).thenReturn(librarySource);
   when(libraryUnit.getContext()).thenReturn(contextA);
   when(libraryUnit.getSource()).thenReturn(librarySource);
   when(libraryUnit.getLibrary()).thenReturn(library);
   // build 2 library units
   CompilationUnitElement unitA = mock(CompilationUnitElement.class);
   CompilationUnitElement unitB = mock(CompilationUnitElement.class);
   when(unitA.getContext()).thenReturn(contextA);
   when(unitB.getContext()).thenReturn(contextA);
   when(unitA.getSource()).thenReturn(sourceA);
   when(unitB.getSource()).thenReturn(sourceB);
   when(unitA.getLibrary()).thenReturn(library);
   when(unitB.getLibrary()).thenReturn(library);
   // prepare locations
   Location locationA = mockLocation(unitA);
   Location locationB = mockLocation(unitB);
   // initially A and B in library
   when(library.getParts()).thenReturn(new CompilationUnitElement[] {unitA, unitB});
   store.aboutToIndex(contextA, libraryUnit);
   store.aboutToIndex(contextA, unitA);
   store.aboutToIndex(contextA, unitB);
   store.recordRelationship(elementA, relationship, locationA);
   store.recordRelationship(elementA, relationship, locationB);
   {
     Location[] locations = store.getRelationships(elementA, relationship);
     assertLocations(locations, locationA, locationB);
   }
   // exclude A from library
   when(library.getParts()).thenReturn(new CompilationUnitElement[] {unitA});
   boolean mayIndex = store.aboutToIndex(contextA, libraryUnit);
   assertTrue(mayIndex);
   {
     Location[] locations = store.getRelationships(elementA, relationship);
     assertLocations(locations, locationA);
   }
   // exclude B from library, empty now
   when(library.getParts()).thenReturn(new CompilationUnitElement[] {});
   store.aboutToIndex(contextA, libraryUnit);
   {
     Location[] locations = store.getRelationships(elementA, relationship);
     assertLocations(locations);
   }
 }