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
400
Pluggable Annotation Processing でコーディング規約っぽいもの
社内勉強会で発表したもの。
HTML を無理やり PDF にしたのでちょっとずれてる。
sinsengumi
April 01, 2016
Tweet
Share
More Decks by sinsengumi
See All by sinsengumi
「ドメイン駆動設計入門」 を読んだ
sinsengumi
0
85
Spring Boot アプリのシステム情報を 可視化する(not Actuator)/spring-boot-not-actuator
sinsengumi
2
1.6k
Spring Boot で Boot した後に作る Web アプリケーション基盤/spring-boot-application-infrastructure
sinsengumi
23
49k
Other Decks in Programming
See All in Programming
3年ぶりにコードを書いた元CTOが Claude Codeと30分でMVPを作った話
maikokojima
0
520
非同期jobをtransaction内で 呼ぶなよ!絶対に呼ぶなよ!
alstrocrack
0
980
NixOS + Kubernetesで構築する自宅サーバーのすべて
ichi_h3
0
1k
AIと人間の共創開発!OSSで試行錯誤した開発スタイル
mae616
1
650
大規模アプリのDIフレームワーク刷新戦略 ~過去最大規模の並行開発を止めずにアプリ全体に導入するまで~
mot_techtalk
1
460
monorepo の Go テストをはやくした〜い!~最小の依存解決への道のり~ / faster-testing-of-monorepos
convto
2
500
What's new in Spring Modulith?
olivergierke
1
160
Web フロントエンドエンジニアに開かれる AI Agent プロダクト開発 - Vercel AI SDK を観察して AI Agent と仲良くなろう! #FEC余熱NIGHT
izumin5210
3
550
overlayPreferenceValue で実現する ピュア SwiftUI な AdMob ネイティブ広告
uhucream
0
190
AI Agent 時代的開發者生存指南
eddie
3
1.8k
CSC305 Lecture 06
javiergs
PRO
0
250
はじめてのDSPy - 言語モデルを『プロンプト』ではなく『プログラミング』するための仕組み
masahiro_nishimi
2
580
Featured
See All Featured
Making Projects Easy
brettharned
120
6.4k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
10
600
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
37
2.6k
Optimizing for Happiness
mojombo
379
70k
How STYLIGHT went responsive
nonsquared
100
5.8k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
34
2.3k
jQuery: Nuts, Bolts and Bling
dougneiner
65
7.9k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4k
Fantastic passwords and where to find them - at NoRuKo
philnash
52
3.4k
The Straight Up "How To Draw Better" Workshop
denniskardys
238
140k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
132
19k
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