Java Code Examples for org.eclipse.xtend.lib.macro.TransformationContext#findClass()

The following examples show how to use org.eclipse.xtend.lib.macro.TransformationContext#findClass() . 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 File: AccessorsAnnotationTest.java    From xtext-xtend with 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 2
Source File: ExtractTest.java    From xtext-xtend with 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 3
Source File: ExternalizedTest.java    From xtext-xtend with 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 4
Source File: AccessorsAnnotationTest.java    From xtext-xtend with 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 5
Source File: _TESTDATA_InternalClassProcessor.java    From xtext-xtend with 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);
}