33rd degree talk: open and automatic coding conventions with walkmod
DESCRIPTION
33rd degree talk about walkmod, an open source tool to apply and share coding conventionsTRANSCRIPT
![Page 1: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/1.jpg)
Platinum Sponsor
OPEN AND AUTOMATIC CODING CONVENTIONS WITH WALKMOD
Raquel Pau
Sponsored talk by:
![Page 2: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/2.jpg)
hello!
@raquelpau @acoroleu
![Page 3: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/3.jpg)
which is our motivation?
DRYDon’t repeat yourself.
![Page 4: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/4.jpg)
What are coding conventions?
“coding conventions are a 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
![Page 5: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/5.jpg)
convention types
• Formatting rules: headers, indentation, documentation, code style..
• Naming rules: labeling rules for methods, classes..
• Best practices: software patterns, common bugs resolution..
![Page 6: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/6.jpg)
google use conventions!
![Page 7: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/7.jpg)
everybody follow conventions
http://sideeffect.kr/popularconvention
![Page 8: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/8.jpg)
“it takes a while to create nothing”
Ron Jeffries
![Page 9: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/9.jpg)
our goals
• automatizing the resolution of problems detected by code quality tools (i.e PMD, , sonar, findbugs).
• automatizing the development and repetitive tasks i.e: creating basic CRUD services for the entire model.
![Page 10: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/10.jpg)
walkmod is open!
• open source: feel free to suggest changes!
• free for everybody: tell it to your boss!
• extensible by plugins do it yourself and share them! DIY+S
Introduction
![Page 11: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/11.jpg)
walkmod is ready for Java 8
but also could by polyglot!
![Page 12: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/12.jpg)
how it works
making awesome things happen!
![Page 13: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/13.jpg)
>_ man walkmod
• >_ walkmod apply modifies the source code according a set of coding conventions
• >_ walkmod check specifies which source files do not follow a set of coding conventions
how it works
![Page 14: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/14.jpg)
conventions driven by transformations
how it works
• Coding conventions are applied with blocks of transformations for each source file.
• Transformations may update the same sources or creating/updating another ones.
![Page 15: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/15.jpg)
overview
• reader: reads the sources. i.e retrieves all files from a directory recursively.
• walker: executes a chain of transformations for each source.
• writer: writes the sources. i.e using the eclipse formatter.
how it works
![Page 16: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/16.jpg)
how it works
![Page 17: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/17.jpg)
reader
• Reads the sources. By default, reads the folder src/main/java.
• Works with multiple include/exclude rules.
• Creates a resource object, whose content is iterated from the walker.
how it works
![Page 18: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/18.jpg)
walker• Executes a chain of transformations for each object allocated in a
resource. i.e all java source files of an specific folder.
• merges the output produced by transformations with existent resources.
• analyzes and reports which changes have been produced by the chain of transformations in each object.
• invokes the writer with the final (and merged) output if there is any change.
how it works
![Page 19: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/19.jpg)
writer
• writes each object allocated in a resource. i.e all java source files of a specific folder.
• Has include/exclude rules.
• There are useful writer implementations, such as the storage of the contents of a toString() object method or the eclipse formatter.
how it works
![Page 20: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/20.jpg)
transformations
Code conventions are a reality by means of code transformations
![Page 21: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/21.jpg)
modify or generate code without changing walkmod
transformations
![Page 22: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/22.jpg)
visitors
transformations
![Page 23: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/23.jpg)
public class HelloVisitor extends VoidVisitor<VisitorContext>{...@Overwritepublic void visit(MethodDeclaration md, VisitorContext ctx){//TODO
}
@Overwritepublic void visit(FieldDeclaration fd, VisitorContext ctx){//TODO
}...
}
transformations
![Page 24: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/24.jpg)
why visitors?
• Visitor design pattern is a way of separating an algorithm from an object structure on which it operates.
• Straight-forward way to design code transformations in java.
• Visitors can just be shared by means of walkmod plugins.
transformations
![Page 25: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/25.jpg)
scripts
transformations
![Page 26: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/26.jpg)
transformations
![Page 27: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/27.jpg)
scripts
• Scripts allow the design of inline transformations.
• Scripts should be used to apply simple modifications in source files.
• Support for multiple languages. Those which implement the standard Java scripting interface. i.e. groovy, javascript, python..
transformations
![Page 28: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/28.jpg)
templates
transformations
![Page 29: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/29.jpg)
transformations
![Page 30: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/30.jpg)
templatestransformations
• What You See Is What You Get (WYSIWYG): Simplest way to dynamic content to follow a given convention.
• groovy is the default template engine, but can be customized.
![Page 31: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/31.jpg)
remembertransformations
• Visitors: a straight-forward way to design code transformations in java.
• scripts: to design inline transformations without creating a java library.
• templates: WYSIWYG transformations.
![Page 32: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/32.jpg)
query engine
Write less to do the same!
![Page 33: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/33.jpg)
query engine
MethodDeclaration method = null;
Collection members = type.getMembers();
Iterator it = members.iterator();
while (it.hasNext()){
BodyDeclaration member = (BodyDeclaration)it.next();
if (member instance of MethodDeclararion){
MethodDeclarion aux = (MethodDeclaration) member;
if(“execute”.equals(aux.getName()){
method = aux;
break;
}
}
}
type.methods.find({it.name.equals(“execute”)})
![Page 34: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/34.jpg)
{ context } + query expression
query engine
![Page 35: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/35.jpg)
query language
• The default query language is gPath (groovy), but you can change it for your favorite language.
• Common used large query expressions can be referenced from Alias. “TypeDeclaration.metaClass.getMethods = { -> delegate.members.findAll({it instanceof MethodDeclaration}); }”
query engine
![Page 36: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/36.jpg)
queries from templates
Using the query object: ${query.resolve(“expr”)}.
query engine
import org.apache.log4j.Logger;
public class ${query.resolve("type.name")}{
public static Logger log = Logger.getLogger(${query.resolve("type.name")}.class);
}
template to add Loggers
![Page 37: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/37.jpg)
queries from scripts
accessing through a binding called query
query engine
..
for( type in node.types) {
def result = query.resolve(type, “methods”);
...
}
...
groovy script querying the type methods
![Page 38: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/38.jpg)
queries from visitors
Implementing QueryEngineAware or extending VisitorSupport.
query engine
public class MyVisitor extends VisitorSupport{
@Overwrite
public void visit(TypeDeclaration td, VisitorContext ctx){
Object result = query(
td, //context
“methods.find({it.name.equals(“foo”)})” //expr
);
}
}
visitor code with a gpath query
![Page 39: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/39.jpg)
merge engine
Don’t loose your changes
![Page 40: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/40.jpg)
why a merge engine?
• Developer changes are respected (e.g. adding a new method).
• Simplify transformations. Otherwise, transformations would check many conditions to avoid repeating code or overwriting it.
merge engine
![Page 41: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/41.jpg)
merge engine
![Page 42: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/42.jpg)
merge engine
![Page 43: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/43.jpg)
merge engine
![Page 44: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/44.jpg)
merge engine
![Page 45: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/45.jpg)
merge engine
![Page 46: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/46.jpg)
semantic merge
• Code is merged according to the meaning of its elements instead of simply merging text.
• Each node type has an specific merge policy.
merge engine
![Page 47: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/47.jpg)
named nodes | anonymous nodes
• named nodes: those nodes identifiable inside an AST.
e.g. methods or fields of a given type
• anonymous nodes: those non-identifiable.
e.g. statements of a method
merge engine
![Page 48: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/48.jpg)
merging named nodes
• append policy: only adds non existing nodes generated by a transformation.
• overwrite policy: always replaces existing nodes by the generated ones.
merge engine
![Page 49: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/49.jpg)
merging anonymous nodes
• assign policy replaces the whole original list of nodes by generated nodes.
• unmodify policy maintains the original list of nodes.
• addall policy appends the whole list of generated nodes into the original list.
merge engine
![Page 50: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/50.jpg)
plugins
Let’s grow up!
![Page 51: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/51.jpg)
walkmod is growing up
plugins
![Page 52: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/52.jpg)
where can I find them?plugins
![Page 53: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/53.jpg)
how to extend walkmod?
• Creating new java libraries and deploying them into a maven repository (public or private) with the name walkmod-xxx-plugin.
• All walkmod extensions need a plugin descriptor of their components in the META-INF/walkmod directory inside the jar library.
plugins
![Page 54: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/54.jpg)
plugins backend
• Walkmod embeds apache ivy to download plugins from maven repositories in runtime.
• Custom repositories are in ${walkmod-home}/conf/ivysettings.xml
• All plugin jars and their dependencies are files loaded dynamically into a new classloader.
plugins
![Page 55: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/55.jpg)
roadmap
![Page 56: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/56.jpg)
next steps
• + plugins: Create and publish new plugins (e.g. naming rules or support for other programming languages).
• - less configuration: reutilization by inheritance / include rules of the XML elements.
• saas: publish online services for running walkmod and all its plugins (also private).
roadmap
![Page 57: 33rd degree talk: open and automatic coding conventions with walkmod](https://reader033.vdocuments.us/reader033/viewer/2022042700/559b12711a28ab9c498b47a9/html5/thumbnails/57.jpg)
thanks for your time!
and.. do cool things that matter ;)
@walkmod