annotation processor and compiler plugin

22
Annotation processor and compiler plugin as code generators.

Upload: oleksandr-radchykov

Post on 25-Jan-2017

76 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Annotation processor and compiler plugin

Annotation processor and compiler plugin as code generators.

Page 2: Annotation processor and compiler plugin

Hello!I am Oleksandr Radchykov

[email protected]

Page 3: Annotation processor and compiler plugin

Agenda

1.Motivation2.How to generate code?3.How to annotation processor4.How to create plugin5.Demo

Page 4: Annotation processor and compiler plugin

Motivation for code generation

Page 5: Annotation processor and compiler plugin

Getting rid of boilerplateclass MyService { private final String name; private final Integer version;

public MyService(String name, Integer version) { this.name = name; This.version = version; }

public String getName() { return name; } public Integer getVersion() { return version; }}

Page 6: Annotation processor and compiler plugin

@Getter@RequiredArgsConstructorclass MyService { private final String name; private final Integer version;}

6

Page 7: Annotation processor and compiler plugin

Getting rid from annoying code duplicationpublic String validate(Object input) { if (income instanceof String

&& !((String) income).isEmpty()) { return (String) income; } …}

Page 8: Annotation processor and compiler plugin

public String validate(Object input) { if (income instanceof String && !income.isEmpty()) { return income; } …}

8

Page 9: Annotation processor and compiler plugin

Two ways to generate code

Page 10: Annotation processor and compiler plugin

Annotation Processing

▷Java 5+▷Takes java code and generates source files

▷You can not manipulate an existing java code

▷Use the model of processing rounds

Page 11: Annotation processor and compiler plugin

Javac Plugin

▷Since Java 8▷Can give us access to AST trees parsed from source code

▷Can be used to add compile-time checks

▷Can run on different phases of compilation process

Page 12: Annotation processor and compiler plugin

Annotation processor

Page 13: Annotation processor and compiler plugin

1. Implement AbstractProcessor interface

import javax.annotation.processing.AbstractProcessor;

class MyProcessor implements AbstractProcessor {

@Override public boolean process(

Set<? Extends TypeElement> annotations, RoundEnvironment roundEnv) {

/*...*/ }

}

Page 14: Annotation processor and compiler plugin

2. Create provider-configuration file

MyProcessor.jar- com/example

- MyProcessor.class- META-INF/services

- javax.annotation.processing.Processor

Provider-configuration file should declare all annotation processors you want to expose.

Page 15: Annotation processor and compiler plugin

3. Compilation

$ javac –cp processor.jar \ *sources*.java

Page 16: Annotation processor and compiler plugin

Javac plugin

Page 17: Annotation processor and compiler plugin

1. Implement Plugin interfaceimport com.sun.source.util.Plugin;

class MyPlugin implements Plugin { @Override public String getName() { return “pluginName”; }

@Override public void init(JavacTask javacTask,

String… strings) { /*...*/ }}

Page 18: Annotation processor and compiler plugin

2. Create provider-configuration file

MyPlugin.jar- com/example

- MyPlugin.class- META-INF/services

- com.sun.source.util.Plugin

Provider-configuration file should declare all plugins you want to expose.

Page 19: Annotation processor and compiler plugin

3. Compilation

$ javac -Xplugin:PluginName \ –cp /path/to/plugin \

Source.java

Page 20: Annotation processor and compiler plugin

Demo

Page 21: Annotation processor and compiler plugin

Questions?