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
410
Pluggable Annotation Processing でコーディング規約っぽいもの
社内勉強会で発表したもの。
HTML を無理やり PDF にしたのでちょっとずれてる。
sinsengumi
April 01, 2016
Tweet
Share
More Decks by sinsengumi
See All by sinsengumi
「ドメイン駆動設計入門」 を読んだ
sinsengumi
0
93
Spring Boot アプリのシステム情報を 可視化する(not Actuator)/spring-boot-not-actuator
sinsengumi
2
1.7k
Spring Boot で Boot した後に作る Web アプリケーション基盤/spring-boot-application-infrastructure
sinsengumi
23
49k
Other Decks in Programming
See All in Programming
ThorVG Viewer In VS Code
nors
0
490
認証・認可の基本を学ぼう後編
kouyuume
0
260
AtCoder Conference 2025
shindannin
0
840
20251212 AI 時代的 Legacy Code 營救術 2025 WebConf
mouson
0
230
GISエンジニアから見たLINKSデータ
nokonoko1203
0
190
C-Shared Buildで突破するAI Agent バックテストの壁
po3rin
0
420
ELYZA_Findy AI Engineering Summit登壇資料_AIコーディング時代に「ちゃんと」やること_toB LLMプロダクト開発舞台裏_20251216
elyza
2
820
AI前提で考えるiOSアプリのモダナイズ設計
yuukiw00w
0
210
AIエージェントの設計で注意するべきポイント6選
har1101
6
2.8k
まだ間に合う!Claude Code元年をふりかえる
nogu66
5
920
メルカリのリーダビリティチームが取り組む、AI時代のスケーラブルな品質文化
cloverrose
2
430
Canon EOS R50 V と R5 Mark II 購入でみえてきた最近のデジイチ VR180 事情、そして VR180 静止画に活路を見出すまで
karad
0
140
Featured
See All Featured
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
61k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1k
What the history of the web can teach us about the future of AI
inesmontani
PRO
0
380
Fashionably flexible responsive web design (full day workshop)
malarkey
408
66k
Joys of Absence: A Defence of Solitary Play
codingconduct
1
260
The #1 spot is gone: here's how to win anyway
tamaranovitovic
1
880
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
7.9k
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
0
180
Building the Perfect Custom Keyboard
takai
1
670
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
300
sira's awesome portfolio website redesign presentation
elsirapls
0
94
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