Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/main/java/groovy/lang/MetaClassImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -3333,7 +3333,11 @@ private void checkIfStdMethod(final MetaMethod metaMethod) {
checkIfGroovyObjectMethod(metaMethod);
break;
case "get":
if (checkMatch(metaMethod, genericGetMethod, GETTER_MISSING_ARGS) && metaMethod.getReturnType() != Void.TYPE) {
// GROOVY-11986: parameter must be String exactly, not just assignable from String,
// to avoid hijacking unrelated single-arg get(X) methods (e.g. get(Serializable),
// get(Object)) as the generic property getter, which silently shadows propertyMissing
if (checkMatch(metaMethod, genericGetMethod, GETTER_MISSING_ARGS) && metaMethod.getReturnType() != Void.TYPE
&& metaMethod.getParameterTypes()[0].getTheClass() == String.class) {
genericGetMethod = metaMethod;
}
break;
Expand Down
93 changes: 93 additions & 0 deletions src/test/groovy/bugs/Groovy11986.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package bugs

import org.junit.jupiter.api.Test

import static groovy.test.GroovyAssert.assertScript

final class Groovy11986 {

// get(Serializable) is the GORM entity-by-ID lookup shape; it must not be
// registered as the genericGetMethod, otherwise dynamic property access on
// an instance hijacks propertyMissing(String).
@Test
void testInheritedGetSerializableDoesNotShadowPropertyMissing() {
assertScript '''
trait T {
Object get(Serializable id) { "by-id:$id".toString() }
Object propertyMissing(String name) { "missing:$name".toString() }
}
class C implements T {
String title
}

def c = new C(title: 'hello')
assert c.title == 'hello' // bean property still wins
assert c.foo == 'missing:foo' // dynamic access falls through to propertyMissing
assert c.get('foo') == 'by-id:foo' // direct call still hits get(Serializable)
'''
}

@Test
void testInheritedGetObjectDoesNotShadowPropertyMissing() {
assertScript '''
class Base {
Object get(Object key) { "by-key:$key".toString() }
}
class C extends Base {
Object propertyMissing(String name) { "missing:$name".toString() }
}

def c = new C()
assert c.foo == 'missing:foo'
assert c.get('foo') == 'by-key:foo'
'''
}

// Sanity: a real get(String) is still picked up as the generic getter.
@Test
void testGetStringStillRegistersAsGenericGetter() {
assertScript '''
class C {
def get(String name) { "value:$name".toString() }
}

def c = new C()
assert c.foo == 'value:foo'
'''
}

// Sanity: get(String) on a class that also inherits get(Serializable) wins
// as the generic getter (closer parameter match).
@Test
void testGetStringWinsOverInheritedGetSerializable() {
assertScript '''
trait T {
Object get(Serializable id) { "by-id:$id".toString() }
}
class C implements T {
Object get(String name) { "by-name:$name".toString() }
}

def c = new C()
assert c.foo == 'by-name:foo'
'''
}
}
Loading