無料で使えるシステムトレードフレームワーク「Jiji」 をリリースしました!

・OANDA Trade APIを利用した、オープンソースのシステムトレードフレームワークです。
・自分だけの取引アルゴリズムで、誰でも、いますぐ、かんたんに、自動取引を開始できます。

Javaクラス解析機を作る 8日目

クラス、メソッド、フィールドのアノテーション解析部分を実装。パラメータのアノテーションは未。

ここまでの解析コード

→Javaクラス解析機 080417

動作サンプル

Runtimeアノテーション。パラメータつき。

package com.example.annotation;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;

@Retention( RetentionPolicy.RUNTIME )
public @interface RuntimeAnnotationA {
    String string();
    String[] stringArray();
    int integer();
    boolean bool();
    java.lang.Class<? extends List> type();
    Thread.State state();
}
package com.example.annotation;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;

@Retention( RetentionPolicy.RUNTIME )
public @interface RuntimeAnnotationB {
    String value();
}

Classアノテーション

package com.example.annotation;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;

@Retention( RetentionPolicy.CLASS )
public @interface ClassAnnotationA {
    String value();
}

Sourceアノテーション

package com.example.annotation;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;

@Retention( RetentionPolicy.SOURCE )
public @interface SourceAnnotationA {
    String value();
}

アノテーションされるクラス。

package com.example.annotation;

import java.util.ArrayList;

@RuntimeAnnotationA(
  string      = "aaa",
  stringArray = { "a", "b", "c" },
  bool        = false,
  integer     = 1000,
  state       = Thread.State.BLOCKED,
  type        = ArrayList.class        
)
@RuntimeAnnotationB("test")
@SourceAnnotationA("test")
@ClassAnnotationA("test")
public class Annotated {
    
    @RuntimeAnnotationA(
      string      = "bbb",
      stringArray = { "a", "b", "c" },
      bool        = false,
      integer     = 1000,
      state       = Thread.State.BLOCKED,
      type        = ArrayList.class        
    )
    @RuntimeAnnotationB("test")
    @SourceAnnotationA("test")
    @ClassAnnotationA("test")
    public String foo;
    
    @RuntimeAnnotationA(
      string      = "ccc",
      stringArray = { "a", "b", "c" },
      bool        = false,
      integer     = 1000,
      state       = Thread.State.BLOCKED,
      type        = ArrayList.class        
    )
    @RuntimeAnnotationB("test")
    @SourceAnnotationA("test")
    @ClassAnnotationA("test")
    void foo() {}
}

解析!

require "javaclass"

["./java_class/com/example/annotation/Annotated.class"].each { |c|
  open( c, "r+b" ) {|io|
    jc = JavaClass.from io
    puts jc.to_s
    puts ""
  }
}

実行結果です。

// version 50.0
// source Annotated.java
com.example.annotation.RuntimeAnnotationA(
    integer = 1000,
    type = java.util.ArrayList.class,
    stringArray = ["a","b","c"],
    state = java.lang.Thread$State.BLOCKED,
    bool = 0,
    string = "aaa"
)
com.example.annotation.RuntimeAnnotationB(
    value = "test"
)
com.example.annotation.ClassAnnotationA(
    value = "test"
)
public class com.example.annotation.Annotated 
extends java.lang.Object {

    com.example.annotation.RuntimeAnnotationA(
        integer = 1000,
        type = java.util.ArrayList.class,
        stringArray = ["a","b","c"],
        state = java.lang.Thread$State.BLOCKED,
        bool = 0,
        string = "bbb"
    )
    com.example.annotation.RuntimeAnnotationB(
        value = "test"
    )
    com.example.annotation.ClassAnnotationA(
        value = "test"
    )
    public java.lang.String foo;

    public void <init> (  ) {};
    com.example.annotation.RuntimeAnnotationA(
        integer = 1000,
        type = java.util.ArrayList.class,
        stringArray = ["a","b","c"],
        state = java.lang.Thread$State.BLOCKED,
        bool = 0,
        string = "ccc"
    )
    com.example.annotation.RuntimeAnnotationB(
        value = "test"
    )
    com.example.annotation.ClassAnnotationA(
        value = "test"
    )
    void foo (  ) {};

}

やはりSourceアノテーションはクラスに含まれてないか。