org.eclipse.xtext.common.types.JvmAnnotationValue Java Examples

The following examples show how to use org.eclipse.xtext.common.types.JvmAnnotationValue. 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: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
private JvmAnnotationValue createLongAnnotationValue(Object value) {
	JvmLongAnnotationValue annotationValue = TypesFactory.eINSTANCE.createJvmLongAnnotationValue();
	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 Long) {
					values.addUnique(element);
				} else if (element != null) {
					values.addUnique(((Number)element).longValue());
				}
			}
		} else if (value instanceof Long) {
			values.addUnique(value);
		} else if (value instanceof Number) {
			values.addUnique(((Number)value).longValue());
		}
	}
	return annotationValue;
}
 
Example #2
Source File: JvmAnnotationReferencePrinter.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
protected String _internalToString(JvmAnnotationReference reference) {
	StringBuilder buffer = new StringBuilder();
	buffer.append("@");
	buffer.append(createLinkWithLabel(XtextElementLinks.XTEXTDOC_SCHEME, EcoreUtil.getURI(reference.getAnnotation()), reference.getAnnotation().getSimpleName()));

	List<JvmAnnotationValue> explicitValues = reference.getExplicitValues();
	boolean needsExplicitProperties = explicitValues.size() > 1 //
			|| (!explicitValues.isEmpty() && explicitValues.get(0).getOperation() != null //
					&& !"value".equals(explicitValues.get(0).getOperation().getSimpleName()));

	if (!explicitValues.isEmpty()) {
		buffer.append("(");
		buffer.append(explicitValues.stream().map(explicitValue -> {
			StringBuilder builder = new StringBuilder();
			if (needsExplicitProperties) {
				Iterable<JvmOperation> declaredOperations = reference.getAnnotation().getDeclaredOperations();
				builder.append(createLinkToOperation(explicitValue.getOperation(), declaredOperations));
				builder.append("=");
			}
			builder.append(internalToString(explicitValue));
			return builder.toString();
		}).collect(Collectors.joining(", ")));
		buffer.append(")");
	}
	return buffer.toString();
}
 
Example #3
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
private JvmAnnotationValue createFloatAnnotationValue(Object value) {
	JvmFloatAnnotationValue annotationValue = TypesFactory.eINSTANCE.createJvmFloatAnnotationValue();
	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 Float) {
					values.addUnique(element);
				} else if (element != null) {
					values.addUnique(((Number)element).floatValue());
				}
			}
		} else if (value instanceof Float) {
			values.addUnique(value);
		} else if (value instanceof Number) {
			values.addUnique(((Number)value).floatValue());
		}
	}
	return annotationValue;
}
 
Example #4
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@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
Source File: JvmModelGenerator.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public void toJava(final JvmAnnotationValue it, final ITreeAppendable appendable, final GeneratorConfig config) {
  JvmOperation _operation = it.getOperation();
  boolean _tripleNotEquals = (_operation != null);
  if (_tripleNotEquals) {
    String _simpleName = it.getOperation().getSimpleName();
    boolean _tripleEquals = (_simpleName == null);
    if (_tripleEquals) {
      return;
    }
    appendable.append(it.getOperation().getSimpleName());
    appendable.append(" = ");
  } else {
    EObject _eContainer = it.eContainer();
    int _size = ((JvmAnnotationReference) _eContainer).getExplicitValues().size();
    boolean _greaterThan = (_size > 1);
    if (_greaterThan) {
      appendable.append("value = ");
    }
  }
  this.toJavaLiteral(it, appendable, config);
}
 
Example #6
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with 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 File: LogicalContainerAwareReentrantTypeResolver.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void recordAnnotationExpressions(List<JvmAnnotationReference> annotations) {
	for(JvmAnnotationReference annotation: annotations) {
		EObject sourceElement = getSourceElement(annotation);
		if (sourceElement != annotation) {
			rootedInstances.add(sourceElement);
		} else {
			for(JvmAnnotationValue value: annotation.getExplicitValues()) {
				if (value instanceof JvmCustomAnnotationValue) {
					JvmCustomAnnotationValue custom = (JvmCustomAnnotationValue) value;
					for(Object object: custom.getValues()) {
						if (object instanceof XExpression) {
							rootedInstances.add(sourceElement);
						}
					}
				} else if (value instanceof JvmAnnotationAnnotationValue) {
					recordAnnotationExpressions(((JvmAnnotationAnnotationValue) value).getValues());
				}
			}
		}
	}
}
 
Example #8
Source File: LogicalContainerAwareReentrantTypeResolver.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void computeAnnotationTypes(ResolvedTypes resolvedTypes, IFeatureScopeSession featureScopeSession, List<JvmAnnotationReference> annotations) {
	for(JvmAnnotationReference annotation: annotations) {
		EObject sourceElement = getSourceElement(annotation);
		if (sourceElement != annotation) {
			computeTypes(resolvedTypes, featureScopeSession, sourceElement);
		} else {
			for(JvmAnnotationValue value: annotation.getExplicitValues()) {
				if (value instanceof JvmCustomAnnotationValue) {
					JvmCustomAnnotationValue custom = (JvmCustomAnnotationValue) value;
					for(Object object: custom.getValues()) {
						if (object instanceof XExpression) {
							AnnotationValueTypeComputationState state = new AnnotationValueTypeComputationState(resolvedTypes, featureScopeSession, value, (XExpression) object);
							state.computeTypes();
						}
					}
				} else if (value instanceof JvmAnnotationAnnotationValue) {
					computeAnnotationTypes(resolvedTypes, featureScopeSession, ((JvmAnnotationAnnotationValue) value).getValues());
				}
			}
		}
	}
}
 
Example #9
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @since 2.4
 */
protected JvmAnnotationReference createAnnotationReference(/* @NonNull */ IAnnotationBinding annotation) {
	JvmAnnotationReference annotationReference = TypesFactory.eINSTANCE.createJvmAnnotationReference();
	ITypeBinding annotationType = annotation.getAnnotationType();
	annotationReference.setAnnotation(createAnnotationProxy(annotationType));
	InternalEList<JvmAnnotationValue> values = (InternalEList<JvmAnnotationValue>)annotationReference.getExplicitValues();
	IMemberValuePairBinding[] allMemberValuePairs = annotation.getDeclaredMemberValuePairs();
	for (IMemberValuePairBinding memberValuePair : allMemberValuePairs) {
		IMethodBinding methodBinding = memberValuePair.getMethodBinding();
		if (methodBinding != null) {
			try {
				values.addUnique(createAnnotationValue(annotationType, memberValuePair.getValue(), methodBinding));
			} catch(NullPointerException npe) {
				// memberValuePair#getValue may throw an NPE if the methodBinding has no return type
				if (methodBinding.getReturnType() != null) {
					throw npe;
				} else {
					if (log.isDebugEnabled()) {
						log.debug(npe.getMessage(), npe);
					}
				}
			}
		}
	}
	return annotationReference;
}
 
Example #10
Source File: AbstractTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@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 #11
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
private JvmAnnotationValue createTypeAnnotationValue(Object value) {
	JvmTypeAnnotationValue annotationValue = TypesFactory.eINSTANCE.createJvmTypeAnnotationValue();
	if (value != null) {
		InternalEList<JvmTypeReference> values = (InternalEList<JvmTypeReference>)annotationValue.getValues();
		if (value instanceof Object[]) {
			for (Object element : (Object[])value) {
				if (element instanceof ITypeBinding) {
					values.addUnique(createTypeReference((ITypeBinding)element));
				}
			}
		} else if (value instanceof ITypeBinding) {
			values.addUnique(createTypeReference((ITypeBinding)value));
		}
	}
	return annotationValue;
}
 
Example #12
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public JvmAnnotationValue getConstructorParameterAnnotationValue(String name, boolean defaultValue) {
	String typeName = TestAnnotation.Annotated.class.getName();
	JvmDeclaredType type = (JvmDeclaredType) getTypeProvider().findTypeByName(typeName);
	JvmConstructor constructor = getConstructorFromType(type, TestAnnotation.Annotated.class,
			"Annotated(java.lang.String,java.lang.String,java.lang.String)");
	JvmAnnotationTarget target = constructor.getParameters().get(0);
	JvmAnnotationValue result = getDefaultOrExplicitAnnotationValue(name, target);
	if (defaultValue) {
		if (isDefaultValueSupported()) {
			assertTrue(result.eContainer() instanceof JvmOperation);
		} else {
			assertFalse(result.eContainer() instanceof JvmOperation);
		}
	} else {
		assertFalse(result.eContainer() instanceof JvmOperation);
	}
	return result;
}
 
Example #13
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
private JvmAnnotationValue createByteAnnotationValue(Object value) {
	JvmByteAnnotationValue annotationValue = TypesFactory.eINSTANCE.createJvmByteAnnotationValue();
	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 Byte) {
					values.addUnique(element);
				} else if (element != null) {
					values.addUnique(((Number)element).byteValue());
				}
			}
		} else if (value instanceof Byte) {
			values.addUnique(value);
		} else if (value instanceof Number) {
			values.addUnique(((Number)value).byteValue());
		}
	}
	return annotationValue;
}
 
Example #14
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
public JvmAnnotationValue getMethodParameterAnnotationValue(String name, boolean defaultValue) {
	String typeName = TestAnnotation.Annotated.class.getName();
	JvmDeclaredType type = (JvmDeclaredType) getTypeProvider().findTypeByName(typeName);
	JvmOperation method = getMethodFromType(type, TestAnnotation.Annotated.class,
			"method(java.lang.String,java.lang.String,java.lang.String)");
	JvmAnnotationTarget target = method.getParameters().get(0);
	JvmAnnotationValue result = getDefaultOrExplicitAnnotationValue(name, target);
	if (defaultValue) {
		if (isDefaultValueSupported()) {
			assertTrue(result.eContainer() instanceof JvmOperation);
		} else {
			assertFalse(result.eContainer() instanceof JvmOperation);
		}
	} else {
		assertFalse(result.eContainer() instanceof JvmOperation);
	}
	return result;
}
 
Example #15
Source File: AbstractTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
public JvmAnnotationValue getMethodParameterAnnotationValue(String name, boolean defaultValue) {
	String typeName = TestAnnotation.Annotated.class.getName();
	JvmDeclaredType type = (JvmDeclaredType) getTypeProvider().findTypeByName(typeName);
	JvmOperation method = getMethodFromType(type, TestAnnotation.Annotated.class,
			"method(java.lang.String,java.lang.String,java.lang.String)");
	JvmAnnotationTarget target = method.getParameters().get(0);
	JvmAnnotationValue result = getDefaultOrExplicitAnnotationValue(name, target);
	if (defaultValue) {
		if (isDefaultValueSupported()) {
			assertTrue(result.eContainer() instanceof JvmOperation);
		} else {
			assertFalse(result.eContainer() instanceof JvmOperation);
		}
	} else {
		assertFalse(result.eContainer() instanceof JvmOperation);
	}
	return result;
}
 
Example #16
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
private JvmAnnotationValue createCharAnnotationValue(Object value) {
	JvmCharAnnotationValue annotationValue = TypesFactory.eINSTANCE.createJvmCharAnnotationValue();
	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 Character) {
					values.addUnique(element);
				}
			}
		} else {
			values.addUnique(value);
		}
	}
	return annotationValue;
}
 
Example #17
Source File: AbstractTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
public JvmAnnotationValue getConstructorParameterAnnotationValue(String name, boolean defaultValue) {
	String typeName = TestAnnotation.Annotated.class.getName();
	JvmDeclaredType type = (JvmDeclaredType) getTypeProvider().findTypeByName(typeName);
	JvmConstructor constructor = getConstructorFromType(type, TestAnnotation.Annotated.class,
			"Annotated(java.lang.String,java.lang.String,java.lang.String)");
	JvmAnnotationTarget target = constructor.getParameters().get(0);
	JvmAnnotationValue result = getDefaultOrExplicitAnnotationValue(name, target);
	if (defaultValue) {
		if (isDefaultValueSupported()) {
			assertTrue(result.eContainer() instanceof JvmOperation);
		} else {
			assertFalse(result.eContainer() instanceof JvmOperation);
		}
	} else {
		assertFalse(result.eContainer() instanceof JvmOperation);
	}
	return result;
}
 
Example #18
Source File: AbstractTypeProviderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@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 #19
Source File: JvmModelTests.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testEmptyListAsAnnotationValueDefault() {
  try {
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("annotation Foo {");
    _builder.newLine();
    _builder.append("\t");
    _builder.append("String[] bar = #[]");
    _builder.newLine();
    _builder.append("}");
    _builder.newLine();
    JvmMember _head = IterableExtensions.<JvmMember>head(this._iXtendJvmAssociations.getInferredAnnotationType(this.annotationType(_builder.toString())).getMembers());
    final JvmOperation inferred = ((JvmOperation) _head);
    Assert.assertEquals("java.lang.String[]", inferred.getReturnType().getIdentifier());
    JvmAnnotationValue _defaultValue = inferred.getDefaultValue();
    Assert.assertTrue((_defaultValue instanceof JvmCustomAnnotationValue));
    JvmAnnotationValue _defaultValue_1 = inferred.getDefaultValue();
    EObject _head_1 = IterableExtensions.<EObject>head(((JvmCustomAnnotationValue) _defaultValue_1).getValues());
    Assert.assertTrue((_head_1 instanceof XListLiteral));
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #20
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 #21
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 #22
Source File: SourceBasedJdtTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testClassAnnotationValue_09() throws Exception {
	IJavaProject project = projectProvider.getJavaProject(null);
	String typeName = EmptyAbstractClass.class.getName();
	IFile javaFile = (IFile) project.getProject().findMember(new Path("src/" + typeName.replace('.', '/') + ".java"));
	assertNotNull(javaFile);
	String content = Files.readStreamIntoString(javaFile.getContents());
	try {
		String newContent = content.replace(
				"public abstract ", 
				"@TestAnnotation( classArray = ) public abstract ");
		javaFile.setContents(new StringInputStream(newContent), IResource.NONE, new NullProgressMonitor());
		
		JvmDeclaredType type = (JvmDeclaredType) getTypeProvider().findTypeByName(typeName);
		List<JvmAnnotationReference> annotations = type.getAnnotations();
		assertEquals(1, annotations.size());
		JvmAnnotationReference annotation = annotations.get(0);
		assertEquals(1, annotation.getExplicitValues().size());
		JvmAnnotationValue value = annotation.getExplicitValues().get(0);
		assertTrue(value instanceof JvmTypeAnnotationValue);
		List<JvmTypeReference> typeLiterals = ((JvmTypeAnnotationValue) value).getValues();
		assertEquals(0, typeLiterals.size());
	} finally {
		javaFile.setContents(new StringInputStream(content), IResource.NONE, new NullProgressMonitor());
	}
}
 
Example #23
Source File: SourceBasedJdtTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testClassAnnotationValue_08() throws Exception {
	IJavaProject project = projectProvider.getJavaProject(null);
	String typeName = EmptyAbstractClass.class.getName();
	IFile javaFile = (IFile) project.getProject().findMember(new Path("src/" + typeName.replace('.', '/') + ".java"));
	assertNotNull(javaFile);
	String content = Files.readStreamIntoString(javaFile.getContents());
	try {
		String newContent = content.replace(
				"public abstract ", 
				"@TestAnnotation( classArray = { String.class, DoesNotExist.class, String.class } ) public abstract ");
		javaFile.setContents(new StringInputStream(newContent), IResource.NONE, new NullProgressMonitor());
		
		JvmDeclaredType type = (JvmDeclaredType) getTypeProvider().findTypeByName(typeName);
		List<JvmAnnotationReference> annotations = type.getAnnotations();
		assertEquals(1, annotations.size());
		JvmAnnotationReference annotation = annotations.get(0);
		assertEquals(1, annotation.getExplicitValues().size());
		JvmAnnotationValue value = annotation.getExplicitValues().get(0);
		assertTrue(value instanceof JvmTypeAnnotationValue);
		List<JvmTypeReference> typeLiterals = ((JvmTypeAnnotationValue) value).getValues();
		assertEquals(2, typeLiterals.size());
	} finally {
		javaFile.setContents(new StringInputStream(content), IResource.NONE, new NullProgressMonitor());
	}
}
 
Example #24
Source File: SourceBasedJdtTypeProviderTest.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testClassAnnotationValue_07() throws Exception {
	IJavaProject project = projectProvider.getJavaProject(null);
	String typeName = EmptyAbstractClass.class.getName();
	IFile javaFile = (IFile) project.getProject().findMember(new Path("src/" + typeName.replace('.', '/') + ".java"));
	assertNotNull(javaFile);
	String content = Files.readStreamIntoString(javaFile.getContents());
	try {
		String newContent = content.replace(
				"public abstract ", 
				"@SimpleAnnotation( type = DoesNotExist.class ) public abstract ");
		javaFile.setContents(new StringInputStream(newContent), IResource.NONE, new NullProgressMonitor());
		
		JvmDeclaredType type = (JvmDeclaredType) getTypeProvider().findTypeByName(typeName);
		List<JvmAnnotationReference> annotations = type.getAnnotations();
		assertEquals(1, annotations.size());
		JvmAnnotationReference annotation = annotations.get(0);
		assertEquals(1, annotation.getExplicitValues().size());
		JvmAnnotationValue value = annotation.getExplicitValues().get(0);
		assertTrue(value instanceof JvmTypeAnnotationValue);
		assertTrue(((JvmTypeAnnotationValue) value).getValues().isEmpty());
	} finally {
		javaFile.setContents(new StringInputStream(content), IResource.NONE, new NullProgressMonitor());
	}
}
 
Example #25
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
private JvmAnnotationValue createBooleanAnnotationValue(Object value) {
	JvmBooleanAnnotationValue annotationValue = TypesFactory.eINSTANCE.createJvmBooleanAnnotationValue();
	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 Boolean) {
					values.addUnique(element);
				}
			}
		} else {
			values.addUnique(value);
		}
	}
	return annotationValue;
}
 
Example #26
Source File: ReflectionTypeFactory.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected JvmAnnotationValue createAnnotationValue(Object value, Class<?> type) {
	EStructuralFeature.Setting result = createAnnotationValue(type);
	@SuppressWarnings("unchecked")
	InternalEList<Object> values = (InternalEList<Object>)result;
	if (type.isPrimitive() || String.class == type) {
		values.addUnique(value);
	} else if (type == Class.class) {
		Class<?> referencedClass = (Class<?>) value;
		JvmTypeReference reference = createTypeReference(referencedClass);
		values.addUnique(reference);
	} else if (type.isAnnotation()) {
		Annotation nestedAnnotation = (Annotation) value;
		values.addUnique(createAnnotationReference(nestedAnnotation));
	} else if (type.isEnum()) {
		Enum<?> e = (Enum<?>) value;
		JvmEnumerationLiteral proxy = createEnumLiteralProxy(e);
		values.addUnique(proxy);
	}
	return (JvmAnnotationValue)result.getEObject();
}
 
Example #27
Source File: JvmAnnotationReferenceImplCustom.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public EList<JvmAnnotationValue> getValues() {
	EList<JvmAnnotationValue> explicitValues = getExplicitValues();
	List<JvmOperation> operations = Lists.newArrayList(getAnnotation().getDeclaredOperations());
	if (operations.size() <= explicitValues.size()) {
		return ECollections.unmodifiableEList(explicitValues);
	}
	Set<JvmOperation> seenOperations = Sets.newHashSetWithExpectedSize(operations.size());
	BasicEList<JvmAnnotationValue> result = new BasicEList<JvmAnnotationValue>(operations.size());
	for(JvmAnnotationValue value: explicitValues) {
		seenOperations.add(value.getOperation());
		result.add(value);
	}
	for(JvmOperation operation: operations) {
		if (seenOperations.add(operation)) {
			JvmAnnotationValue defaultValue = operation.getDefaultValue();
			if (defaultValue != null) {
				result.add(defaultValue);
			}
		}
	}
	return ECollections.unmodifiableEList(result);
}
 
Example #28
Source File: AnnotationReferenceBuildContextImpl.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
protected void _setValue(final JvmAnnotationValue it, final Object value, final String componentType, final boolean mustBeArray) {
  if ((componentType == null)) {
    this.throwNotApplicable(it, value.getClass().getName());
  }
  boolean _or = false;
  if (mustBeArray) {
    _or = true;
  } else {
    JvmTypeReference _returnType = it.getOperation().getReturnType();
    JvmType _type = null;
    if (_returnType!=null) {
      _type=_returnType.getType();
    }
    EClass _eClass = null;
    if (_type!=null) {
      _eClass=_type.eClass();
    }
    boolean _equals = Objects.equal(_eClass, TypesPackage.Literals.JVM_ARRAY_TYPE);
    _or = _equals;
  }
  if (_or) {
    this.throwNotApplicable(it, (componentType + "[]"));
  }
  this.throwNotApplicable(it, componentType);
}
 
Example #29
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
private JvmAnnotationValue createIntAnnotationValue(Object value) {
	JvmIntAnnotationValue annotationValue = TypesFactory.eINSTANCE.createJvmIntAnnotationValue();
	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 Integer) {
					values.addUnique(element);
				} else if (element != null) {
					values.addUnique(((Number)element).intValue());
				}
			}
		} else if (value instanceof Integer) {
			values.addUnique(value);
		} else if (value instanceof Number) {
			values.addUnique(((Number)value).intValue());
		}
	}
	return annotationValue;
}
 
Example #30
Source File: JvmOperationImpl.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public void setDefaultValue(JvmAnnotationValue newDefaultValue)
{
	if (newDefaultValue != defaultValue)
	{
		NotificationChain msgs = null;
		if (defaultValue != null)
			msgs = ((InternalEObject)defaultValue).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - TypesPackage.JVM_OPERATION__DEFAULT_VALUE, null, msgs);
		if (newDefaultValue != null)
			msgs = ((InternalEObject)newDefaultValue).eInverseAdd(this, EOPPOSITE_FEATURE_BASE - TypesPackage.JVM_OPERATION__DEFAULT_VALUE, null, msgs);
		msgs = basicSetDefaultValue(newDefaultValue, msgs);
		if (msgs != null) msgs.dispatch();
	}
	else if (eNotificationRequired())
		eNotify(new ENotificationImpl(this, Notification.SET, TypesPackage.JVM_OPERATION__DEFAULT_VALUE, newDefaultValue, newDefaultValue));
}