Пример #1
0
 public void testStaticMethods() throws Exception {
   XbaseScopeProvider provider = get(XbaseScopeProvider.class);
   XExpression expression = expression("'x' != 'y'", true);
   IScope scope =
       provider.getScope(expression, XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE);
   final List<IEObjectDescription> allElements = newArrayList(scope.getAllElements());
   try {
     find(
         allElements,
         new Predicate<IEObjectDescription>() {
           public boolean apply(IEObjectDescription input) {
             return input.getName().toString().equals("!=");
           }
         });
   } catch (NoSuchElementException e) {
     fail("operator not found : " + allElements.toString());
   }
   try {
     find(
         allElements,
         new Predicate<IEObjectDescription>() {
           public boolean apply(IEObjectDescription input) {
             return input.getName().toString().equals("-");
           }
         });
     fail("operator + is not defined for type string");
   } catch (NoSuchElementException e) {
     // expected
   }
 }
Пример #2
0
 /** override to add any other implicit feature calls. */
 protected IScope createImplicitFeatureCallScope(
     final EObject call, final IScope parent, IScope localVariableScope) {
   IScope result = parent;
   IEObjectDescription thisVariable = localVariableScope.getSingleElement(THIS);
   if (thisVariable != null) {
     EObject implicitReceiver = thisVariable.getEObjectOrProxy();
     JvmTypeReference implicitReceiverType =
         typeProvider.getTypeForIdentifiable((JvmIdentifiableElement) implicitReceiver);
     if (implicitReceiverType != null && implicitReceiver instanceof JvmIdentifiableElement) {
       XFeatureCall receiver = XbaseFactory.eINSTANCE.createXFeatureCall();
       receiver.setFeature((JvmIdentifiableElement) implicitReceiver);
       result =
           createFeatureScopeForTypeRef(
               implicitReceiverType, call, getContextType(call), receiver, result);
     }
   }
   IEObjectDescription itVariable = localVariableScope.getSingleElement(IT);
   if (itVariable != null) {
     EObject implicitReceiver = itVariable.getEObjectOrProxy();
     JvmTypeReference implicitReceiverType =
         typeProvider.getTypeForIdentifiable((JvmIdentifiableElement) implicitReceiver);
     if (implicitReceiverType != null && implicitReceiver instanceof JvmIdentifiableElement) {
       XFeatureCall receiver = XbaseFactory.eINSTANCE.createXFeatureCall();
       receiver.setFeature((JvmIdentifiableElement) implicitReceiver);
       result =
           createFeatureScopeForTypeRef(
               implicitReceiverType, call, getContextType(call), receiver, result);
     }
   }
   return result;
 }
 public void testVariable() throws IOException {
   Program program = getProgramFromString("class A { int i; int m(A a, Object b) { return a; }}");
   Method method = program.getClasses().get(0).getMethods().get(0);
   IScope scope = fixture.scope_Variable_variable(method, null);
   System.out.println("scope: " + scope.getAllContents());
   assertEquals(2, sizeOfIterable(scope.getAllContents()));
   assertTrue(scope.getContentByName("a") != null);
   assertTrue(scope.getContentByName("b") != null);
 }
 @Override
 public List<EObject> getLinkedObjects(EObject context, EReference ref, INode node)
     throws IllegalNodeException {
   try {
     depth++;
     String text = getText(node);
     boolean traceLookup = BaseScopeProvider.LOOKUP.isActive();
     if ((text == null) || "".equals(text)) { // Avoid IQualifiedNameConverter IAE
       if (traceLookup) {
         BaseScopeProvider.LOOKUP.println("" + depth + " Lookup null");
       }
       return Collections.emptyList();
     }
     IScope scope = getScope(context, ref);
     if (traceLookup) {
       //				EObject target = ((ScopeView)scope).getTarget();
       //				String inString = target instanceof ElementCS ? ((ElementCS)target).getSignature() :
       // target.toString();
       //				BaseScopeProvider.LOOKUP.println("" + depth + " Lookup " + text + " in " + inString);
       BaseScopeProvider.LOOKUP.println("" + depth + " Lookup " + text);
     }
     if (scope == null) {
       return Collections.emptyList();
     }
     QualifiedName qualifiedName = QualifiedName.create(text);
     List<EObject> linkedObjects = lookUp(scope, qualifiedName);
     if ((linkedObjects.size() <= 0) && text.startsWith("_")) { // Deprecated compatibility
       linkedObjects = lookUp(scope, QualifiedName.create(text.substring(1)));
     }
     if (traceLookup) {
       BaseScopeProvider.LOOKUP.println("" + depth + " Lookup " + text + " failed");
     }
     List<Adapter> eAdapters = context.eAdapters();
     Adapter adapter = EcoreUtil.getAdapter(eAdapters, ExceptionAdapter.class);
     if (adapter != null) {
       eAdapters.remove(adapter);
     }
     if (linkedObjects.size() > 1) {
       if (DEBUG_RETRY.isActive()) {
         scope.getElements(qualifiedName);
       }
       AmbiguitiesAdapter.setAmbiguities(context, linkedObjects);
       return Collections.emptyList();
     }
     if (linkedObjects.size() <= 0) {
       if (DEBUG_RETRY.isActive()) {
         scope.getElements(qualifiedName);
       }
     }
     return linkedObjects;
   } catch (IllegalLibraryException e) {
     context.eAdapters().add(new ExceptionAdapter(e));
     return Collections.emptyList();
   } finally {
     depth--;
   }
 }
 public void testNewType() throws IOException {
   Program program = getProgramFromString("class B {} class A extends B { Object a; } new B()");
   Expression main = program.getMain();
   EReference type = ((New) main).getType().eClass().getEReferences().get(0);
   IScope scope = fixture.getScope(main, type);
   System.out.println("scope for new: " + scope.getAllContents());
   assertEquals(3, sizeOfIterable(scope.getAllContents()));
   assertTrue(scope.getContentByName("Object") != null);
   assertTrue(scope.getContentByName("A") != null);
   assertTrue(scope.getContentByName("B") != null);
 }
 public void testClass() throws IOException {
   Program program = getProgramFromString("class B {} class A extends B { Object a; }");
   Class B = program.getClasses().get(0);
   EReference extends_ = B.eClass().getEReferences().get(0);
   IScope scope = fixture.getScope(B, extends_);
   System.out.println("scope for B: " + scope.getAllContents());
   assertEquals(3, sizeOfIterable(scope.getAllContents()));
   assertTrue(scope.getContentByName("Object") != null);
   assertTrue(scope.getContentByName("A") != null);
   assertTrue(scope.getContentByName("B") != null);
 }
Пример #7
0
 public void testOverriddenExtensionMethods_02() throws Exception {
   XbaseScopeProvider provider = get(XbaseScopeProvider.class);
   XExpression expression = expression("(null as java.util.Collection<String>).map(null)", true);
   IScope scope =
       provider.getScope(expression, XbasePackage.Literals.XABSTRACT_FEATURE_CALL__FEATURE);
   Iterable<IEObjectDescription> elements = scope.getElements(QualifiedName.create("map"));
   assertEquals(elements.toString(), 1, size(elements));
   IEObjectDescription description = elements.iterator().next();
   JvmFeature feature = (JvmFeature) description.getEObjectOrProxy();
   assertEquals(
       IterableExtensions.class.getCanonicalName(), feature.getDeclaringType().getIdentifier());
 }
  public void testFieldSelectionChain() throws IOException {
    Program program =
        getProgramFromString("class B { A f; } class A extends B {  A m() { return this.f.f; }}");

    Expression exp = program.getClasses().get(1).getMethods().get(0).getBody().getExpression();
    Selection body = (Selection) exp;
    IScope scope = fixture.scope_FieldSelection_name(body, null);
    System.out.println("scope: " + scope.getAllContents());
    assertEquals(1, sizeOfIterable(scope.getAllContents()));
    assertTrue(scope.getContentByName("f") != null);
    assertEquals("f", ((Field) scope.getContentByName("f").getEObjectOrProxy()).getName());
  }
  public IScope getScope(
      Resource context, EReference reference, Predicate<IEObjectDescription> filter) {
    IScope scope = super.getScope(context, reference, filter);
    IScope globalScope = delegate.getScope(context, reference, filter);
    FilteringScope filteringScope =
        new FilteringScope(
            globalScope,
            new Predicate<IEObjectDescription>() {

              public boolean apply(IEObjectDescription input) {
                return input.getEClass() == SGraphPackage.Literals.STATECHART;
              }
            });
    return new SimpleScope(
        Iterables.concat(scope.getAllElements(), filteringScope.getAllElements()));
  }
  public String getCrossRefText(
      EObject owner,
      CrossReference crossref,
      EObject target,
      RefTextEvaluator refTextEvaluator,
      ITextRegion linkTextRegion,
      StatusWrapper status) {
    try {
      final EReference ref = GrammarUtil.getReference(crossref, owner.eClass());
      final IScope scope = scopeProvider.getScope(owner, ref);
      if (scope == null) {
        throw new IllegalStateException("Could not create scope for the given cross reference.");
      }
      String ruleName = linkingHelper.getRuleNameFrom(crossref);

      Iterable<IEObjectDescription> descriptionsForCrossRef = scope.getElements(target);
      String bestRefText = null;
      for (IEObjectDescription desc : descriptionsForCrossRef) {
        try {
          String unconvertedRefText = qualifiedNameConverter.toString(desc.getName());
          String convertedRefText = valueConverter.toString(unconvertedRefText, ruleName);
          if (refTextEvaluator.isValid(convertedRefText)
              && (bestRefText == null
                  || refTextEvaluator.isBetterThan(convertedRefText, bestRefText)))
            bestRefText = convertedRefText;
        } catch (ValueConverterException e) {
          status.add(
              RefactoringStatus.WARNING,
              "Missconfigured language: New reference text has invalid syntax.",
              owner,
              linkTextRegion);
        }
      }
      if (bestRefText == null)
        status.add(
            RefactoringStatus.ERROR,
            "Refactoring introduces a name conflict.",
            owner,
            linkTextRegion);
      return bestRefText;

    } catch (Exception exc) {
      status.add(ERROR, exc.getMessage(), owner, linkTextRegion);
      return null;
    }
  }
  public void testFieldSelectionNew() throws IOException {
    Program program =
        getProgramFromString(
            "class B { String f; } "
                + "class A extends B { "
                + "	int g; "
                + "	A m() { return new A('foo',10).f; }"
                + "}");

    Expression exp = program.getClasses().get(1).getMethods().get(0).getBody().getExpression();
    Selection body = (Selection) exp;
    IScope scope = fixture.scope_FieldSelection_name(body, null);
    System.out.println("scope: " + scope.getAllContents());
    // the elements reachable from new B().f are f and g
    assertEquals(2, sizeOfIterable(scope.getAllContents()));
    assertTrue(scope.getContentByName("f") != null);
    assertEquals("f", ((Field) scope.getContentByName("f").getEObjectOrProxy()).getName());
  }
 @Override
 protected Iterable<IEObjectDescription> getAllLocalElements() {
   Map<String, IEObjectDescription> result = new HashMap<String, IEObjectDescription>();
   for (IEObjectDescription ePkgDesc : parentScope.getAllElements()) {
     EPackage ePkg = (EPackage) ePkgDesc.getEObjectOrProxy();
     ePkg = (EPackage) EcoreUtil.resolve(ePkg, context);
     result.put(ePkg.getNsURI(), EObjectDescription.create(ePkg.getNsURI(), ePkg));
   }
   return result.values();
 }
Пример #13
0
 protected List<EObject> lookUp(@NonNull IScope scope, QualifiedName qualifiedName) {
   @NonNull List<EObject> linkedObjects = new ArrayList<EObject>();
   for (IEObjectDescription eObjectDescription : scope.getElements(qualifiedName)) {
     EObject eObjectOrProxy = eObjectDescription.getEObjectOrProxy();
     linkedObjects.add(eObjectOrProxy);
     if (BaseScopeProvider.LOOKUP.isActive()) {
       BaseScopeProvider.LOOKUP.println(
           "" + depth + " Lookup " + qualifiedName + " => " + eObjectOrProxy);
     }
   }
   return linkedObjects;
 }
Пример #14
0
  @Override
  protected void createContent(
      IManagedForm mform, Composite body, DataBindingContext bindingContext) {
    Result notReferenced = ValidationUtil.isFreeOfReferences(port);
    boolean multiplicityAnyAllowed = true;
    ActorContainerClass parent = (ActorContainerClass) port.eContainer();
    if (parent instanceof ActorClass) {
      if (ValidationUtil.isReferencedAsReplicatedInModel((ActorClass) parent))
        multiplicityAnyAllowed = false;
    }
    NameValidator nv = new NameValidator();
    ProtocolValidator pv = new ProtocolValidator();
    MultiplicityValidator mv =
        new MultiplicityValidator(
            newPort || notReferenced.isOk(), port.getMultiplicity(), multiplicityAnyAllowed);

    ArrayList<IEObjectDescription> protocols = new ArrayList<IEObjectDescription>();
    Iterator<IEObjectDescription> it = scope.getAllElements().iterator();
    while (it.hasNext()) {
      IEObjectDescription desc = it.next();
      EObject obj = desc.getEObjectOrProxy();
      if (obj instanceof GeneralProtocolClass) protocols.add(desc);
    }

    Text name = createText(body, "&Name:", port, RoomPackage.eINSTANCE.getInterfaceItem_Name(), nv);
    Combo protocol =
        createComboUsingDesc(
            body,
            "&Protocol:",
            port,
            GeneralProtocolClass.class,
            RoomPackage.eINSTANCE.getPort_Protocol(),
            protocols,
            RoomPackage.eINSTANCE.getRoomClass_Name(),
            pv);
    Button conj =
        createCheck(body, "&Conjugated:", port, RoomPackage.eINSTANCE.getPort_Conjugated());
    if (!internal && !refitem && (acc instanceof ActorClass))
      createRelayCheck(body, notReferenced, mform.getToolkit());

    Multiplicity2StringConverter m2s = new Multiplicity2StringConverter();
    String2MultiplicityConverter s2m = new String2MultiplicityConverter();
    Text multi =
        createText(
            body,
            "&Multiplicity:",
            port,
            RoomPackage.eINSTANCE.getPort_Multiplicity(),
            mv,
            s2m,
            m2s,
            false);

    if (!newPort) {
      if (!notReferenced.isOk()) {
        protocol.setEnabled(false);
        createInfoDecorator(protocol, notReferenced.getMsg());
        conj.setEnabled(false);
        createInfoDecorator(conj, notReferenced.getMsg());
        if (port.getMultiplicity() == 1) {
          multi.setEnabled(false);
          createInfoDecorator(multi, notReferenced.getMsg());
        }
      }

      if (refitem) {
        name.setEnabled(false);
        createInfoDecorator(name, "inherited");
        protocol.setEnabled(false);
        createInfoDecorator(protocol, "inherited");
        conj.setEnabled(false);
        createInfoDecorator(conj, "inherited");
        multi.setEnabled(false);
        createInfoDecorator(multi, "inherited");
      }
    }

    createDecorator(name, "invalid name");
    createDecorator(protocol, "no protocol selected");
    createDecorator(multi, "multiplicity must be greater 1");

    name.selectAll();
    name.setFocus();
  }