小编vit*_*aly的帖子

javax.annotation:@Nullable vs @CheckForNull

两者有什么区别?两者似乎都意味着该值可以为null并且应该相应地处理,即检查为null.

更新: 上面的两个注释是JSR-305/FindBugs的一部分:http: //findbugs.sourceforge.net/manual/annotations.html

java annotations static-analysis findbugs

43
推荐指数
3
解决办法
6万
查看次数

Google Guava Function界面中的@Nullable输入会触发FindBugs警告

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或我自己的用法有什么问题?

java findbugs guava

35
推荐指数
1
解决办法
1万
查看次数

注释不是从接口方法继承的

我有一个带注释方法的接口.注释标有@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():[] …

java annotations

10
推荐指数
2
解决办法
8486
查看次数

杰克逊是否支持java 8 stream()?

我想将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解决方案呢?

json jackson guava java-8 java-stream

10
推荐指数
1
解决办法
9128
查看次数

Spring命名参数:如何在查询中参数化Oracle时间间隔?

我在尝试在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)

java sql oracle spring

6
推荐指数
1
解决办法
1972
查看次数

Scala App初始化顺序

所以我有一个简单的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的初始化时调用它,它应该在调用它的方法时发生.

scala

2
推荐指数
1
解决办法
645
查看次数