public void test_unspecifiedReferences() throws Exception {
   this.addNodeById("2906110183022219846");
   this.addNodeById("2906110183022219807");
   this.addNodeById("2906110183022219843");
   this.addNodeById("2906110183022354865");
   this.addNodeById("2906110183022432276");
   // Adding one unspecified reference and checking if it was added properly
   int initialSize =
       Sequence.fromIterable(
               SNodeOperations.getReferences(
                   SNodeOperations.cast(
                       this.getNodeById("2906110183022432277"),
                       "jetbrains.mps.lang.smodelTests.structure.ReferenceContainer")))
           .count();
   String unspecifiedReferenceName =
       this.addUnspecifiedReference(
           SNodeOperations.cast(
               this.getNodeById("2906110183022432277"),
               "jetbrains.mps.lang.smodelTests.structure.ReferenceContainer"),
           SNodeOperations.cast(
               this.getNodeById("2906110183022219849"),
               "jetbrains.mps.lang.smodelTests.structure.GrandChild"));
   Assert.assertEquals(
       initialSize + 1,
       Sequence.fromIterable(
               SNodeOperations.getReferences(
                   SNodeOperations.cast(
                       this.getNodeById("2906110183022432277"),
                       "jetbrains.mps.lang.smodelTests.structure.ReferenceContainer")))
           .count());
   // Checking if unspecified reference is working properly
   Iterable<SReference> unspecifiedReferences =
       Sequence.fromIterable(
               SNodeOperations.getReferences(
                   SNodeOperations.cast(
                       this.getNodeById("2906110183022432277"),
                       "jetbrains.mps.lang.smodelTests.structure.ReferenceContainer")))
           .where(
               new IWhereFilter<SReference>() {
                 public boolean accept(SReference it) {
                   return (SLinkOperations.findLinkDeclaration(it) == null);
                 }
               });
   Assert.assertEquals(1, Sequence.fromIterable(unspecifiedReferences).count());
   SReference theReference = Sequence.fromIterable(unspecifiedReferences).first();
   Assert.assertEquals(unspecifiedReferenceName, SLinkOperations.getRole(theReference));
   Assert.assertEquals(
       SNodeOperations.cast(
           this.getNodeById("2906110183022219849"),
           "jetbrains.mps.lang.smodelTests.structure.GrandChild"),
       SLinkOperations.getTargetNode(theReference));
 }
 public void test_forNull() throws Exception {
   this.addNodeById("2906110183022219846");
   this.addNodeById("2906110183022219807");
   this.addNodeById("2906110183022219843");
   this.addNodeById("2906110183022354865");
   this.addNodeById("2906110183022432276");
   SNode nullNode = null;
   Assert.assertTrue(Sequence.fromIterable(SNodeOperations.getReferences(nullNode)).isEmpty());
   SReference nullReference = null;
   Assert.assertNull(SLinkOperations.findLinkDeclaration(nullReference));
   Assert.assertNull(SLinkOperations.getRole(nullReference));
   Assert.assertNull(SLinkOperations.getTargetNode(nullReference));
   Assert.assertNull(SLinkOperations.getResolveInfo(nullReference));
 }
    @Override
    public List<SearchResult<ModelCheckerIssue>> checkModel(
        SModel model, ProgressMonitor monitor, SRepository repository) {
      List<SearchResult<ModelCheckerIssue>> results =
          ListSequence.fromList(new ArrayList<SearchResult<ModelCheckerIssue>>());

      monitor.start("adapter usages", 1);
      for (SNode node : ListSequence.fromList(SModelOperations.getNodes(model, null))) {
        if (monitor.isCanceled()) {
          break;
        }
        // Check for adapter references
        for (SReference ref : Sequence.fromIterable(SNodeOperations.getReferences(node))) {
          SModelReference targetSModelReference = ref.getTargetSModelReference();
          if (targetSModelReference != null
              && SModelStereotype.getStereotype(targetSModelReference.getModelName())
                  .equals("java_stub")) {
            SNode target = SLinkOperations.getTargetNode(ref);
            if ((target != null)
                && SNodeOperations.isInstanceOf(
                    target, "jetbrains.mps.baseLanguage.structure.ClassConcept")) {
              String qualifiedName =
                  BehaviorReflection.invokeVirtual(
                      String.class,
                      SNodeOperations.cast(
                          target, "jetbrains.mps.baseLanguage.structure.ClassConcept"),
                      "virtual_getFqName_1213877404258",
                      new Object[] {});
              if ("jetbrains.mps.smodel.INodeAdapter".equals(qualifiedName)
                  || "jetbrains.mps.smodel.BaseAdapter".equals(qualifiedName)) {
                SpecificChecker.addIssue(
                    results,
                    node,
                    "Reference to "
                        + qualifiedName
                        + " in role `"
                        + SLinkOperations.getRole(ref)
                        + "'",
                    ModelChecker.SEVERITY_ERROR,
                    "adapter class usage",
                    null);
              }

              if (SModelStereotype.withoutStereotype(targetSModelReference.getModelName())
                      .endsWith(".structure")
                  && SModelUtil.findConceptDeclaration(qualifiedName) != null) {
                SpecificChecker.addIssue(
                    results,
                    node,
                    "Using adapter of "
                        + qualifiedName
                        + " concept in role `"
                        + SLinkOperations.getRole(ref)
                        + "'",
                    ModelChecker.SEVERITY_ERROR,
                    "adapter class usage",
                    null);
              }
            }
          }
        }
      }
      monitor.done();
      return results;
    }
Beispiel #4
0
 public void checkNode(
     final SNode node,
     LanguageErrorsComponent component,
     final IOperationContext operationContext,
     IScope scope) {
   if (operationContext == null) {
     return;
   }
   if (SNodeUtil.getMetaLevel(node) != 0) {
     return;
   }
   SNode concept = SNodeOperations.getConceptDeclaration(node);
   for (SReference ref : SNodeOperations.getReferences(node)) {
     SNode target = SLinkOperations.getTargetNode(ref);
     SNode ld = SLinkOperations.findLinkDeclaration(ref);
     // don't check unresolved and broken references, they should already have an error message
     if ((target == null) || ld == null) {
       continue;
     }
     component.addDependency(target);
     component.addDependency(ld);
     component.addDependency(node);
     component.addDependency(SNodeOperations.getParent(node));
     for (SNode c : SNodeOperations.getChildren(node)) {
       component.addDependency(c);
     }
     String linkRole = SModelUtil.getGenuineLinkRole(ld);
     final SNode linkTarget = SLinkOperations.getTarget(ld, "target", false);
     final INodeReferentSearchScopeProvider scopeProvider =
         ModelConstraintsUtil.getSearchScopeProvider(concept, linkRole);
     SearchScopeStatus searchScopeStatus =
         component.runCheckingAction(
             new _FunctionTypes._return_P0_E0<SearchScopeStatus>() {
               public SearchScopeStatus invoke() {
                 return ModelConstraintsUtil.createSearchScope(
                     scopeProvider,
                     SNodeOperations.getModel(node),
                     SNodeOperations.getParent(node),
                     node,
                     linkTarget,
                     operationContext);
               }
             });
     if (searchScopeStatus.isError()) {
       component.addError(
           node,
           searchScopeStatus.getMessage(),
           (SNode) null,
           new ReferenceMessageTarget(SLinkOperations.getRole(ref)));
     } else if (!(searchScopeStatus.isDefault()
         || searchScopeStatus.getSearchScope().isInScope(target))) {
       String name = target.getName();
       component.addError(
           node,
           "reference"
               + ((name == null ? "" : " " + name))
               + " ("
               + SLinkOperations.getRole(ref)
               + ") is out of search scope",
           searchScopeStatus.getReferenceValidatorNode(),
           new ReferenceMessageTarget(SLinkOperations.getRole(ref)));
     }
   }
 }
  @Override
  public List<SearchResult<ModelCheckerIssue>> checkModel(
      SModel model, ProgressMonitor monitor, final IOperationContext operationContext) {
    List<SearchResult<ModelCheckerIssue>> results =
        ListSequence.fromList(new ArrayList<SearchResult<ModelCheckerIssue>>());
    String title =
        "Checking " + SModelOperations.getModelName(model) + " for wrong aspect dependencies...";
    monitor.start(title, 1);

    int modelKind = getModelKind(model, null);
    if (modelKind == OTHER) {
      monitor.done();
      return results;
    }

    for (SNode node : ListSequence.fromList(SModelOperations.getNodes(model, null))) {
      if (monitor.isCanceled()) {
        break;
      }
      // Check for unresolved references
      for (final SReference ref : Sequence.fromIterable(SNodeOperations.getReferences(node))) {
        if ((AttributeOperations.getAttribute(
                node,
                new IAttributeDescriptor.LinkAttribute(
                    SConceptOperations.findConceptDeclaration(
                        "jetbrains.mps.lang.generator.structure.ReferenceMacro"),
                    SLinkOperations.getRole(ref)))
            != null)) {
          continue;
        }
        SNode targetNode = jetbrains.mps.util.SNodeOperations.getTargetNodeSilently(ref);
        if (targetNode == null) {
          addIssue(
              results,
              node,
              "Unresolved reference: " + SLinkOperations.getResolveInfo(ref),
              ModelChecker.SEVERITY_ERROR,
              "unresolved reference",
              new IModelCheckerFix() {
                public boolean doFix() {
                  return ResolverComponent.getInstance().resolve(ref, operationContext);
                }
              });
          continue;
        }

        SModel targetModel = SNodeOperations.getModel(targetNode);
        int targetKind = getModelKind(targetModel, ref);
        if (targetKind > modelKind) {
          addIssue(
              results,
              node,
              "Wrong reference: "
                  + SLinkOperations.getResolveInfo(ref)
                  + ", reference from "
                  + kindToString(modelKind)
                  + " to "
                  + kindToString(targetKind),
              ModelChecker.SEVERITY_ERROR,
              "wrong aspect dependency (" + kindToString(modelKind) + ")",
              null);
        }
      }
    }
    monitor.done();
    return results;
  }
    public void test_unresolvedReference() throws Exception {
      this.addNodeById("2906110183022219846");
      this.addNodeById("2906110183022219807");
      this.addNodeById("2906110183022219843");
      this.addNodeById("2906110183022354865");
      this.addNodeById("2906110183022432276");
      SLinkOperations.getTarget(
          SNodeOperations.cast(
              this.getNodeById("2906110183022219844"),
              "jetbrains.mps.lang.smodelTests.structure.ReferenceContainer"),
          "root",
          false);
      SLinkOperations.getTarget(
          SNodeOperations.cast(
              this.getNodeById("2906110183022219844"),
              "jetbrains.mps.lang.smodelTests.structure.ReferenceContainer"),
          "leftChild",
          false);
      SLinkOperations.getTarget(
          SNodeOperations.cast(
              this.getNodeById("2906110183022219844"),
              "jetbrains.mps.lang.smodelTests.structure.ReferenceContainer"),
          "rightChild",
          false);

      SLinkOperations.getTarget(
          SNodeOperations.cast(
              this.getNodeById("2906110183022354866"),
              "jetbrains.mps.lang.smodelTests.structure.ReferenceContainerSubConcept"),
          "root",
          false);
      SLinkOperations.getTarget(
          SNodeOperations.cast(
              this.getNodeById("2906110183022354866"),
              "jetbrains.mps.lang.smodelTests.structure.ReferenceContainerSubConcept"),
          "leftChild",
          false);
      SLinkOperations.getTarget(
          SNodeOperations.cast(
              this.getNodeById("2906110183022354866"),
              "jetbrains.mps.lang.smodelTests.structure.ReferenceContainerSubConcept"),
          "rightChild",
          false);

      int initialSize =
          Sequence.fromIterable(
                  SNodeOperations.getReferences(
                      SNodeOperations.cast(
                          this.getNodeById("2906110183022432277"),
                          "jetbrains.mps.lang.smodelTests.structure.ReferenceContainer")))
              .count();
      Assert.assertTrue(
          Sequence.fromIterable(
                  SNodeOperations.getReferences(
                      SNodeOperations.cast(
                          this.getNodeById("2906110183022432277"),
                          "jetbrains.mps.lang.smodelTests.structure.ReferenceContainer")))
              .where(
                  new IWhereFilter<SReference>() {
                    public boolean accept(SReference it) {
                      return (SLinkOperations.getTargetNode(it) == null);
                    }
                  })
              .isEmpty());
      SLinkOperations.getTarget(
              SNodeOperations.cast(
                  this.getNodeById("2906110183022432277"),
                  "jetbrains.mps.lang.smodelTests.structure.ReferenceContainer"),
              "root",
              false)
          .delete();
      UnregisteredNodes.instance().clear();
      Assert.assertEquals(
          initialSize,
          Sequence.fromIterable(
                  SNodeOperations.getReferences(
                      SNodeOperations.cast(
                          this.getNodeById("2906110183022432277"),
                          "jetbrains.mps.lang.smodelTests.structure.ReferenceContainer")))
              .count());
      Iterable<SReference> brokenReferences =
          Sequence.fromIterable(
                  SNodeOperations.getReferences(
                      SNodeOperations.cast(
                          this.getNodeById("2906110183022432277"),
                          "jetbrains.mps.lang.smodelTests.structure.ReferenceContainer")))
              .where(
                  new IWhereFilter<SReference>() {
                    public boolean accept(SReference it) {
                      return jetbrains.mps.util.SNodeOperations.getTargetNodeSilently(it) == null;
                    }
                  });
      Assert.assertEquals(1, Sequence.fromIterable(brokenReferences).count());
      SReference theReference = Sequence.fromIterable(brokenReferences).first();
      Assert.assertEquals(
          SPropertyOperations.getString(
              SLinkOperations.findLinkDeclaration(
                  "jetbrains.mps.lang.smodelTests.structure.ReferenceContainer", "root"),
              "role"),
          SLinkOperations.getRole(theReference));
      Assert.assertEquals(
          SLinkOperations.findLinkDeclaration(
              "jetbrains.mps.lang.smodelTests.structure.ReferenceContainer", "root"),
          SLinkOperations.findLinkDeclaration(theReference));
      Assert.assertNotNull(SLinkOperations.getResolveInfo(theReference));
    }