Last active
December 16, 2015 15:49
-
-
Save rzymek/5458734 to your computer and use it in GitHub Desktop.
Creates fields for all the annotated constructor parameters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package xtend.active.annotations | |
import java.util.List | |
import org.eclipse.xtend.lib.macro.Active | |
import org.eclipse.xtend.lib.macro.TransformationContext | |
import org.eclipse.xtend.lib.macro.TransformationParticipant | |
import org.eclipse.xtend.lib.macro.declaration.MutableConstructorDeclaration | |
import org.eclipse.xtend.lib.macro.declaration.Visibility | |
@Active(typeof(DataContructorProcessor)) | |
annotation DataConstructor { | |
} | |
class DataContructorProcessor implements TransformationParticipant<MutableConstructorDeclaration> { | |
def doTransform(MutableConstructorDeclaration constructor, extension TransformationContext context) { | |
val type = constructor.declaringType | |
constructor.parameters.forEach [ arg | | |
type.addField(arg.simpleName) [ field | | |
field.final = true | |
field.type = arg.type | |
] | |
] | |
type.addMethod("__init") [ init | | |
init.body = constructor.body | |
init.visibility = Visibility::PRIVATE; | |
constructor.parameters.forEach [ arg | | |
init.addParameter(arg.simpleName, arg.type) | |
] | |
] | |
constructor.body = [ | |
''' | |
«FOR arg : constructor.parameters» | |
this.«arg.simpleName» = «arg.simpleName»; | |
«ENDFOR» | |
__init(«constructor.parameters.map[simpleName].join(',')»); | |
''' | |
] | |
} | |
override doTransform(List<? extends MutableConstructorDeclaration> annotatedTargetElements, | |
extension TransformationContext context) { | |
annotatedTargetElements.forEach[doTransform(it, context)] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment