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
390
Pluggable Annotation Processing でコーディング規約っぽいもの
社内勉強会で発表したもの。
HTML を無理やり PDF にしたのでちょっとずれてる。
sinsengumi
April 01, 2016
Tweet
Share
More Decks by sinsengumi
See All by sinsengumi
「ドメイン駆動設計入門」 を読んだ
sinsengumi
0
79
Spring Boot アプリのシステム情報を 可視化する(not Actuator)/spring-boot-not-actuator
sinsengumi
2
1.6k
Spring Boot で Boot した後に作る Web アプリケーション基盤/spring-boot-application-infrastructure
sinsengumi
23
48k
Other Decks in Programming
See All in Programming
『自分のデータだけ見せたい!』を叶える──Laravel × Casbin で複雑権限をスッキリ解きほぐす 25 分
akitotsukahara
2
660
テスト駆動Kaggle
isax1015
1
640
リバースエンジニアリング新時代へ! GhidraとClaude DesktopをMCPで繋ぐ/findy202507
tkmru
3
1k
MCPを使ってイベントソーシングのAIコーディングを効率化する / Streamlining Event Sourcing AI Coding with MCP
tomohisa
0
170
Hack Claude Code with Claude Code
choplin
7
2.6k
ソフトウェア品質を数字で捉える技術。事業成長を支えるシステム品質の マネジメント
takuya542
2
15k
Goで作る、開発・CI環境
sin392
0
260
AI コーディングエージェントの時代へ:JetBrains が描く開発の未来
masaruhr
1
200
AI時代のソフトウェア開発を考える(2025/07版) / Agentic Software Engineering Findy 2025-07 Edition
twada
PRO
99
37k
코딩 에이전트 체크리스트: Claude Code ver.
nacyot
0
930
チームで開発し事業を加速するための"良い"設計の考え方 @ サポーターズCoLab 2025-07-08
agatan
1
470
効率的な開発手段として VRTを活用する
ishkawa
0
160
Featured
See All Featured
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
8
700
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
282
13k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.3k
Art, The Web, and Tiny UX
lynnandtonic
299
21k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
Why You Should Never Use an ORM
jnunemaker
PRO
58
9.5k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
30
2.2k
Git: the NoSQL Database
bkeepers
PRO
430
65k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
29
9.6k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Building Applications with DynamoDB
mza
95
6.5k
Code Reviewing Like a Champion
maltzj
524
40k
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