org.eclipse.xtend.lib.macro.TransformationContext Java Examples

The following examples show how to use org.eclipse.xtend.lib.macro.TransformationContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: xtext-lib   Author: eclipse   File: PropertyProcessor.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void doTransform(final MutableFieldDeclaration it, @Extension final TransformationContext context) {
  @Extension
  final AccessorsProcessor.Util util = new AccessorsProcessor.Util(context);
  boolean _hasGetter = util.hasGetter(it);
  boolean _not = (!_hasGetter);
  if (_not) {
    util.addGetter(it, Visibility.PUBLIC);
  }
  if (((!it.isFinal()) && (!util.hasSetter(it)))) {
    util.addSetter(it, Visibility.PUBLIC);
  }
  String _firstLower = StringExtensions.toFirstLower(it.getSimpleName());
  String _plus = ("_" + _firstLower);
  it.setSimpleName(_plus);
}
 
Example #2
Source Project: xtext-xtend   Author: eclipse   File: AddInterfaceWithDefaultProcessor.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
  super.doTransform(annotatedClass, context);
  Type _findTypeGlobally = context.findTypeGlobally("de.test.Test");
  final MutableInterfaceDeclaration ifType = ((MutableInterfaceDeclaration) _findTypeGlobally);
  final Procedure1<MutableMethodDeclaration> _function = (MutableMethodDeclaration it) -> {
    StringConcatenationClient _client = new StringConcatenationClient() {
      @Override
      protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
        _builder.append("System.out.println(\"Hello World\");");
      }
    };
    it.setBody(_client);
    it.setDefault(true);
  };
  ifType.addMethod("sayHello", _function);
}
 
Example #3
Source Project: xtext-xtend   Author: eclipse   File: AnnotationWithNestedEnumProcessor.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
  final MutableInterfaceDeclaration interfaceType = context.findInterface(this.getInterfaceName(annotatedClass));
  context.setPrimarySourceElement(interfaceType, annotatedClass);
  try {
    final EnumerationValueDeclaration value = annotatedClass.findAnnotation(context.findTypeGlobally(AnnotationWithNestedEnum.class)).getEnumValue("value");
    final Procedure1<AnnotationReferenceBuildContext> _function = (AnnotationReferenceBuildContext it) -> {
      it.setEnumValue("value", value);
    };
    final AnnotationReference ref = context.newAnnotationReference(AnnotationWithNestedEnum.class, _function);
    interfaceType.addAnnotation(ref);
  } catch (final Throwable _t) {
    if (_t instanceof Exception) {
      final Exception exc = (Exception)_t;
      String _message = exc.getMessage();
      String _plus = ("failed: " + _message);
      context.addError(annotatedClass, _plus);
    } else {
      throw Exceptions.sneakyThrow(_t);
    }
  }
}
 
Example #4
Source Project: xtext-xtend   Author: eclipse   File: ToAnnoProcessor.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
  super.doTransform(annotatedClass, context);
  Type _findTypeGlobally = context.findTypeGlobally(this.generatedAnnotationName(annotatedClass));
  final MutableAnnotationTypeDeclaration annotationType = ((MutableAnnotationTypeDeclaration) _findTypeGlobally);
  final Procedure1<MutableAnnotationTypeElementDeclaration> _function = (MutableAnnotationTypeElementDeclaration it) -> {
    it.setDocComment("Best building strategy game ever");
    it.setType(context.newTypeReference(Integer.TYPE));
    boolean _booleanValue = annotatedClass.findAnnotation(context.findTypeGlobally(ToAnno.class)).getBooleanValue("defaultValue");
    if (_booleanValue) {
      StringConcatenationClient _client = new StringConcatenationClient() {
        @Override
        protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
          _builder.append("1602");
        }
      };
      it.setDefaultValueExpression(_client);
    }
  };
  annotationType.addAnnotationTypeElement("anno", _function);
}
 
Example #5
Source Project: xtext-xtend   Author: eclipse   File: AccessorsAnnotationTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testProperty() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("import org.eclipse.xtend.core.tests.macro.Accessors");
  _builder.newLine();
  _builder.newLine();
  _builder.append("class A {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("@Accessors String field");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("@Accessors val String finalField = \'foo\'");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  final IAcceptor<XtendCompilerTester.CompilationResult> _function = (XtendCompilerTester.CompilationResult it) -> {
    final TransformationContext ctx = it.getTransformationContext();
    final MutableClassDeclaration classA = ctx.findClass("A");
    Assert.assertEquals(Visibility.PUBLIC, classA.findDeclaredMethod("getField").getVisibility());
    Assert.assertEquals(Visibility.PUBLIC, classA.findDeclaredMethod("setField", ctx.getString()).getVisibility());
    Assert.assertEquals(Visibility.PUBLIC, classA.findDeclaredMethod("getFinalField").getVisibility());
    Assert.assertNull(classA.findDeclaredMethod("setFinalField", ctx.getString()));
  };
  this._xtendCompilerTester.compile(_builder, _function);
}
 
Example #6
Source Project: xtext-core   Author: eclipse   File: LogProcessor.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void doTransform(MutableClassDeclaration cls, TransformationContext context) {
	cls.addField("LOG", field -> {
		field.setStatic(true);
		field.setFinal(true);
		field.setType(context.newTypeReference(Logger.class));
		field.setInitializer(new StringConcatenationClient() {
			@Override
			protected void appendTo(TargetStringConcatenation target) {
				target.append(Logger.class);
				target.append(".getLogger(");
				target.append(cls.getSimpleName());
				target.append(".class)");
				target.newLineIfNotEmpty();
			}
		});
		context.setPrimarySourceElement(field, cls);
	});
}
 
Example #7
Source Project: lsp4j   Author: eclipse   File: TypeAdapterImplProcessor.java    License: Eclipse Public License 2.0 6 votes vote down vote up
private ArrayList<FieldDeclaration> getTargetFields(final TypeReference targetType, @Extension final TransformationContext context) {
  final Type objectType = context.newTypeReference(Object.class).getType();
  final ArrayList<FieldDeclaration> targetFields = CollectionLiterals.<FieldDeclaration>newArrayList();
  TypeReference typeRef = targetType;
  while ((!Objects.equal(typeRef.getType(), objectType))) {
    {
      Type _type = typeRef.getType();
      final ClassDeclaration clazz = ((ClassDeclaration) _type);
      final Function1<FieldDeclaration, Boolean> _function = (FieldDeclaration it) -> {
        boolean _isStatic = it.isStatic();
        return Boolean.valueOf((!_isStatic));
      };
      Iterable<? extends FieldDeclaration> _filter = IterableExtensions.filter(clazz.getDeclaredFields(), _function);
      Iterables.<FieldDeclaration>addAll(targetFields, _filter);
      typeRef = clazz.getExtendedClass();
    }
  }
  return targetFields;
}
 
Example #8
Source Project: xtext-lib   Author: eclipse   File: EqualsHashCodeProcessor.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doTransform(final MutableClassDeclaration it, @Extension final TransformationContext context) {
  AnnotationReference _findAnnotation = it.findAnnotation(context.findTypeGlobally(Data.class));
  boolean _tripleNotEquals = (_findAnnotation != null);
  if (_tripleNotEquals) {
    return;
  }
  @Extension
  final EqualsHashCodeProcessor.Util util = new EqualsHashCodeProcessor.Util(context);
  boolean _hasEquals = util.hasEquals(it);
  if (_hasEquals) {
    final AnnotationReference annotation = it.findAnnotation(context.findTypeGlobally(EqualsHashCode.class));
    context.addWarning(annotation, "equals is already defined, this annotation has no effect");
  } else {
    boolean _hasHashCode = util.hasHashCode(it);
    if (_hasHashCode) {
      context.addWarning(it, "hashCode is already defined, this annotation has no effect");
    } else {
      final Function1<MutableFieldDeclaration, Boolean> _function = (MutableFieldDeclaration it_1) -> {
        return Boolean.valueOf((((!it_1.isStatic()) && (!it_1.isTransient())) && context.isThePrimaryGeneratedJavaElement(it_1)));
      };
      final Iterable<? extends MutableFieldDeclaration> fields = IterableExtensions.filter(it.getDeclaredFields(), _function);
      util.addEquals(it, fields, util.hasSuperEquals(it));
      util.addHashCode(it, fields, util.hasSuperHashCode(it));
    }
  }
}
 
Example #9
Source Project: xtext-lib   Author: eclipse   File: FinalFieldsConstructorProcessor.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doTransform(final List<? extends MutableTypeParameterDeclarator> elements, @Extension final TransformationContext context) {
  final Consumer<MutableTypeParameterDeclarator> _function = (MutableTypeParameterDeclarator it) -> {
    this.transform(it, context);
  };
  elements.forEach(_function);
}
 
Example #10
Source Project: xtext-lib   Author: eclipse   File: FinalFieldsConstructorProcessor.java    License: Eclipse Public License 2.0 5 votes vote down vote up
protected void _transform(final MutableClassDeclaration it, @Extension final TransformationContext context) {
  AnnotationReference _findAnnotation = it.findAnnotation(context.findTypeGlobally(Data.class));
  boolean _tripleNotEquals = (_findAnnotation != null);
  if (_tripleNotEquals) {
    return;
  }
  AnnotationReference _findAnnotation_1 = it.findAnnotation(context.findTypeGlobally(Accessors.class));
  boolean _tripleNotEquals_1 = (_findAnnotation_1 != null);
  if (_tripleNotEquals_1) {
    return;
  }
  @Extension
  final FinalFieldsConstructorProcessor.Util util = new FinalFieldsConstructorProcessor.Util(context);
  util.addFinalFieldsConstructor(it);
}
 
Example #11
Source Project: xtext-lib   Author: eclipse   File: FinalFieldsConstructorProcessor.java    License: Eclipse Public License 2.0 5 votes vote down vote up
public void transform(final MutableTypeParameterDeclarator it, final TransformationContext context) {
  if (it instanceof MutableConstructorDeclaration) {
    _transform((MutableConstructorDeclaration)it, context);
    return;
  } else if (it instanceof MutableClassDeclaration) {
    _transform((MutableClassDeclaration)it, context);
    return;
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(it, context).toString());
  }
}
 
Example #12
Source Project: xtext-lib   Author: eclipse   File: AccessorsProcessor.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doTransform(final List<? extends MutableMemberDeclaration> elements, @Extension final TransformationContext context) {
  final Consumer<MutableMemberDeclaration> _function = (MutableMemberDeclaration it) -> {
    this.transform(it, context);
  };
  elements.forEach(_function);
}
 
Example #13
Source Project: xtext-lib   Author: eclipse   File: AccessorsProcessor.java    License: Eclipse Public License 2.0 5 votes vote down vote up
public void transform(final MutableMemberDeclaration it, final TransformationContext context) {
  if (it instanceof MutableClassDeclaration) {
    _transform((MutableClassDeclaration)it, context);
    return;
  } else if (it instanceof MutableFieldDeclaration) {
    _transform((MutableFieldDeclaration)it, context);
    return;
  } else {
    throw new IllegalArgumentException("Unhandled parameter types: " +
      Arrays.<Object>asList(it, context).toString());
  }
}
 
Example #14
Source Project: xtext-lib   Author: eclipse   File: DelegateProcessor.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doTransform(final List<? extends MutableMemberDeclaration> elements, @Extension final TransformationContext context) {
  @Extension
  final DelegateProcessor.Util util = new DelegateProcessor.Util(context);
  final Consumer<MutableMemberDeclaration> _function = (MutableMemberDeclaration it) -> {
    boolean _isValidDelegate = util.isValidDelegate(it);
    if (_isValidDelegate) {
      final Consumer<ResolvedMethod> _function_1 = (ResolvedMethod method) -> {
        util.implementMethod(it, method);
      };
      util.getMethodsToImplement(it).forEach(_function_1);
    }
  };
  elements.forEach(_function);
}
 
Example #15
Source Project: xtext-lib   Author: eclipse   File: ToStringProcessor.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doTransform(final MutableClassDeclaration it, @Extension final TransformationContext context) {
  AnnotationReference _findAnnotation = it.findAnnotation(context.findTypeGlobally(Data.class));
  boolean _tripleNotEquals = (_findAnnotation != null);
  if (_tripleNotEquals) {
    return;
  }
  @Extension
  final ToStringProcessor.Util util = new ToStringProcessor.Util(context);
  final AnnotationReference annotation = it.findAnnotation(context.findTypeGlobally(ToString.class));
  final ToStringConfiguration configuration = new ToStringConfiguration(annotation);
  boolean _hasToString = util.hasToString(it);
  if (_hasToString) {
    context.addWarning(annotation, "toString is already defined, this annotation has no effect.");
  } else {
    TypeReference _extendedClass = it.getExtendedClass();
    TypeReference _object = context.getObject();
    boolean _notEquals = (!Objects.equal(_extendedClass, _object));
    if (_notEquals) {
      util.addReflectiveToString(it, configuration);
    } else {
      final Function1<MutableFieldDeclaration, Boolean> _function = (MutableFieldDeclaration it_1) -> {
        return Boolean.valueOf(((context.isThePrimaryGeneratedJavaElement(it_1) && (!it_1.isStatic())) && (!it_1.isTransient())));
      };
      util.addToString(it, IterableExtensions.filter(it.getDeclaredFields(), _function), configuration);
    }
  }
}
 
Example #16
Source Project: gef   Author: eclipse   File: DotAttributeProcessor.java    License: Eclipse Public License 2.0 5 votes vote down vote up
private Object annotationValue(MutableFieldDeclaration field,
		TransformationContext context, String property) {
	for (AnnotationReference reference : field.getAnnotations()) {
		if (DotAttribute.class.getName().equals(reference
				.getAnnotationTypeDeclaration().getQualifiedName())) {
			return reference.getValue(property);
		}
	}
	throw new IllegalArgumentException("No DotAttribute annotation found.");
}
 
Example #17
Source Project: gef   Author: eclipse   File: DotAttributeProcessor.java    License: Eclipse Public License 2.0 5 votes vote down vote up
private TypeReference paramType(Context c, TransformationContext context) {
	switch (c) {
	case GRAPH:
		return context.newTypeReference("org.eclipse.gef.graph.Graph");
	case NODE:
		return context.newTypeReference("org.eclipse.gef.graph.Node");
	case EDGE:
		return context.newTypeReference("org.eclipse.gef.graph.Edge");
	default:
		throw new IllegalArgumentException(
				"Cluster and Subgraph not yet supported.");
	}
}
 
Example #18
Source Project: xtext-xtend   Author: eclipse   File: ExtractTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testExtractAnnotation() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("@extract.Extract");
  _builder.newLine();
  _builder.append("class MyClass {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("override doStuff(String myParam) throws IllegalArgumentException {");
  _builder.newLine();
  _builder.append("\t\t");
  _builder.append("return myParam");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("}");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  final IAcceptor<XtendCompilerTester.CompilationResult> _function = (XtendCompilerTester.CompilationResult it) -> {
    @Extension
    final TransformationContext ctx = it.getTransformationContext();
    final MutableInterfaceDeclaration interf = ctx.findInterface("MyClassInterface");
    final MutableClassDeclaration clazz = ctx.findClass("MyClass");
    Assert.assertEquals(IterableExtensions.head(clazz.getImplementedInterfaces()).getType(), interf);
    MutableMethodDeclaration _head = IterableExtensions.head(interf.getDeclaredMethods());
    final Procedure1<MutableMethodDeclaration> _function_1 = (MutableMethodDeclaration it_1) -> {
      Assert.assertEquals("doStuff", it_1.getSimpleName());
      Assert.assertEquals(ctx.getString(), it_1.getReturnType());
      Assert.assertEquals(ctx.newTypeReference(IllegalArgumentException.class), IterableExtensions.head(it_1.getExceptions()));
    };
    ObjectExtensions.<MutableMethodDeclaration>operator_doubleArrow(_head, _function_1);
  };
  this.compilerTester.compile(_builder, _function);
}
 
Example #19
Source Project: xtext-xtend   Author: eclipse   File: ExtractProcessor.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@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);
    }
  }
}
 
Example #20
Source Project: xtext-xtend   Author: eclipse   File: ExternalizedTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testExtractAnnotation() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("package i18n");
  _builder.newLine();
  _builder.newLine();
  _builder.append("@Externalized");
  _builder.newLine();
  _builder.append("class MyMessages {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("val GREETING = \"Hello {0}\"");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("val DATE_MESSAGE = \"Today, is ${0,date}.\"");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  final IAcceptor<XtendCompilerTester.CompilationResult> _function = (XtendCompilerTester.CompilationResult it) -> {
    @Extension
    final TransformationContext ctx = it.getTransformationContext();
    final MutableClassDeclaration clazz = ctx.findClass("i18n.MyMessages");
    Assert.assertEquals(2, IterableExtensions.size(clazz.getDeclaredMethods()));
    final Path path = it.getCompilationUnit().getFilePath();
    Path _targetFolder = ctx.getTargetFolder(path);
    String _replace = clazz.getQualifiedName().replace(".", "/");
    String _plus = (_replace + ".properties");
    final Path properties = _targetFolder.append(_plus);
    StringConcatenation _builder_1 = new StringConcatenation();
    _builder_1.append("GREETING = Hello {0}");
    _builder_1.newLine();
    _builder_1.append("DATE_MESSAGE = Today, is ${0,date}.");
    _builder_1.newLine();
    Assert.assertEquals(_builder_1.toString(), ctx.getContents(properties).toString());
  };
  this.compilerTester.compile(_builder, _function);
}
 
Example #21
Source Project: xtext-xtend   Author: eclipse   File: CheckMutableParameterDeclarationProcessor.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doTransform(final List<? extends MutableParameterDeclaration> annotatedTargetElements, @Extension final TransformationContext context) {
  for (final MutableParameterDeclaration annotatedTargetElement : annotatedTargetElements) {
    final Procedure1<String> _function = (String identifier) -> {
      annotatedTargetElement.setSimpleName(identifier);
    };
    MutableAssert.assertValidJavaIdentifier("name", _function);
  }
}
 
Example #22
Source Project: xtext-xtend   Author: eclipse   File: Bug464136Processor.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
  final Procedure0 _function = () -> {
    throw new LinkageError("Just a test :-/");
  };
  context.validateLater(_function);
}
 
Example #23
Source Project: xtext-xtend   Author: eclipse   File: GetProcessor.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doTransform(final List<? extends MutableMethodDeclaration> methods, @Extension final TransformationContext context) {
  for (final MutableMethodDeclaration m : methods) {
    {
      final AnnotationReference annotation = m.findAnnotation(context.findTypeGlobally(Get.class));
      final Object pattern = annotation.getValue("value");
      if ((pattern == null)) {
        context.addError(annotation, "A URL pattern must be provided.");
      } else {
      }
    }
  }
}
 
Example #24
Source Project: xtext-xtend   Author: eclipse   File: CheckMutableFieldDeclarationProcessor.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doTransform(final MutableFieldDeclaration annotatedField, @Extension final TransformationContext context) {
  final Procedure0 _function = () -> {
    annotatedField.setInitializer(((CompilationStrategy) null));
  };
  MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "initializer cannot be null", _function);
  final Procedure0 _function_1 = () -> {
    annotatedField.setType(null);
  };
  MutableAssert.<IllegalArgumentException>assertThrowable(IllegalArgumentException.class, "type cannot be null", _function_1);
}
 
Example #25
Source Project: xtext-xtend   Author: eclipse   File: AccessorsAnnotationTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testProperty2() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("import org.eclipse.xtend.core.tests.macro.Accessors");
  _builder.newLine();
  _builder.newLine();
  _builder.append("class A {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("@Accessors String field");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("private def String getField() {");
  _builder.newLine();
  _builder.append("\t\t");
  _builder.append("return null");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("} ");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  final IAcceptor<XtendCompilerTester.CompilationResult> _function = (XtendCompilerTester.CompilationResult it) -> {
    final TransformationContext ctx = it.getTransformationContext();
    final MutableClassDeclaration classA = ctx.findClass("A");
    Assert.assertEquals(Visibility.PRIVATE, classA.findDeclaredMethod("getField").getVisibility());
    Assert.assertEquals(Visibility.PUBLIC, classA.findDeclaredMethod("setField", ctx.getString()).getVisibility());
    Assert.assertEquals(2, IterableExtensions.size(classA.getDeclaredMethods()));
  };
  this._xtendCompilerTester.compile(_builder, _function);
}
 
Example #26
Source Project: xtext-xtend   Author: eclipse   File: _TESTDATA_InternalClassProcessor.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
  String _qualifiedName = annotatedClass.getQualifiedName();
  String _plus = (_qualifiedName + ".InternalClass");
  MutableClassDeclaration _findClass = context.findClass(_plus);
  final Procedure1<MutableClassDeclaration> _function = (MutableClassDeclaration it) -> {
    final Procedure1<MutableFieldDeclaration> _function_1 = (MutableFieldDeclaration it_1) -> {
      it_1.setType(context.getString());
    };
    it.addField("myField", _function_1);
  };
  ObjectExtensions.<MutableClassDeclaration>operator_doubleArrow(_findClass, _function);
}
 
Example #27
Source Project: xtext-xtend   Author: eclipse   File: FileProcessor.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
  final Path path = annotatedClass.getCompilationUnit().getFilePath();
  final String contents = context.getContents(context.getProjectFolder(path).append("res/template.txt")).toString();
  final String[] segments = contents.trim().split(",");
  for (final String segment : segments) {
    final Procedure1<MutableFieldDeclaration> _function = (MutableFieldDeclaration it) -> {
      it.setType(context.getString());
    };
    annotatedClass.addField(segment, _function);
  }
}
 
Example #28
Source Project: lsp4j   Author: eclipse   File: TypeAdapterImplProcessor.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doTransform(final MutableClassDeclaration annotatedClass, @Extension final TransformationContext context) {
  final AnnotationReference typeAdapterImplAnnotation = annotatedClass.findAnnotation(context.findTypeGlobally(TypeAdapterImpl.class));
  final TypeReference targetType = typeAdapterImplAnnotation.getClassValue("value");
  this.generateImpl(annotatedClass, targetType, context);
  String _qualifiedName = annotatedClass.getQualifiedName();
  String _plus = (_qualifiedName + ".Factory");
  this.generateFactory(context.findClass(_plus), annotatedClass, targetType, context);
}
 
Example #29
Source Project: lsp4j   Author: eclipse   File: TypeAdapterImplProcessor.java    License: Eclipse Public License 2.0 5 votes vote down vote up
protected MutableMethodDeclaration generateFactory(final MutableClassDeclaration factory, final MutableClassDeclaration impl, final TypeReference targetType, @Extension final TransformationContext context) {
  MutableMethodDeclaration _xblockexpression = null;
  {
    TypeReference _newTypeReference = context.newTypeReference("com.google.gson.TypeAdapterFactory");
    factory.setImplementedInterfaces(Collections.<TypeReference>unmodifiableList(CollectionLiterals.<TypeReference>newArrayList(_newTypeReference)));
    final Procedure1<MutableMethodDeclaration> _function = (MutableMethodDeclaration it) -> {
      final MutableTypeParameterDeclaration t = it.addTypeParameter("T");
      it.addParameter("gson", context.newTypeReference("com.google.gson.Gson"));
      it.addParameter("typeToken", context.newTypeReference("com.google.gson.reflect.TypeToken", context.newTypeReference(t)));
      it.setReturnType(context.newTypeReference("com.google.gson.TypeAdapter", context.newTypeReference(t)));
      StringConcatenationClient _client = new StringConcatenationClient() {
        @Override
        protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
          _builder.append("if (!");
          _builder.append(targetType);
          _builder.append(".class.isAssignableFrom(typeToken.getRawType())) {");
          _builder.newLineIfNotEmpty();
          _builder.append("\t");
          _builder.append("return null;");
          _builder.newLine();
          _builder.append("}");
          _builder.newLine();
          _builder.append("return (TypeAdapter<T>) new ");
          _builder.append(impl);
          _builder.append("(gson);");
          _builder.newLineIfNotEmpty();
        }
      };
      it.setBody(_client);
    };
    _xblockexpression = factory.addMethod("create", _function);
  }
  return _xblockexpression;
}
 
Example #30
Source Project: lsp4j   Author: eclipse   File: JsonRpcDataProcessor.java    License: Eclipse Public License 2.0 5 votes vote down vote up
private TypeReference getPreconditionsUtil(final Type type, @Extension final TransformationContext context) {
  TypeReference _xifexpression = null;
  boolean _startsWith = type.getQualifiedName().startsWith("org.eclipse.lsp4j.debug");
  if (_startsWith) {
    _xifexpression = context.newTypeReference("org.eclipse.lsp4j.debug.util.Preconditions");
  } else {
    _xifexpression = context.newTypeReference("org.eclipse.lsp4j.util.Preconditions");
  }
  return _xifexpression;
}