org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration Java Examples

The following examples show how to use org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration. 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: DataProcessor.java    From xtext-lib with 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 File: DeclarationsTest.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Test
public void testMutableInterfaceDeclaration() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("package foo");
  _builder.newLine();
  _builder.newLine();
  _builder.append("interface MyInterface {");
  _builder.newLine();
  _builder.append("\t");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
    final MutableInterfaceDeclaration genInterface = it.getTypeLookup().findInterface("foo.MyInterface");
    final Procedure1<MutableMethodDeclaration> _function_1 = (MutableMethodDeclaration it_1) -> {
    };
    final MutableMethodDeclaration m = genInterface.addMethod("newMethod", _function_1);
    Assert.assertTrue(m.isAbstract());
  };
  this.asCompilationUnit(this.validFile(_builder), _function);
}
 
Example #3
Source File: JvmInterfaceDeclarationImpl.java    From xtext-xtend with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public MutableMethodDeclaration addMethod(final String name, final Procedure1<MutableMethodDeclaration> initializer) {
  this.checkMutable();
  ConditionUtils.checkJavaIdentifier(name, "name");
  Preconditions.checkArgument((initializer != null), "initializer cannot be null");
  final JvmOperation newMethod = TypesFactory.eINSTANCE.createJvmOperation();
  newMethod.setVisibility(JvmVisibility.PUBLIC);
  newMethod.setSimpleName(name);
  newMethod.setReturnType(this.getCompilationUnit().toJvmTypeReference(this.getCompilationUnit().getTypeReferenceProvider().getPrimitiveVoid()));
  newMethod.setAbstract(true);
  this.getDelegate().getMembers().add(newMethod);
  MemberDeclaration _memberDeclaration = this.getCompilationUnit().toMemberDeclaration(newMethod);
  final MutableMethodDeclaration mutableMethodDeclaration = ((MutableMethodDeclaration) _memberDeclaration);
  initializer.apply(mutableMethodDeclaration);
  return mutableMethodDeclaration;
}
 
Example #4
Source File: AddInterfaceWithDefaultProcessor.java    From xtext-xtend with 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 #5
Source File: JvmTypeDeclarationImpl.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public MutableMethodDeclaration addMethod(final String name, final Procedure1<MutableMethodDeclaration> initializer) {
  this.checkMutable();
  ConditionUtils.checkJavaIdentifier(name, "name");
  Preconditions.checkArgument((initializer != null), "initializer cannot be null");
  final JvmOperation newMethod = TypesFactory.eINSTANCE.createJvmOperation();
  newMethod.setVisibility(JvmVisibility.PUBLIC);
  newMethod.setSimpleName(name);
  newMethod.setReturnType(this.getCompilationUnit().toJvmTypeReference(this.getCompilationUnit().getTypeReferenceProvider().getPrimitiveVoid()));
  this.getDelegate().getMembers().add(newMethod);
  MemberDeclaration _memberDeclaration = this.getCompilationUnit().toMemberDeclaration(newMethod);
  final MutableMethodDeclaration mutableMethodDeclaration = ((MutableMethodDeclaration) _memberDeclaration);
  initializer.apply(mutableMethodDeclaration);
  return mutableMethodDeclaration;
}
 
Example #6
Source File: JvmAnnotationTypeDeclarationImpl.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public MutableMethodDeclaration addMethod(final String name, final Procedure1<MutableMethodDeclaration> initializer) {
  String _simpleName = this.getSimpleName();
  String _plus = ("The annotation \'" + _simpleName);
  String _plus_1 = (_plus + "\' cannot declare any methods.");
  throw new UnsupportedOperationException(_plus_1);
}
 
Example #7
Source File: GetProcessor.java    From xtext-xtend with 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 #8
Source File: ImmutableProcessor.java    From xtext-xtend with 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 #9
Source File: AccessorsProcessor.java    From xtext-xtend with Eclipse Public License 2.0 5 votes vote down vote up
public MutableMethodDeclaration tryAddMethod(final MutableTypeDeclaration 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 #10
Source File: ExtractProcessor.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) {
  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 #11
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 #12
Source File: TypeAdapterImplProcessor.java    From lsp4j with 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 #13
Source File: JsonRpcDataProcessor.java    From lsp4j with Eclipse Public License 2.0 5 votes vote down vote up
protected void addEitherSetter(final MutableFieldDeclaration field, final String setterName, final EitherTypeArgument argument, @Extension final JsonRpcDataTransformationContext context) {
  final Procedure1<MutableMethodDeclaration> _function = (MutableMethodDeclaration method) -> {
    context.setPrimarySourceElement(method, context.getPrimarySourceElement(field));
    method.addParameter(field.getSimpleName(), argument.getType());
    method.setStatic(field.isStatic());
    method.setVisibility(Visibility.PUBLIC);
    method.setReturnType(context.getPrimitiveVoid());
    final CompilationStrategy _function_1 = (CompilationStrategy.CompilationContext ctx) -> {
      return this.compileEitherSetterBody(field, argument, field.getSimpleName(), ctx, context);
    };
    method.setBody(_function_1);
  };
  field.getDeclaringType().addMethod(setterName, _function);
}
 
Example #14
Source File: MutableJvmInterfaceDeclarationImpl.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Iterable<? extends MutableMethodDeclaration> getDeclaredMethods() {
  Iterable<? extends MethodDeclaration> _declaredMethods = super.getDeclaredMethods();
  return ((Iterable<? extends MutableMethodDeclaration>) _declaredMethods);
}
 
Example #15
Source File: MutableJvmInterfaceDeclarationImpl.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public MutableMethodDeclaration findDeclaredMethod(final String name, final TypeReference... parameterTypes) {
  MethodDeclaration _findDeclaredMethod = super.findDeclaredMethod(name, parameterTypes);
  return ((MutableMethodDeclaration) _findDeclaredMethod);
}
 
Example #16
Source File: MutableJvmEnumerationTypeDeclarationImpl.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Iterable<? extends MutableMethodDeclaration> getDeclaredMethods() {
  Iterable<? extends MethodDeclaration> _declaredMethods = super.getDeclaredMethods();
  return ((Iterable<? extends MutableMethodDeclaration>) _declaredMethods);
}
 
Example #17
Source File: MutableJvmEnumerationTypeDeclarationImpl.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public MutableMethodDeclaration findDeclaredMethod(final String name, final TypeReference... parameterTypes) {
  MethodDeclaration _findDeclaredMethod = super.findDeclaredMethod(name, parameterTypes);
  return ((MutableMethodDeclaration) _findDeclaredMethod);
}
 
Example #18
Source File: AbstractMethodProcessor.java    From xtext-lib with Eclipse Public License 2.0 4 votes vote down vote up
public void doTransform(List<? extends MutableMethodDeclaration> annotatedMethods, @Extension TransformationContext context) {
	for (MutableMethodDeclaration annotatedMethod : annotatedMethods) {
		doTransform(annotatedMethod, context);
	}
}
 
Example #19
Source File: JsonRpcDataProcessor.java    From lsp4j with Eclipse Public License 2.0 4 votes vote down vote up
protected MutableMethodDeclaration generateToString(final MutableClassDeclaration impl, @Extension final TransformationContext context) {
  MutableMethodDeclaration _xblockexpression = null;
  {
    final ArrayList<FieldDeclaration> toStringFields = CollectionLiterals.<FieldDeclaration>newArrayList();
    ClassDeclaration c = impl;
    do {
      {
        Iterable<? extends FieldDeclaration> _declaredFields = c.getDeclaredFields();
        Iterables.<FieldDeclaration>addAll(toStringFields, _declaredFields);
        TypeReference _extendedClass = c.getExtendedClass();
        Type _type = null;
        if (_extendedClass!=null) {
          _type=_extendedClass.getType();
        }
        c = ((ClassDeclaration) _type);
      }
    } while(((c != null) && (!Objects.equal(c, context.getObject()))));
    final Procedure1<MutableMethodDeclaration> _function = (MutableMethodDeclaration it) -> {
      it.setReturnType(context.getString());
      it.addAnnotation(context.newAnnotationReference(Override.class));
      it.addAnnotation(context.newAnnotationReference(Pure.class));
      final AccessorsProcessor.Util accessorsUtil = new AccessorsProcessor.Util(context);
      StringConcatenationClient _client = new StringConcatenationClient() {
        @Override
        protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
          _builder.append(ToStringBuilder.class);
          _builder.append(" b = new ");
          _builder.append(ToStringBuilder.class);
          _builder.append("(this);");
          _builder.newLineIfNotEmpty();
          {
            for(final FieldDeclaration field : toStringFields) {
              _builder.append("b.add(\"");
              String _simpleName = field.getSimpleName();
              _builder.append(_simpleName);
              _builder.append("\", ");
              {
                TypeDeclaration _declaringType = field.getDeclaringType();
                boolean _equals = Objects.equal(_declaringType, impl);
                if (_equals) {
                  _builder.append("this.");
                  String _simpleName_1 = field.getSimpleName();
                  _builder.append(_simpleName_1);
                } else {
                  String _getterName = accessorsUtil.getGetterName(field);
                  _builder.append(_getterName);
                  _builder.append("()");
                }
              }
              _builder.append(");");
              _builder.newLineIfNotEmpty();
            }
          }
          _builder.append("return b.toString();");
          _builder.newLine();
        }
      };
      it.setBody(_client);
    };
    _xblockexpression = impl.addMethod("toString", _function);
  }
  return _xblockexpression;
}
 
Example #20
Source File: MutableJvmClassDeclarationImpl.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Iterable<? extends MutableMethodDeclaration> getDeclaredMethods() {
  Iterable<? extends MethodDeclaration> _declaredMethods = super.getDeclaredMethods();
  return ((Iterable<? extends MutableMethodDeclaration>) _declaredMethods);
}
 
Example #21
Source File: MutableJvmClassDeclarationImpl.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public MutableMethodDeclaration findDeclaredMethod(final String name, final TypeReference... parameterTypes) {
  MethodDeclaration _findDeclaredMethod = super.findDeclaredMethod(name, parameterTypes);
  return ((MutableMethodDeclaration) _findDeclaredMethod);
}
 
Example #22
Source File: MutableJvmAnnotationTypeDeclarationImpl.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Iterable<? extends MutableMethodDeclaration> getDeclaredMethods() {
  Iterable<? extends MethodDeclaration> _declaredMethods = super.getDeclaredMethods();
  return ((Iterable<? extends MutableMethodDeclaration>) _declaredMethods);
}
 
Example #23
Source File: MutableJvmAnnotationTypeDeclarationImpl.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public MutableMethodDeclaration findDeclaredMethod(final String name, final TypeReference... parameterTypes) {
  MethodDeclaration _findDeclaredMethod = super.findDeclaredMethod(name, parameterTypes);
  return ((MutableMethodDeclaration) _findDeclaredMethod);
}
 
Example #24
Source File: DeclarationsTest.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testMutableClassDeclaration() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("package foo");
  _builder.newLine();
  _builder.newLine();
  _builder.append("class MyClass<T extends CharSequence> {");
  _builder.newLine();
  _builder.append("\t");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("String myField");
  _builder.newLine();
  _builder.append("\t");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("new(String initial) {");
  _builder.newLine();
  _builder.append("\t\t");
  _builder.append("this.myField = initial");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("}");
  _builder.newLine();
  _builder.append("\t");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("def <T2 extends CharSequence> MyClass myMethod(T2 a, T b) {");
  _builder.newLine();
  _builder.append("\t\t");
  _builder.append("myField = myField + a + b");
  _builder.newLine();
  _builder.append("\t\t");
  _builder.append("return this");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("}");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
    final MutableClassDeclaration genClazz = it.getTypeLookup().findClass("foo.MyClass");
    final Procedure1<MutableMethodDeclaration> _function_1 = (MutableMethodDeclaration it_1) -> {
      CompilationUnit _compilationUnit = genClazz.getCompilationUnit();
      it_1.setReturnType(((CompilationUnitImpl) _compilationUnit).getTypeReferenceProvider().getString());
      it_1.setVisibility(Visibility.PRIVATE);
      final CompilationStrategy _function_2 = (CompilationStrategy.CompilationContext it_2) -> {
        StringConcatenation _builder_1 = new StringConcatenation();
        _builder_1.append("return \"foo\";");
        _builder_1.newLine();
        return _builder_1;
      };
      it_1.setBody(_function_2);
    };
    genClazz.addMethod("newMethod", _function_1);
    final MutableMethodDeclaration mutableMethod = genClazz.findDeclaredMethod("newMethod");
    Assert.assertSame(mutableMethod, ((Object[])Conversions.unwrapArray(genClazz.getDeclaredMethods(), Object.class))[1]);
    Assert.assertEquals("String", mutableMethod.getReturnType().toString());
    Assert.assertEquals(Visibility.PRIVATE, mutableMethod.getVisibility());
  };
  this.asCompilationUnit(this.validFile(_builder), _function);
}
 
Example #25
Source File: DeclarationsTest.java    From xtext-xtend with Eclipse Public License 2.0 4 votes vote down vote up
@Test
public void testXtendClassWithMethodFieldAndConstructor() {
  StringConcatenation _builder = new StringConcatenation();
  _builder.append("package foo");
  _builder.newLine();
  _builder.newLine();
  _builder.append("class MyClass<T extends CharSequence> {");
  _builder.newLine();
  _builder.append("\t");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("String myField");
  _builder.newLine();
  _builder.append("\t");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("new(String initial) {");
  _builder.newLine();
  _builder.append("\t\t");
  _builder.append("this.myField = initial");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("}");
  _builder.newLine();
  _builder.append("\t");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("def <T2 extends CharSequence> MyClass myMethod(T2 a, T b) {");
  _builder.newLine();
  _builder.append("\t\t");
  _builder.append("myField = myField + a + b");
  _builder.newLine();
  _builder.append("\t\t");
  _builder.append("return this");
  _builder.newLine();
  _builder.append("\t");
  _builder.append("}");
  _builder.newLine();
  _builder.append("}");
  _builder.newLine();
  final Procedure1<CompilationUnitImpl> _function = (CompilationUnitImpl it) -> {
    Assert.assertEquals("foo", it.getPackageName());
    TypeDeclaration _head = IterableExtensions.head(it.getSourceTypeDeclarations());
    final ClassDeclaration clazz = ((ClassDeclaration) _head);
    final MutableClassDeclaration genClazz = it.getTypeLookup().findClass("foo.MyClass");
    Assert.assertEquals("foo.MyClass", clazz.getQualifiedName());
    Assert.assertNull(clazz.getExtendedClass());
    Assert.assertTrue(IterableExtensions.isEmpty(clazz.getImplementedInterfaces()));
    Assert.assertEquals(3, IterableExtensions.size(clazz.getDeclaredMembers()));
    Assert.assertEquals("T", IterableExtensions.head(clazz.getTypeParameters()).getSimpleName());
    Assert.assertEquals("CharSequence", IterableExtensions.head(IterableExtensions.head(clazz.getTypeParameters()).getUpperBounds()).toString());
    Assert.assertSame(clazz, IterableExtensions.head(clazz.getTypeParameters()).getTypeParameterDeclarator());
    final FieldDeclaration field = IterableExtensions.head(clazz.getDeclaredFields());
    Assert.assertSame(clazz, field.getDeclaringType());
    Assert.assertEquals("myField", field.getSimpleName());
    Assert.assertEquals("String", field.getType().toString());
    Assert.assertFalse(field.isFinal());
    final ConstructorDeclaration constructor = IterableExtensions.head(clazz.getDeclaredConstructors());
    Assert.assertSame(clazz, constructor.getDeclaringType());
    Assert.assertEquals("MyClass", constructor.getSimpleName());
    Assert.assertEquals("initial", IterableExtensions.head(constructor.getParameters()).getSimpleName());
    Assert.assertEquals("String", IterableExtensions.head(constructor.getParameters()).getType().toString());
    final MethodDeclaration method = IterableExtensions.head(clazz.getDeclaredMethods());
    final MutableMethodDeclaration genMethod = IterableExtensions.head(genClazz.getDeclaredMethods());
    Assert.assertSame(clazz, method.getDeclaringType());
    Assert.assertEquals("a", IterableExtensions.head(method.getParameters()).getSimpleName());
    Assert.assertEquals("T2", IterableExtensions.head(method.getParameters()).getType().toString());
    Assert.assertSame(IterableExtensions.head(genMethod.getTypeParameters()), IterableExtensions.head(method.getParameters()).getType().getType());
    Assert.assertEquals("T", (((ParameterDeclaration[])Conversions.unwrapArray(method.getParameters(), ParameterDeclaration.class))[1]).getType().toString());
    Assert.assertSame(IterableExtensions.head(genClazz.getTypeParameters()), (((ParameterDeclaration[])Conversions.unwrapArray(method.getParameters(), ParameterDeclaration.class))[1]).getType().getType());
    Assert.assertSame(genClazz, method.getReturnType().getType());
    Assert.assertEquals("T2", IterableExtensions.head(method.getTypeParameters()).getSimpleName());
    Assert.assertEquals("CharSequence", IterableExtensions.head(IterableExtensions.head(method.getTypeParameters()).getUpperBounds()).toString());
    Assert.assertSame(IterableExtensions.head(method.getTypeParameters()), IterableExtensions.head(method.getTypeParameters()));
    Assert.assertSame(method, IterableExtensions.head(method.getTypeParameters()).getTypeParameterDeclarator());
    Assert.assertSame(field, ((Object[])Conversions.unwrapArray(clazz.getDeclaredMembers(), Object.class))[0]);
    Assert.assertSame(constructor, ((Object[])Conversions.unwrapArray(clazz.getDeclaredMembers(), Object.class))[1]);
    Assert.assertSame(method, ((Object[])Conversions.unwrapArray(clazz.getDeclaredMembers(), Object.class))[2]);
  };
  this.asCompilationUnit(this.validFile(_builder), _function);
}
 
Example #26
Source File: ToStringProcessor.java    From xtext-lib with Eclipse Public License 2.0 4 votes vote down vote up
public void addToString(final MutableClassDeclaration cls, final Iterable<? extends FieldDeclaration> fields, final ToStringConfiguration config) {
  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(ToStringBuilder.class);
        _builder.append(" b = new ");
        _builder.append(ToStringBuilder.class);
        _builder.append("(this);");
        _builder.newLineIfNotEmpty();
        {
          boolean _isSkipNulls = config.isSkipNulls();
          if (_isSkipNulls) {
            _builder.append("b.skipNulls();");
          }
        }
        _builder.newLineIfNotEmpty();
        {
          boolean _isSingleLine = config.isSingleLine();
          if (_isSingleLine) {
            _builder.append("b.singleLine();");
          }
        }
        _builder.newLineIfNotEmpty();
        {
          boolean _isHideFieldNames = config.isHideFieldNames();
          if (_isHideFieldNames) {
            _builder.append("b.hideFieldNames();");
          }
        }
        _builder.newLineIfNotEmpty();
        {
          boolean _isVerbatimValues = config.isVerbatimValues();
          if (_isVerbatimValues) {
            _builder.append("b.verbatimValues();");
          }
        }
        _builder.newLineIfNotEmpty();
        {
          for(final FieldDeclaration field : fields) {
            _builder.append("b.add(\"");
            String _simpleName = field.getSimpleName();
            _builder.append(_simpleName);
            _builder.append("\", this.");
            String _simpleName_1 = field.getSimpleName();
            _builder.append(_simpleName_1);
            _builder.append(");");
            _builder.newLineIfNotEmpty();
          }
        }
        _builder.append("return b.toString();");
        _builder.newLine();
      }
    };
    it.setBody(_client);
  };
  cls.addMethod("toString", _function);
}
 
Example #27
Source File: ToStringProcessor.java    From xtext-lib with Eclipse Public License 2.0 4 votes vote down vote up
public void addReflectiveToString(final MutableClassDeclaration cls, final ToStringConfiguration config) {
  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("return new ");
        _builder.append(ToStringBuilder.class);
        _builder.append("(this)");
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append(".addAllFields()");
        _builder.newLine();
        _builder.append("\t");
        {
          boolean _isSkipNulls = config.isSkipNulls();
          if (_isSkipNulls) {
            _builder.append(".skipNulls()");
          }
        }
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        {
          boolean _isSingleLine = config.isSingleLine();
          if (_isSingleLine) {
            _builder.append(".singleLine()");
          }
        }
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        {
          boolean _isHideFieldNames = config.isHideFieldNames();
          if (_isHideFieldNames) {
            _builder.append(".hideFieldNames()");
          }
        }
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        {
          boolean _isVerbatimValues = config.isVerbatimValues();
          if (_isVerbatimValues) {
            _builder.append(".verbatimValues()");
          }
        }
        _builder.newLineIfNotEmpty();
        _builder.append("\t");
        _builder.append(".toString();");
        _builder.newLine();
      }
    };
    it.setBody(_client);
  };
  cls.addMethod("toString", _function);
}
 
Example #28
Source File: EqualsHashCodeProcessor.java    From xtext-lib with Eclipse Public License 2.0 4 votes vote down vote up
public void addHashCode(final MutableClassDeclaration cls, final Iterable<? extends FieldDeclaration> includedFields, final boolean includeSuper) {
  String _xifexpression = null;
  if (includeSuper) {
    _xifexpression = "super.hashCode()";
  } else {
    _xifexpression = "1";
  }
  final String defaultBase = _xifexpression;
  final int fields = IterableExtensions.size(includedFields);
  final Procedure1<MutableMethodDeclaration> _function = (MutableMethodDeclaration it) -> {
    this.context.setPrimarySourceElement(it, this.context.getPrimarySourceElement(cls));
    it.setReturnType(this.context.getPrimitiveInt());
    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) {
        {
          if ((fields >= 2)) {
            _builder.append("final int prime = ");
            _builder.append(EqualsHashCodeProcessor.Util.PRIME_VALUE);
            _builder.append(";");
            _builder.newLineIfNotEmpty();
            _builder.append("int result = ");
            _builder.append(defaultBase);
            _builder.append(";");
            _builder.newLineIfNotEmpty();
            {
              ExclusiveRange _doubleDotLessThan = new ExclusiveRange(0, fields, true);
              for(final Integer i : _doubleDotLessThan) {
                {
                  if (((i).intValue() == (fields - 1))) {
                    _builder.append("return");
                  } else {
                    _builder.append("result =");
                  }
                }
                _builder.append(" prime * result + ");
                StringConcatenationClient _contributeToHashCode = Util.this.contributeToHashCode(((FieldDeclaration[])Conversions.unwrapArray(includedFields, FieldDeclaration.class))[(i).intValue()]);
                _builder.append(_contributeToHashCode);
                _builder.append(";");
                _builder.newLineIfNotEmpty();
              }
            }
          } else {
            if ((fields == 1)) {
              _builder.append("return ");
              _builder.append(EqualsHashCodeProcessor.Util.PRIME_VALUE);
              _builder.append(" * ");
              _builder.append(defaultBase);
              _builder.append(" + ");
              StringConcatenationClient _contributeToHashCode_1 = Util.this.contributeToHashCode(IterableExtensions.head(includedFields));
              _builder.append(_contributeToHashCode_1);
              _builder.append(";");
              _builder.newLineIfNotEmpty();
            } else {
              _builder.append("return ");
              _builder.append(defaultBase);
              _builder.append(";");
              _builder.newLineIfNotEmpty();
            }
          }
        }
      }
    };
    it.setBody(_client);
  };
  cls.addMethod("hashCode", _function);
}
 
Example #29
Source File: EqualsHashCodeProcessor.java    From xtext-lib with 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);
}
 
Example #30
Source File: AbstractMethodProcessor.java    From xtext-lib with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * @param annotatedMethod a mutable method representation annotated with the annotation this processor is responsible for.
 * @param context
 */
public void doTransform(MutableMethodDeclaration annotatedMethod, @Extension TransformationContext context) {}