Java Code Examples for org.eclipse.xtext.common.types.JvmStringAnnotationValue

The following examples show how to use org.eclipse.xtext.common.types.JvmStringAnnotationValue. These examples are extracted from open source projects. 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
@Test
public void testDefaultAnnotationAnnotationValue_02() throws Exception {
	JvmAnnotationAnnotationValue value = (JvmAnnotationAnnotationValue) getDefaultAnnotationValue(
			"annotationArrayValue");
	assertEquals(2, value.getValues().size());
	JvmAnnotationReference reference1 = value.getValues().get(0);
	assertEquals(TestAnnotationWithDefaults.NestedAnnotation.class.getName(),
			reference1.getAnnotation().getIdentifier());
	JvmAnnotationValue nestedAnnotationValue1 = reference1.getValues().get(0);
	assertTrue(nestedAnnotationValue1 instanceof JvmStringAnnotationValue);
	assertEquals("AnotherString", ((JvmStringAnnotationValue) nestedAnnotationValue1).getValues().get(0));
	JvmAnnotationReference reference2 = value.getValues().get(1);
	assertEquals(TestAnnotationWithDefaults.NestedAnnotation.class.getName(),
			reference2.getAnnotation().getIdentifier());
	JvmAnnotationValue nestedAnnotationValue2 = reference2.getValues().get(0);
	assertTrue(nestedAnnotationValue2 instanceof JvmStringAnnotationValue);
	assertEquals("MyString", ((JvmStringAnnotationValue) nestedAnnotationValue2).getValues().get(0));
}
 
Example 2
Source Project: xtext-extras   Source File: JvmTypesBuilder.java    License: Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Creates and returns an annotation reference of the given annotation type's name and the given value.
 * 
 * @param sourceElement
 *            the source element to associate the created element with.
 * @param annotationTypeName
 *            the type name of the created annotation.
 * @param value
 *            the value of the annotation reference. Can be <code>null</code> if the reference doesn't have any value.
 *            
 * @return a result representing an annotation reference to the given annotation type, <code>null<code> if 
 * 		sourceElement or annotationType are <code>null</code>.
 * 
 * @deprecated use {@link JvmAnnotationReferenceBuilder#annotationRef(String, String...)} instead
 */
//TODO Move up the code used in Xtend's CompilationUnitImpl so we can reuse it here.
/* @Nullable */ 
@Deprecated
public JvmAnnotationReference toAnnotation(/* @Nullable */ EObject sourceElement, /* @Nullable */ String annotationTypeName, /* @Nullable */ Object value) {
	JvmAnnotationReference result = typesFactory.createJvmAnnotationReference();
	JvmType jvmType = references.findDeclaredType(annotationTypeName, sourceElement);
	if (jvmType == null) {
		throw new IllegalArgumentException("The type "+annotationTypeName +" is not on the classpath.");
	}
	if (!(jvmType instanceof JvmAnnotationType)) {
		throw new IllegalArgumentException("The given class " + annotationTypeName + " is not an annotation type.");
	}
	result.setAnnotation((JvmAnnotationType) jvmType);
	if (value != null) {
		if (value instanceof String) {
			JvmStringAnnotationValue annotationValue = typesFactory.createJvmStringAnnotationValue();
			annotationValue.getValues().add((String) value);
			result.getExplicitValues().add(annotationValue);
		}
	}
	return result;
}
 
Example 3
/**
 * Creates and returns an annotation reference of the given annotation type's name and the given value.
 * 
 * @param annotationTypeName
 *            the type name of the created annotation.
 * @param values
 *            the string value of the annotation's 'values' property.
 *            
 * @return a result representing an annotation reference to the given annotation type, <code>null<code> if 
 * 		annotationType are <code>null</code>.  
 */
//TODO Move up the code used in Xtend's CompilationUnitImpl so we can reuse it here.
//TODO Support other types and setting non default properties
/* @Nullable */ 
public JvmAnnotationReference annotationRef(/* @Nullable */ String annotationTypeName, /* @Nullable */ String... values) {
	if (context == null || annotationTypeName == null)
		return null;
	JvmAnnotationReference result = typesFactory.createJvmAnnotationReference();
	JvmType jvmType = references.findDeclaredType(annotationTypeName, context);
	if (jvmType == null) {
		throw new IllegalArgumentException("The type "+annotationTypeName +" is not on the classpath.");
	}
	if (!(jvmType instanceof JvmAnnotationType)) {
		throw new IllegalArgumentException("The given class " + annotationTypeName + " is not an annotation type.");
	}
	result.setAnnotation((JvmAnnotationType) jvmType);
	if (values != null && values.length>0) {
		JvmStringAnnotationValue annotationValue = typesFactory.createJvmStringAnnotationValue();
		for (String value : values) {
			annotationValue.getValues().add(value);
		}
		result.getExplicitValues().add(annotationValue);
	}
	return result;
}
 
Example 4
@Test
public void testDefaultAnnotationAnnotationValue_02() throws Exception {
	JvmAnnotationAnnotationValue value = (JvmAnnotationAnnotationValue) getDefaultAnnotationValue(
			"annotationArrayValue");
	assertEquals(2, value.getValues().size());
	JvmAnnotationReference reference1 = value.getValues().get(0);
	assertEquals(TestAnnotationWithDefaults.NestedAnnotation.class.getName(),
			reference1.getAnnotation().getIdentifier());
	JvmAnnotationValue nestedAnnotationValue1 = reference1.getValues().get(0);
	assertTrue(nestedAnnotationValue1 instanceof JvmStringAnnotationValue);
	assertEquals("AnotherString", ((JvmStringAnnotationValue) nestedAnnotationValue1).getValues().get(0));
	JvmAnnotationReference reference2 = value.getValues().get(1);
	assertEquals(TestAnnotationWithDefaults.NestedAnnotation.class.getName(),
			reference2.getAnnotation().getIdentifier());
	JvmAnnotationValue nestedAnnotationValue2 = reference2.getValues().get(0);
	assertTrue(nestedAnnotationValue2 instanceof JvmStringAnnotationValue);
	assertEquals("MyString", ((JvmStringAnnotationValue) nestedAnnotationValue2).getValues().get(0));
}
 
Example 5
@Test
public void testDefaultAnnotationAnnotationValue_02() throws Exception {
	JvmAnnotationAnnotationValue value = (JvmAnnotationAnnotationValue) getDefaultAnnotationValue(
			"annotationArrayValue");
	assertEquals(2, value.getValues().size());
	JvmAnnotationReference reference1 = value.getValues().get(0);
	assertEquals(TestAnnotationWithDefaults.NestedAnnotation.class.getName(),
			reference1.getAnnotation().getIdentifier());
	JvmAnnotationValue nestedAnnotationValue1 = reference1.getValues().get(0);
	assertTrue(nestedAnnotationValue1 instanceof JvmStringAnnotationValue);
	assertEquals("AnotherString", ((JvmStringAnnotationValue) nestedAnnotationValue1).getValues().get(0));
	JvmAnnotationReference reference2 = value.getValues().get(1);
	assertEquals(TestAnnotationWithDefaults.NestedAnnotation.class.getName(),
			reference2.getAnnotation().getIdentifier());
	JvmAnnotationValue nestedAnnotationValue2 = reference2.getValues().get(0);
	assertTrue(nestedAnnotationValue2 instanceof JvmStringAnnotationValue);
	assertEquals("MyString", ((JvmStringAnnotationValue) nestedAnnotationValue2).getValues().get(0));
}
 
Example 6
Source Project: xtext-eclipse   Source File: JdtBasedTypeFactory.java    License: Eclipse Public License 2.0 6 votes vote down vote up
private JvmAnnotationValue createStringAnnotationValue(Object value) {
	JvmStringAnnotationValue annotationValue = TypesFactory.eINSTANCE.createJvmStringAnnotationValue();
	if (value != null) {
		@SuppressWarnings("unchecked")
		InternalEList<Object> values = (InternalEList<Object>)(InternalEList<?>)annotationValue.getValues();
		if (value instanceof Object[]) {
			for (Object element : (Object[])value) {
				if (element instanceof String) {
					values.addUnique(element);
				}
			}
		} else {
			values.addUnique(value);
		}
	}
	return annotationValue;
}
 
Example 7
Source Project: sarl   Source File: SARLValidator.java    License: Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"checkstyle:cyclomaticcomplexity"})
private static String parseIssueOnCallAnnotation(List<JvmAnnotationValue> values) {
	final StringBuilder message = new StringBuilder();
	for (final JvmAnnotationValue value : values) {
		if (value instanceof JvmStringAnnotationValue) {
			message.append(((JvmStringAnnotationValue) value).getValues());
		} else if (value instanceof JvmCustomAnnotationValue) {
			for (final Object obj : ((JvmCustomAnnotationValue) value).getValues()) {
				if (obj instanceof XStringLiteral) {
					message.append(((XStringLiteral) obj).getValue());
				}
			}
		}
	}
	return message.toString();
}
 
Example 8
@Test
public void testStringAnnotationValue_01() throws Exception {
	JvmStringAnnotationValue value = (JvmStringAnnotationValue) getAnnotationValue("stringValue");
	assertEquals(1, value.getValues().size());
	String s = value.getValues().get(0);
	assertEquals("stringValue", s);
}
 
Example 9
@Test
public void testStringAnnotationValue_02() throws Exception {
	JvmStringAnnotationValue value = (JvmStringAnnotationValue) getAnnotationValue("stringArrayValue");
	assertEquals(4, value.getValues().size());
	String s = value.getValues().get(0);
	assertEquals("array", s);
	s = value.getValues().get(1);
	assertEquals("value", s);
	s = value.getValues().get(2);
	assertEquals("duplicate", s);
	s = value.getValues().get(3);
	assertEquals("duplicate", s);
}
 
Example 10
@Test
public void testStringAnnotationValue_03() throws Exception {
	JvmStringAnnotationValue value = (JvmStringAnnotationValue) getMethodParameterAnnotationValue("stringValue");
	assertEquals(1, value.getValues().size());
	String s = value.getValues().get(0);
	assertEquals("stringValue", s);
}
 
Example 11
@Test
public void testStringAnnotationValue_04() throws Exception {
	JvmStringAnnotationValue value = (JvmStringAnnotationValue) getMethodParameterAnnotationValue(
			"stringArrayValue");
	assertEquals(2, value.getValues().size());
	String s = value.getValues().get(0);
	assertEquals("array", s);
	s = value.getValues().get(1);
	assertEquals("value", s);
}
 
Example 12
@Test
public void testStringAnnotationValue_05() throws Exception {
	JvmStringAnnotationValue value = (JvmStringAnnotationValue) getConstructorParameterAnnotationValue(
			"stringValue");
	assertEquals(1, value.getValues().size());
	String s = value.getValues().get(0);
	assertEquals("stringValue", s);
}
 
Example 13
@Test
public void testStringAnnotationValue_06() throws Exception {
	JvmStringAnnotationValue value = (JvmStringAnnotationValue) getConstructorParameterAnnotationValue(
			"stringArrayValue");
	assertEquals(2, value.getValues().size());
	String s = value.getValues().get(0);
	assertEquals("array", s);
	s = value.getValues().get(1);
	assertEquals("value", s);
}
 
Example 14
@Test
public void testAnnotationAnnotationValue_01() throws Exception {
	JvmAnnotationAnnotationValue value = (JvmAnnotationAnnotationValue) getAnnotationValue("annotationValue");
	assertEquals(1, value.getValues().size());
	JvmAnnotationReference annotationReference = value.getValues().get(0);
	assertEquals(TestAnnotation.NestedAnnotation.class.getName(),
			annotationReference.getAnnotation().getIdentifier());
	assertEquals(1, annotationReference.getValues().size());
	JvmStringAnnotationValue nestedValue = (JvmStringAnnotationValue) annotationReference.getValues().get(0);
	assertEquals(1, nestedValue.getValues().size());
	assertEquals("MyString", nestedValue.getValues().get(0));
}
 
Example 15
@Test
public void testAnnotationAnnotationValue_02() throws Exception {
	JvmAnnotationAnnotationValue value = (JvmAnnotationAnnotationValue) getConstructorParameterAnnotationValue(
			"annotationValue");
	assertEquals(1, value.getValues().size());
	JvmAnnotationReference annotationReference = value.getValues().get(0);
	assertEquals(TestAnnotation.NestedAnnotation.class.getName(),
			annotationReference.getAnnotation().getIdentifier());
	assertEquals(1, annotationReference.getValues().size());
	JvmStringAnnotationValue nestedValue = (JvmStringAnnotationValue) annotationReference.getValues().get(0);
	assertEquals(1, nestedValue.getValues().size());
	assertEquals("MyString", nestedValue.getValues().get(0));
}
 
Example 16
@Test
public void testAnnotationAnnotationValue_03() throws Exception {
	JvmAnnotationAnnotationValue value = (JvmAnnotationAnnotationValue) getMethodParameterAnnotationValue(
			"annotationValue");
	assertEquals(1, value.getValues().size());
	JvmAnnotationReference annotationReference = value.getValues().get(0);
	assertEquals(TestAnnotation.NestedAnnotation.class.getName(),
			annotationReference.getAnnotation().getIdentifier());
	assertEquals(1, annotationReference.getValues().size());
	JvmStringAnnotationValue nestedValue = (JvmStringAnnotationValue) annotationReference.getValues().get(0);
	assertEquals(1, nestedValue.getValues().size());
	assertEquals("MyString", nestedValue.getValues().get(0));
}
 
Example 17
@Test
public void testAnnotatedMethod_01() throws Exception {
	String typeName = TestAnnotation.Annotated.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmOperation operation = getMethodFromType(type, TestAnnotation.Annotated.class, "method()");
	assertNotNull(operation);
	JvmStringAnnotationValue value = (JvmStringAnnotationValue) getExplicitAnnotationValue("value", operation);
	assertEquals(1, value.getValues().size());
	String s = value.getValues().get(0);
	assertEquals("method", s);
}
 
Example 18
@Test
public void testAnnotatedConstructor_01() throws Exception {
	String typeName = TestAnnotation.Annotated.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmConstructor constructor = getConstructorFromType(type, TestAnnotation.Annotated.class, "Annotated()");
	assertNotNull(constructor);
	JvmStringAnnotationValue value = (JvmStringAnnotationValue) getExplicitAnnotationValue("value", constructor);
	assertEquals(1, value.getValues().size());
	String s = value.getValues().get(0);
	assertEquals("constructor", s);
}
 
Example 19
@Test
public void testAnnotatedConstructor_02() throws Exception {
	String typeName = TestAnnotation.Annotated.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmConstructor constructor = getConstructorFromType(type, TestAnnotation.Annotated.class,
			"Annotated(java.lang.String)");
	assertNotNull(constructor);
	JvmStringAnnotationValue value = (JvmStringAnnotationValue) getExplicitAnnotationValue("value", constructor);
	assertEquals(1, value.getValues().size());
	String s = value.getValues().get(0);
	assertEquals("secondConstructor", s);
}
 
Example 20
@Test
public void testAnnotatedConstructor_03() throws Exception {
	String typeName = TestAnnotation.Annotated.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmConstructor constructor = getConstructorFromType(type, TestAnnotation.Annotated.class,
			"Annotated(java.lang.String,T)");
	assertNotNull(constructor);
	JvmStringAnnotationValue value = (JvmStringAnnotationValue) getExplicitAnnotationValue("value", constructor);
	assertEquals(1, value.getValues().size());
	String s = value.getValues().get(0);
	assertEquals("parameterizedConstructor", s);
}
 
Example 21
@Test
public void testAnnotatedConstructor_04() throws Exception {
	String typeName = TestAnnotation.Annotated.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmConstructor constructor = getConstructorFromType(type, TestAnnotation.Annotated.class,
			"Annotated(java.lang.String,java.lang.String)");
	assertNotNull(constructor);
	JvmStringAnnotationValue value = (JvmStringAnnotationValue) getExplicitAnnotationValue("value", constructor);
	assertEquals(1, value.getValues().size());
	String s = value.getValues().get(0);
	assertEquals("thirdConstructorWithBody", s);
}
 
Example 22
@Test
public void testAnnotatedField_01() throws Exception {
	String typeName = TestAnnotation.Annotated.class.getName();
	JvmGenericType type = (JvmGenericType) getTypeProvider().findTypeByName(typeName);
	JvmField field = getFieldFromType(type, TestAnnotation.Annotated.class, "field");
	assertNotNull(field);
	JvmStringAnnotationValue value = (JvmStringAnnotationValue) getDefaultOrExplicitAnnotationValue("value", field);
	assertEquals(1, value.getValues().size());
	String s = value.getValues().get(0);
	assertEquals("MyString", s);
}
 
Example 23
@Test
public void testDefaultAnnotationAnnotationValue_01() throws Exception {
	JvmAnnotationAnnotationValue value = (JvmAnnotationAnnotationValue) getDefaultAnnotationValue(
			"annotationValue");
	assertEquals(1, value.getValues().size());
	JvmAnnotationReference reference = value.getValues().get(0);
	assertEquals(TestAnnotationWithDefaults.NestedAnnotation.class.getName(),
			reference.getAnnotation().getIdentifier());
	JvmAnnotationValue nestedAnnotationValue = reference.getValues().get(0);
	assertTrue(nestedAnnotationValue instanceof JvmStringAnnotationValue);
	assertEquals("AnotherString", ((JvmStringAnnotationValue) nestedAnnotationValue).getValues().get(0));
}
 
Example 24
@Test
public void testDefaultStringAnnotationValue_01() throws Exception {
	JvmStringAnnotationValue value = (JvmStringAnnotationValue) getDefaultAnnotationValue("stringValue");
	assertEquals(1, value.getValues().size());
	String string = value.getValues().get(0);
	assertEquals("", string);
}
 
Example 25
@Test
public void testDefaultStringAnnotationValue_02() throws Exception {
	JvmStringAnnotationValue value = (JvmStringAnnotationValue) getDefaultAnnotationValue("stringArrayValue");
	assertEquals(1, value.getValues().size());
	String string = value.getValues().get(0);
	assertEquals("arrayValue", string);
}
 
Example 26
protected void checkDefaultAnnotationValues(JvmAnnotationReference annotationReference) {
	List<JvmAnnotationValue> values = annotationReference.getValues();
	assertEquals(2, values.size());
	Map<String, String> nameToValue = Maps.newHashMap();
	nameToValue.put("emptyString", "");
	nameToValue.put("string", "string");
	for (JvmAnnotationValue value : values) {
		String defaultValue = nameToValue.remove(value.getValueName());
		assertNotNull(value.getValueName(), defaultValue);
		JvmStringAnnotationValue castedValue = (JvmStringAnnotationValue) value;
		assertEquals(1, castedValue.getValues().size());
		assertEquals(defaultValue, castedValue.getValues().get(0));
	}
	assertTrue(nameToValue.isEmpty());
}
 
Example 27
Source Project: xtext-extras   Source File: JvmModelGenerator.java    License: Eclipse Public License 2.0 5 votes vote down vote up
protected void _toJavaLiteral(final JvmStringAnnotationValue it, final ITreeAppendable appendable, final GeneratorConfig config) {
  final Procedure1<String> _function = (String it_1) -> {
    String _doConvertToJavaString = this.doConvertToJavaString(it_1.toString());
    String _plus = ("\"" + _doConvertToJavaString);
    String _plus_1 = (_plus + "\"");
    appendable.append(_plus_1);
  };
  this._loopExtensions.<String>forEachWithShortcut(appendable, it.getValues(), _function);
}
 
Example 28
@Test
public void testStringAnnotationValue_01() throws Exception {
	JvmStringAnnotationValue value = (JvmStringAnnotationValue) getAnnotationValue("stringValue");
	assertEquals(1, value.getValues().size());
	String s = value.getValues().get(0);
	assertEquals("stringValue", s);
}
 
Example 29
@Test
public void testStringAnnotationValue_02() throws Exception {
	JvmStringAnnotationValue value = (JvmStringAnnotationValue) getAnnotationValue("stringArrayValue");
	assertEquals(4, value.getValues().size());
	String s = value.getValues().get(0);
	assertEquals("array", s);
	s = value.getValues().get(1);
	assertEquals("value", s);
	s = value.getValues().get(2);
	assertEquals("duplicate", s);
	s = value.getValues().get(3);
	assertEquals("duplicate", s);
}
 
Example 30
@Test
public void testStringAnnotationValue_03() throws Exception {
	JvmStringAnnotationValue value = (JvmStringAnnotationValue) getMethodParameterAnnotationValue("stringValue");
	assertEquals(1, value.getValues().size());
	String s = value.getValues().get(0);
	assertEquals("stringValue", s);
}