Java Code Examples for org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration#setBody()
The following examples show how to use
org.eclipse.xtend.lib.macro.declaration.MutableMethodDeclaration#setBody() .
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 |
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: AddInterfaceWithDefaultProcessor.java From xtext-xtend with Eclipse Public License 2.0 | 6 votes |
@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 File: TypeAdapterImplProcessor.java From lsp4j with Eclipse Public License 2.0 | 5 votes |
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 4
Source File: JsonRpcDataProcessor.java From lsp4j with Eclipse Public License 2.0 | 5 votes |
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 5
Source File: EqualsHashCodeProcessor.java From xtext-lib with Eclipse Public License 2.0 | 4 votes |
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 6
Source File: EqualsHashCodeProcessor.java From xtext-lib with Eclipse Public License 2.0 | 4 votes |
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 7
Source File: ToStringProcessor.java From xtext-lib with Eclipse Public License 2.0 | 4 votes |
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 8
Source File: ToStringProcessor.java From xtext-lib with Eclipse Public License 2.0 | 4 votes |
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 9
Source File: DeclarationsTest.java From xtext-xtend with Eclipse Public License 2.0 | 4 votes |
@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 10
Source File: JsonRpcDataProcessor.java From lsp4j with Eclipse Public License 2.0 | 4 votes |
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; }