use of org.eclipse.xtext.common.types.JvmAnnotationType in project xtext-xtend by eclipse.
the class XtendValidator method checkAnnotationTarget.
@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);
}
}
}
}
use of org.eclipse.xtext.common.types.JvmAnnotationType in project xtext-xtend by eclipse.
the class ActiveAnnotationContextProvider method computeContext.
public ActiveAnnotationContexts computeContext(final XtendFile file) {
final Stopwatches.StoppedTask task = Stopwatches.forTask("[macros] findActiveAnnotations (ActiveAnnotationContextProvider.computeContext)");
task.start();
try {
final ActiveAnnotationContexts result = ActiveAnnotationContexts.installNew(file.eResource());
final CompilationUnitImpl compilationUnit = this.compilationUnitProvider.get();
compilationUnit.setXtendFile(file);
result.compilationUnit = compilationUnit;
final IAcceptor<Pair<JvmAnnotationType, XAnnotation>> _function = (Pair<JvmAnnotationType, XAnnotation> it) -> {
boolean _containsKey = result.getContexts().containsKey(it.getKey());
boolean _not = (!_containsKey);
if (_not) {
final ActiveAnnotationContext fa = new ActiveAnnotationContext();
fa.setCompilationUnit(compilationUnit);
final JvmType processorType = this._xAnnotationExtensions.getProcessorType(it.getKey());
try {
final Object processorInstance = this._processorInstanceForJvmTypeProvider.getProcessorInstance(processorType);
if ((processorInstance == null)) {
String _identifier = processorType.getIdentifier();
String _plus = ("Couldn\'t instantiate the annotation processor of type \'" + _identifier);
String _plus_1 = (_plus + "\'. This is usually the case when the processor resides in the same project as the annotated element.");
throw new IllegalStateException(_plus_1);
}
fa.setProcessorInstance(processorInstance);
} catch (final Throwable _t) {
if (_t instanceof VirtualMachineError) {
final VirtualMachineError e = (VirtualMachineError) _t;
throw e;
} else if (_t instanceof Throwable) {
final Throwable e_1 = (Throwable) _t;
this.operationCanceledManager.propagateAsErrorIfCancelException(e_1);
String _switchResult = null;
boolean _matched = false;
if (e_1 instanceof ExceptionInInitializerError) {
_matched = true;
_switchResult = ((ExceptionInInitializerError) e_1).getException().getMessage();
}
if (!_matched) {
_switchResult = e_1.getMessage();
}
final String msg = _switchResult;
EList<Resource.Diagnostic> _errors = file.eResource().getErrors();
StringConcatenation _builder = new StringConcatenation();
_builder.append("Problem while loading annotation processor: ");
_builder.append(msg);
XAnnotation _value = it.getValue();
EObjectDiagnosticImpl _eObjectDiagnosticImpl = new EObjectDiagnosticImpl(Severity.ERROR, IssueCodes.PROCESSING_ERROR, _builder.toString(), _value, XAnnotationsPackage.Literals.XANNOTATION__ANNOTATION_TYPE, (-1), null);
_errors.add(_eObjectDiagnosticImpl);
} else {
throw Exceptions.sneakyThrow(_t);
}
}
result.getContexts().put(it.getKey(), fa);
}
List<XtendAnnotationTarget> _annotatedSourceElements = result.getContexts().get(it.getKey()).getAnnotatedSourceElements();
XtendAnnotationTarget _annotatedTarget = this._xAnnotationExtensions.getAnnotatedTarget(it.getValue());
_annotatedSourceElements.add(_annotatedTarget);
};
this.searchAnnotatedElements(file, _function);
return result;
} catch (final Throwable _t) {
if (_t instanceof Throwable) {
final Throwable e = (Throwable) _t;
this.operationCanceledManager.propagateAsErrorIfCancelException(e);
boolean _matched = false;
if (e instanceof VirtualMachineError) {
_matched = true;
throw ((VirtualMachineError) e);
}
if (!_matched) {
if (e instanceof LinkageError) {
_matched = true;
throw ((LinkageError) e);
}
}
ActiveAnnotationContextProvider.logger.warn("Error finding the elements to be processed by active annotations", e);
return ActiveAnnotationContexts.installNew(file.eResource());
} else {
throw Exceptions.sneakyThrow(_t);
}
} finally {
task.stop();
}
}
use of org.eclipse.xtext.common.types.JvmAnnotationType in project xtext-xtend by eclipse.
the class ActiveAnnotationContextProvider method registerMacroAnnotations.
private void registerMacroAnnotations(final XtendAnnotationTarget candidate, final IAcceptor<Pair<JvmAnnotationType, XAnnotation>> acceptor) {
final Function1<XAnnotation, Boolean> _function = (XAnnotation it) -> {
return Boolean.valueOf(this._xAnnotationExtensions.isProcessed(it));
};
Iterable<XAnnotation> _filter = IterableExtensions.<XAnnotation>filter(candidate.getAnnotations(), _function);
for (final XAnnotation annotation : _filter) {
{
final JvmAnnotationType activeAnnotationDeclaration = this._xAnnotationExtensions.tryFindAnnotationType(annotation);
if ((activeAnnotationDeclaration != null)) {
boolean _isValid = this.isValid(annotation, activeAnnotationDeclaration);
if (_isValid) {
Pair<JvmAnnotationType, XAnnotation> _mappedTo = Pair.<JvmAnnotationType, XAnnotation>of(activeAnnotationDeclaration, annotation);
acceptor.accept(_mappedTo);
}
}
}
}
}
use of org.eclipse.xtext.common.types.JvmAnnotationType in project xtext-xtend by eclipse.
the class XtendJvmModelInferrer method doInferTypeSceleton.
protected JvmDeclaredType doInferTypeSceleton(final XtendTypeDeclaration declaration, final IJvmDeclaredTypeAcceptor acceptor, boolean preIndexingPhase, XtendFile xtendFile, List<Runnable> doLater) {
if (Strings.isEmpty(declaration.getName()))
return null;
if (declaration instanceof XtendAnnotationType) {
final JvmAnnotationType annotation = typesFactory.createJvmAnnotationType();
if (!preIndexingPhase) {
doLater.add(new Runnable() {
@Override
public void run() {
initialize((XtendAnnotationType) declaration, annotation);
}
});
}
return annotation;
} else if (declaration instanceof XtendClass) {
XtendClass xtendClass = (XtendClass) declaration;
final JvmGenericType javaType = typesFactory.createJvmGenericType();
copyTypeParameters(xtendClass.getTypeParameters(), javaType);
if (!preIndexingPhase) {
doLater.add(new Runnable() {
@Override
public void run() {
initialize((XtendClass) declaration, javaType);
}
});
}
return javaType;
} else if (declaration instanceof XtendInterface) {
XtendInterface xtendInterface = (XtendInterface) declaration;
final JvmGenericType javaType = typesFactory.createJvmGenericType();
javaType.setInterface(true);
copyTypeParameters(xtendInterface.getTypeParameters(), javaType);
if (!preIndexingPhase) {
doLater.add(new Runnable() {
@Override
public void run() {
initialize((XtendInterface) declaration, javaType);
}
});
}
return javaType;
} else if (declaration instanceof XtendEnum) {
final JvmEnumerationType javaType = typesFactory.createJvmEnumerationType();
if (!preIndexingPhase) {
doLater.add(new Runnable() {
@Override
public void run() {
initialize((XtendEnum) declaration, javaType);
}
});
}
return javaType;
} else {
return null;
}
}
use of org.eclipse.xtext.common.types.JvmAnnotationType in project xtext-xtend by eclipse.
the class JvmModelTests method testEnumInAnnotationIsStatic.
@Test
public void testEnumInAnnotationIsStatic() {
try {
StringConcatenation _builder = new StringConcatenation();
_builder.append("annotation Foo {");
_builder.newLine();
_builder.append("\t");
_builder.append("enum E { L0, L1 }");
_builder.newLine();
_builder.append("}");
_builder.newLine();
final JvmAnnotationType inferred = this._iXtendJvmAssociations.getInferredAnnotationType(this.annotationType(_builder.toString()));
JvmMember _head = IterableExtensions.<JvmMember>head(inferred.getMembers());
final JvmEnumerationType e = ((JvmEnumerationType) _head);
Assert.assertTrue(e.isStatic());
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
Aggregations