From 84462bb563b0c1c0faf1ef0af623561e60c557e7 Mon Sep 17 00:00:00 2001 From: paulk Date: Wed, 25 Jan 2017 21:15:17 +1000 Subject: [PATCH] GROOVY-5318: generic types in fully-qualified class names parsing error (closes #479) --- .../groovy/antlr/AntlrParserPlugin.java | 19 +++++++++++- src/test/groovy/bugs/Groovy5318Bug.groovy | 30 +++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 src/test/groovy/bugs/Groovy5318Bug.groovy diff --git a/src/main/org/codehaus/groovy/antlr/AntlrParserPlugin.java b/src/main/org/codehaus/groovy/antlr/AntlrParserPlugin.java index 28a0395307d..6cacbfd66df 100644 --- a/src/main/org/codehaus/groovy/antlr/AntlrParserPlugin.java +++ b/src/main/org/codehaus/groovy/antlr/AntlrParserPlugin.java @@ -2907,13 +2907,14 @@ public static String qualifiedName(AST qualifiedNameNode) { StringBuilder buffer = new StringBuilder(); boolean first = true; - for (; node != null && !isType(TYPE_ARGUMENTS, node); node = node.getNextSibling()) { + while (node != null && !isType(TYPE_ARGUMENTS, node)) { if (first) { first = false; } else { buffer.append("."); } buffer.append(qualifiedName(node)); + node = node.getNextSibling(); } return buffer.toString(); } else { @@ -3038,6 +3039,7 @@ protected ClassNode makeType(AST typeNode) { if (isType(INDEX_OP, node) || isType(ARRAY_DECLARATOR, node)) { answer = makeType(node).makeArray(); } else { + checkTypeArgs(node, false); answer = ClassHelper.make(qualifiedName(node)); if (answer.isUsingGenerics()) { ClassNode newAnswer = ClassHelper.makeWithoutCaching(answer.getName()); @@ -3050,6 +3052,21 @@ protected ClassNode makeType(AST typeNode) { return answer; } + private boolean checkTypeArgs(AST node, boolean seenTypeArgs) { + if (isType(IDENT, node) && seenTypeArgs) { + throw new ASTRuntimeException(node, "Unexpected type arguments found prior to: " + qualifiedName(node)); + } + if (isType(DOT, node)) { + AST next = node.getFirstChild(); + while (next != null && !isType(TYPE_ARGUMENTS, next)) { + seenTypeArgs |= checkTypeArgs(next, seenTypeArgs); + seenTypeArgs |= isType(TYPE_ARGUMENTS, next.getFirstChild()) || isType(TYPE_ARGUMENTS, next.getNextSibling()); + next = next.getNextSibling(); + } + } + return seenTypeArgs; + } + /** * Performs a name resolution to see if the given name is a type from imports, * aliases or newly created classes diff --git a/src/test/groovy/bugs/Groovy5318Bug.groovy b/src/test/groovy/bugs/Groovy5318Bug.groovy new file mode 100644 index 00000000000..819dd58e8cd --- /dev/null +++ b/src/test/groovy/bugs/Groovy5318Bug.groovy @@ -0,0 +1,30 @@ +/* + * 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 gls.CompilableTestSupport + +class Groovy5318Bug extends CompilableTestSupport { + void testTypeArgumentsOnlyOnTheLastComponent() { + def message = shouldNotCompile """ + def a = new java.util.ArrayList>() + """ + assert message.contains('Unexpected type arguments found prior to: ArrayList') + } +}