如何检查java中的类型是否为instanceof接口?
How to check if the type is instanceof an interface in java?
我有一个class
(CreateAccountRequest
)实现了一个interface
(Iloggable
),接口没有方法,只是为了标记目的。
public interface Iloggable {}
public class CreateAccountRequest implements Iloggable{
//some private fields, getters and setters
}
在我的习惯中 RequestBodyAdviceAdapter
class,尝试检查请求是否是 Iloggable 的实例,以继续或忽略请求(例如是否进行日志记录)
我知道我们可以使用 instanceOf
运算符来检查一个对象是否实现了一个接口,下面的单元测试批准了它:
@Test
void CreateAccountRequest_Object_Instance_Of_Iloggable_Test() {
CreateAccountRequest request = new CreateAccountRequest();
assertTrue(request instanceof Iloggable);
}
但是在RequestBodyAdviceAdapter支持的方法中,结果一直是false或true,我尝试了不同的方法来区分参数是否实现了接口
@ControllerAdvice
public class CustomRequestBodyAdviceAdapter extends RequestBodyAdviceAdapter {
@Override
public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
//if(methodParameter.getParameterType().isInstance(Iloggable.class)) //return false all the time
//if(methodParameter.getParameter().getType().isInstance(Iloggable.class))// return false all the time
if(methodParameter.getParameter().getClass().isInstance(Iloggable.class))// return false all the time
// if(type instanceof Iloggable)// return false all the time
//if(type.getClass().isInstance(Iloggable.class)) //return true all the time
//if(type != null && Iloggable.class.isAssignableFrom(type.getClass()))//return false all the time
return true;
return false;
}
//other override methods
}
为了排除疑惑,我在支持方法中放了一张调试截图:
在这种情况下 type
是 java.lang.reflect.Type
(实例)而不是 CreateAccountRequest
(实例;)
您可以通过以下方式获得更多幸运:
if (type instanceof Class) { // type != null
Class<?> aClazz = (Class<?>) type;
return Iloggable.class.isAssignableFrom(aClazz);
}
Class.isAssignableFrom(...)
-javadoc-17
深度:
-
if(type instanceof Iloggable) //always false, because type is the
// (java.lang.reflect.)Type(->class) (occasionally) of the Iloggable and not/ever an instance of it.
-
if(type.getClass().isInstance(Iloggable.class)) //equivalent to
// java.lang.reflect.Type.class.isInstance(Iloggable.class)
// always false, except when Iloggable *implements* Type (pervert, but possible!;)
-
Iloggable.class.isAssignableFrom(type.getClass()) //incorporates
// my "wrong assumptions" on type... type.getClass() would
// evaluate to java.lang.reflect.Type.class, which is similar to bullet 2
但我假设(如果正确使用 -> 实验)一些 MethodParameter 方法也应该达到预期效果。特别是:
您要确保该类型代表实现 Illoggable 的 class。
@Override
public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
if ( type instanceof Class ) {
cls = ( Class ) type;
result = Iloggable.class.isAssignableFrom( cls );
} else {
result = false;
}
return result;
}
尝试以下比较,target.getTypeName()==Iloggable.class.getTypeName()
我有一个class
(CreateAccountRequest
)实现了一个interface
(Iloggable
),接口没有方法,只是为了标记目的。
public interface Iloggable {}
public class CreateAccountRequest implements Iloggable{
//some private fields, getters and setters
}
在我的习惯中 RequestBodyAdviceAdapter
class,尝试检查请求是否是 Iloggable 的实例,以继续或忽略请求(例如是否进行日志记录)
我知道我们可以使用 instanceOf
运算符来检查一个对象是否实现了一个接口,下面的单元测试批准了它:
@Test
void CreateAccountRequest_Object_Instance_Of_Iloggable_Test() {
CreateAccountRequest request = new CreateAccountRequest();
assertTrue(request instanceof Iloggable);
}
但是在RequestBodyAdviceAdapter支持的方法中,结果一直是false或true,我尝试了不同的方法来区分参数是否实现了接口
@ControllerAdvice
public class CustomRequestBodyAdviceAdapter extends RequestBodyAdviceAdapter {
@Override
public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
//if(methodParameter.getParameterType().isInstance(Iloggable.class)) //return false all the time
//if(methodParameter.getParameter().getType().isInstance(Iloggable.class))// return false all the time
if(methodParameter.getParameter().getClass().isInstance(Iloggable.class))// return false all the time
// if(type instanceof Iloggable)// return false all the time
//if(type.getClass().isInstance(Iloggable.class)) //return true all the time
//if(type != null && Iloggable.class.isAssignableFrom(type.getClass()))//return false all the time
return true;
return false;
}
//other override methods
}
为了排除疑惑,我在支持方法中放了一张调试截图:
在这种情况下 type
是 java.lang.reflect.Type
(实例)而不是 CreateAccountRequest
(实例;)
您可以通过以下方式获得更多幸运:
if (type instanceof Class) { // type != null
Class<?> aClazz = (Class<?>) type;
return Iloggable.class.isAssignableFrom(aClazz);
}
Class.isAssignableFrom(...)
-javadoc-17
深度:
-
if(type instanceof Iloggable) //always false, because type is the // (java.lang.reflect.)Type(->class) (occasionally) of the Iloggable and not/ever an instance of it.
-
if(type.getClass().isInstance(Iloggable.class)) //equivalent to // java.lang.reflect.Type.class.isInstance(Iloggable.class) // always false, except when Iloggable *implements* Type (pervert, but possible!;)
-
Iloggable.class.isAssignableFrom(type.getClass()) //incorporates // my "wrong assumptions" on type... type.getClass() would // evaluate to java.lang.reflect.Type.class, which is similar to bullet 2
但我假设(如果正确使用 -> 实验)一些 MethodParameter 方法也应该达到预期效果。特别是:
您要确保该类型代表实现 Illoggable 的 class。
@Override
public boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {
if ( type instanceof Class ) {
cls = ( Class ) type;
result = Iloggable.class.isAssignableFrom( cls );
} else {
result = false;
}
return result;
}
尝试以下比较,target.getTypeName()==Iloggable.class.getTypeName()