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

The following examples show how to use org.eclipse.xtext.common.types.JvmCustomAnnotationValue. 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: 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 #2
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 #3
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 #4
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 #5
Source File: JvmTypesBuilderTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testIntegerAnnotation() {
  try {
    final XAnnotationsFactory f = XAnnotationsFactory.eINSTANCE;
    final XExpression e = this.expression("\'Foo\'");
    final XAnnotation anno = f.createXAnnotation();
    JvmType _findDeclaredType = this.references.findDeclaredType(TestAnnotation3.class, e);
    final JvmAnnotationType annotatiomType = ((JvmAnnotationType) _findDeclaredType);
    anno.setAnnotationType(annotatiomType);
    final XAnnotationElementValuePair pair = f.createXAnnotationElementValuePair();
    pair.setElement(IterableExtensions.<JvmOperation>head(annotatiomType.getDeclaredOperations()));
    pair.setValue(this.expression("10"));
    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.assertEquals(1, IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getValues().size());
    JvmAnnotationValue _head = IterableExtensions.<JvmAnnotationValue>head(IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getValues());
    final JvmCustomAnnotationValue value = ((JvmCustomAnnotationValue) _head);
    EObject _head_1 = IterableExtensions.<EObject>head(value.getValues());
    Assert.assertTrue((_head_1 instanceof XNumberLiteral));
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #6
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 #7
Source File: JvmAnnotationReferenceImpl.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public Expression getExpression(final String property) {
  final JvmOperation op = this.findOperation(property);
  final Function1<JvmAnnotationValue, Boolean> _function = (JvmAnnotationValue it) -> {
    return Boolean.valueOf((Objects.equal(it.getOperation(), op) || ((it.getOperation() == null) && Objects.equal(op.getSimpleName(), "value"))));
  };
  final JvmAnnotationValue annotationValue = IterableExtensions.<JvmAnnotationValue>findFirst(this.getDelegate().getValues(), _function);
  boolean _matched = false;
  if (annotationValue instanceof JvmCustomAnnotationValue) {
    _matched=true;
    EObject _head = IterableExtensions.<EObject>head(((JvmCustomAnnotationValue)annotationValue).getValues());
    final XExpression expression = ((XExpression) _head);
    if (((expression != null) && this.getCompilationUnit().isBelongedToCompilationUnit(expression))) {
      return this.getCompilationUnit().toExpression(expression);
    }
  }
  return null;
}
 
Example #8
Source File: SARLValidator.java    From sarl with 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 #9
Source File: JvmTypesBuilder.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Removes the given expression from its current logical container and creates a
 * fresh detached {@link JvmAnnotationValue}, that needs to be put into some {@link JvmAnnotationReference}
 * 
 * @param value the expression to use as annotation value
 * @return a {@link JvmAnnotationValue} that needs to be put into some {@link JvmAnnotationReference}
 * @since 2.4
 */
/* @Nullable */ 
public JvmAnnotationValue toJvmAnnotationValue(/* @Nullable */ XExpression value) {
	if (value != null) {
		JvmIdentifiableElement logicalContainer = logicalContainerProvider.getLogicalContainer(value);
		if (logicalContainer != null) {
			associator.removeLogicalChildAssociation(logicalContainer);
		}
		JvmCustomAnnotationValue annotationValue = typesFactory.createJvmCustomAnnotationValue();
		associator.associate(value, annotationValue);
		annotationValue.getValues().add(value);
		return annotationValue;
	}
	return null;
}
 
Example #10
Source File: FeatureCallCompiler.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
private static boolean isConstantExpression(JvmAnnotationReference reference) {
	for (final JvmAnnotationValue annotationValue: reference.getValues()) {
		if ("constantExpression".equals(annotationValue.getValueName())) {
			if (annotationValue instanceof JvmBooleanAnnotationValue) {
				return ((JvmBooleanAnnotationValue) annotationValue).getValues().get(0).booleanValue();
			} else if (annotationValue instanceof JvmCustomAnnotationValue) {
				final EObject value = ((JvmCustomAnnotationValue) annotationValue).getValues().get(0);
				if (value instanceof XBooleanLiteral) {
					return ((XBooleanLiteral) value).isIsTrue();
				}
			}
		}
	}
	return false;
}
 
Example #11
Source File: JvmModelGenerator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected void _toJavaLiteral(final JvmCustomAnnotationValue it, final ITreeAppendable appendable, final GeneratorConfig config) {
  boolean _isEmpty = it.getValues().isEmpty();
  if (_isEmpty) {
    appendable.append("{}");
  } else {
    final Procedure1<XExpression> _function = (XExpression it_1) -> {
      this.compiler.toJavaExpression(it_1, appendable);
    };
    this._loopExtensions.<XExpression>forEachWithShortcut(appendable, Iterables.<XExpression>filter(it.getValues(), XExpression.class), _function);
  }
}
 
Example #12
Source File: JvmCustomAnnotationValueItemProvider.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * This handles model notifications by calling {@link #updateChildren} to update any cached
 * children and by creating a viewer notification, which it passes to {@link #fireNotifyChanged}.
 * <!-- begin-user-doc -->
 * <!-- end-user-doc -->
 * @generated
 */
@Override
public void notifyChanged(Notification notification)
{
	updateChildren(notification);

	switch (notification.getFeatureID(JvmCustomAnnotationValue.class))
	{
		case TypesPackage.JVM_CUSTOM_ANNOTATION_VALUE__VALUES:
			fireNotifyChanged(new ViewerNotification(notification, notification.getNotifier(), false, true));
			return;
	}
	super.notifyChanged(notification);
}
 
Example #13
Source File: JvmModelGenerator.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public void toJavaLiteral(final JvmAnnotationValue value, final ITreeAppendable appendable, final GeneratorConfig config) {
  if (value instanceof JvmAnnotationAnnotationValue) {
    _toJavaLiteral((JvmAnnotationAnnotationValue)value, appendable, config);
    return;
  } else if (value instanceof JvmBooleanAnnotationValue) {
    _toJavaLiteral((JvmBooleanAnnotationValue)value, appendable, config);
    return;
  } else if (value instanceof JvmByteAnnotationValue) {
    _toJavaLiteral((JvmByteAnnotationValue)value, appendable, config);
    return;
  } else if (value instanceof JvmCharAnnotationValue) {
    _toJavaLiteral((JvmCharAnnotationValue)value, appendable, config);
    return;
  } else if (value instanceof JvmCustomAnnotationValue) {
    _toJavaLiteral((JvmCustomAnnotationValue)value, appendable, config);
    return;
  } else if (value instanceof JvmDoubleAnnotationValue) {
    _toJavaLiteral((JvmDoubleAnnotationValue)value, appendable, config);
    return;
  } else if (value instanceof JvmEnumAnnotationValue) {
    _toJavaLiteral((JvmEnumAnnotationValue)value, appendable, config);
    return;
  } else if (value instanceof JvmFloatAnnotationValue) {
    _toJavaLiteral((JvmFloatAnnotationValue)value, appendable, config);
    return;
  } else if (value instanceof JvmIntAnnotationValue) {
    _toJavaLiteral((JvmIntAnnotationValue)value, appendable, config);
    return;
  } else if (value instanceof JvmLongAnnotationValue) {
    _toJavaLiteral((JvmLongAnnotationValue)value, appendable, config);
    return;
  } else if (value instanceof JvmShortAnnotationValue) {
    _toJavaLiteral((JvmShortAnnotationValue)value, appendable, config);
    return;
  } else if (value instanceof JvmStringAnnotationValue) {
    _toJavaLiteral((JvmStringAnnotationValue)value, appendable, config);
    return;
  } else if (value instanceof JvmTypeAnnotationValue) {
    _toJavaLiteral((JvmTypeAnnotationValue)value, appendable, config);
    return;
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(value, appendable, config).toString());
  }
}
 
Example #14
Source File: AnnotationReferenceBuildContextImpl.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
protected void _setValue(final JvmCustomAnnotationValue it, final XtendAnnotationReferenceImpl value, final String componentType, final boolean mustBeArray) {
  it.getValues().add(value.getDelegate());
}
 
Example #15
Source File: SarlCompiler.java    From sarl with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"checkstyle:returncount", "checkstyle:npathcomplexity", "checkstyle:cyclomaticcomplexity"})
private static String getAnnotationStringValue(JvmAnnotationValue value) {
	if (value instanceof JvmAnnotationAnnotationValue) {
		return ((JvmAnnotationAnnotationValue) value).getValues().get(0).getAnnotation().getIdentifier();
	}
	if (value instanceof JvmBooleanAnnotationValue) {
		return ((JvmBooleanAnnotationValue) value).getValues().get(0).toString();
	}
	if (value instanceof JvmByteAnnotationValue) {
		return ((JvmByteAnnotationValue) value).getValues().get(0).toString();
	}
	if (value instanceof JvmCharAnnotationValue) {
		return ((JvmCharAnnotationValue) value).getValues().get(0).toString();
	}
	if (value instanceof JvmCustomAnnotationValue) {
		final EObject evalue = ((JvmCustomAnnotationValue) value).getValues().get(0);
		if (evalue instanceof XStringLiteral) {
			return ((XStringLiteral) evalue).getValue();
		}
		if (evalue instanceof XNumberLiteral) {
			return ((XNumberLiteral) evalue).getValue();
		}
		if (evalue instanceof XBooleanLiteral) {
			return ((XNumberLiteral) evalue).getValue();
		}
		if (evalue instanceof XTypeLiteral) {
			return ((XTypeLiteral) evalue).getType().getIdentifier();
		}
	}
	if (value instanceof JvmDoubleAnnotationValue) {
		return ((JvmDoubleAnnotationValue) value).getValues().get(0).toString();
	}
	if (value instanceof JvmEnumAnnotationValue) {
		return ((JvmEnumAnnotationValue) value).getValues().get(0).getSimpleName();
	}
	if (value instanceof JvmFloatAnnotationValue) {
		return ((JvmFloatAnnotationValue) value).getValues().get(0).toString();
	}
	if (value instanceof JvmIntAnnotationValue) {
		return ((JvmIntAnnotationValue) value).getValues().get(0).toString();
	}
	if (value instanceof JvmLongAnnotationValue) {
		return ((JvmLongAnnotationValue) value).getValues().get(0).toString();
	}
	if (value instanceof JvmShortAnnotationValue) {
		return ((JvmShortAnnotationValue) value).getValues().get(0).toString();
	}
	if (value instanceof JvmStringAnnotationValue) {
		return ((JvmStringAnnotationValue) value).getValues().get(0);
	}
	if (value instanceof JvmTypeAnnotationValue) {
		return ((JvmTypeAnnotationValue) value).getValues().get(0).getIdentifier();
	}
	return null;
}