Javaな部屋
概要 ここではJavaに関して学んだこと身に付けたことを記載したいと思います。 Java5.0、Spring、iBatis、struts、JavaEE5など記載していく予定です。
public @interface Marker {}簡単ですね!
public @interface SingleValue { String value() default "nothing"; }アノテーションには複数の値を保持させることも出来ます。
public @interface MultiValue { String[] values(); int amount(); }
@Marker //クラス class AnnotationSample{ @Marker AnnotationSample(){} //コンストラクタ @Marker String word; //フィールド @Marker void doSomething(){} //メソッド }マーカーアノテーション以外で、値を持つアノテーションの付与方法は以下のようにします。
class AnnotationUser { @SingleValue("Hello") public void doSingleValue(){} @MultiValue(values={"Japan","Chiba"},amount=100) public AnnotationUser() {} }値を1つしか持たないアノテーションは、アノテーションのどのフィールドに代入するかを指定しなくてもいいんですね。
import java.lang.annotation.ElementType; import java.lang.annotation.Target; @Target(ElementType.ANNOTATION_TYPE) @interface MetaTargetAnnotation {} @Target(ElementType.CONSTRUCTOR) @interface MetaTargetConstructor {} @Target(ElementType.FIELD) @interface MetaTargetField {} @Target(ElementType.LOCAL_VARIABLE) @interface MetaTargetLocalValue {} @Target(ElementType.METHOD) @interface MetaTargetMethod {} @Target(ElementType.PACKAGE) @interface MetaTargetPackage {} @Target(ElementType.PARAMETER) @interface MetaTargetParameter {} @Target(ElementType.TYPE) @interface MetaTargetType {} @Target({ElementType.METHOD,ElementType.FIELD}) @interface MetaTargetMulti {}
import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.SOURCE) @interface MetaRetentionSource{} @Retention(RetentionPolicy.CLASS) @interface MetaRetentionClass{} @Retention(RetentionPolicy.RUNTIME) @interface MetaRetentionRuntime{}
import java.lang.annotation.Inherited; @Inherited public @interface MetaInheritedAnnotation {} @MetaInheritedAnnotation class AbstractSample{} class Sample extends AbstractSample{}この場合、Sampleクラスにも@MetaInheritedAnotationが付加された状態と同じになります。
package com.yoheim.java5.main; import java.lang.annotation.Annotation; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @Retention(RetentionPolicy.RUNTIME) @interface Marker{} @Retention(RetentionPolicy.RUNTIME) @interface SingleValue{ String value() default "nothing"; } @Marker @SingleValue("something") public class GetAnnotation { public static void main(String[] args){ //全てのアノテーションを取得 GetAnnotation me = new GetAnnotation(); Annotation[] ans = me.getClass().getAnnotations(); for (Annotation an : ans) { System.out.println("Annotation: " + an); } //指定したアノテーションを取得 Annotation annotation = me.getClass().getAnnotation(SingleValue.class); SingleValue single = (SingleValue) annotation; //アノテーションの中身を取り出す System.out.println("SingleValue: " + single.value()); } }コーディングのお作法的にはよくないとこもありますが、それはご勘弁を。
package com.yoheim.java5.main; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.reflect.Field; @Retention(RetentionPolicy.RUNTIME) @interface CheckRequired{} //必須チェックを示すアノテーション class User{ @CheckRequired String id; @CheckRequired String name; String address; //getter・setterは省略 } public class AnnoationUsingSamle { public static void main(String[] args) throws Exception { User user = getUser(); checkUserInfo(user); System.out.println("OK"); } //必須チェックを行う場所 private static void checkUserInfo(User user) throws Exception { Field[] fields = user.getClass().getDeclaredFields(); for (Field field : fields) { CheckRequired cr = field.getAnnotation(CheckRequired.class); if(cr != null){ //必須チェック if(field.get(user) == null) throw new IllegalStateException(); } } } private static User getUser() { User user = new User(); user.id = "001"; //user.name = "yoheim"; //必須のnameを指定しない。→checkに引っかかる。 user.address = "Japan"; return user; } }
ここでは美しいプログラムを書くという目標のため、リファクタリングについて記述します。 美しいかどうかは、人によりけりかと思いますが。。。 ここでの美しいとは、読みやすいコード/保守しやすいコードという意味で仮に定義したいと思います。
○条件のメソッド化 | |
条件式内の条件をメソッド化し、分かりやすくする。 以下の例は、引数dateの日付によって、支払う給与を判定するロジックです。 夏季期間ならば夏のボーナス、それ以外ならば冬のボーナスをaddして給与を支払います。 |
|
《前》
if(date.before(SUMMER_TIME_START) || date.after(SUMMER_TIME_END))
|
|
《後》
if (notSummerTime(date))
|