Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Xtend does not accept annotation @Nullable on parameter #2864

Open
frankbenoit opened this issue Dec 4, 2023 · 9 comments
Open

Xtend does not accept annotation @Nullable on parameter #2864

frankbenoit opened this issue Dec 4, 2023 · 9 comments

Comments

@frankbenoit
Copy link
Contributor

frankbenoit commented Dec 4, 2023

Using org.eclipse.jdt.annotations.

Is there a problem if the annotation has

@Target({ TYPE_USE })

like @Nullable/@nonnull have it?

def String func( @Nullable String s ){ ...

results in: The annotation @nullable is disallowed at this location

@cdietrich
Copy link
Member

see also #2360

@LorenzoBettini
Copy link
Contributor

If I understand it correctly, the "culprit" is in XtendValidator here:

	@Check
	public void checkAnnotationTarget(XAnnotation annotation) {
		JvmType annotationType = annotation.getAnnotationType();
		if (annotationType == null || annotationType.eIsProxy() || !(annotationType instanceof JvmAnnotationType)) {
			return;
		}
		Set<ElementType> targets = annotationUtil.getAnnotationTargets((JvmAnnotationType) annotationType);
		if (targets.isEmpty())
			return;
		final EObject eContainer = getContainingAnnotationTarget(annotation);
		Class<? extends EObject> clazz = eContainer.getClass();
		if (eContainer instanceof XtendField && eContainer.eContainer() instanceof XtendAnnotationType) {
			clazz = XtendFunction.class;
		}
		for (Entry<Class<?>, Collection<ElementType>> mapping : targetInfos.asMap().entrySet()) {
			if (mapping.getKey().isAssignableFrom(clazz)) {
				targets.retainAll(mapping.getValue());
				if (targets.isEmpty()) {
					error("The annotation @" + annotation.getAnnotationType().getSimpleName()
							+ " is disallowed for this location.", annotation, null, INSIGNIFICANT_INDEX,
							ANNOTATION_WRONG_TARGET);
				}
			}
		}
	}

it should be enough to update this initialization accordingly:

	protected final Multimap<Class<?>, ElementType> targetInfos;
	
	{
		ImmutableMultimap.Builder<Class<?>, ElementType> result = ImmutableMultimap.builder();
		result.put(XtendClass.class, ElementType.TYPE);
		result.put(XtendInterface.class, ElementType.TYPE);
		result.put(XtendEnum.class, ElementType.TYPE);
		result.putAll(XtendAnnotationType.class, ElementType.ANNOTATION_TYPE, ElementType.TYPE);
		result.put(XtendField.class, ElementType.FIELD);
		result.put(XtendFunction.class, ElementType.METHOD);
		result.put(XtendParameter.class, ElementType.PARAMETER);
		targetInfos = result.build();
	}

Do I underestimate anything? cc @szarnekow

@cdietrich
Copy link
Member

maybe:
#2360 (comment)

@LorenzoBettini
Copy link
Contributor

@cdietrich but that is a syntactic issue for fields. I was focusing on this issue, which is about parameter types.

@cdietrich
Copy link
Member

@X @Y Type name

@cdietrich
Copy link
Member

Do x and y go to the type or the field
What are you targeting

@LorenzoBettini
Copy link
Contributor

But this issue is about parameters

@cdietrich
Copy link
Member

So you would allow it just at this place ?

@LorenzoBettini
Copy link
Contributor

Just as a first step; I had the feeling that for this issue you don't need to change the grammar but only the validator. For parameters, the grammar already allows the annotation to be placed there, but the validator only allows for ElementType.PARAMETER.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants