Javaクラス解析機を作る 9日目
パラメータアノテーション属性、アノテーションの初期値属性の読み込み部分をごそごそと作る。
ここまでの解析コード
動作サンプル
初期値を持つアノテーション。
package com.example.annotation; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.List; @Retention( RetentionPolicy.RUNTIME ) public @interface HasDefaultValue { String value() default "default"; }
パラメータにアノテーションが設定されたメソッドを持つクラス。
package com.example.annotation; import java.util.ArrayList; public class AnnotatedMethod { // アノテーション付きパラメータを持つメソッド。 void foo( @A("test") @B @C String str, @B @C int x ) {} static @interface A { String value(); } static @interface B {} static @interface C {} }
解析!
require "javaclass" [ "./java_class/com/example/annotation/HasDefaultValue.class", "./java_class/com/example/annotation/AnnotatedMethod.class"].each { |c| open( c, "r+b" ) {|io| jc = JavaClass.from io puts jc.to_s puts "" } }
実行結果です。
// version 49.0 // source HasDefaultValue.java java.lang.annotation.Retention( value = java.lang.annotation.RetentionPolicy.RUNTIME ) public abstract @interface com.example.annotation.HasDefaultValue extends java.lang.Object implements java.lang.annotation.Annotation { public abstract java.lang.String value ( ) {} default "default"; } // version 49.0 // source AnnotatedMethod.java public class com.example.annotation.AnnotatedMethod extends java.lang.Object { // use inner static abstract @interface com.example.annotation.AnnotatedMethod$A // use inner static abstract @interface com.example.annotation.AnnotatedMethod$B // use inner static abstract @interface com.example.annotation.AnnotatedMethod$C public void <init> ( ) {}; void foo ( java.lang.String com.example.annotation.AnnotatedMethod$A( value = "test" ) com.example.annotation.AnnotatedMethod$B com.example.annotation.AnnotatedMethod$C arg1, int com.example.annotation.AnnotatedMethod$B com.example.annotation.AnnotatedMethod$C arg2 ) {}; }
ところで、気がつくと情報セキュリティ技術者試験が明後日だったりするわけですよ。