2. @walkmod#walkmodevoxx
A bit of me
• 7 years working with Java
• Team leader in the research group DAMA-UPC
• Experience in Social Network Analysis, Deduplication algorithms &
recommendation engines
• Research publications about software engineering
• Currently involved in CoherentPaaS (EU project)
4. @walkmod#walkmodevoxx
Coding conventions
“Set of guidelines for a specific programming language that
recommend programming style, practices and methods for each
aspect of a piece program written in this language” - wikipedia
• Define rules to accomplish DRY and make code readable
• Are partially supported by IDEs & Text editors
5.
6. @walkmod#walkmodevoxx
... shall we dream again? yes!
walkmod
• Tool to apply coding conventions
• Extensible tool by plugins (DIY)
• Respects your code changes
• Works with Java 8 code
• Open source + free
8. @walkmod#walkmodevoxx
How walkmod works?
•Most code conventions can be automatically assured using
code transformations. Types:
•Visitors (Visitor pattern): an straight-way java
transformation
•Scripts: Polyglot inline transformations
•Template: DYSWYG transformations
9. @walkmod#walkmodevoxx
public class HelloVisitor extends VoidVisitor<VisitorContext>{
...
@Overwrite
public void visit(MethodDeclaration md, VisitorContext ctx){
//TODO
}
@Overwrite
public void visit(FieldDeclaration fd, VisitorContext ctx){
//TODO
}
...
}
Visitor Example
10. @walkmod#walkmodevoxx
import org.walkmod.javalang.ast.body.ModifierSet;
import java.lang.reflect.Modifier;
import org.walkmod.javalang.ast.body.FieldDeclaration;
for(type in node.types){!
def fields = type.members.findAll({it instanceof FieldDeclaration});
for (field in fields){
int modifiers = ModifierSet.addModifier(field.modifiers, Modifier.PRIVATE);
modifiers = ModifierSet.removeModifier(modifiers, Modifier.PROTECTED);
modifiers = ModifierSet.removeModifier(modifiers, Modifier.PUBLIC);
field.setModifiers(modifiers);
}
}
Script Example (Groovy)
11. @walkmod#walkmodevoxx
package ${query.resolve("root.package.name")};
import javax.persistence.Id;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
public class ${query.resolve("type.name")} {
! @Id
! @GeneratedValue(strategy = GenerationType.SEQUENCE)
! Integer id;
! @Override
! public boolean equals(Object o) {
! ! if (o instanceof ${query.resolve("type.name")}) {
! ! ! return id.equals(((${query.resolve("type.name")}) o).id);
! ! }
! ! return false;
! }
}
Template Example (Groovy)
12. @walkmod#walkmodevoxx
How does walkmod respect your changes?
• Applies a semantic merge after each transformation.
• Each source code element (e.g. methods, fields,
statements...) has an specific merge policy.
• Liberates code transformations from change-management
responsibility.
13. @walkmod#walkmodevoxx
Can walkmod grow up?
• Creating new plugins and deploying them into maven
• The whole architecture is extensible and configurable.
• Plugins are loaded in runtime before applying
transformations.
15. @walkmod#walkmodevoxx
Thanks / Creative Commons
•Presentation Template — Guillaume LaForge
•The Queen — A prestigious heritage with some
inspiration from The Sex Pistols and funny Devoxxians
•Girl with a Balloon & Follow your dreams — Banksy
•Tube — Michael Keen