Java Code Examples for org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration

The following examples show how to use org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration. These examples are extracted from open source projects. 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: DataProcessor.java    License: Eclipse Public License 2.0 6 votes vote down vote up
public void addDataToString(final MutableClassDeclaration cls) {
  final Procedure1<MutableMethodDeclaration> _function = (MutableMethodDeclaration it) -> {
    this.context.setPrimarySourceElement(it, this.context.getPrimarySourceElement(cls));
    it.setReturnType(this.context.getString());
    it.addAnnotation(this.context.newAnnotationReference(Override.class));
    it.addAnnotation(this.context.newAnnotationReference(Pure.class));
    StringConcatenationClient _client = new StringConcatenationClient() {
      @Override
      protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
        _builder.append("String result = new ");
        _builder.append(ToStringHelper.class);
        _builder.append("().toString(this);");
        _builder.newLineIfNotEmpty();
        _builder.append("return result;");
        _builder.newLine();
      }
    };
    it.setBody(_client);
  };
  cls.addMethod("toString", _function);
}
 
Example #2
Source Project: xtext-lib   Author: eclipse   File: FinalFieldsConstructorProcessor.java    License: Eclipse Public License 2.0 6 votes vote down vote up
public void addFinalFieldsConstructor(final MutableClassDeclaration it) {
  boolean _isEmpty = this.getFinalFieldsConstructorArgumentTypes(it).isEmpty();
  if (_isEmpty) {
    final AnnotationReference anno = it.findAnnotation(this.context.findTypeGlobally(FinalFieldsConstructor.class));
    StringConcatenation _builder = new StringConcatenation();
    _builder.append("There are no final fields, this annotation has no effect");
    this.context.addWarning(anno, _builder.toString());
    return;
  }
  boolean _hasFinalFieldsConstructor = this.hasFinalFieldsConstructor(it);
  if (_hasFinalFieldsConstructor) {
    this.context.addError(it, this.getConstructorAlreadyExistsMessage(it));
    return;
  }
  final Procedure1<MutableConstructorDeclaration> _function = (MutableConstructorDeclaration it_1) -> {
    this.context.setPrimarySourceElement(it_1, this.context.getPrimarySourceElement(it_1.getDeclaringType()));
    this.makeFinalFieldsConstructor(it_1);
  };
  it.addConstructor(_function);
}
 
Example #3
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 #4
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 #5
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 #6
Source Project: xtext-xtend   Author: eclipse   File: DeclarationsTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testAnnotation2() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("class MyClass {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("@com.google.inject.Inject() MyClass foo");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
    TypeDeclaration _head = IterableExtensions.head(it.getSourceTypeDeclarations());
    final ClassDeclaration sourceClazz = ((ClassDeclaration) _head);
    final MutableClassDeclaration javaClass = it.getTypeLookup().findClass("MyClass");
    Assert.assertEquals(javaClass.getQualifiedName(), sourceClazz.getQualifiedName());
    final FieldDeclaration field = IterableExtensions.head(sourceClazz.getDeclaredFields());
    Assert.assertEquals(Boolean.FALSE, IterableExtensions.head(field.getAnnotations()).getValue("optional"));
    final MutableFieldDeclaration javaField = IterableExtensions.head(javaClass.getDeclaredFields());
    Object _value = IterableExtensions.head(javaField.getAnnotations()).getValue("optional");
    Assert.assertFalse((((Boolean) _value)).booleanValue());
  };
  this.asCompilationUnit(this.validFile(_builder), _function);
}
 
Example #7
Source Project: xtext-xtend   Author: eclipse   File: DeclarationsTest.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testRemove() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("class C {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("def void m() {}");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
    TypeDeclaration _head = IterableExtensions.head(it.getSourceTypeDeclarations());
    final ClassDeclaration c = ((ClassDeclaration) _head);
    final MutableClassDeclaration mutable = it.getTypeLookup().findClass(c.getQualifiedName());
    final Consumer<MutableMemberDeclaration> _function_1 = (MutableMemberDeclaration it_1) -> {
      it_1.remove();
    };
    mutable.getDeclaredMembers().forEach(_function_1);
    Assert.assertTrue(IterableExtensions.isEmpty(mutable.getDeclaredMembers()));
  };
  this.asCompilationUnit(this.validFile(_builder), _function);
}
 
Example #8
Source Project: xtext-xtend   Author: eclipse   File: ProblemSupportTests.java    License: Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testErrorOnDerivedElement() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("class MyClass {");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("String foo = \'foo\'");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
    final MutableClassDeclaration derived = it.getTypeLookup().findClass("MyClass");
    it.getProblemSupport().addError(derived, "error on derived element");
    Assert.assertEquals("error on derived element", IterableExtensions.<Resource.Diagnostic>head(it.getXtendFile().eResource().getErrors()).getMessage());
    Resource.Diagnostic _head = IterableExtensions.<Resource.Diagnostic>head(it.getXtendFile().eResource().getErrors());
    Assert.assertEquals(IterableExtensions.<XtendTypeDeclaration>head(it.getXtendFile().getXtendTypes()), ((EObjectDiagnosticImpl) _head).getProblematicObject());
  };
  this.asCompilationUnit(this.validFile(_builder), _function);
}
 
Example #9
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 #10
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 #11
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 #12
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 #13
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 #14
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 #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: 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 #17
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 #18
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 #19
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 #20
Source Project: xtext-xtend   Author: eclipse   File: ImmutableProcessor.java    License: Eclipse Public License 2.0 5 votes vote down vote up
public MutableMethodDeclaration tryAddMethod(final MutableClassDeclaration it, final String name, final Procedure1<? super MutableMethodDeclaration> initializer) {
  MutableMethodDeclaration _elvis = null;
  MutableMethodDeclaration _findDeclaredMethod = it.findDeclaredMethod(name);
  if (_findDeclaredMethod != null) {
    _elvis = _findDeclaredMethod;
  } else {
    MutableMethodDeclaration _addMethod = it.addMethod(name, ((Procedure1<MutableMethodDeclaration>)initializer));
    _elvis = _addMethod;
  }
  return _elvis;
}
 
Example #21
Source Project: xtext-xtend   Author: eclipse   File: DeclarationsTest.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Test
public void testSetImplementedInterfaces() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("class BaseClass {}");
  _builder.newLine();
  _builder.append("interface Interface {}");
  _builder.newLine();
  final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
    final MutableClassDeclaration baseClass = it.getTypeLookup().findClass("BaseClass");
    final MutableInterfaceDeclaration interf = it.getTypeLookup().findInterface("Interface");
    final TypeReference objectType = baseClass.getExtendedClass();
    Assert.assertEquals("Object", objectType.getSimpleName());
    Assert.assertTrue(IterableExtensions.isEmpty(baseClass.getImplementedInterfaces()));
    final TypeReference superType = it.getTypeReferenceProvider().newTypeReference(AccessibleObject.class);
    baseClass.setExtendedClass(superType);
    Assert.assertEquals("AccessibleObject", baseClass.getExtendedClass().getSimpleName());
    Assert.assertTrue(IterableExtensions.isEmpty(baseClass.getImplementedInterfaces()));
    baseClass.setExtendedClass(null);
    Assert.assertEquals("Object", baseClass.getExtendedClass().getSimpleName());
    Assert.assertTrue(IterableExtensions.isEmpty(baseClass.getImplementedInterfaces()));
    TypeReference _newTypeReference = it.getTypeReferenceProvider().newTypeReference(interf);
    baseClass.setImplementedInterfaces(Collections.<TypeReference>unmodifiableList(CollectionLiterals.<TypeReference>newArrayList(_newTypeReference)));
    Assert.assertEquals("Interface", IterableExtensions.head(baseClass.getImplementedInterfaces()).getSimpleName());
    baseClass.setImplementedInterfaces(Collections.<TypeReference>unmodifiableList(CollectionLiterals.<TypeReference>newArrayList()));
    Assert.assertTrue(IterableExtensions.isEmpty(baseClass.getImplementedInterfaces()));
  };
  this.asCompilationUnit(this.validFile(_builder), _function);
}
 
Example #22
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 #23
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 #24
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 #25
Source Project: xtext-xtend   Author: eclipse   File: TypeLookupImpl.java    License: Eclipse Public License 2.0 5 votes vote down vote up
@Override
public MutableClassDeclaration findClass(final String qualifiedName) {
  final Type type = this.findType(qualifiedName);
  MutableClassDeclaration _switchResult = null;
  boolean _matched = false;
  if (type instanceof MutableClassDeclaration) {
    _matched=true;
    _switchResult = ((MutableClassDeclaration)type);
  }
  return _switchResult;
}
 
Example #26
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 #27
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 #28
Source Project: xtext-lib   Author: eclipse   File: AbstractClassProcessor.java    License: Eclipse Public License 2.0 4 votes vote down vote up
public void doTransform(List<? extends MutableClassDeclaration> annotatedClasses, @Extension TransformationContext context) {
	for (MutableClassDeclaration annotatedClass : annotatedClasses) {
		doTransform(annotatedClass, context);
	}
}
 
Example #29
Source Project: xtext-lib   Author: eclipse   File: DataProcessor.java    License: Eclipse Public License 2.0 4 votes vote down vote up
public Iterable<? extends MutableFieldDeclaration> getDataFields(final MutableClassDeclaration it) {
  final Function1<MutableFieldDeclaration, Boolean> _function = (MutableFieldDeclaration it_1) -> {
    return Boolean.valueOf(((!it_1.isStatic()) && this.context.isThePrimaryGeneratedJavaElement(it_1)));
  };
  return IterableExtensions.filter(it.getDeclaredFields(), _function);
}
 
Example #30
Source Project: xtext-lib   Author: eclipse   File: EqualsHashCodeProcessor.java    License: Eclipse Public License 2.0 4 votes vote down vote up
public void addEquals(final MutableClassDeclaration cls, final Iterable<? extends FieldDeclaration> includedFields, final boolean includeSuper) {
  final Procedure1<MutableMethodDeclaration> _function = (MutableMethodDeclaration it) -> {
    this.context.setPrimarySourceElement(it, this.context.getPrimarySourceElement(cls));
    it.setReturnType(this.context.getPrimitiveBoolean());
    it.addAnnotation(this.context.newAnnotationReference(Override.class));
    it.addAnnotation(this.context.newAnnotationReference(Pure.class));
    it.addParameter("obj", this.context.getObject());
    StringConcatenationClient _client = new StringConcatenationClient() {
      @Override
      protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
        _builder.append("if (this == obj)");
        _builder.newLine();
        _builder.append("  ");
        _builder.append("return true;");
        _builder.newLine();
        _builder.append("if (obj == null)");
        _builder.newLine();
        _builder.append("  ");
        _builder.append("return false;");
        _builder.newLine();
        _builder.append("if (getClass() != obj.getClass())");
        _builder.newLine();
        _builder.append("  ");
        _builder.append("return false;");
        _builder.newLine();
        {
          if (includeSuper) {
            _builder.append("if (!super.equals(obj))");
            _builder.newLine();
            _builder.append("  ");
            _builder.append("return false;");
            _builder.newLine();
          }
        }
        {
          int _size = IterableExtensions.size(includedFields);
          boolean _greaterThan = (_size > 0);
          if (_greaterThan) {
            TypeReference _newWildCardSelfTypeReference = Util.this.newWildCardSelfTypeReference(cls);
            _builder.append(_newWildCardSelfTypeReference);
            _builder.append(" other = (");
            TypeReference _newWildCardSelfTypeReference_1 = Util.this.newWildCardSelfTypeReference(cls);
            _builder.append(_newWildCardSelfTypeReference_1);
            _builder.append(") obj;");
            _builder.newLineIfNotEmpty();
          }
        }
        {
          for(final FieldDeclaration field : includedFields) {
            StringConcatenationClient _contributeToEquals = Util.this.contributeToEquals(field);
            _builder.append(_contributeToEquals);
            _builder.newLineIfNotEmpty();
          }
        }
        _builder.append("return true;");
        _builder.newLine();
      }
    };
    it.setBody(_client);
  };
  cls.addMethod("equals", _function);
}