org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotation Java Examples

The following examples show how to use org.eclipse.xtext.xbase.annotations.xAnnotations.XAnnotation. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JvmTypesBuilderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testStringAnnotationWithNullExpression() {
  try {
    final XAnnotationsFactory f = XAnnotationsFactory.eINSTANCE;
    final XExpression context = this.expression("\'Foo\'");
    final XAnnotation anno = f.createXAnnotation();
    JvmType _findDeclaredType = this.references.findDeclaredType(Inject.class, context);
    anno.setAnnotationType(((JvmAnnotationType) _findDeclaredType));
    final XAnnotationElementValuePair pair = f.createXAnnotationElementValuePair();
    EList<XAnnotationElementValuePair> _elementValuePairs = anno.getElementValuePairs();
    this._jvmTypesBuilder.<XAnnotationElementValuePair>operator_add(_elementValuePairs, pair);
    final JvmGenericType type = this.typesFactory.createJvmGenericType();
    this._jvmTypesBuilder.addAnnotation(type, anno);
    Assert.assertEquals(anno.getAnnotationType(), IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getAnnotation());
    Assert.assertTrue(IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getExplicitValues().isEmpty());
    Assert.assertFalse(IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getValues().isEmpty());
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #2
Source File: SARLValidator.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Check for {@code @Inline} annotation usage.
 *
 * @param annotationTarget thee target to test.
 */
@Check
public void checkManualInlineDefinition(XtendAnnotationTarget annotationTarget) {
	if (!isIgnored(MANUAL_INLINE_DEFINITION)) {
		if (annotationTarget.getAnnotations().isEmpty() || !isRelevantAnnotationTarget(annotationTarget)) {
			return;
		}
		final String inlineAnnotation = Inline.class.getName();
		for (final XAnnotation annotation : annotationTarget.getAnnotations()) {
			final JvmType type = annotation.getAnnotationType();
			if (type != null && !type.eIsProxy()) {
				if (Objects.equal(type.getIdentifier(), inlineAnnotation)) {
					addIssue(
							Messages.SARLValidator_16,
							annotation,
							MANUAL_INLINE_DEFINITION);
				}
			}
		}
	}
}
 
Example #3
Source File: XbaseWithAnnotationsValidator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Check
public void checkAllAttributesConfigured(XAnnotation annotation) {
	JvmType annotationType = annotation.getAnnotationType();
	if (annotationType == null || annotationType.eIsProxy() || !(annotationType instanceof JvmAnnotationType))
		return;
	Iterable<JvmOperation> attributes = ((JvmAnnotationType) annotationType).getDeclaredOperations();
	for (JvmOperation jvmOperation : attributes) {
		XExpression value = annotationUtil.findValue(annotation, jvmOperation);
		if(value == null) {
			if (jvmOperation.getDefaultValue() == null) {
				error("The annotation must define the attribute '"+jvmOperation.getSimpleName()+"'.", annotation, null, 
						ValidationMessageAcceptor.INSIGNIFICANT_INDEX, ANNOTATIONS_MISSING_ATTRIBUTE_DEFINITION);
			}
		} else
			annotationValueValidator.validateAnnotationValue(value, this);
	}
}
 
Example #4
Source File: XtendQuickfixProvider.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Fix(IssueCodes.MISSING_OVERRIDE)
public void fixMissingOverride(final Issue issue, IssueResolutionAcceptor acceptor) {
	acceptor.accept(issue, "Change 'def' to 'override'", "Marks this function as 'override'", "fix_indent.gif",
			new ISemanticModification() {
				@Override
				public void apply(EObject element, IModificationContext context) throws Exception {
					replaceKeyword(grammarAccess.getMethodModifierAccess().findKeywords("def").get(0), "override", element,
							context.getXtextDocument());
					if (element instanceof XtendFunction) {
						XtendFunction function = (XtendFunction) element;
						for (XAnnotation anno : Lists.reverse(function.getAnnotations())) {
							if (anno != null && anno.getAnnotationType() != null && Override.class.getName().equals(anno.getAnnotationType().getIdentifier())) {
								ICompositeNode node = NodeModelUtils.findActualNodeFor(anno);
								context.getXtextDocument().replace(node.getOffset(), node.getLength(), "");
							}
						}
					}
				}
			});
}
 
Example #5
Source File: XtendHoverSignatureProviderTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testAnnotationReference() {
  try {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("package testPackage");
    _builder.newLine();
    _builder.append("@Foo");
    _builder.newLine();
    _builder.append("class Bar {}");
    _builder.newLine();
    _builder.append("annotation Foo { }");
    _builder.newLine();
    final XtendFile xtendFile = this.parseHelper.parse(_builder, this.getResourceSet());
    XtendTypeDeclaration _head = IterableExtensions.<XtendTypeDeclaration>head(xtendFile.getXtendTypes());
    final JvmType in = IterableExtensions.<XAnnotation>head(((XtendClass) _head).getAnnotations()).getAnnotationType();
    Assert.assertEquals("Foo", this.signatureProvider.getSignature(in));
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #6
Source File: CompilationUnitImpl.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected Object translateAnnotationValue(final XExpression value, final JvmTypeReference expectedType) {
  Object _xblockexpression = null;
  {
    if ((value instanceof XAnnotation)) {
      return this.translateAnnotation(((XAnnotation)value));
    }
    if ((value instanceof XListLiteral)) {
      final Iterable<XAnnotation> annotations = Iterables.<XAnnotation>filter(((XListLiteral)value).getElements(), XAnnotation.class);
      int _size = ((XListLiteral)value).getElements().size();
      int _size_1 = IterableExtensions.size(annotations);
      boolean _equals = (_size == _size_1);
      if (_equals) {
        final ArrayList<AnnotationReference> annotationReferences = CollectionLiterals.<AnnotationReference>newArrayList();
        for (final XAnnotation annotation : annotations) {
          AnnotationReference _translateAnnotation = this.translateAnnotation(annotation);
          annotationReferences.add(_translateAnnotation);
        }
        final AnnotationReference[] result = ((AnnotationReference[])Conversions.unwrapArray(annotationReferences, AnnotationReference.class));
        return result;
      }
    }
    _xblockexpression = this.translate(this.evaluate(value, expectedType));
  }
  return _xblockexpression;
}
 
Example #7
Source File: ErrorTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testErrorModel_003() throws Exception {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("class TestXtend {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("val static int a = 4");
  _builder.newLine();
  _builder.append("\t");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("@Click(#[ a, a ])");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("d");
  _builder.newLine();
  final XtendFile file = this.processWithoutException(_builder);
  XtendTypeDeclaration _head = IterableExtensions.<XtendTypeDeclaration>head(file.getXtendTypes());
  final XtendClass clazz = ((XtendClass) _head);
  final XtendMember lastMember = IterableExtensions.<XtendMember>last(clazz.getMembers());
  final EList<XAnnotation> annotations = lastMember.getAnnotations();
  final IResolvedTypes resolvedTypes = this.typeResolver.resolveTypes(IterableExtensions.<XAnnotation>head(annotations));
  Assert.assertNotNull(resolvedTypes.getActualType(IterableExtensions.<XAnnotation>head(annotations)));
}
 
Example #8
Source File: ErrorTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testErrorModel_004() throws Exception {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("class TestXtend {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("val static int a = 4");
  _builder.newLine();
  _builder.append("\t");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("@Click(#[ a, a ])");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("def m");
  _builder.newLine();
  final XtendFile file = this.processWithoutException(_builder);
  XtendTypeDeclaration _head = IterableExtensions.<XtendTypeDeclaration>head(file.getXtendTypes());
  final XtendClass clazz = ((XtendClass) _head);
  final XtendMember lastMember = IterableExtensions.<XtendMember>last(clazz.getMembers());
  final EList<XAnnotation> annotations = lastMember.getAnnotations();
  final IResolvedTypes resolvedTypes = this.typeResolver.resolveTypes(IterableExtensions.<XAnnotation>head(annotations));
  Assert.assertNotNull(resolvedTypes.getActualType(IterableExtensions.<XAnnotation>head(annotations)));
}
 
Example #9
Source File: XbaseCompiler.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void _toJavaExpression(final XAnnotation annotation, final ITreeAppendable b) {
	b.append("@");
	b.append(annotation.getAnnotationType());
	XExpression value = annotation.getValue();
	if (value != null) {
		b.append("(");
		internalToJavaExpression(value, b);
		b.append(")");
	} else {
		EList<XAnnotationElementValuePair> valuePairs = annotation.getElementValuePairs();
		if (valuePairs.isEmpty())
			return;
		b.append("(");
		for (int i = 0; i < valuePairs.size(); i++) {
			XAnnotationElementValuePair pair = valuePairs.get(i);
			b.append(pair.getElement().getSimpleName());
			b.append(" = ");
			internalToJavaExpression(pair.getValue(), b);
			if (i < valuePairs.size()-1) {
				b.append(", ");
			}
		}
		b.append(")");
	}
}
 
Example #10
Source File: JvmTypesBuilderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testAnnotationDefaultValue() {
  try {
    final XAnnotationsFactory f = XAnnotationsFactory.eINSTANCE;
    final XExpression e = this.expression("\'Foo\'");
    final XAnnotation anno = f.createXAnnotation();
    JvmType _findDeclaredType = this.references.findDeclaredType(Named.class, e);
    anno.setAnnotationType(((JvmAnnotationType) _findDeclaredType));
    anno.setValue(e);
    final JvmGenericType type = this.typesFactory.createJvmGenericType();
    this._jvmTypesBuilder.addAnnotation(type, anno);
    Assert.assertEquals(anno.getAnnotationType(), IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getAnnotation());
    JvmAnnotationValue _head = IterableExtensions.<JvmAnnotationValue>head(IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getValues());
    EObject _head_1 = IterableExtensions.<EObject>head(((JvmCustomAnnotationValue) _head).getValues());
    Assert.assertTrue((_head_1 instanceof XStringLiteral));
    Assert.assertNull(IterableExtensions.<JvmAnnotationValue>head(IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getValues()).getOperation());
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #11
Source File: ConstantExpressionsInterpreter.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
private boolean isEnumExpectationInAnnotationValue(final XFeatureCall it, final JvmType expectedRawType) {
  if (((expectedRawType instanceof JvmEnumerationType) || ((expectedRawType instanceof JvmArrayType) && (((JvmArrayType) expectedRawType).getComponentType() instanceof JvmEnumerationType)))) {
    EObject container = it.eContainer();
    if ((container instanceof XAnnotationElementValuePair)) {
      return true;
    }
    if ((container instanceof XAnnotation)) {
      return true;
    }
    if ((container instanceof XListLiteral)) {
      container = ((XListLiteral)container).eContainer();
      if ((container instanceof XAnnotationElementValuePair)) {
        return true;
      }
      if ((container instanceof XAnnotation)) {
        return true;
      }
    }
  }
  return false;
}
 
Example #12
Source File: JvmTypesBuilderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testStringAnnotation() {
  try {
    final XAnnotationsFactory f = XAnnotationsFactory.eINSTANCE;
    final XExpression e = this.expression("\'Foo\'");
    final XAnnotation anno = f.createXAnnotation();
    JvmType _findDeclaredType = this.references.findDeclaredType(Inject.class, e);
    anno.setAnnotationType(((JvmAnnotationType) _findDeclaredType));
    anno.setValue(e);
    final JvmGenericType type = this.typesFactory.createJvmGenericType();
    this._jvmTypesBuilder.addAnnotation(type, anno);
    Assert.assertEquals(anno.getAnnotationType(), IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getAnnotation());
    JvmAnnotationValue _head = IterableExtensions.<JvmAnnotationValue>head(IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getValues());
    EObject _head_1 = IterableExtensions.<EObject>head(((JvmCustomAnnotationValue) _head).getValues());
    Assert.assertTrue((_head_1 instanceof XStringLiteral));
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #13
Source File: XAnnotationExtensions.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
public JvmAnnotationType tryFindAnnotationType(final XAnnotation it) {
  Object _eGet = it.eGet(XAnnotationsPackage.Literals.XANNOTATION__ANNOTATION_TYPE, false);
  final Object proxy = _eGet;
  boolean _matched = false;
  if (proxy instanceof EObject) {
    boolean _eIsProxy = ((EObject)proxy).eIsProxy();
    if (_eIsProxy) {
      _matched=true;
      final URI uri = ((InternalEObject) proxy).eProxyURI();
      EObject _eObject = it.eResource().getResourceSet().getEObject(uri, true);
      return ((JvmAnnotationType) _eObject);
    }
  }
  if (!_matched) {
    if (proxy instanceof JvmAnnotationType) {
      _matched=true;
      return ((JvmAnnotationType)proxy);
    }
  }
  return null;
}
 
Example #14
Source File: AnnotationsValidatorTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testConstantExpression_7() throws Exception {
	XAnnotation annotation = annotation("@testdata.Annotation3("
			+ "intValue = 1 + 4 + 6 * 42 - 4 / 45,"
			+ "longValue = 42 + 4 + 6 * 42 - testdata.Constants1.INT_CONSTANT / 45,"
			+ "stringValue = 'foo' + 'baz',"
			+ "booleanArrayValue = #[true, false],"
			+ "intArrayValue = #[ -1, 34 + 45, 2 - 6 ],"
			+ "longArrayValue = #[42, 5 * -3],"
			+ "stringArrayValue = #['foo', 'bla' + 'buzz'],"
			+ "typeValue = String,"
			+ "typeArrayValue = #[String, Integer],"
			+ "annotation2Value = @testdata.Annotation2(#['foo' + 'wuppa']),"
			+ "annotation2ArrayValue = #[@testdata.Annotation2(#['foo']), @testdata.Annotation2(#['foo'+'wuppa'])]"
			+ ")", false);
	validator.assertNoErrors(annotation);
}
 
Example #15
Source File: XtendJvmModelInferrer.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected void transform(XtendEnumLiteral literal, JvmEnumerationType container) {
	if (literal.getName() == null)
		return;
	JvmEnumerationLiteral jvmLiteral = typesFactory.createJvmEnumerationLiteral();
	associator.associatePrimary(literal, jvmLiteral);
	jvmLiteral.setSimpleName(literal.getName());
	jvmLiteral.setVisibility(JvmVisibility.PUBLIC);
	jvmLiteral.setStatic(true);
	jvmLiteral.setFinal(true);
	jvmTypesBuilder.copyDocumentationTo(literal, jvmLiteral);
	for (XAnnotation anno : literal.getAnnotations()) {
		if (!annotationTranslationFilter.apply(anno))
			continue;
		JvmAnnotationReference annotationReference = jvmTypesBuilder.getJvmAnnotationReference(anno);
		if(annotationReference != null)
			jvmLiteral.getAnnotations().add(annotationReference);
	}
	container.getMembers().add(jvmLiteral);
}
 
Example #16
Source File: XtendValidator.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected void doCheckFunctionOverrides(IResolvedOperation operation, Set<EObject> flaggedOperations) {
	EObject sourceElement = findPrimarySourceElement(operation);
	if (sourceElement != null) {
		List<IResolvedOperation> allInherited = operation.getOverriddenAndImplementedMethods();
		if (allInherited.isEmpty()) {
			if (sourceElement instanceof XtendFunction && flaggedOperations.add(sourceElement)) {
				XtendFunction function = (XtendFunction) sourceElement;
				if (function.isOverride()) {
					error("The method "+ operation.getSimpleSignature() +" of type "+getDeclaratorName(operation.getDeclaration())+" must override a superclass method.", 
							function, XTEND_MEMBER__MODIFIERS, function.getModifiers().indexOf("override"), OBSOLETE_OVERRIDE);
				} else {
					for (XAnnotation anno : function.getAnnotations()) {
						if (anno != null && anno.getAnnotationType() != null && Override.class.getName().equals(anno.getAnnotationType().getIdentifier())) {
							error("Superfluous @Override annotation", anno, null, OBSOLETE_ANNOTATION_OVERRIDE);
						}
					}
				}
			}
		} else if (flaggedOperations.add(sourceElement)) {
			doCheckFunctionOverrides(sourceElement, operation, allInherited);
		}
	}
}
 
Example #17
Source File: Utils.java    From sarl with Apache License 2.0 6 votes vote down vote up
private static void addAnnotationToSignature(StringBuilder textRepresentation, SARLGrammarKeywordAccess elements,
		ISerializer serializer, ImportManager importManager, XAnnotation annotation) {
	textRepresentation.append(elements.getCommercialAtKeyword());
	textRepresentation.append(getSignatureType(annotation.getAnnotationType(), importManager));
	final XExpression value = annotation.getValue();
	if (value != null) {
		textRepresentation.append(elements.getLeftParenthesisKeyword());
		textRepresentation.append(serializer.serialize(value).trim());
		textRepresentation.append(elements.getRightParenthesisKeyword());
	} else if (!annotation.getElementValuePairs().isEmpty()) {
		textRepresentation.append(elements.getLeftParenthesisKeyword());
		boolean addComa = false;
		for (final XAnnotationElementValuePair pair : annotation.getElementValuePairs()) {
			if (addComa) {
				textRepresentation.append(elements.getCommaKeyword());
			} else {
				addComa = true;
			}
			textRepresentation.append(elements.getEqualsSignKeyword());
			textRepresentation.append(serializer.serialize(pair.getValue()).trim());
		}
		textRepresentation.append(elements.getRightParenthesisKeyword());
	}
}
 
Example #18
Source File: SuppressWarningsAddModification.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Override
public void apply(EObject element, IModificationContext context) throws Exception {
	final EObject currentElement;
	if (this.uri == null) {
		currentElement = element;
	} else {
		currentElement = element.eResource().getResourceSet().getEObject(this.uri, true);
	}
	if (currentElement instanceof XtendAnnotationTarget) {
		final JvmType swtype = getTools().getTypeServices().getTypeReferences().findDeclaredType(
				SuppressWarnings.class, element);
		final XtendAnnotationTarget annotationTarget = (XtendAnnotationTarget) currentElement;
		final XAnnotation annotation = findAnnotation(annotationTarget, swtype);
		if (annotation == null) {
			addAnnotation(currentElement, context, swtype);
		} else {
			addAnnotation(currentElement, annotation, context);
		}
	}
}
 
Example #19
Source File: XbaseWithAnnotationsProposalProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void completeXFeatureCall_Feature(EObject model, Assignment assignment, ContentAssistContext context,
		ICompletionProposalAcceptor acceptor) {
	if (model instanceof XAnnotation) {
		createLocalVariableAndImplicitProposals(model, IExpressionScope.Anchor.WITHIN, context, acceptor);
		return;
	} else if (model instanceof XAnnotationElementValuePair) {
		XAnnotationElementValuePair pair = (XAnnotationElementValuePair) model;
		XAnnotation annotation = (XAnnotation) pair.eContainer();
		createLocalVariableAndImplicitProposals(annotation, IExpressionScope.Anchor.WITHIN, context, acceptor);
		return;
	}
	super.completeXFeatureCall_Feature(model, assignment, context, acceptor);
}
 
Example #20
Source File: XtendUIValidator.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Check
protected void checkAnnotationInSameProject(XAnnotation annotation) throws JavaModelException {
	try {
		if (annotationExtensions.isProcessed(annotation)) {
			JvmType annotationType = annotation.getAnnotationType();
			if (isSameProject(annotation, annotationType)) {
				error("The referenced active annotation cannot be used from within the same project.",XAnnotationsPackage.Literals.XANNOTATION__ANNOTATION_TYPE, -1, ACTIVE_ANNOTATION_IN_SAME_CONTAINER);
			}
		}
	} catch(JavaModelException e) {
		if (!e.isDoesNotExist()) {
			throw e;
		}
	}
}
 
Example #21
Source File: XtendAnnotationTargetImpl.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Iterable<? extends AnnotationReference> getAnnotations() {
  final Function1<XAnnotation, AnnotationReference> _function = (XAnnotation it) -> {
    return this.getCompilationUnit().toAnnotationReference(it);
  };
  return ImmutableList.<AnnotationReference>copyOf(ListExtensions.<XAnnotation, AnnotationReference>map(this.getDelegate().getAnnotations(), _function));
}
 
Example #22
Source File: ProblemSupportImpl.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public EStructuralFeature getSignificantFeature(final EObject obj) {
  EStructuralFeature _switchResult = null;
  boolean _matched = false;
  if (obj instanceof XtendTypeDeclaration) {
    _matched=true;
    _switchResult = XtendPackage.eINSTANCE.getXtendTypeDeclaration_Name();
  }
  if (!_matched) {
    if (obj instanceof XtendField) {
      _matched=true;
      _switchResult = XtendPackage.eINSTANCE.getXtendField_Name();
    }
  }
  if (!_matched) {
    if (obj instanceof XtendFunction) {
      _matched=true;
      _switchResult = XtendPackage.eINSTANCE.getXtendFunction_Name();
    }
  }
  if (!_matched) {
    if (obj instanceof JvmFormalParameter) {
      _matched=true;
      _switchResult = TypesPackage.eINSTANCE.getJvmFormalParameter_Name();
    }
  }
  if (!_matched) {
    if (obj instanceof XAnnotation) {
      _matched=true;
      _switchResult = XAnnotationsPackage.eINSTANCE.getXAnnotation_AnnotationType();
    }
  }
  return _switchResult;
}
 
Example #23
Source File: ErrorTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testErrorModel_007() throws Exception {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("@Data class A {}");
  _builder.newLine();
  _builder.append("@Data class");
  _builder.newLine();
  final XtendFile file = this.processWithoutException(_builder);
  final XtendTypeDeclaration typeDeclaration = IterableExtensions.<XtendTypeDeclaration>last(file.getXtendTypes());
  final EList<XAnnotation> annotations = typeDeclaration.getAnnotations();
  final IResolvedTypes resolvedTypes = this.typeResolver.resolveTypes(IterableExtensions.<XAnnotation>head(annotations));
  Assert.assertNotNull(resolvedTypes.getActualType(IterableExtensions.<XAnnotation>head(annotations)));
}
 
Example #24
Source File: ConstantExpressionsInterpreter.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public Object internalEvaluate(final XExpression it, final Context ctx) {
  if (it instanceof XBinaryOperation) {
    return _internalEvaluate((XBinaryOperation)it, ctx);
  } else if (it instanceof XFeatureCall) {
    return _internalEvaluate((XFeatureCall)it, ctx);
  } else if (it instanceof XListLiteral) {
    return _internalEvaluate((XListLiteral)it, ctx);
  } else if (it instanceof XMemberFeatureCall) {
    return _internalEvaluate((XMemberFeatureCall)it, ctx);
  } else if (it instanceof XUnaryOperation) {
    return _internalEvaluate((XUnaryOperation)it, ctx);
  } else if (it instanceof XBooleanLiteral) {
    return _internalEvaluate((XBooleanLiteral)it, ctx);
  } else if (it instanceof XCastedExpression) {
    return _internalEvaluate((XCastedExpression)it, ctx);
  } else if (it instanceof XNumberLiteral) {
    return _internalEvaluate((XNumberLiteral)it, ctx);
  } else if (it instanceof XStringLiteral) {
    return _internalEvaluate((XStringLiteral)it, ctx);
  } else if (it instanceof XTypeLiteral) {
    return _internalEvaluate((XTypeLiteral)it, ctx);
  } else if (it instanceof XAnnotation) {
    return _internalEvaluate((XAnnotation)it, ctx);
  } else if (it != null) {
    return _internalEvaluate(it, ctx);
  } else if (it == null) {
    return _internalEvaluate((Void)null, ctx);
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(it, ctx).toString());
  }
}
 
Example #25
Source File: XtendHighlightingCalculator.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
protected void highlightElement(XtendAnnotationType xtendType, IHighlightedPositionAcceptor acceptor, CancelIndicator cancelIndicator) {		
	for(XAnnotation annotation: xtendType.getAnnotations()) {
		JvmType annotationType = annotation.getAnnotationType();
		if (annotationType != null && !annotationType.eIsProxy() && Active.class.getName().equals(annotationType.getIdentifier())) {
			highlightFeature(acceptor, annotation, XtendPackage.Literals.XTEND_TYPE_DECLARATION__NAME, ACTIVE_ANNOTATION);
			break;
		}
	}
}
 
Example #26
Source File: SARLValidator.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Check for reserved annotations.
 *
 * @param annotationTarget thee target to test.
 */
@Check
public void checkReservedAnnotation(XtendAnnotationTarget annotationTarget) {
	if (!isIgnored(USED_RESERVED_SARL_ANNOTATION)) {
		if (annotationTarget.getAnnotations().isEmpty() || !isRelevantAnnotationTarget(annotationTarget)) {
			return;
		}
		final QualifiedName reservedPackage = this.qualifiedNameConverter.toQualifiedName(
				EarlyExit.class.getPackage().getName());
		final String earlyExitAnnotation = EarlyExit.class.getName();
		final String errorOnCallAnnotation = ErrorOnCall.class.getName();
		final String warningOnCallAnnotation = WarningOnCall.class.getName();
		final String infoOnCallAnnotation = InfoOnCall.class.getName();
		for (final XAnnotation annotation : annotationTarget.getAnnotations()) {
			final JvmType type = annotation.getAnnotationType();
			if (type != null && !type.eIsProxy()) {
				if (Objects.equal(type.getIdentifier(), earlyExitAnnotation)) {
					// Special case: EarlyExit is allowed on events for declaring early-exit events
					if (!(annotationTarget instanceof SarlEvent)) {
						addIssue(
								MessageFormat.format(Messages.SARLValidator_87, type.getSimpleName()),
								annotation,
								USED_RESERVED_SARL_ANNOTATION);
					}
				} else if (!Objects.equal(type.getIdentifier(), errorOnCallAnnotation)
						&& !Objects.equal(type.getIdentifier(), warningOnCallAnnotation)
						&& !Objects.equal(type.getIdentifier(), infoOnCallAnnotation)) {
					final QualifiedName annotationName = this.qualifiedNameConverter.toQualifiedName(
							type.getIdentifier());
					if (annotationName.startsWith(reservedPackage)) {
						addIssue(
								MessageFormat.format(Messages.SARLValidator_87, type.getSimpleName()),
								annotation,
								USED_RESERVED_SARL_ANNOTATION);
					}
				}
			}
		}
	}
}
 
Example #27
Source File: ErrorTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testErrorModel_009() throws Exception {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("class X {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("@Property val S");
  _builder.newLine();
  final XtendFile file = this.processWithoutException(_builder);
  final XtendTypeDeclaration typeDeclaration = IterableExtensions.<XtendTypeDeclaration>last(file.getXtendTypes());
  final EList<XAnnotation> annotations = IterableExtensions.<XtendMember>last(typeDeclaration.getMembers()).getAnnotations();
  final IResolvedTypes resolvedTypes = this.typeResolver.resolveTypes(IterableExtensions.<XAnnotation>head(annotations));
  Assert.assertNotNull(resolvedTypes.getActualType(IterableExtensions.<XAnnotation>head(annotations)));
}
 
Example #28
Source File: SuppressWarningsAddModification.java    From sarl with Apache License 2.0 5 votes vote down vote up
private static XAnnotation findAnnotation(XtendAnnotationTarget target, JvmType suppressWarningsAnnotation) {
	for (final XAnnotation annotation : target.getAnnotations()) {
		if (Objects.equals(annotation.getAnnotationType().getQualifiedName(),
				suppressWarningsAnnotation.getIdentifier())) {
			return annotation;
		}
	}
	return null;
}
 
Example #29
Source File: ErrorTest.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testErrorModel_006() throws Exception {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("@Data class A {}");
  _builder.newLine();
  _builder.append("@");
  _builder.newLine();
  final XtendFile file = this.processWithoutException(_builder);
  final XtendTypeDeclaration typeDeclaration = IterableExtensions.<XtendTypeDeclaration>last(file.getXtendTypes());
  final EList<XAnnotation> annotations = typeDeclaration.getAnnotations();
  final IResolvedTypes resolvedTypes = this.typeResolver.resolveTypes(IterableExtensions.<XAnnotation>head(annotations));
  Assert.assertNotNull(resolvedTypes.getActualType(IterableExtensions.<XAnnotation>head(annotations)));
}
 
Example #30
Source File: AnnotationsValidatorTest.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Test public void testReferencedTypeIsNoEnum() throws Exception {
	XAnnotation annotation = annotation("@java.lang.Object(unknown = #[ new String() ])", false);
	List<Issue> issues = validator.validate(annotation);
	assertEquals(issues.toString(), 1, issues.size());
	Issue singleIssue = issues.get(0);
	assertEquals(IssueCodes.INCOMPATIBLE_TYPES, singleIssue.getCode());
	assertEquals(1, singleIssue.getOffset().intValue());
	assertEquals("java.lang.Object".length(), singleIssue.getLength().intValue());
}