Skip to content

Instantly share code, notes, and snippets.

@hrj
Created September 19, 2013 06:11
Show Gist options
  • Save hrj/6619630 to your computer and use it in GitHub Desktop.
Save hrj/6619630 to your computer and use it in GitHub Desktop.
@properties annotation for Xtend
package properties
import org.eclipse.xtend.lib.macro.AbstractClassProcessor
import org.eclipse.xtend.lib.macro.Active
import org.eclipse.xtend.lib.macro.TransformationContext
import org.eclipse.xtend.lib.macro.declaration.MutableClassDeclaration
import static extension properties.ASTExtensions.*
/**
* Adds getters and setters for all fields
* and default equals, hashcode and toString methods.
*/
@Active(PropertiesProcessor)
public annotation Properties {
}
class PropertiesProcessor extends AbstractClassProcessor {
override doTransform(MutableClassDeclaration cls, extension TransformationContext context) {
val extension transformations = new CommonTransformations(context)
cls.final = true
cls.persistentState.forEach [ field |
field.addGetter
field.addSetter
]
if(!cls.hasDataConstructor) cls.addDataConstructor
if(!cls.hasEquals) cls.addDataEquals
if(!cls.hasHashCode) cls.addDataHashCode
if(!cls.hasToString) cls.addDataToString
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment