相关疑难解决方法(0)

尝试用资源引入无法访问的字节码

javac是否有可能为以下过程生成无法访问的字节码?

public void ex06(String name) throws Exception {
    File config = new File(name);
    try (FileOutputStream fos = new FileOutputStream(config);
            PrintWriter writer = new PrintWriter(new OutputStreamWriter(
                    fos , "rw"))) {
        bar();
    }
}
Run Code Online (Sandbox Code Playgroud)

当我查看字节码的异常表(javap -v)时,有以下条目看起来很奇怪:

43    48    86   Class java/lang/Throwable
43    48    95   any
Run Code Online (Sandbox Code Playgroud)

21   135   170   Class java/lang/Throwable
21   135   179   any
Run Code Online (Sandbox Code Playgroud)

现在的问题是,如果捕获了类型为"any"而不是Throwable的异常,则只能访问某些代码.有没有可能发生这种情况的情况?

======编辑======感谢目前为止的答案.让我给出另一个证据来表明我真的不理解异常处理:考虑以下过程

Object constraintsLock;
private String[] constraints;
private String constraint;
public void fp01() {
    // Add this constraint to the set for our web application
    synchronized (constraintsLock) …
Run Code Online (Sandbox Code Playgroud)

java jvm exception-handling javac try-catch

17
推荐指数
1
解决办法
1343
查看次数

尝试资源单元测试覆盖率

我想问一下你是否有任何技术可以用单元测试覆盖try-with-resource.我用它打开一些流,eclemma告诉我,我已经在这个try-with-resource块上发现了分支.据我所知,这个块在编译后显然被翻译成了其他东西,但这是否意味着如果我使用它,我不能100%覆盖emma?你有什么技术可以解决这个问题吗?我喜欢100%的报道.

谢谢!

java unit-testing

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

Sonar报告了try-with-resources块中分支覆盖不足的误报

使用SonarQube的最新版本(4.3.2),try-with-resources块会给catch线路的分支覆盖带来误报.例如:

public List<String> getLines(String filename) {
    try (InputStream inputStream = getInputStream(filename)){
        return IOUtils.readLines(inputStream);
    } catch (IOException e) { // <<<<<<< REPORTS AS BRANCH COVERAGE 2/8 
        throw new IllegalArgumentException(e);
    }
}
Run Code Online (Sandbox Code Playgroud)

但我的单元测试涵盖了每个点抛出的异常,所有其他线路都有100%覆盖 - 实际覆盖率为100%.那里的"8"来自哪里?抛出异常不是8个地方.

我尝试添加// NOSONAR到问题行,甚至尝试将其添加到每一行,但报告是相同的.

其他类型的问题使用的时候忽略了// NOSONAR,所以它不是一个声纳配置问题.

我怀疑这是因为声纳不允许try-with-resources块产生的字节码中的额外try-catch块.

有没有办法装饰成功导致声纳忽略这种特殊误报的代码?

false-positive try-with-resources sonarqube

3
推荐指数
1
解决办法
3768
查看次数

由编译器完成的 Java try-with-resource 实现

我想知道,当发生异常时,try with resource 语句如何在进入 catch 块之前设法关闭资源。当异常发生时,执行立即跳转到 catch 块。所以实际上 try-with-resource 关闭资源的地方。

为了更好地理解它是如何工作的,我决定看看编译器是如何实现它的。我编写了以下代码并编译了它。

public class Test
{
    public static void main(final String[] args) {
       //I used same JDK for compilation and execution.
        System.out.println("Java version: " + System.getProperty("java.version") + "\n");
        try(CloseMe me = new CloseMe(); 
                CloseMeToo meToo = new CloseMeToo()){
                
                System.out.println("trying");
                throw new Exception("try failed");

            } catch(Exception e) {
                System.out.println("failed");
                System.out.println("\n");
                System.out.println(e.getMessage());
                System.out.println(e.getSuppressed()[0].getMessage());
                System.out.println(e.getSuppressed()[1].getMessage());
            }
    }
}

class CloseMe implements AutoCloseable {
    @Override
    public void close() throws Exception {
        System.out.println("me closing!");
        throw new …
Run Code Online (Sandbox Code Playgroud)

java try-with-resources

3
推荐指数
1
解决办法
70
查看次数