Top Banner
Platinum Sponsor OPEN AND AUTOMATIC CODING CONVENTIONS WITH WALKMOD Raquel Pau Sponsored talk by:
57

33rd degree talk: open and automatic coding conventions with walkmod

Jul 07, 2015

Download

Software

walkmod

33rd degree talk about walkmod, an open source tool to apply and share coding conventions
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 33rd degree talk: open and automatic coding conventions with walkmod

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

hello!

@raquelpau @acoroleu

Page 3: 33rd degree talk: open and automatic coding conventions with walkmod

which is our motivation?

DRYDon’t repeat yourself.

Page 4: 33rd degree talk: open and automatic coding conventions with walkmod

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

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

google use conventions!

Page 7: 33rd degree talk: open and automatic coding conventions with walkmod

everybody follow conventions

http://sideeffect.kr/popularconvention

Page 8: 33rd degree talk: open and automatic coding conventions with walkmod

“it takes a while to create nothing”

Ron Jeffries

Page 9: 33rd degree talk: open and automatic coding conventions with walkmod

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

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

walkmod is ready for Java 8

but also could by polyglot!

Page 12: 33rd degree talk: open and automatic coding conventions with walkmod

how it works

making awesome things happen!

Page 13: 33rd degree talk: open and automatic coding conventions with walkmod

>_ 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

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

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

how it works

Page 17: 33rd degree talk: open and automatic coding conventions with walkmod

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

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

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

transformations

Code conventions are a reality by means of code transformations

Page 21: 33rd degree talk: open and automatic coding conventions with walkmod

modify or generate code without changing walkmod

transformations

Page 22: 33rd degree talk: open and automatic coding conventions with walkmod

visitors

transformations

Page 23: 33rd degree talk: open and automatic coding conventions with walkmod

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

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

scripts

transformations

Page 26: 33rd degree talk: open and automatic coding conventions with walkmod

transformations

Page 27: 33rd degree talk: open and automatic coding conventions with walkmod

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

templates

transformations

Page 29: 33rd degree talk: open and automatic coding conventions with walkmod

transformations

Page 30: 33rd degree talk: open and automatic coding conventions with walkmod

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

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

query engine

Write less to do the same!

Page 33: 33rd degree talk: open and automatic coding conventions with walkmod

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

{ context } + query expression

query engine

Page 35: 33rd degree talk: open and automatic coding conventions with walkmod

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

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

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

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

merge engine

Don’t loose your changes

Page 40: 33rd degree talk: open and automatic coding conventions with walkmod

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

merge engine

Page 42: 33rd degree talk: open and automatic coding conventions with walkmod

merge engine

Page 43: 33rd degree talk: open and automatic coding conventions with walkmod

merge engine

Page 44: 33rd degree talk: open and automatic coding conventions with walkmod

merge engine

Page 45: 33rd degree talk: open and automatic coding conventions with walkmod

merge engine

Page 46: 33rd degree talk: open and automatic coding conventions with walkmod

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

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

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

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

plugins

Let’s grow up!

Page 51: 33rd degree talk: open and automatic coding conventions with walkmod

walkmod is growing up

plugins

Page 52: 33rd degree talk: open and automatic coding conventions with walkmod

where can I find them?plugins

Page 53: 33rd degree talk: open and automatic coding conventions with walkmod

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

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

roadmap

Page 56: 33rd degree talk: open and automatic coding conventions with walkmod

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

thanks for your time!

and.. do cool things that matter ;)

@walkmod