You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2022/01/15 18:35:18 UTC
[groovy] 01/01: GROOVY-5358: getProperty, setProperty, invokeMethod from category method
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch GROOVY-5358
in repository https://gitbox.apache.org/repos/asf/groovy.git
commit fdce61c06c7fd4ae40e3645527c60a3e356bf71c
Author: Eric Milles <er...@thomsonreuters.com>
AuthorDate: Sat Jan 15 12:34:55 2022 -0600
GROOVY-5358: getProperty, setProperty, invokeMethod from category method
---
src/main/java/groovy/lang/MetaClassImpl.java | 4 +-
src/test/groovy/bugs/Groovy5358.groovy | 69 ++++++++++++++++++++++++++++
2 files changed, 72 insertions(+), 1 deletion(-)
diff --git a/src/main/java/groovy/lang/MetaClassImpl.java b/src/main/java/groovy/lang/MetaClassImpl.java
index fdaeca6..94af0a3 100644
--- a/src/main/java/groovy/lang/MetaClassImpl.java
+++ b/src/main/java/groovy/lang/MetaClassImpl.java
@@ -3180,7 +3180,9 @@ public class MetaClassImpl implements MetaClass, MutableMetaClass {
* @see GroovyObject
*/
protected final void checkIfGroovyObjectMethod(MetaMethod metaMethod) {
- if (metaMethod instanceof ClosureMetaMethod || metaMethod instanceof MixinInstanceMetaMethod) {
+ if (metaMethod instanceof ClosureMetaMethod
+ || metaMethod instanceof NewInstanceMetaMethod
+ || metaMethod instanceof MixinInstanceMetaMethod) {
if (isGetPropertyMethod(metaMethod)) {
getPropertyMethod = metaMethod;
} else if (isInvokeMethod(metaMethod)) {
diff --git a/src/test/groovy/bugs/Groovy5358.groovy b/src/test/groovy/bugs/Groovy5358.groovy
new file mode 100644
index 0000000..28a1000
--- /dev/null
+++ b/src/test/groovy/bugs/Groovy5358.groovy
@@ -0,0 +1,69 @@
+/*
+ * 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 groovy.bugs
+
+import org.junit.Test
+
+import static groovy.test.GroovyAssert.assertScript
+
+final class Groovy5358 {
+ @Test
+ void testGetPropertyOverrides() {
+ assertScript '''
+ class FooWorksAsMap {
+ def getProperty(String name) {
+ "OK:FooWorksAsMap.$name"
+ }
+ }
+
+ class BarWorksAsMap {}
+ @Category(BarWorksAsMap) class C {
+ def getProperty(String name) {
+ "OK:BarWorksAsMap.$name"
+ }
+ }
+ BarWorksAsMap.mixin C
+
+ class BazWorksAsMap {}
+ BazWorksAsMap.metaClass.getProperty = { String name ->
+ "OK:BazWorksAsMap.$name"
+ }
+
+ //
+
+ def objects = [
+ new FooWorksAsMap(),
+ new BarWorksAsMap(),
+ new BazWorksAsMap(),
+ [foo:'OK:LinkedHashMap.foo']
+ ]
+ for (def name in ['foo']) {
+ for (def obj in objects) {
+ def which = "${obj.getClass().getSimpleName()}.$name"
+ try {
+ String value = obj."$name"
+ assert value.startsWith('OK') : "$which -> $value"
+ } catch (any) {
+ assert false : "$which -> FAIL:$any"
+ }
+ }
+ }
+ '''
+ }
+}