当前位置:网站首页>[error record] groovy function parameter dynamic type error (guess: groovy.lang.missingmethodexception: no signature of method)
[error record] groovy function parameter dynamic type error (guess: groovy.lang.missingmethodexception: no signature of method)
2022-07-05 22:18:00 【Programmer community】
One 、 Error message
Definition Groovy function ,
void fun(object) {
object.hello()}
If the incoming Instance object in , No definition hello Method , This will result in the following error reports ;
Error code :
class Student {
def hello(){
println "Hello Student" }}class Worker {
def hello(){
println "Hello Worker" }}class Farmer {
}void fun(object) {
object.hello()}fun(new Student())fun(new Worker())// The following usage will report // Caught: groovy.lang.MissingMethodException abnormal fun(new Farmer())
Error message :
Caught: groovy.lang.MissingMethodException: No signature of method: Farmer.hello() is applicable for argument types: () values: []Possible solutions: sleep(long), sleep(long, groovy.lang.Closure), getAt(java.lang.String), each(groovy.lang.Closure), split(groovy.lang.Closure), wait()groovy.lang.MissingMethodException: No signature of method: Farmer.hello() is applicable for argument types: () values: []Possible solutions: sleep(long), sleep(long, groovy.lang.Closure), getAt(java.lang.String), each(groovy.lang.Closure), split(groovy.lang.Closure), wait() at Worker$hello.call(Unknown Source) at Groovy.fun(Groovy.groovy:20) at Groovy$fun.callCurrent(Unknown Source) at Groovy.run(Groovy.groovy:28)
Two 、 Solution
have access to respondsTo Method , Determine whether hello function ;
void fun(object) {
if (object.respondsTo("hello")) {
object.hello() }}
Also can reference 【Groovy】Groovy Dynamic language features ( Groovy Automatic type inference of function arguments in | Precautions for function dynamic parameters ) Blog , At the expense of dynamic characteristics , Limit it to static languages , Then the above runtime error will not occur ;
The complete code is as follows :
class Student {
def hello(){
println "Hello Student" }}class Worker {
def hello(){
println "Hello Worker" }}class Farmer {
}void fun(object) {
if (object.respondsTo("hello")) {
object.hello() }}fun(new Student())fun(new Worker())// The following usage will report // Caught: groovy.lang.MissingMethodException abnormal fun(new Farmer())
Execution results :
Hello StudentHello Worker
边栏推荐
- Regular expressions and re Libraries
- "Chris Richardson microservices series" uses API gateway to build microservices
- AD637使用笔记
- Interview questions for famous enterprises: Coins represent a given value
- 119. Pascal‘s Triangle II. Sol
- How to use tensorflow2 for cat and dog classification and recognition
- A trip to Suzhou during the Dragon Boat Festival holiday
- Daily question brushing record (XIV)
- Leetcode simple question: check whether each row and column contain all integers
- 每日刷题记录 (十四)
猜你喜欢
How can Bluetooth in notebook computer be used to connect headphones
K210 learning notes (IV) k210 runs multiple models at the same time
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
Meituan dynamic thread pool practice ideas, open source
笔记本电脑蓝牙怎么用来连接耳机
A substring with a length of three and different characters in the leetcode simple question
Sparse array [matrix]
50. Pow(x, n). O(logN) Sol
Matlab draws a cute fat doll
AD637使用笔记
随机推荐
CA certificate trampled pit
Wonderful review of the digital Expo | highlight scientific research strength, and Zhongchuang computing power won the digital influence enterprise award
The difference between MVVM and MVC
极狐公司官方澄清声明
Business learning of mall order module
Calculation method of boundary IOU
When the industrial Internet era is truly mature, we will look at the emergence of a series of new industrial giants
Pinctrl subsystem and GPIO subsystem
数据泄露怎么办?'华生·K'7招消灭安全威胁
Server optimization of performance tuning methodology
What changes has Web3 brought to the Internet?
Regular expressions and re Libraries
Database recovery strategy
Code bug correction, char is converted to int high-order symbol extension, resulting in changes in positivity and negativity and values. Int num = (int) (unsigned int) a, which will occur in older com
How to use tensorflow2 for cat and dog classification and recognition
Character conversion PTA
Search: Future Vision (moving sword)
Performance monitoring of database tuning solutions
Advantages and disadvantages of the "Chris Richardson microservice series" microservice architecture
Platform bus