Spec2.8Chap11a

第 11 章 ユーザー定義アノテーション (User-Defined Annotations)

構文:

   Annotation       ::=       '@' SimpleType {ArgumentExprs}
   ConstrAnnotation ::=       '@' SimpleType ArgumentExprs

User-defined annotations associate meta-information with definitions . A simple annotation has the form @c or @c(a1,...,an). Here, c is a constructor of a class C , which must conform to the class scala.Annotation .

ユーザー定義アノテーションは、定義とメタ情報を関連づけます。 単純なアノテーションは、@c あるいは @c(a1,...,an) の形をしています。 ここで、c はクラス C のコンストラクタで、クラス scala.Annotation に適合しなくてはなりません。

Annotations may apply to definitions or declarations, types, or expressions . An annotation of a definition or declaration appears in front of that definition . An annotation of a type appears after that type . An annotation of an expression e appears after the expression e, separated by a colon . More than one annotation clause may apply to an entity . The order in which these annotations are given does not matter .

アノテーションは、定義または宣言、型、あるいは式に適用できます。 定義または宣言のアノテーションは、その定義の前に現われます。 型のアノテーションは、その型の後に現れます。 式 e のアノテーションは、式 e の後にコロンによって分離されて現われます。 1 つ以上のアノテーション節をエンティティに適用できます。 与えられたアノテーションの順番は重要ではありません。 例:

   @serializable class C { ... }       //   クラスアノテーション
   @transient @volatile var m: Int     //   変数アノテーション
   String @local                       //   型アノテーション
   (e: @unchecked) match { ... }       //   式アノテーション

The meaning of annotation clauses is implementation-dependent . On the Java platform , the following annotations have a standard meaning .

アノテーション節の意味は処理系依存です。 Java プラットフォームでは、次のアノテーションは標準的な意味を持っています。


@transient

Marks a field to be non-persistent; this is equivalent to the transient modifier in Java .

フィールドが非永続的であるとマークします。;これは Java の transient 修飾子に等価です。


@volatile

Marks a field which can change its value outside the control of the program ; this is equivalent to the volatile modifier in Java .

プログラムの制御外でそのフィールド値が変化する可能性があるとマークします。; これは、Java の volatile 修飾子に等価です。


@serializable

Marks a class to be serializable; this is equivalent to inheriting from the java.io.Serializable interface in Java .

クラスがシリアライズ可能であるとマークします。;これは Java の java.io.Serializable インターフェースの継承に等価です。


@SerialVersionUID(<longlit>)

Attaches a serial version identifier (a long constant) to a class . This is equivalent to a the following field definition in Java:

シリアルバージョン識別子(長い定数)をクラスに付属させます。 これは Java の次のフィールド定義に等価です。:

   private final static    SerialVersionUID = <longlit>


@throws(<classlit>)

A Java compiler checks that a program contains handlers for checked exceptions by analyzing which checked exceptions can result from execution of a method or constructor . For each checked exception which is a possible result, the throws clause for the method or constructor must mention the class of that exception or one of the superclasses of the class of that exception .

Java コンパイラは、 メソッド/コンストラクタの実行で生じる可能性のあるチェック例外を分析し、 プログラムがそれらチェック例外に対するハンドラーを含んでいるか確かめます。 起きる可能性のある各チェック例外に対して、メソッド/コンストラクタの throw 節では、その例外クラスあるいはその例外クラスのスーパークラスの 1 つに言及しなくてはなりません。


@deprecated(<stringlit>) (廃止)

Marks a definition as deprecated . Accesses to the defined entity will then cause a deprecated warning mentioning the message <stringlit> to be issued from the compiler . Deprecated warnings are suppressed in code that belongs itself to a definition that is labeled deprecated .

定義が廃止であるとマークします。 定義されたエンティティへのアクセスで、コンパイラはメッセージ <stringlit> に言及する廃止警告を発行します。 廃止警告は、deprecated と印された定義自身に属するコードでは発行されません。


@scala.reflect.BeanProperty

When prefixed to a definition of some variable X, this annotation causes getter and setter methods getX, setX in the Java bean style to be added in the class containing the variable . The first letter of the variable appears capitalized after the get or set . When the annotation is added to the definition of an immutable value definition X, only a getter is generated . The construction of these methods is part of code-generation; therefore, these methods become visible only once a classfile for the containing class is generated .

このアノテーションがある変数 X の定義の前に置かれたときは、 Java bean スタイルのゲッターおよびセッターメソッド getX、setX を、 変数 X を含むクラスに付け加えます。 変数の最初の文字は大文字化されて get/set の後に現れます。 このアノテーションがイミュータブルな値定義 X の定義に付加されたときは、 ゲッターだけが生成されます。 これらメソッドの組立はコード生成の一部です。;ですからそれらメソッドは、 それを含むクラスを生成するための classfile にただ一度見えるだけです。


@scala.reflect.BooleanBeanProperty

This annotation is equivalent to scala.reflect.BeanProperty, but the generated getter method is named isX instead of getX .

このアノテーションは、生成されたゲッターメソッドが getX の代わりに isX と名付けられる点を除けば、scala.reflect.BeanProperty と同じです。


@unchecked

When applied to the selector of a match expression, this attribute suppresses any warnings about non-exhaustive pattern matches which would otherwise be emitted . For instance, no warnings would be produced for the method definition below .

この属性は、マッチ式のセレクタに適用されたとき、そうでなければ発せられるはずの、 非網羅的パターンマッチに関するあらゆる警告を止めます。 例えば、次のメソッド定義に対しては警告はないでしょう。

   def f(x: Option[Int]) = (x: @unchecked) match {
     case Some(y) => y
   }

Without the @unchecked annotation, a Scala compiler could infer that the pattern match is non-exhaustive, and could produce a warning because Option is a sealed class .

@unchecked アノテーションがなければ、 Scala コンパイラはパターンマッチが非網羅的であると推論し、 Option が sealed クラスなので警告を発します。


@uncheckedStable

When applied a value declaration or definition, it allows the defined value to appear in a path, even if its type is volatile (§3.6). For instance, the following member definitions are legal:

値宣言/定義に適用すると、たとえその型が volatile (§3.6)であっても、 パス中でその定義された値を使えます。 例えば、次のメンバー定義は正しいです:

   type A { type T }
   type B
   @uncheckedStable val x: A with B // volatile type
   val y: x.T                       // OK since 'x' is still a path

Without the @uncheckedStable annotation, the designator x would not be a path since its type A with B is volatile . Hence, the reference x.T would be malformed .

When applied to value declarations or definitions that have non-volatile types, the annotation has no effect .

@uncheckedStable アノテーションがなければ、型 A with B は volatile なので、 指定子 x はパスではありません。 ですから、参照 x.T は不正となります。

このアノテーションを非 volatile 型を持つ値宣言/定義に適用しても効果はありません。


@specialized

When applied to the definition of a type parameter, this annotation causes the compiler to generate specialized definitions for primitive types . An optional list of primitive types may be given, in which case specialization takes into account only those types . For instance, the following code would generate specialized traits for Unit, Int and Double

このアノテーションを型パラメータの定義に適用すると、 コンパイラはプリミティブ型に特化した定義を生成します。 プリミティブ型のオプションリストが与えることができ、その場合、特化は、 それらの型だけについて考慮されます。 例えば、次のコードは Unit、Int と Double に対して、 特化したトレイトを生成します。

   trait Function0[@specialized(Unit, Int, Double) T] {
     def apply: T
   }

Whenever the static type of an expression matches a specialized variant of a definition, the compiler will instead use the specialized version . See [Dra10] for more details of the implementation .

式の静的な型が、定義の特化した変位指定(variant)と一致するときはいつでも、 コンパイラは代わりに、特化したバージョンを使います。 実装のさらなる詳細については [Dra10] を見てください。

Other annotations may be interpreted by platform- or application-dependent tools . Class scala.Annotation has two sub-traits which are used to indicate how these annotations are retained .


他のアノテーションは、 プラットフォームあるいはアプリケーション依存のツールによって解釈されるでしょう。 クラス scala.Annotation には、 これらアノテーションの保持方法を示すのに使われる 2 つのサブトレイトがあります。

Instances of an annotation class inheriting from trait scala.ClassfileAnnotation will be stored in the generated class files . Instances of an annotation class inheriting from trait scala.StaticAnnotation will be visible to the Scala type-checker in every compilation unit where the annotated symbol is accessed . An annotation class can inherit from both scala.ClassfileAnnotation and scala.StaticAnnotation . If an annotation class inherits from neither scala.ClassfileAnnotation nor scala.StaticAnnotation, its instances are visible only locally during the compilation run that analyzes them .

トレイト scala.ClassfileAnnotation を継承するアノテーションクラスのインスタンスは、 生成されたクラスファイル内に格納されます。 トレイト scala.StaticAnnotation を継承するアノテーションクラスのインスタンスは、 アノテーション付きのシンボルをアクセスするすべてのコンパイル単位中で、 Scala の型チェックから可視です。 アノテーションクラスは、scala.ClassfileAnnotation と scala.StaticAnnotation の両方を継承できます。 もしアノテーションクラスが scala.ClassfileAnnotation と scala.StaticAnnotation のいずれも継承しないなら、そのインスタンスは、 それらを解析するコンパイル実行中だけローカルに可視です。

Classes inheriting from scala.ClassfileAnnotation may be subject to further restrictions in order to assure that they can be mapped to the host environment . In particular, on both the Java and the .NET platforms, such classes must be toplevel; i.e. they may not be contained in another class or object . Additionally, on both Java and .NET, all constructor arguments must be constant expressions .

scala.ClassfileAnnotation を継承するクラスは、 それらをホスト環境にマップできることを保証するために、 さらに制限を受けるかもしれません。 特に、Java と.NET プラットフォームの両方において、 そのようなクラスはトップレベルでなければなりません。; つまり、それらを他のクラス/オブジェクトに含めることはできません。 さらに、Java と.NET 両方において、 すべてのコンストラクタ引数は定数式でなければなりません。

タグ:

+ タグ編集
  • タグ:

このサイトはreCAPTCHAによって保護されており、Googleの プライバシーポリシー利用規約 が適用されます。

最終更新:2011年03月01日 06:06
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。