@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()); } } }
@Test @Override public void testFindTypeByName_AbstractMultimap_02() { String typeName = "com.google.common.collect.AbstractMultimap"; JvmType _findTypeByName = this.getTypeProvider().findTypeByName(typeName); JvmGenericType type = ((JvmGenericType) _findTypeByName); JvmFeature _onlyElement = Iterables.<JvmFeature>getOnlyElement(type.findAllFeaturesByName("containsValue")); JvmOperation containsValue = ((JvmOperation) _onlyElement); Assert.assertNotNull(containsValue); JvmFormalParameter firstParam = containsValue.getParameters().get(0); Assert.assertEquals(1, firstParam.getAnnotations().size()); JvmAnnotationReference annotationReference = firstParam.getAnnotations().get(0); JvmAnnotationType annotationType = annotationReference.getAnnotation(); Assert.assertTrue(annotationType.eIsProxy()); Assert.assertEquals("java:/Objects/javax.annotation.Nullable", EcoreUtil.getURI(annotationType).trimFragment().toString()); }
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()); } } } } }
/** * 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; }
/** * 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; }
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(); }
protected void featureCalltoJavaExpression(final XAbstractFeatureCall call, ITreeAppendable b, boolean isExpressionContext) { if (call instanceof XAssignment) { assignmentToJavaExpression((XAssignment) call, b, isExpressionContext); } else { if (needMultiAssignment(call)) { appendLeftOperand(call, b, isExpressionContext).append(" = "); } final JvmAnnotationReference annotationRef = this.expressionHelper.findInlineAnnotation(call); if (annotationRef == null || !isConstantExpression(annotationRef)) { boolean hasReceiver = appendReceiver(call, b, isExpressionContext); if (hasReceiver) { b.append("."); b = appendTypeArguments(call, b); } } appendFeatureCall(call, b); } }
public void serializeSafely(final JvmAnnotationReference annotationRef, final ITreeAppendable appendable, final Procedure1<? super ITreeAppendable> onSuccess) { if (((annotationRef == null) || (annotationRef.getAnnotation() == null))) { final ITreeAppendable errorChild = this.openErrorAppendable(appendable, appendable); errorChild.append("annotation is \'null\'"); this.closeErrorAppendable(appendable, errorChild); } else { boolean _eIsProxy = annotationRef.getAnnotation().eIsProxy(); if (_eIsProxy) { final ITreeAppendable errorChild_1 = this.openErrorAppendable(appendable, appendable); appendable.append("@"); appendable.append(annotationRef.getAnnotation()); this.closeErrorAppendable(appendable, errorChild_1); } else { appendable.append("@"); appendable.append(annotationRef.getAnnotation()); onSuccess.apply(appendable); } } }
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 testStringAnnotationWithNullExpression() { try { final XAnnotationsFactory f = XAnnotationsFactory.eINSTANCE; final XExpression context = this.expression("\'Foo\'"); final XAnnotation anno = f.createXAnnotation(); JvmType _findDeclaredType = this.references.findDeclaredType(Inject.class, context); anno.setAnnotationType(((JvmAnnotationType) _findDeclaredType)); final XAnnotationElementValuePair pair = f.createXAnnotationElementValuePair(); EList<XAnnotationElementValuePair> _elementValuePairs = anno.getElementValuePairs(); this._jvmTypesBuilder.<XAnnotationElementValuePair>operator_add(_elementValuePairs, pair); final JvmGenericType type = this.typesFactory.createJvmGenericType(); this._jvmTypesBuilder.addAnnotation(type, anno); Assert.assertEquals(anno.getAnnotationType(), IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getAnnotation()); Assert.assertTrue(IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getExplicitValues().isEmpty()); Assert.assertFalse(IterableExtensions.<JvmAnnotationReference>head(type.getAnnotations()).getValues().isEmpty()); } catch (Throwable _e) { throw Exceptions.sneakyThrow(_e); } }
@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 JvmFormalParameter createFormalParameter(Type parameterType, String paramName, JvmMember container, GenericDeclaration member, Annotation[] annotations) { JvmFormalParameter result = TypesFactory.eINSTANCE.createJvmFormalParameter(); result.setName(paramName); if (isLocal(parameterType, member)) { result.setParameterType(createLocalTypeReference(parameterType, (JvmTypeParameterDeclarator) container, member)); } else { result.setParameterType(createTypeReference(parameterType)); } if (annotations.length != 0) { InternalEList<JvmAnnotationReference> annotationsReferences = (InternalEList<JvmAnnotationReference>)result.getAnnotations(); for (Annotation annotation : annotations) { annotationsReferences.addUnique(createAnnotationReference(annotation)); } } return result; }
private void doTestAnnotation_01(JvmAnnotationTarget target) { JvmAnnotationType annotationType = (JvmAnnotationType) getTypeProvider() .findTypeByName(TestAnnotation.class.getName()); assertEquals(1, target.getAnnotations().size()); JvmAnnotationReference annotationReference = target.getAnnotations().get(0); assertSame(annotationType, annotationReference.getAnnotation()); if (isDefaultValueSupported()) assertEquals(14, annotationReference.getExplicitValues().size()); assertEquals(15, annotationReference.getValues().size()); }
@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)); }
@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)); }
@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)); }
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 testAnnotatedParameter_03() throws Exception { String typeName = TestAnnotation.Annotated.class.getName(); JvmAnnotationType annotationType = (JvmAnnotationType) getTypeProvider() .findTypeByName(TestAnnotation.NestedAnnotation.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(2); assertEquals(1, target.getAnnotations().size()); JvmAnnotationReference annotationReference = target.getAnnotations().get(0); assertSame(annotationType, annotationReference.getAnnotation()); }
@Test public void testAnnotatedParameter_06() throws Exception { String typeName = TestAnnotation.Annotated.class.getName(); JvmAnnotationType annotationType = (JvmAnnotationType) getTypeProvider() .findTypeByName(TestAnnotation.NestedAnnotation.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(2); assertEquals(1, target.getAnnotations().size()); JvmAnnotationReference annotationReference = target.getAnnotations().get(0); assertSame(annotationType, annotationReference.getAnnotation()); }
@Test public void testAnnotationWithStringDefault_01() throws Exception { String typeName = TestAnnotationWithStringDefault.Annotated.class.getName(); JvmDeclaredType type = (JvmDeclaredType) getTypeProvider().findTypeByName(typeName); List<JvmAnnotationReference> annotations = type.getAnnotations(); assertEquals(1, annotations.size()); JvmAnnotationReference annotationReference = annotations.get(0); assertEquals(TestAnnotationWithStringDefault.class.getName(), annotationReference.getAnnotation().getIdentifier()); checkDefaultAnnotationValues(annotationReference); }
@Test public void testAnnotationWithStringDefault_02() throws Exception { String typeName = AnnotatedInterfaceWithStringDefault.class.getName(); JvmDeclaredType type = (JvmDeclaredType) getTypeProvider().findTypeByName(typeName); List<JvmAnnotationReference> annotations = type.getAnnotations(); assertEquals(1, annotations.size()); JvmAnnotationReference annotationReference = annotations.get(0); assertEquals(TestAnnotationWithStringDefault.class.getName(), annotationReference.getAnnotation().getIdentifier()); checkDefaultAnnotationValuesAnnotatedExternalClass(annotationReference); }
@Test public void testAnnotationWithStringDefault_03() throws Exception { String typeName = TestAnnotationWithStringDefault.AnnotatedInterface.class.getName(); JvmDeclaredType type = (JvmDeclaredType) getTypeProvider().findTypeByName(typeName); List<JvmAnnotationReference> annotations = type.getAnnotations(); assertEquals(1, annotations.size()); JvmAnnotationReference annotationReference = annotations.get(0); assertEquals(TestAnnotationWithStringDefault.class.getName(), annotationReference.getAnnotation().getIdentifier()); checkDefaultAnnotationValues(annotationReference); }
@Test public void testAnnotationWithStringDefault_04() throws Exception { String typeName = AnnotatedClassWithStringDefault.class.getName(); JvmDeclaredType type = (JvmDeclaredType) getTypeProvider().findTypeByName(typeName); List<JvmAnnotationReference> annotations = type.getAnnotations(); assertEquals(1, annotations.size()); JvmAnnotationReference annotationReference = annotations.get(0); assertEquals(TestAnnotationWithStringDefault.class.getName(), annotationReference.getAnnotation().getIdentifier()); checkDefaultAnnotationValuesAnnotatedExternalClass(annotationReference); }
@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)); }
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()); }
@Test public void testBug427098() { String typeName = Bug427098.class.getName(); JvmDeclaredType type = (JvmDeclaredType) getTypeProvider().findTypeByName(typeName); JvmAnnotationReference annotationReference = type.getAnnotations().get(0); JvmTypeAnnotationValue annotationValue = getClassArrayAnnotationValue(annotationReference); assertEquals("classArray", annotationValue.getOperation().getSimpleName()); List<JvmTypeReference> typeReferences = annotationValue.getValues(); assertEquals(5, typeReferences.size()); assertTypeReference("int", typeReferences, 0); assertTypeReference("void", typeReferences, 1); assertTypeReference("double[][][]", typeReferences, 2); assertTypeReference("CharSequence[]", typeReferences, 3); assertTypeReference("Iterable", typeReferences, 4); }
@Test public void testBug428340() { String typeName = Bug428340.class.getName(); JvmDeclaredType type = (JvmDeclaredType) getTypeProvider().findTypeByName(typeName); JvmAnnotationReference myAnnotation = type.getAnnotations().get(0); assertEquals("MyAnnotation", myAnnotation.getAnnotation().getSimpleName()); }
/** * Translates an {@link XAnnotation} to a {@link JvmAnnotationReference} * and adds them to the given {@link JvmAnnotationTarget}. * * @param target the annotation target. If <code>null</code> this method does nothing. * @param annotation the annotation. If <code>null</code> this method does nothing. */ public void addAnnotation(/* @Nullable */ JvmAnnotationTarget target, /* @Nullable */ XAnnotation annotation) { if(annotation == null || target == null) return; JvmAnnotationReference annotationReference = getJvmAnnotationReference(annotation); if(annotationReference != null) { target.getAnnotations().add(annotationReference); } }
/** * 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; }
/** * <!-- begin-user-doc --> * <!-- end-user-doc --> * @generated */ @SuppressWarnings("unchecked") @Override public void eSet(int featureID, Object newValue) { switch (featureID) { case TypesPackage.JVM_ANNOTATION_ANNOTATION_VALUE__VALUES: getValues().clear(); getValues().addAll((Collection<? extends JvmAnnotationReference>)newValue); return; } super.eSet(featureID, newValue); }
protected JvmAnnotationReference findAnnotation(JvmAnnotationTarget feature, String annotationType) { if (feature == null) return null; if (annotationType == null) throw new NullPointerException(); List<JvmAnnotationReference> annotations = feature.getAnnotations(); for (JvmAnnotationReference annotation : annotations) { if (annotationType.equals(annotation.getAnnotation().getQualifiedName())) { return annotation; } } 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; }
/** * @deprecated Additional annotations should be created in the JVM model. */ @Deprecated public ITreeAppendable generateAnnotationsWithSyntheticSuppressWarnings(final JvmDeclaredType it, final ITreeAppendable appendable, final GeneratorConfig config) { ITreeAppendable _xblockexpression = null; { final Function1<JvmAnnotationReference, Boolean> _function = (JvmAnnotationReference it_1) -> { JvmAnnotationType _annotation = it_1.getAnnotation(); String _identifier = null; if (_annotation!=null) { _identifier=_annotation.getIdentifier(); } String _name = SuppressWarnings.class.getName(); return Boolean.valueOf((!Objects.equal(_identifier, _name))); }; final Function1<JvmAnnotationReference, Boolean> noSuppressWarningsFilter = _function; this.generateAnnotations(IterableExtensions.<JvmAnnotationReference>filter(it.getAnnotations(), noSuppressWarningsFilter), appendable, true, config); ITreeAppendable _xifexpression = null; EObject _eContainer = it.eContainer(); boolean _tripleEquals = (_eContainer == null); if (_tripleEquals) { StringConcatenation _builder = new StringConcatenation(); _builder.append("@SuppressWarnings(\"all\")"); _xifexpression = appendable.append(_builder).newLine(); } _xblockexpression = _xifexpression; } return _xblockexpression; }