use of org.eclipse.xtend.lib.macro.declaration.MutableParameterDeclaration in project xtext-xtend by eclipse.
the class ExtractProcessor method doTransform.
@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
final MutableInterfaceDeclaration interfaceType = context.findInterface(this.getInterfaceName(annotatedClass));
context.setPrimarySourceElement(interfaceType, annotatedClass);
Iterable<? extends TypeReference> _implementedInterfaces = annotatedClass.getImplementedInterfaces();
TypeReference _newTypeReference = context.newTypeReference(interfaceType);
Iterable<TypeReference> _plus = Iterables.<TypeReference>concat(_implementedInterfaces, Collections.<TypeReference>unmodifiableList(CollectionLiterals.<TypeReference>newArrayList(_newTypeReference)));
annotatedClass.setImplementedInterfaces(_plus);
Iterable<? extends MutableMethodDeclaration> _declaredMethods = annotatedClass.getDeclaredMethods();
for (final MutableMethodDeclaration method : _declaredMethods) {
Visibility _visibility = method.getVisibility();
boolean _equals = Objects.equal(_visibility, Visibility.PUBLIC);
if (_equals) {
final Procedure1<MutableMethodDeclaration> _function = (MutableMethodDeclaration it) -> {
it.setDocComment(method.getDocComment());
it.setReturnType(method.getReturnType());
Iterable<? extends MutableParameterDeclaration> _parameters = method.getParameters();
for (final MutableParameterDeclaration p : _parameters) {
it.addParameter(p.getSimpleName(), p.getType());
}
it.setExceptions(((TypeReference[]) Conversions.unwrapArray(method.getExceptions(), TypeReference.class)));
context.setPrimarySourceElement(it, method);
};
interfaceType.addMethod(method.getSimpleName(), _function);
}
}
}
use of org.eclipse.xtend.lib.macro.declaration.MutableParameterDeclaration in project xtext-core by eclipse.
the class TracedProcessor method doTransform.
@Override
public void doTransform(MutableMethodDeclaration annotatedMethod, @Extension TransformationContext context) {
boolean useForDebugging = annotatedMethod.findAnnotation(context.findTypeGlobally(Traced.class)).getBooleanValue("useForDebugging");
TypeReference traceSugar = context.newTypeReference(TracingSugar.class);
TypeReference templateClient = context.newTypeReference(StringConcatenationClient.class);
TypeReference nodeType = context.newTypeReference(IGeneratorNode.class);
TypeReference eobjectType = context.newTypeReference(EObject.class);
MutableClassDeclaration clazz = (MutableClassDeclaration) annotatedMethod.getDeclaringType();
MutableFieldDeclaration field = IterableExtensions.findFirst(clazz.getDeclaredFields(), it -> Boolean.valueOf(traceSugar.isAssignableFrom(it.getType())));
if (field == null) {
context.addError(annotatedMethod, "@" + Traced.class.getSimpleName() + " methods can only declared in a class with a field of type " + TracingSugar.class);
return;
}
MutableParameterDeclaration traceParam = IterableExtensions.findFirst(annotatedMethod.getParameters(), it -> eobjectType.isAssignableFrom(it.getType()));
if (traceParam == null) {
context.addError(annotatedMethod, "@" + Traced.class.getSimpleName() + " methods need at least one parameter of type " + EObject.class + ".");
return;
}
clazz.addMethod("_" + annotatedMethod.getSimpleName(), it -> {
for (MutableParameterDeclaration p : annotatedMethod.getParameters()) it.addParameter(p.getSimpleName(), p.getType());
it.setReturnType(templateClient);
it.setBody(annotatedMethod.getBody());
});
annotatedMethod.setReturnType(nodeType);
annotatedMethod.setBody(new StringConcatenationClient() {
@Override
protected void appendTo(TargetStringConcatenation builder) {
builder.append(ILocationData.class);
builder.append(" _location = this.");
builder.append(field.getSimpleName());
builder.append(".location(");
builder.append(traceParam.getSimpleName());
builder.append(");");
builder.newLineIfNotEmpty();
builder.append(CompositeGeneratorNode.class);
builder.append(" _traceNode = this.");
builder.append(field.getSimpleName());
builder.append(".trace(_location, ");
builder.append(useForDebugging);
builder.append(");");
builder.newLineIfNotEmpty();
builder.append("this.");
builder.append(field.getSimpleName());
builder.append(".appendTemplate(_traceNode, _");
builder.append(annotatedMethod.getSimpleName());
builder.append("(");
builder.append(IterableExtensions.join(annotatedMethod.getParameters(), ",", it -> it.getSimpleName()));
builder.append("));");
builder.newLineIfNotEmpty();
builder.append("return _traceNode;");
builder.newLine();
}
});
}
Aggregations