質問

私のインターネットはモバイルが人生を楽しむことができるように基本的な一般課題:

final Detail detail = field.getAnnotation(Detail.class);
final String example = detail.example();
final Class<?> type = field.getType();
if (List.class.isAssignableFrom(type))
                    ...
else if (Enum.class.isAssignableFrom(type))
    setValue(contract, field, Enum.valueOf(type, example));
else if (...)
.....

その Enum.valueOf() 少し難しい話、私の場合、エラー:

valueOf(java.lang.Class java.lang.文字列)をjava.lang.列挙型には適用されません(java.lang.Class java.lang.文字列)

この自の製造方法により感覚でタイプ Class<Object>.ですが Enum はCRTPまいに良い意味でキャスト型のコンパイラます。の原型 Enum.valueOf((Class)type, example)) の答えはどうでしょうか?ちょっと2つの警告います。

役に立ちましたか?

解決

次の行は、唯一の警告でそれを行います。

...
setValue( contract, field, Enum.valueOf( type.asSubclass( Enum.class ), example ) );
...

他のヒント

あなたは、列挙型の要件を満たす「T」タイプをキャプチャするためのヘルパーメソッドを書くことができます:

private <T extends Enum<T>> T helper(Class<?> type, String example) {
    return Enum.valueOf((Class<T>)type, example);
}

これは一つだけ注意が必要です。

、その後、あなたは

のようにそれを使用することができます
else if (Enum.class.isAssignableFrom(type))
    setValue(contract, field, helper(type, example));

編集:大丈夫、その方法について:

private <T extends Enum<T>> Object helper(Class<?> type, String example) {
    return Enum.valueOf((Class<T>)type, example);
}

いと思うのを取り出すことが可能になコンパイラの警告

最高の場合のシナリオを低減すべての誤差につい@tangensます。

さらなるフォーラムのスレッドが失敗回答について解説を行っていなぜ少します。

それで、一例を発揮する問題としています。

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.util.List;

public class Test {
  @Retention(RetentionPolicy.RUNTIME)
  @Target(ElementType.FIELD)
  public @interface Detail {
    String example();
  }

  public enum ExampleEnum {
    FOO_BAR, HELLO_WORLD
  }

  @Detail(example = "FOO_BAR")
  public ExampleEnum test;

  public static void main(String[] args) throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
    populate(new Test());
  }

  public static void populate(Object o) throws IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchFieldException {
    final Field field = o.getClass().getField("test");
    final Detail detail = field.getAnnotation(Detail.class);
    System.out.println("Annotation = " + detail);
    final String example = detail.example();
    final Class<?> type = field.getType();
    System.out.println("Field Class = " + type.getName());
    if (List.class.isAssignableFrom(type)) {
    } else if (Enum.class.isAssignableFrom(type)) {
      Class<? extends Enum> enumType = type.asSubclass(Enum.class); // Enum is a raw type. References to generic type Enum<E> should be parameterized
      Enum val = Enum.valueOf(enumType, example); // 1) Enum is a raw type. References to generic type Enum<E> should be parameterized
                                                  // 2) Type safety: Unchecked invocation valueOf(Class<capture#7-of ? extends Enum>, String) of the generic
                                                  //    method valueOf(Class<T>, String) of type Enum
      field.set(o, val);
    }
  }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top