Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Pluggable Annotation Processing でコーディング規約っぽいもの
Search
sinsengumi
April 01, 2016
Programming
1
340
Pluggable Annotation Processing でコーディング規約っぽいもの
社内勉強会で発表したもの。
HTML を無理やり PDF にしたのでちょっとずれてる。
sinsengumi
April 01, 2016
Tweet
Share
More Decks by sinsengumi
See All by sinsengumi
「ドメイン駆動設計入門」 を読んだ
sinsengumi
0
50
Spring Boot アプリのシステム情報を 可視化する(not Actuator)/spring-boot-not-actuator
sinsengumi
2
1.5k
Spring Boot で Boot した後に作る Web アプリケーション基盤/spring-boot-application-infrastructure
sinsengumi
23
47k
Other Decks in Programming
See All in Programming
Google's Recipe for Scaling (Web) Security – LocoMocoSec 2024
lweichselbaum
0
170
AWS初心者ってどうやってAWSを学ぶ?〜アプリエンジニアがやってよかったアーキテクチャ学習方法〜
yamanashi_ren01
0
190
Jetpack for KMP
fornewid
1
290
CSC307 Lecture 06
javiergs
PRO
0
360
20240706_CDKConf
takuyay0ne
0
1.2k
I/O Extended Android in Korea 2024 ~ Whats new in Android development tools
pluu
0
250
APIのない大学ログインWebサービスをWKWebViewとJavaScriptでアプリ化した話
akidon0000
1
330
データカタログ運用物語 〜令和6年夏の理想と現実〜
kuro_kurorrr
0
110
CSC307 Lecture 05
javiergs
PRO
0
210
Ruby メモリ管理 プログラミング
megmogmog1965
0
130
Rustのweb開発を助ける 便利なツール紹介
yuki0418
1
190
CSC307 Lecture 09
javiergs
PRO
1
500
Featured
See All Featured
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
502
140k
Building Better People: How to give real-time feedback that sticks.
wjessup
357
18k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
29
2.5k
4 Signs Your Business is Dying
shpigford
178
21k
GitHub's CSS Performance
jonrohan
1026
450k
The Language of Interfaces
destraynor
151
23k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
13
430
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
149
45k
No one is an island. Learnings from fostering a developers community.
thoeni
17
2.8k
How STYLIGHT went responsive
nonsquared
93
5k
The Invisible Side of Design
smashingmag
294
50k
Testing 201, or: Great Expectations
jmmastey
33
6.9k
Transcript
Pluggable Annotation Processing でコーディング規約っぽいもの M3 TechTalk 2016/04/01 吉田 朋也 1
/ 17
Pluggable Annotation Processing API コンパイル時にアノテーションを処理するための仕組み。 Java 1.6 から追加された。 主にコンパイル時にアノテーションを読み込んで、ソースコードを自動生成したり する。
Java でよくあるボイラープレート書かなくてよくするとか。 ちなみに Java 1.5 で提供されていた Annotation Processing Tool (apt) とは別 物。 Pluggable Annotation Processing API を使ったプロダクト Lombok Doma2 AndroidAnnotations JPA Metamodel etc ... 2 / 17
試してみる 1. Annotation Processor を実装する 2. Annotation Processor をコンパイルする 3.
Annotation Processor をコンパイル時に組み込む 3 / 17
1. Annotation Processor を実装する AbstractProcessor を継承して、process メソッドに処理を書く。 @SupportedSourceVersion(SourceVersion.RELEASE_8) @SupportedAnnotationTypes("*") public
class SampleAnnotationProcessor extends AbstractProcessor { private int round = 1; @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) Messager messager = super.processingEnv.getMessager(); System.out.println("Round : " + (this.round++)); System.out.println("RoundEnvironment : " + roundEnv); System.out.println("annotations : " + annotations); messager.printMessage(Kind.NOTE, "Hello"); return true; } } 4 / 17
2. Annotation Processor をコンパイルする $ javac com/m3/yoshida/sample/SampleAnnotationProcessor.java $ ls -l
com/m3/yoshida/sample/SampleAnnotationProcessor* -rw-r--r-- 1 t-yoshida staff 2026 3 30 16:59 com/m3/yoshida/sample/SampleAnnotationProcess -rw-r--r-- 1 t-yoshida staff 851 3 30 16:33 com/m3/yoshida/sample/SampleAnnotationProcess 5 / 17
3. Annotation Processor をコンパイル時に組 み込む @Deprecated @SuppressWarnings("unchecked") public class Main
{ public static void main(String[] args) { System.out.println("Hello, world !"); } } 6 / 17
3. Annotation Processor をコンパイル時に組 み込む 普通にコンパイルする場合 $ javac com/m3/yoshida/sample/Main.java Annotation
Processor を組み込んでコンパイルする場合 $ javac -processor com.m3.yoshida.sample.SampleAnnotationProcessor com/m3/yoshida/sample/Main. Round : 1 RoundEnvironment : [errorRaised=false, rootElements=[com.m3.yoshida.sample.Main], processingOv annotations : [java.lang.SuppressWarnings, java.lang.Deprecated] 注意:Hello Round : 2 RoundEnvironment : [errorRaised=false, rootElements=[], processingOver= annotations : [] 注意:Hello 7 / 17
説明 プロセッサーによる処理(process)の呼び出しは、複数回行われて、それぞ れの処理を ラウンド と呼ぶ。 ソース生成を行った際に生成されたソースにも アノテーションが付いていた場合に再度 process が走るように再帰的な動き をする。
process 内で Messager#printMessage() でコンパイル時にメッセージを出 力できる。 Kind.ERROR を指定することで、コンパイル結果をエラーにでき る。 Set<? extends TypeElement> annotations 引数から、どこにアノテ ートされているかの情報が取れる。 毎回 -processor するのは手間なので、META- INF/services/javax.annotation.processing.Processor に Processor クラスの FQDN を書いて jar にして、classpath に放り込んでお けばよい。 8 / 17
コーディング規約っぽいものを作る 9 / 17
コーディング規約っぽいものを作る 10 / 17
コーディング規約っぽいものを作る 突然の Spring Framework !! 11 / 17
最近の Spring では、 コントローラークラスには @Controller サービス(ビジネスロジック)クラスには @Service DAO クラスには @Repository
というアノテーションを付けて各コンポーネント(Bean)を明示的に使い分け る。 各コンポーネントは以下のような呼び出し順を守ったほうがよいとされている。 @Controller -> @Service -> @Repository 例えば、 トランザクション境界が @Service に設定されてたりして、いきなり @Controller から @Repository を呼ばれると、トランザクション効かないとか発生しうる。 12 / 17
これを開発ルールにして守っていきたいが・・・・ 人手(コードレビュー等)や、既存の静的解析では検出できない。 13 / 17
これを開発ルールにして守っていきたいが・・・・ 人手(コードレビュー等)や、既存の静的解析では検出できない。 Pluggable Annotation Processing 14 / 17
デモ http://maven:8081/artifactory/repo/com/m3/spring‒layer‒convention 15 / 17
その他 Pluggable Annotation Processing は IDE に組み込む時、一手間かけないと いけないので少しめんどくさい。 あと、自動生成系は生成結果が上手く認識されなくて、IDE 上でエラーが消え
ないとかよくある。 maven で Proccessor のプロジェクトと Processor を適用させるプロジェ クトを同居させるやり方が分からない(compile フェーズを2回走らせないと いけないから無理なのか?) 最近の Java は何でもかんでもアノテーションなので、結構使いどころはあり そう。プロジェクト独自のルールとかもコンパイラで強制できたり。 16 / 17
ご静聴ありがとうございました 17 / 17