的常规问题,如果这是一个错误或功能,但它绝对不直观从Java背景的人来跟踪异常的原因。我不知道不正确的getter
Groovy的允许变量被称为即使没有定义的变量。 例如,考虑下面的类:
class B {
def infos;
public B(String param)
{
infos = param
}
public getInfo()
{
return info;
}
}
如果你注意到了,里面getInfo()
,我回到这是从未定义info
。但是,Eclipse不会给出警告。所以,我继续写作以下:
class A
{
static main(def args)
{
B bObj = new B("Mahesh")
println "Hello groovy"
println bObj.getInfo()
println "Hello groovy"
}
}
现在,这给StackOverflowError
一个巨大的堆栈跟踪:这里
Exception in thread "main" java.lang.StackOverflowError
at java.lang.Exception.<init>(Exception.java:102)
at java.lang.ReflectiveOperationException.<init>(ReflectiveOperationException.java:89)
at java.lang.reflect.InvocationTargetException.<init>(InvocationTargetException.java:72)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at groovy.lang.MetaClassImpl$GetBeanMethodMetaProperty.getProperty(MetaClassImpl.java:3493)
at org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.callGroovyObjectGetProperty(GetEffectivePogoPropertySite.java:67)
--> at packages.B.getInfo(ThreadDumpsExp.groovy:169) <--
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at groovy.lang.MetaClassImpl$GetBeanMethodMetaProperty.getProperty(MetaClassImpl.java:3493)
at org.codehaus.groovy.runtime.callsite.GetEffectivePogoPropertySite.callGroovyObjectGetProperty(GetEffectivePogoPropertySite.java:67)
:
:
堆栈跟踪是确定的,因为在堆栈中某处跟踪它指出了这个特定的行我在上面的堆栈跟踪中用箭头强调了这一点。我期待着堆栈跟踪中的这条线,这就是为什么我能够快速追踪它。当我在我的项目中遇到同样的问题时,问题就出现了。堆栈跟踪同样巨大。我不知道它究竟在哪里出错,所以我不能猜测哪条线实际上可能是错误的。在调试过程中,最糟糕的是它停止在Groovy的源代码中。我不得不在不同的地方放置断点来停止对它们的执行。一段时间后,我发现调试器实际停止在我的代码中的那一行。从那里我通过我的整个代码找到导致问题的线路。这条线是一个简单的getter,它返回了错误的东西。
现在我知道我应该在编写代码时更加自觉和不应该从一个getter返回一个不存在的变量这样的错误。但是有什么办法可以让它不做它以前的做法吗?
编辑
另外之后加入@TypeChecked
,下面会出现误差。它在早些时候正常工作。
在您的类或方法上使用@TypeChecked注释,在这方面它的行为更像Java。 – cjstehno
不知怎的,现在的问题是,方法名称“getInfo”正在阻止编译器显示错误。 (添加了上述截图。)如果我将'getInfo()'更改为'getInfos()'。它使用'@ TypeChecked'正确给出了错误。 – Mahesha999