我想使用 void java 类型,但不能。这是我的代码,它是在所有具有 @TraceLog
注释的方法之后运行的一个方面
@AfterReturning(value = "@annotation(log)",
returning = "returnValue",
argNames = "joinPoint, log, returnValue"
)
public void afterReturning(final JoinPoint joinPoint, final TraceLog log,
final Object returnValue) {
Class<?> returnType = ((MethodSignature) joinPoint.getSignature())
.getReturnType();
//It works when comperaing with string. But I want to write it with type of
if ("void".equals(returnType.getName()) ) {
//Do some log
}
}
作为编码规则类不应按名称进行比较( http://cwe.mitre.org/data/definitions/486.html ),我尝试使用 (returnType instanceof Void
) 但我面临这两个编译时Eclipse 中的错误:
- Incompatible conditional operand types Class<capture#5-of ?> and Void
- The type Void is not generic; it cannot be parameterized with arguments <?>
我想知道如何解决它?!
最佳答案
你可以使用
if (Void.class.isAssignableFrom (returnType)) {
}
示例:
Class<?> returnType = Void.class;
if (Void.class.isAssignableFrom (returnType)) {
System.out.println (returnType.getName ());
}
会打印
java.lang.Void
关于java - 在java中使用instanceof Void,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34248693/