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
95
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
AIの誤りが許されない業務システムにおいて“信頼されるAI” を目指す / building-trusted-ai-systems
yuya4
7
4.2k
令和最新版Android Studioで化石デバイス向けアプリを作る
arkw
0
470
フルサイクルエンジニアリングをAI Agentで全自動化したい 〜構想と現在地〜
kamina_zzz
0
340
【卒業研究】会話ログ分析によるユーザーごとの関心に応じた話題提案手法
momok47
0
160
Spinner 軸ズレ現象を調べたらレンダリング深淵に飲まれた #レバテックMeetup
bengo4com
1
210
AI前提で考えるiOSアプリのモダナイズ設計
yuukiw00w
0
210
ゲームの物理 剛体編
fadis
0
390
LLM Çağında Backend Olmak: 10 Milyon Prompt'u Milisaniyede Sorgulamak
selcukusta
0
140
tsgolintはいかにしてtypescript-goの非公開APIを呼び出しているのか
syumai
7
2.4k
DevFest Android in Korea 2025 - 개발자 커뮤니티를 통해 얻는 가치
wisemuji
0
180
Flutter On-device AI로 완성하는 오프라인 앱, 박제창 @DevFest INCHEON 2025
itsmedreamwalker
1
180
gunshi
kazupon
1
130
Featured
See All Featured
B2B Lead Gen: Tactics, Traps & Triumph
marketingsoph
0
37
Navigating the Design Leadership Dip - Product Design Week Design Leaders+ Conference 2024
apolaine
0
130
Statistics for Hackers
jakevdp
799
230k
Build The Right Thing And Hit Your Dates
maggiecrowley
38
3k
So, you think you're a good person
axbom
PRO
0
1.9k
Test your architecture with Archunit
thirion
1
2.1k
WENDY [Excerpt]
tessaabrams
9
35k
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
250
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
359
30k
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
72
The Curse of the Amulet
leimatthew05
0
6.6k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
254
22k
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