You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by pa...@apache.org on 2021/09/26 07:46:40 UTC

[groovy] branch master updated: GROOVY-10240: Support record grammar (detect invalid component names)

This is an automated email from the ASF dual-hosted git repository.

paulk pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git


The following commit(s) were added to refs/heads/master by this push:
     new 5910e25  GROOVY-10240: Support record grammar (detect invalid component names)
5910e25 is described below

commit 5910e2500fcdaeffa29b2e5a2fb067c85978cd5a
Author: Paul King <pa...@asert.com.au>
AuthorDate: Sun Sep 26 17:46:32 2021 +1000

    GROOVY-10240: Support record grammar (detect invalid component names)
---
 .../java/org/codehaus/groovy/classgen/Verifier.java | 13 +++++++++++++
 .../fail/RecordDeclaration_10x.groovy               | 21 +++++++++++++++++++++
 .../fail/RecordDeclaration_11x.groovy               | 21 +++++++++++++++++++++
 .../groovy/parser/antlr4/SyntaxErrorTest.groovy     |  2 ++
 4 files changed, 57 insertions(+)

diff --git a/src/main/java/org/codehaus/groovy/classgen/Verifier.java b/src/main/java/org/codehaus/groovy/classgen/Verifier.java
index 5a79b26..9133f7d 100644
--- a/src/main/java/org/codehaus/groovy/classgen/Verifier.java
+++ b/src/main/java/org/codehaus/groovy/classgen/Verifier.java
@@ -138,6 +138,7 @@ import static org.codehaus.groovy.ast.tools.PropertyNodeUtils.adjustPropertyModi
  *     <li>Uninitialized variables</li>
  *     <li>Bad code in object initializers or constructors</li>
  *     <li>Mismatches in modifiers or return types between implementations and interfaces/abstract classes</li>
+ *     <li>Invalid record component names</li>
  * </ul>
  *
  * Added code includes:
@@ -225,6 +226,9 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
                 !node.getAnnotations(ClassHelper.make(POJO.class)).isEmpty();
         this.classNode = node;
 
+        if (node.isRecord()) {
+            detectInvalidRecordComponentNames(node);
+        }
         if (Traits.isTrait(node) // maybe possible to have this true in joint compilation mode
                 || classNode.isInterface()) {
             //interfaces have no constructors, but this code expects one,
@@ -281,6 +285,15 @@ public class Verifier implements GroovyClassVisitor, Opcodes {
         checkFinalVariables(node);
     }
 
+    private static final List<String> invalidNames = List.of("clone", "finalize", "getClass", "hashCode", "notify", "notifyAll", "toString", "wait");
+    private void detectInvalidRecordComponentNames(ClassNode node) {
+        for (FieldNode fn : node.getFields()) {
+            if (invalidNames.contains(fn.getName())) {
+                throw new RuntimeParserException("Illegal record component name '" + fn.getName() + "'", fn);
+            }
+        }
+    }
+
     private void detectNonSealedClasses(ClassNode node) {
         if (isFinal(node.getModifiers())) return;
         if (Boolean.TRUE.equals(node.getNodeMetaData(SEALED_CLASS))) return;
diff --git a/src/test-resources/fail/RecordDeclaration_10x.groovy b/src/test-resources/fail/RecordDeclaration_10x.groovy
new file mode 100644
index 0000000..031cb10
--- /dev/null
+++ b/src/test-resources/fail/RecordDeclaration_10x.groovy
@@ -0,0 +1,21 @@
+/*
+ *  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 core
+
+record Bad(String clone) { }
diff --git a/src/test-resources/fail/RecordDeclaration_11x.groovy b/src/test-resources/fail/RecordDeclaration_11x.groovy
new file mode 100644
index 0000000..da045bb
--- /dev/null
+++ b/src/test-resources/fail/RecordDeclaration_11x.groovy
@@ -0,0 +1,21 @@
+/*
+ *  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 core
+
+record Bad(int hashCode) { }
diff --git a/src/test/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy b/src/test/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
index 9e10086..1f3ad7c 100644
--- a/src/test/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
+++ b/src/test/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
@@ -442,6 +442,8 @@ final class SyntaxErrorTest extends GroovyTestCase {
         TestUtils.doRunAndShouldFail('fail/RecordDeclaration_07x.groovy')
         TestUtils.doRunAndShouldFail('fail/RecordDeclaration_08x.groovy')
         TestUtils.doRunAndShouldFail('fail/RecordDeclaration_09x.groovy')
+        TestUtils.doRunAndShouldFail('fail/RecordDeclaration_10x.groovy')
+        TestUtils.doRunAndShouldFail('fail/RecordDeclaration_11x.groovy')
     }
 
     void 'test groovy core - Array'() {