gpt4 book ai didi

groovy - 编译时使用groovy方法进行NPE

转载 作者:行者123 更新时间:2023-12-02 09:58:46 25 4
gpt4 key购买 nike

我以一种奇怪的方式得到了 NPE。接下来是发生这种情况的方法调用:

    String exec(String command) {
if (command == null || isConnected()) return null;

Session session = null;
boolean error = false;
try {
session = ssh.startSession();
// try {
// final Session.Command cmd = session.exec(command);
// String result = cmd.getOutputAsString();
// if (cmd.getExitStatus() != null && cmd.getExitStatus() != 0){
// //TODO:command execution failure should be logged
// error = true;
// } else {
// return result;
// }

// } catch (Exception ex){
// //TODO:command execution failure should be logged
// ex.printStackTrace();
// error = true;
// }
} catch (Exception ex) {
// TODO:session creation failure should be logged
ex.printStackTrace();
error = true;
} finally {
session.close();
if (error) return null;
}
}

如果我取消注释内部 try/catch block (甚至仅声明),我会得到 groovy 生成的 NPE,如下所示:

java.lang.NullPointerException
at org.objectweb.asm.MethodWriter.visitMaxs(Unknown Source)
at org.objectweb.asm.MethodAdapter.visitMaxs(Unknown Source)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitConstructorOrMethod(AsmClassGenerator.java:605)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitMethod(ClassCodeVisitorSupport.java:123)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitMethod(AsmClassGenerator.java:696)
at org.codehaus.groovy.ast.ClassNode.visitContents(ClassNode.java:1039)
at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClass(ClassCodeVisitorSupport.java:50)
at org.codehaus.groovy.classgen.AsmClassGenerator.visitClass(AsmClassGenerator.java:276)
at org.codehaus.groovy.control.CompilationUnit$12.call(CompilationUnit.java:748)
at org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:942)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:519)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:497)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:474)
at org.jetbrains.groovy.compiler.rt.GroovyCompilerWrapper.compile(GroovyCompilerWrapper.java:43)
at org.jetbrains.groovy.compiler.rt.GroovycRunner.main(GroovycRunner.java:128)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.intellij.rt.execution.CommandLineWrapper.main(CommandLineWrapper.java:75)

代码没什么特别的,ssh变量不为空。但我不明白到底发生了什么。

最佳答案

看起来像是将 if (error) return null;finally block 中放入方法 end 中就可以达到目的,但还不完全确定原因。

关于groovy - 编译时使用groovy方法进行NPE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5501185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com