Skip to content

Commit 2e2e407

Browse files
committed
separate class and method within MissingMethodException message
1 parent 5406d67 commit 2e2e407

11 files changed

Lines changed: 156 additions & 164 deletions

File tree

src/main/java/groovy/lang/MissingMethodException.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,19 @@ public Object[] getArguments() {
5454

5555
@Override
5656
public String getMessage() {
57-
return "No signature of method: "
58-
+ (isStatic ? "static " : "")
57+
Class<?> type = getType();
58+
Object[] args = getArguments();
59+
return "No signature of "
60+
+ (isStatic() ? "static " : "")
61+
+ "method: "
62+
+ getMethod()
63+
+ " for class: "
5964
+ (type != null ? type.getName() : "<unknown>")
60-
+ "."
61-
+ method
62-
+ "() is applicable for argument types: ("
63-
+ FormatHelper.toTypeString(arguments, 80)
65+
+ " is applicable for argument types: ("
66+
+ FormatHelper.toTypeString(args, 80)
6467
+ ") values: "
65-
+ FormatHelper.toArrayString(arguments, 80, true)
66-
+ (type != null ? MethodRankHelper.getMethodSuggestionString(method, type, arguments) : "");
68+
+ FormatHelper.toArrayString(args, 80, true)
69+
+ (type != null ? MethodRankHelper.getMethodSuggestionString(getMethod(), type, args) : "");
6770
}
6871

6972
/**

src/spec/test/typing/StaticCompilationIntroTest.groovy

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818
*/
1919
package typing
2020

21-
import groovy.test.GroovyTestCase
21+
import org.junit.jupiter.api.Test
2222

23-
class StaticCompilationIntroTest extends GroovyTestCase {
23+
import static groovy.test.GroovyAssert.assertScript
24+
import static groovy.test.GroovyAssert.shouldFail
25+
26+
final class StaticCompilationIntroTest {
2427

2528
private static String TYPESAFE_PROGRAM = '''
2629
// tag::intro_typesafe[]
@@ -66,31 +69,27 @@ class StaticCompilationIntroTest extends GroovyTestCase {
6669

6770
private static final String RUN = '''
6871
test()
69-
'''
72+
'''
7073

7174
private static final String RUNTIME_MAGIC = '''
7275
// tag::intro_typesafe_magic[]
7376
Computer.metaClass.compute = { String str -> new Date() }
7477
// end::intro_typesafe_magic[]
7578
'''
7679

80+
@Test
7781
void testTypeSafeProgram() {
78-
def shell = new GroovyShell()
79-
shell.evaluate(TYPESAFE_PROGRAM+RUN)
82+
assertScript(TYPESAFE_PROGRAM+RUN)
8083
}
8184

85+
@Test
8286
void testTypeSafeProgramBroken() {
83-
def shell = new GroovyShell()
84-
try {
85-
shell.evaluate(TYPESAFE_PROGRAM+RUNTIME_MAGIC+RUN)
86-
assert false
87-
} catch (MissingMethodException e) {
88-
assert e.message.contains('No signature of method: Computer.compute() is applicable for argument types: (Date)')
89-
}
87+
def e = shouldFail(MissingMethodException, TYPESAFE_PROGRAM+RUNTIME_MAGIC+RUN)
88+
assert e.message.contains('No signature of method: compute for class: Computer is applicable for argument types: (Date)')
9089
}
9190

91+
@Test
9292
void testTypeSafeProgramFixedWithCompileStatic() {
93-
def shell = new GroovyShell()
94-
shell.evaluate(TYPESAFE_COMPILESTATIC_PROGRAM+RUNTIME_MAGIC+RUN)
93+
assertScript(TYPESAFE_COMPILESTATIC_PROGRAM+RUNTIME_MAGIC+RUN)
9594
}
9695
}
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,20 @@
1818
*/
1919
package bugs
2020

21-
import groovy.test.GroovyTestCase
21+
import org.junit.jupiter.api.Test
2222

23-
class Groovy3645Bug extends GroovyTestCase {
23+
import static groovy.test.GroovyAssert.shouldFail
24+
25+
final class Groovy3645 {
26+
@Test
2427
void testMethodCallOnSuperInAStaticMethod() {
25-
try{
26-
assertScript """
27-
class Foo3645 {
28-
static main(args) {
29-
super.bar()
30-
}
28+
def err = shouldFail MissingMethodException, '''
29+
class Foo3645 {
30+
static main(args) {
31+
super.bar()
3132
}
32-
"""
33-
} catch(MissingMethodException ex) {
34-
assertTrue ex.message.contains("No signature of method: static java.lang.Object.bar()")
35-
}
33+
}
34+
'''
35+
assert err.message.contains('No signature of static method: bar for class: java.lang.Object')
3636
}
3737
}
Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,36 +18,35 @@
1818
*/
1919
package bugs
2020

21-
import groovy.test.GroovyTestCase
21+
import org.junit.jupiter.api.Test
2222

23-
class Groovy4857Bug extends GroovyTestCase {
23+
import static groovy.test.GroovyAssert.shouldFail
24+
25+
final class Groovy4857 {
26+
27+
@Test
2428
void testMissingMethodNotUnsupportedOperation() {
25-
try {
26-
new GroovyShell().evaluate """
27-
interface A { def getValue() }
29+
def err = shouldFail MissingMethodException, '''
30+
interface A { def getValue() }
2831
29-
class B { }
32+
class B { }
3033
31-
def test = [ getValue: { 'getValue() called' } ] as A
34+
def test = [ getValue: { 'getValue() called' } ] as A
3235
33-
def b = new B()
36+
def b = new B()
3437
35-
b.call(test)
36-
"""
37-
fail('The compilation should have failed with No signature of method: B.call()')
38-
} catch (e) {
39-
assert e.message.contains('No signature of method: B.call()')
40-
assert e.class.name == 'groovy.lang.MissingMethodException'
41-
}
38+
b.call(test)
39+
'''
40+
assert err.message.contains('No signature of method: call for class: B')
4241
}
4342

43+
@Test
4444
void testTrygveAmundsensExample() {
45-
def val = new GroovyShell().evaluate """
45+
def val = new GroovyShell().evaluate '''
4646
[run: {}] as Runnable
47-
"""
47+
'''
4848

4949
assert val.toString()
5050
assert val instanceof Runnable
5151
}
5252
}
53-
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,19 @@
1818
*/
1919
package bugs
2020

21-
import groovy.test.GroovyTestCase
21+
import org.junit.jupiter.api.Test
2222

23-
class Groovy6072Bug extends GroovyTestCase {
23+
import static groovy.test.GroovyAssert.shouldFail
24+
25+
final class Groovy6072 {
26+
@Test
2427
void testShouldNotChangeBinExpToClassExp() {
25-
assertScript '''import groovy.transform.ASTTest
28+
def err = shouldFail MissingMethodException, '''
29+
import groovy.transform.ASTTest
2630
import org.codehaus.groovy.ast.expr.BinaryExpression
2731
2832
class OhNo {}
2933
30-
try {
3134
@ASTTest(phase=CANONICALIZATION, value={
3235
def right = node.rightExpression
3336
assert right instanceof BinaryExpression
@@ -39,10 +42,7 @@ class Groovy6072Bug extends GroovyTestCase {
3942
assert right instanceof BinaryExpression
4043
})
4144
def expr2 = OhNo | []
42-
} catch (MissingMethodException ex) {
43-
assert ex.message.contains('or()')
44-
// alright, what we wanted to test has gone
45-
}
4645
'''
46+
assert err.message.contains('No signature of static method: or for class: OhNo')
4747
}
4848
}

src/test/groovy/bugs/Groovy8678.groovy

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919
package bugs
2020

21-
import org.junit.Test
21+
import org.junit.jupiter.api.Test
2222

2323
import static groovy.test.GroovyAssert.assertScript
2424
import static groovy.test.GroovyAssert.shouldFail
@@ -79,87 +79,86 @@ final class Groovy8678 {
7979
@Test
8080
void testMissingPropertyOrMethod() {
8181
def x = new WithMethods()
82-
def throwable = shouldFail () -> x.'42'
83-
assert throwable.class == MissingPropertyException.class
82+
83+
def throwable = shouldFail(MissingPropertyException) { x.'42' }
8484
assert throwable.message.startsWith('No such property: 42 for class: bugs.Groovy8678$WithMethods')
85-
throwable = shouldFail () -> x.'42d'
86-
assert throwable.class == MissingPropertyException.class
85+
86+
throwable = shouldFail(MissingPropertyException) { x.'42d' }
8787
assert throwable.message.startsWith('No such property: 42d for class: bugs.Groovy8678$WithMethods')
88-
throwable = shouldFail () -> x.'84'()
89-
assert throwable.class == MissingMethodException.class
90-
assert throwable.message.startsWith('No signature of method: bugs.Groovy8678$WithMethods.84() is applicable for argument types: () values: []')
91-
throwable = shouldFail () -> x.'84f'()
92-
assert throwable.class == MissingMethodException.class
93-
assert throwable.message.startsWith('No signature of method: bugs.Groovy8678$WithMethods.84f() is applicable for argument types: () values: []')
88+
89+
throwable = shouldFail(MissingMethodException) { x.'84'() }
90+
assert throwable.message.startsWith('No signature of method: 84 for class: bugs.Groovy8678$WithMethods is applicable for argument types: () values: []')
91+
92+
throwable = shouldFail(MissingMethodException) { x.'84f'() }
93+
assert throwable.message.startsWith('No signature of method: 84f for class: bugs.Groovy8678$WithMethods is applicable for argument types: () values: []')
9494
}
9595

9696
@Test
97-
@SuppressWarnings("all")
9897
void testCompilationFailure() {
9998
shouldNotCompile('\'42\'', '\'FortyTwo\'', '42',
10099
// after GROOVY-8678
101-
cls -> cls == MissingMethodException.class,
102-
msg -> msg.startsWith('No signature of method: WithMethods.call() is applicable for argument types: (BigDecimal) values: [0.42]'))
100+
MissingMethodException,
101+
msg -> msg.startsWith('No signature of method: call for class: WithMethods is applicable for argument types: (BigDecimal) values: [0.42]'))
103102
// before GROOVY-8678
104-
// cls -> cls == MultipleCompilationErrorsException.class,
103+
// MultipleCompilationErrorsException,
105104
// msg -> msg.contains('Unexpected input: \'x.42\''))
106105
shouldNotCompile('\'42\'', '\'FortyTwo\'', '42()',
107106
// after GROOVY-8678
108-
cls -> cls == MissingMethodException.class,
109-
msg -> msg.startsWith('No signature of method: java.math.BigDecimal.call() is applicable for argument types: () values: []'))
107+
MissingMethodException,
108+
msg -> msg.startsWith('No signature of method: call for class: java.math.BigDecimal is applicable for argument types: () values: []'))
110109
// before GROOVY-8678
111-
// cls -> cls == MultipleCompilationErrorsException.class,
110+
// MultipleCompilationErrorsException,
112111
// msg -> msg.contains('Unexpected input: \'x.42\''))
113112
shouldNotCompile('\'42d\'', '\'FortyTwo\'', '42d',
114113
// after GROOVY-8678
115-
cls -> cls == MissingMethodException.class,
116-
msg -> msg.startsWith('No signature of method: WithMethods.call() is applicable for argument types: (Double) values: [0.42]'))
114+
MissingMethodException,
115+
msg -> msg.startsWith('No signature of method: call for class: WithMethods is applicable for argument types: (Double) values: [0.42]'))
117116
// before GROOVY-8678
118-
// cls -> cls == MultipleCompilationErrorsException.class,
117+
// MultipleCompilationErrorsException,
119118
// msg -> msg.contains('Unexpected input: \'x.42d\''))
120119
shouldNotCompile('\'42d\'', '\'FortyTwo\'', '42d()',
121120
// after GROOVY-8678
122-
cls -> cls == MissingMethodException.class,
123-
msg -> msg.startsWith('No signature of method: java.lang.Double.call() is applicable for argument types: () values: []'))
121+
MissingMethodException,
122+
msg -> msg.startsWith('No signature of method: call for class: java.lang.Double is applicable for argument types: () values: []'))
124123
// before GROOVY-8678
125-
// cls -> cls == MultipleCompilationErrorsException.class,
124+
// MultipleCompilationErrorsException,
126125
// msg -> msg.contains('Unexpected input: \'x.42d\''))
127126
shouldNotCompile('get84', '\'EightyFour\'', '84',
128127
// after GROOVY-8678
129-
cls -> cls == MissingMethodException.class,
130-
msg -> msg.startsWith('No signature of method: WithMethods.call() is applicable for argument types: (BigDecimal) values: [0.84]'))
128+
MissingMethodException,
129+
msg -> msg.startsWith('No signature of method: call for class: WithMethods is applicable for argument types: (BigDecimal) values: [0.84]'))
131130
// before GROOVY-8678
132-
// cls -> cls == MultipleCompilationErrorsException.class,
131+
// MultipleCompilationErrorsException,
133132
// msg -> msg.contains('Unexpected input: \'x.84\''))
134133
shouldNotCompile('get84', '\'EightyFour\'', '84()',
135134
// after GROOVY-8678
136-
cls -> cls == MissingMethodException.class,
137-
msg -> msg.startsWith('No signature of method: java.math.BigDecimal.call() is applicable for argument types: () values: []'))
135+
MissingMethodException,
136+
msg -> msg.startsWith('No signature of method: call for class: java.math.BigDecimal is applicable for argument types: () values: []'))
138137
// before GROOVY-8678
139-
// cls -> cls == MultipleCompilationErrorsException.class,
138+
// MultipleCompilationErrorsException,
140139
// msg -> msg.contains('Unexpected input: \'x.84\''))
141140
shouldNotCompile('get84f', '\'EightyFourEff\'', '84f',
142141
// after GROOVY-8678
143-
cls -> cls == MissingMethodException.class,
144-
msg -> msg.startsWith('No signature of method: WithMethods.call() is applicable for argument types: (Float) values: [0.84]'))
142+
MissingMethodException,
143+
msg -> msg.startsWith('No signature of method: call for class: WithMethods is applicable for argument types: (Float) values: [0.84]'))
145144
// before GROOVY-8678
146-
// cls -> cls == MultipleCompilationErrorsException.class,
145+
// MultipleCompilationErrorsException,
147146
// msg -> msg.contains('Unexpected input: \'x.84f\''))
148147
shouldNotCompile('get84f', '\'EightyFourEff\'', '84f()',
149148
// after GROOVY-8678
150-
cls -> cls == MissingMethodException.class,
151-
msg -> msg.startsWith('No signature of method: java.lang.Float.call() is applicable for argument types: () values: []'))
149+
MissingMethodException,
150+
msg -> msg.startsWith('No signature of method: call for class: java.lang.Float is applicable for argument types: () values: []'))
152151
// before GROOVY-8678
153-
// cls -> cls == MultipleCompilationErrorsException.class,
152+
// MultipleCompilationErrorsException,
154153
// msg -> msg.contains('Unexpected input: \'x.84f\''))
155154
}
156155

157156
private static void shouldNotCompile(String methodName,
158157
String returnValue,
159158
String expression,
160-
Closure<Class<? extends Exception>> exceptionAssertion,
159+
Class<? extends Exception> exceptionType,
161160
Closure<String> messageAssertion) {
162-
def throwable = shouldFail """
161+
def throwable = shouldFail exceptionType, """
163162
class WithMethods {
164163
def ${methodName}() {
165164
${returnValue}
@@ -168,7 +167,6 @@ final class Groovy8678 {
168167
def x = new WithMethods()
169168
x.${expression}
170169
""".stripIndent()
171-
assert exceptionAssertion(throwable.class)
172170
assert messageAssertion(throwable.message)
173171
}
174172

src/test/groovy/bugs/Groovy9779.groovy

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@
1818
*/
1919
package bugs
2020

21-
import groovy.transform.CompileStatic
22-
import org.junit.Test
21+
import org.junit.jupiter.api.Test
2322

2423
import static groovy.test.GroovyAssert.assertScript
2524
import static groovy.test.GroovyAssert.shouldFail
2625

27-
@CompileStatic
2826
final class Groovy9779 {
27+
2928
@Test
3029
void testCallOperatorOnDynamicProperties1() {
3130
assertScript '''
@@ -73,7 +72,7 @@ final class Groovy9779 {
7372
}
7473
C.x()
7574
'''
76-
assert err.message.contains('No signature of method: B.call() is applicable')
75+
assert err.message.contains('No signature of method: call for class: B is applicable')
7776
}
7877

7978
@Test // don't chain call properties together
@@ -90,7 +89,7 @@ final class Groovy9779 {
9089
}
9190
assert new C() + 1 == 42
9291
'''
93-
assert err.message.contains('No signature of method: C.plus() is applicable')
92+
assert err.message.contains('No signature of method: plus for class: C is applicable')
9493
}
9594

9695
@Test

src/test/groovy/bugs/MethodClosureTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
package bugs
2020

2121
import org.codehaus.groovy.runtime.MethodClosure
22-
import org.junit.Test
22+
import org.junit.jupiter.api.Test
2323

2424
import static groovy.test.GroovyAssert.assertScript
2525
import static groovy.test.GroovyAssert.shouldFail

0 commit comments

Comments
 (0)