はじめに

どうするか?

OmikujiI18Nアスペクト

package com.snail.exam.aj.omikuji;

import java.util.Locale;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class OmikujiI18N {

  @Around("call(* *.setText(String)) || call(* *.setTitle(String)))")
  public void setText(ProceedingJoinPoint thisJoinPoint) {

    // メソッド引数の取得
    Object[] args = thisJoinPoint.getArgs();
    String srcText = (String) args[0];

    try {
      // 第一引数を入れ替える
      String key = srcText.replaceAll(" ", "_").replaceAll("=", "_");

      ResourceBundle resource = PropertyResourceBundle.getBundle("label",
          Locale.getDefault(), this.getClass().getClassLoader());
      String destText = resource.getString(key);

      args[0] = destText;
      
      // メソッド( setText() / setTitle() )の実行。
      thisJoinPoint.proceed(args);
    } catch (Throwable th) {
      
      // 何らかのエラーが起きたときには、元の引数でメソッドを実行する
      // (label_ja.properties に変換するエントリがなかった場合)
      
      // System.out.println(srcText + "は、"
      // + Locale.getDefault().getDisplayLanguage() + "化できませんでした。");

      try {
        thisJoinPoint.proceed();
      } catch (Throwable ignoreEx) {
        // if RuntimeExceptin occurred, throw it.
        if (ignoreEx instanceof RuntimeException) {
          throw (RuntimeException) ignoreEx;
        }
        // Should not happen
        ignoreEx = null;
      }
    }
  }

}

Java#AspectJ


添付ファイル: fileAjdt6.png 2020件 [詳細] fileAjdt5.png 2000件 [詳細] fileAjdt4.png 2067件 [詳細]

トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2008-04-11 (金) 01:25:20 (5852d)
Short-URL: https://at-sushi.com:443/pukiwiki/index.php?cmd=s&k=25c928ba6a
ISBN10
ISBN13
9784061426061