Dropwizard の Application Main クラスの型指定を忘れたところ、以下のエラーが出力されました。 対処した結果を記載します。
エラー内容
ServerCommand の コンストラクタ内でエラーが発生しました。
Exception in thread "main" java.lang.IllegalStateException:
Cannot figure out type parameterization for xxx.xxx.xxx.xxx.BatchApplication
at io.dropwizard.util.Generics.getTypeParameter(Generics.java:62)
at io.dropwizard.Application.getConfigurationClass(Application.java:30)
at io.dropwizard.cli.ServerCommand.<init>(ServerCommand.java:25)
at io.dropwizard.Application.run(Application.java:69)
at xxx.xxx.xxx.xxx.BatchApplication.main(BatchApplication.java:38)
修正前の記述
以下のように、Application の後ろに Generics の型指定を行っていません。
public class BatchApplication extends Application {
public static void main(String[] args) throws Exception {
new BatchApplication().run(args);
}
}
修正後の記述
以下型指定を行うようにしました。
public class BatchApplication extends Application<Configuration> {
public static void main(String[] args) throws Exception {
new BatchApplication().run(args);
}
}
エラーの発生箇所について
dropwizard/Application.java at master · dropwizard/dropwizard のApplication.getConfigurationClass
では以下のように、Generics.getTypeParameter()
を呼び出しています。
/**
* Returns the {@link Class} of the configuration class type parameter.
*
* @return the configuration class
* @see Generics#getTypeParameter(Class, Class)
*/
public Class<T> getConfigurationClass() {
return Generics.getTypeParameter(getClass(), Configuration.class);
}
Generics.getTypeParameter
では、型指定した Class を取得しますが、指定し忘れると、Class が取得できずにエラーとなります。
/**
* Finds the type parameter for the given class which is assignable to the bound class.
*
* @param klass a parameterized class
* @param bound the type bound
* @param <T> the type bound
* @return the class's type parameter
*/
public static <T> Class<T> getTypeParameter(Class<?> klass, Class<? super T> bound) {
Type t = requireNonNull(klass);
while (t instanceof Class<?>) {
t = ((Class<?>) t).getGenericSuperclass();
}
/* This is not guaranteed to work for all cases with convoluted piping
* of type parameters: but it can at least resolve straight-forward
* extension with single type parameter (as per [Issue-89]).
* And when it fails to do that, will indicate with specific exception.
*/
if (t instanceof ParameterizedType) {
// should typically have one of type parameters (first one) that matches:
for (Type param : ((ParameterizedType) t).getActualTypeArguments()) {
if (param instanceof Class<?>) {
final Class<T> cls = determineClass(bound, param);
if (cls != null) {
return cls;
}
} else if (param instanceof TypeVariable) {
for (Type paramBound : ((TypeVariable<?>) param).getBounds()) {
if (paramBound instanceof Class<?>) {
final Class<T> cls = determineClass(bound, paramBound);
if (cls != null) {
return cls;
}
}
}
} else if (param instanceof ParameterizedType) {
final Type rawType = ((ParameterizedType) param).getRawType();
if (rawType instanceof Class<?>) {
final Class<T> cls = determineClass(bound, rawType);
if (cls != null) {
return cls;
}
}
}
}
}
throw new IllegalStateException("Cannot figure out type parameterization for " + klass.getName());
}
throw new IllegalStateException("Cannot figure out type parameterization for " + klass.getName());
が呼び出されています。
これで2時間くらい消費してしまいました。
以上です。
コメント