两者有什么区别?两者似乎都意味着该值可以为null并且应该相应地处理,即检查为null.
更新: 上面的两个注释是JSR-305/FindBugs的一部分:http: //findbugs.sourceforge.net/manual/annotations.html
将com.google.common.base.Function(从接口谷歌番石榴)定义apply为:
@Nullable T apply(@Nullable F input);
该方法具有以下javadoc注释:
@throws NullPointerException if {@code input} is null and this function does not accept null arguments.
FindBugs抱怨我的Function实现:
private static final class Example implements Function<MyBean, String> {
@Override
@Nullable
public String apply(@Nullable MyBean input) {
if (null == input) {
throw new NullPointerException();
}
return input.field;
}
}
Run Code Online (Sandbox Code Playgroud)
具有高优先级警告:
NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE,优先级:高
输入必须是非空的,但标记为可为空
此参数始终以要求它为非空的方式使用,但该参数明确注释为Nullable.使用参数或注释是错误的.
我的函数不支持null输入,如果是这种情况则抛出异常.如果我理解正确,FindBugs会将此视为非null的要求.
对我来说,它看起来像一个矛盾:输入是@Nullable但是当它为null时,方法@throws NullPointerException.我错过了什么吗?
摆脱我能看到的警告的唯一方法是手动抑制.(显然,番石榴代码不受我的控制).
关于@Nullable注释,FindBugs,Guava或我自己的用法有什么问题?
我有一个带注释方法的接口.注释标有@Inherited,所以我希望实现者继承它.但事实并非如此:
码:
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
import java.util.Arrays;
public class Example {
public static void main(String[] args) throws SecurityException, NoSuchMethodException {
TestInterface obj = new TestInterface() {
@Override
public void m() {}
};
printMethodAnnotations(TestInterface.class.getMethod("m"));
printMethodAnnotations(obj.getClass().getMethod("m"));
}
private static void printMethodAnnotations(Method m) {
System.out.println(m + ": " + Arrays.toString(m.getAnnotations()));
}
}
interface TestInterface {
@TestAnnotation
public void m();
}
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@interface TestAnnotation {}
Run Code Online (Sandbox Code Playgroud)
上面的代码打印:
public abstract void annotations.TestInterface.m():[@ annotations.TestAnnotation()]
public void annotations.Example $ 1.m():[] …
我想将Jackson的Tree Model与Java 8流API一起使用,如下所示:
JsonNode jn = new ObjectMapper().readValue(src, JsonNode.class);
return jn.stream().anyMatch(myPredicate);
Run Code Online (Sandbox Code Playgroud)
但是,JsonNode似乎没有实现stream(),我找不到任何标准帮助程序.
JsonNode 实现Iterable,因此我可以使用Google Guava获得相同的结果:
JsonNode jn = new ObjectMapper().readValue(src, JsonNode.class);
return Iterables.find(jn, myPredicate);
Run Code Online (Sandbox Code Playgroud)
但纯Java解决方案呢?
我在尝试在Oracle查询中参数化interval参数时遇到问题:
select current_timestamp - interval :hours hour from dual
Run Code Online (Sandbox Code Playgroud)
如果我用constant替换interval参数,那么它执行得很好.
在SQL中尝试引用和不引用的参数.
请参阅下面的使用最小代码段的插图:
public class Main {
private static String SQL_CONSTANT_INTERVAL = "select current_timestamp - interval '1' hour from dual";
private static String SQL_PARAMETERIZED_INTERVAL_QUOTED = "select current_timestamp - interval ':hours' hour from dual";
private static String SQL_PARAMETERIZED_INTERVAL_UNQUOTED = "select current_timestamp - interval :hours hour from dual";
public static void main(String[] args) throws Exception {
Properties properties = new Properties();
properties.load(Main.class.getClassLoader().getSystemResourceAsStream("db.properties"));
DataSource dataSource = BasicDataSourceFactory.createDataSource(properties);
NamedParameterJdbcTemplate npTemplate = new NamedParameterJdbcTemplate(dataSource);
Map<String, String> …Run Code Online (Sandbox Code Playgroud) 所以我有一个简单的Scala文件:
object Main extends App {
println("Init")
def test=println("Method")
}
Main.test
println(Main)
print("End")
Run Code Online (Sandbox Code Playgroud)
当我将其作为脚本运行时,我得到以下输出:
$ scala Main.scala
Method
Main$$anon$1$Main$@2449a2da
End
Run Code Online (Sandbox Code Playgroud)
我想知道为什么这println("Init")条线永远不会被调用?我希望在Main的初始化时调用它,它应该在调用它的方法时发生.