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; }
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; }
@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)); }
@Test public void testDefaultAnnotationAnnotationValueByReference() throws Exception { String typeName = Bug334943Client.class.getName(); JvmDeclaredType client = (JvmDeclaredType) getTypeProvider().findTypeByName(typeName); JvmOperation operation = Iterables.get(client.getDeclaredOperations(), 0); List<JvmAnnotationReference> annotations = operation.getAnnotations(); assertEquals(1, annotations.size()); JvmAnnotationReference annotation = annotations.get(0); for (JvmAnnotationValue value : annotation.getValues()) { if ("enumValue".equals(value.getValueName())) { JvmEnumAnnotationValue enumValue = (JvmEnumAnnotationValue) value; assertEquals(1, enumValue.getValues().size()); assertEquals("FirstValue", enumValue.getValues().get(0).getSimpleName()); } } }
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()); } } } } }
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()); } } } } }
public Set<ElementType> getAnnotationTargets(JvmAnnotationType annotation) { EList<JvmAnnotationReference> annotations = annotation.getAnnotations(); for (JvmAnnotationReference annoRef : annotations) { if (Target.class.getName().equals(annoRef.getAnnotation().getIdentifier())) { EList<JvmAnnotationValue> values = annoRef.getValues(); JvmAnnotationValue value = values.isEmpty() ? null : values.get(0); if (value instanceof JvmEnumAnnotationValue) { Set<ElementType> result = newHashSet(); for (JvmEnumerationLiteral elementType : ((JvmEnumAnnotationValue) value).getValues()) { final String simpleName = elementType.getSimpleName(); result.add(ElementType.valueOf(simpleName)); } return result; } } } return emptySet(); }
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); }
@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); } }
@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); } }
@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); } }
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(); }
@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); }
/** * <!-- begin-user-doc --> * <!-- end-user-doc --> * @generated */ 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)); }
/** * <!-- begin-user-doc --> * <!-- end-user-doc --> * @generated */ @SuppressWarnings("unchecked") @Override public void eSet(int featureID, Object newValue) { switch (featureID) { case TypesPackage.JVM_ANNOTATION_REFERENCE__ANNOTATION: setAnnotation((JvmAnnotationType)newValue); return; case TypesPackage.JVM_ANNOTATION_REFERENCE__EXPLICIT_VALUES: getExplicitValues().clear(); getExplicitValues().addAll((Collection<? extends JvmAnnotationValue>)newValue); return; } super.eSet(featureID, newValue); }
public JvmAnnotationValue getAnnotationValue(String name, boolean defaultValue) { String typeName = TestAnnotation.Annotated.class.getName(); JvmAnnotationTarget target = (JvmAnnotationTarget) getTypeProvider().findTypeByName(typeName); 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; }
public JvmAnnotationValue getConstructorParameterAnnotationValue(String name) { 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); return getExplicitAnnotationValue(name, target); }
public JvmAnnotationValue getMethodParameterAnnotationValue(String name) { 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); return getExplicitAnnotationValue(name, target); }
public JvmAnnotationValue getDefaultOrExplicitAnnotationValue(String name, JvmAnnotationTarget target) { JvmAnnotationReference annotationReference = target.getAnnotations().get(0); for (JvmAnnotationValue value : annotationReference.getValues()) { if (name.equals(value.getValueName())) return value; } fail("Cannot find annotationValue " + name); return null; }
public JvmAnnotationValue getExplicitAnnotationValue(String name, JvmAnnotationTarget target) { JvmAnnotationReference annotationReference = target.getAnnotations().get(0); for (JvmAnnotationValue value : annotationReference.getExplicitValues()) { if (name.equals(value.getValueName())) return value; } fail("Cannot find annotationValue " + name); return null; }
@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)); }
public JvmAnnotationValue getDefaultAnnotationValue(String name) { String typeName = TestAnnotationWithDefaults.class.getName(); JvmAnnotationType type = (JvmAnnotationType) getTypeProvider().findTypeByName(typeName); JvmOperation operation = getMethodFromType(type, TestAnnotationWithDefaults.class, name + "()"); JvmAnnotationValue result = operation.getDefaultValue(); assertNotNull(result); assertSame(operation, result.getOperation()); return result; }
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()); }
/** * Translates a single {@link XAnnotation} to {@link JvmAnnotationReference} that can be added to a {@link JvmAnnotationTarget}. * * @param anno the source annotation * * @return a {@link JvmAnnotationReference} that can be attached to some {@link JvmAnnotationTarget} */ /* @Nullable */ public JvmAnnotationReference getJvmAnnotationReference(/* @Nullable */ XAnnotation anno) { if(anno == null) return null; JvmAnnotationReference reference = typesFactory.createJvmAnnotationReference(); final JvmType annotation = (JvmType) anno.eGet( XAnnotationsPackage.Literals.XANNOTATION__ANNOTATION_TYPE, false); if (annotation.eIsProxy()) { JvmAnnotationType copiedProxy = TypesFactory.eINSTANCE.createJvmAnnotationType(); ((InternalEObject)copiedProxy).eSetProxyURI(EcoreUtil.getURI(annotation)); reference.setAnnotation(copiedProxy); } else if (annotation instanceof JvmAnnotationType){ reference.setAnnotation((JvmAnnotationType) annotation); } for (XAnnotationElementValuePair val : anno.getElementValuePairs()) { XExpression valueExpression = val.getValue(); JvmAnnotationValue annotationValue = toJvmAnnotationValue(valueExpression); if (annotationValue != null) { JvmOperation op = (JvmOperation) val.eGet( XAnnotationsPackage.Literals.XANNOTATION_ELEMENT_VALUE_PAIR__ELEMENT, false); annotationValue.setOperation(op); reference.getExplicitValues().add(annotationValue); } } if (anno.getValue() != null) { JvmAnnotationValue value = toJvmAnnotationValue(anno.getValue()); if (value != null) { reference.getExplicitValues().add(value); } } associate(anno, reference); return reference; }
/** * 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; }
private static boolean isConstantExpression(JvmAnnotationReference reference) { for (final JvmAnnotationValue annotationValue: reference.getValues()) { if ("constantExpression".equals(annotationValue.getValueName())) { return ((JvmBooleanAnnotationValue) annotationValue).getValues().get(0).booleanValue(); } } return false; }
@Override protected JvmTypeAnnotationValue getClassArrayAnnotationValue(JvmAnnotationReference annotationReference) { for(JvmAnnotationValue candidate: annotationReference.getExplicitValues()) { if (candidate instanceof JvmTypeAnnotationValue && candidate.getValueName().equals("classArray")) { return (JvmTypeAnnotationValue) candidate; } } fail("Cannot find annotation value 'classArray'"); return null; }
@Test public void testBug380754() { try { final XExpression expression = this.expression("null"); final Procedure1<JvmGenericType> _function = (JvmGenericType it) -> { EList<JvmMember> _members = it.getMembers(); final Procedure1<JvmOperation> _function_1 = (JvmOperation it_1) -> { this.builder.setBody(it_1, expression); final JvmAnnotationReference annotation = this.builder.toAnnotation(expression, TestAnnotations.class); final JvmAnnotationAnnotationValue annotationAnnotationValue = this.typesFactory.createJvmAnnotationAnnotationValue(); EList<JvmAnnotationReference> _values = annotationAnnotationValue.getValues(); JvmAnnotationReference _annotation = this.builder.toAnnotation(expression, TestAnnotation.class); this.builder.<JvmAnnotationReference>operator_add(_values, _annotation); EList<JvmAnnotationReference> _values_1 = annotationAnnotationValue.getValues(); JvmAnnotationReference _annotation_1 = this.builder.toAnnotation(expression, TestAnnotation.class); this.builder.<JvmAnnotationReference>operator_add(_values_1, _annotation_1); EList<JvmAnnotationReference> _values_2 = annotationAnnotationValue.getValues(); JvmAnnotationReference _annotation_2 = this.builder.toAnnotation(expression, TestAnnotation.class); this.builder.<JvmAnnotationReference>operator_add(_values_2, _annotation_2); EList<JvmAnnotationValue> _explicitValues = annotation.getExplicitValues(); this.builder.<JvmAnnotationAnnotationValue>operator_add(_explicitValues, annotationAnnotationValue); EList<JvmAnnotationReference> _annotations = it_1.getAnnotations(); this.builder.<JvmAnnotationReference>operator_add(_annotations, annotation); }; JvmOperation _method = this.builder.toMethod(expression, "doStuff", this.references.getTypeForName("java.lang.Object", expression), _function_1); this.builder.<JvmOperation>operator_add(_members, _method); }; final JvmGenericType clazz = this.builder.toClass(expression, "my.test.Foo", _function); this.compile(expression.eResource(), clazz); } catch (Throwable _e) { throw Exceptions.sneakyThrow(_e); } }