ModelElement.fromPropertyInducingElement constructor

ModelElement.fromPropertyInducingElement(
  1. PropertyInducingElement e,
  2. Library library,
  3. PackageGraph packageGraph,
  4. {Container enclosingContainer,
  5. @required Accessor getter,
  6. @required Accessor setter}
)

Creates a ModelElement from PropertyInducingElement e.

Do not construct any ModelElements except from this constructor or ModelElement.from. Specify enclosingContainer if and only if this is to be an inherited or extended object.

Implementation

factory ModelElement.fromPropertyInducingElement(
    PropertyInducingElement e, Library library, PackageGraph packageGraph,
    {Container enclosingContainer,
    @required Accessor getter,
    @required Accessor setter}) {
  assert(packageGraph != null);
  assert(e != null);
  assert(library != null);

  // TODO(jcollins-g): Refactor object model to instantiate 'ModelMembers'
  //                   for members?
  if (e is Member) {
    e = e.declaration;
  }

  // Return the cached ModelElement if it exists.
  var key =
      Tuple3<Element, Library, Container>(e, library, enclosingContainer);
  if (packageGraph.allConstructedModelElements.containsKey(key)) {
    return packageGraph.allConstructedModelElements[key];
  }

  ModelElement newModelElement;
  if (e is FieldElement) {
    if (enclosingContainer == null) {
      if (e.isEnumConstant) {
        var index = e.computeConstantValue().getField(e.name).toIntValue();
        newModelElement =
            EnumField.forConstant(index, e, library, packageGraph, getter);
      } else if (e.enclosingElement is ExtensionElement) {
        newModelElement = Field(e, library, packageGraph, getter, setter);
      } else if (e.enclosingElement is ClassElement &&
          (e.enclosingElement as ClassElement).isEnum) {
        newModelElement = EnumField(e, library, packageGraph, getter, setter);
      } else {
        newModelElement = Field(e, library, packageGraph, getter, setter);
      }
    } else {
      // EnumFields can't be inherited, so this case is simpler.
      newModelElement = Field.inherited(
          e, enclosingContainer, library, packageGraph, getter, setter);
    }
  }
  if (e is TopLevelVariableElement) {
    assert(getter != null || setter != null);
    newModelElement =
        TopLevelVariable(e, library, packageGraph, getter, setter);
  }

  if (enclosingContainer != null) assert(newModelElement is Inheritable);
  _cacheNewModelElement(e, newModelElement, library,
      enclosingContainer: enclosingContainer);

  assert(newModelElement.element is! MultiplyInheritedExecutableElement);
  return newModelElement;
}