2011-08-23 16 views

答えて

7

If I replace Foo with FooImpl the code is accepted.

FooImplが列挙型でない限り、これをコンパイルすると非常に驚きます。

注釈メンバーのみ、以下を含んでいてもよい:

  • プリミティブ型
  • ストリング
  • クラスリテラル
  • 注釈
  • 列挙項目
  • または任意の1次元配列上記

It is a compile-time error if the return type of a method declared in an annotation type is any type other than one of the following: one of the primitive types, String, Class and any invocation of Class, an enum type (§8.9), an annotation type, or an array (§10) of one of the preceding types. It is also a compile-time error if any method declared in an annotation type has a signature that is override-equivalent to that of any public or protected method declared in class Object or in the interface annotation.Annotation.

出典:JLS

3

http://java.sun.com/docs/books/jls/third_edition/html/interfaces.html#9.7

注釈部材型のいずれかでなければならない:要素タイプである場合、プリミティブ、ストリング、クラス、列挙、上記

の任意の配列はまた、コンパイル時エラーでありますElementValueに釣り合わない。

希望すると便利です。同様に、このドキュメントで同じことを発見

http://download.oracle.com/javase/1.5.0/docs/guide/language/annotations.html

は「戻り値の型は、プリミティブ、文字列、クラス、列挙、注釈、前の型の配列に制限されています。」前述したように、「インターフェース」は許可されていません。

+0

私は最初の文を果たし、もう一つは、問題のようです。なぜこれが注釈に許可されていないのだろう... – soc

+3

@soc *私は最初の文章を実行する*いいえ、あなたはしません。 'クラス<? extends Foo> fooType() 'は有効ですが、' Foo foo() 'は有効ではありません。 –

+0

Foo Foo()をメソッド(単純化のため)として考えてみましょう...メソッドの戻り値の型としてinterfaceを挙げることはできますか? :) – Nik

関連する問題