|
159 | 159 | import static org.codehaus.groovy.ast.tools.GeneralUtils.callThisX; |
160 | 160 | import static org.codehaus.groovy.ast.tools.GeneralUtils.callX; |
161 | 161 | import static org.codehaus.groovy.ast.tools.GeneralUtils.closureX; |
162 | | -import static org.codehaus.groovy.ast.tools.GeneralUtils.declS; |
163 | 162 | import static org.codehaus.groovy.ast.tools.GeneralUtils.declX; |
164 | 163 | import static org.codehaus.groovy.ast.tools.GeneralUtils.listX; |
165 | | -import static org.codehaus.groovy.ast.tools.GeneralUtils.localVarX; |
166 | 164 | import static org.codehaus.groovy.ast.tools.GeneralUtils.returnS; |
167 | 165 | import static org.codehaus.groovy.ast.tools.GeneralUtils.stmt; |
168 | 166 | import static org.codehaus.groovy.ast.tools.GeneralUtils.varX; |
@@ -1820,19 +1818,24 @@ private MethodNode createConstructorOrMethodNodeForClass(final MethodDeclaration |
1820 | 1818 | } |
1821 | 1819 |
|
1822 | 1820 | private MethodNode createMethodNodeForClass(final MethodDeclarationContext ctx, final ModifierManager modifierManager, final String methodName, final ClassNode returnType, final Parameter[] parameters, final ClassNode[] exceptions, Statement code, final ClassNode classNode, int modifiers) { |
1823 | | - if (asBoolean(ctx.elementValue())) { // the code of annotation method |
1824 | | - code = configureAST( |
1825 | | - new ExpressionStatement( |
1826 | | - this.visitElementValue(ctx.elementValue())), |
1827 | | - ctx.elementValue()); |
| 1821 | + ElementValueContext elementValue = ctx.elementValue(); |
| 1822 | + if (asBoolean(elementValue)) { |
| 1823 | + assert this.isAnnotationDeclaration(classNode); |
| 1824 | + Expression defaultValue = this.visitElementValue(elementValue); |
| 1825 | + if (returnType.isArray() && defaultValue instanceof ClosureExpression closure // GROOVY-11492 |
| 1826 | + && ClosureUtils.hasImplicitParameter(closure) && closure.getCode() instanceof BlockStatement block) { |
| 1827 | + defaultValue = listX(block.getStatements().stream().map(s -> ((ExpressionStatement) s).getExpression()).toList()); |
| 1828 | + } |
| 1829 | + code = configureAST(stmt(defaultValue), elementValue); |
| 1830 | + } |
1828 | 1831 |
|
| 1832 | + if (classNode.isInterface() && !modifierManager.containsAny(STATIC) && !(isTrue(classNode, IS_INTERFACE_WITH_DEFAULT_METHODS) && modifierManager.containsAny(DEFAULT, PRIVATE))) { |
| 1833 | + modifiers |= Opcodes.ACC_ABSTRACT; |
1829 | 1834 | } |
1830 | 1835 |
|
1831 | | - modifiers |= !modifierManager.containsAny(STATIC) && classNode.isInterface() && !(isTrue(classNode, IS_INTERFACE_WITH_DEFAULT_METHODS) && modifierManager.containsAny(DEFAULT, PRIVATE)) ? Opcodes.ACC_ABSTRACT : 0; |
1832 | 1836 | MethodNode methodNode = new MethodNode(methodName, modifiers, returnType, parameters, exceptions, code); |
| 1837 | + methodNode.setAnnotationDefault(asBoolean(elementValue)); |
1833 | 1838 | classNode.addMethod(methodNode); |
1834 | | - |
1835 | | - methodNode.setAnnotationDefault(asBoolean(ctx.elementValue())); |
1836 | 1839 | return methodNode; |
1837 | 1840 | } |
1838 | 1841 |
|
@@ -2246,34 +2249,19 @@ public Expression visitCommandExpression(final CommandExpressionContext ctx) { |
2246 | 2249 | * ,it may be a command expression |
2247 | 2250 | */ |
2248 | 2251 | private void validateInvalidMethodDefinition(final Expression baseExpr, final Expression arguments) { |
2249 | | - if (baseExpr instanceof VariableExpression) { |
2250 | | - if (isBuiltInType(baseExpr) || Character.isUpperCase(baseExpr.getText().codePointAt(0))) { |
2251 | | - if (arguments instanceof ArgumentListExpression) { |
2252 | | - List<Expression> expressionList = ((ArgumentListExpression) arguments).getExpressions(); |
2253 | | - if (1 == expressionList.size()) { |
2254 | | - final Expression expression = expressionList.get(0); |
2255 | | - if (expression instanceof MethodCallExpression mce) { |
2256 | | - final Expression methodCallArguments = mce.getArguments(); |
2257 | | - |
2258 | | - // check the method call tails with a closure |
2259 | | - if (methodCallArguments instanceof ArgumentListExpression) { |
2260 | | - List<Expression> methodCallArgumentExpressionList = ((ArgumentListExpression) methodCallArguments).getExpressions(); |
2261 | | - final int argumentCnt = methodCallArgumentExpressionList.size(); |
2262 | | - if (argumentCnt > 0) { |
2263 | | - final Expression lastArgumentExpression = methodCallArgumentExpressionList.get(argumentCnt - 1); |
2264 | | - if (lastArgumentExpression instanceof ClosureExpression) { |
2265 | | - if (ClosureUtils.hasImplicitParameter(((ClosureExpression) lastArgumentExpression))) { |
2266 | | - throw createParsingFailedException( |
2267 | | - "Method definition not expected here", |
2268 | | - tuple(baseExpr.getLineNumber(), baseExpr.getColumnNumber()), |
2269 | | - tuple(expression.getLastLineNumber(), expression.getLastColumnNumber()) |
2270 | | - ); |
2271 | | - } |
2272 | | - } |
2273 | | - } |
2274 | | - } |
2275 | | - } |
2276 | | - } |
| 2252 | + if (baseExpr instanceof VariableExpression && arguments instanceof ArgumentListExpression argsList |
| 2253 | + && (isBuiltInType(baseExpr) || Character.isUpperCase(baseExpr.getText().codePointAt(0)))) { |
| 2254 | + List<Expression> exprList = argsList.getExpressions(); |
| 2255 | + if (exprList.size() == 1 |
| 2256 | + && exprList.get(0) instanceof MethodCallExpression callExpr |
| 2257 | + && callExpr.getArguments() instanceof ArgumentListExpression callArgs) { |
| 2258 | + exprList = callArgs.getExpressions(); // check the method call tails with a closure |
| 2259 | + if (!exprList.isEmpty() && last(exprList) instanceof ClosureExpression closure && ClosureUtils.hasImplicitParameter(closure)) { |
| 2260 | + throw createParsingFailedException( |
| 2261 | + "Method definition not expected here", |
| 2262 | + tuple(baseExpr.getLineNumber(), baseExpr.getColumnNumber()), |
| 2263 | + tuple(callExpr.getLastLineNumber(), callExpr.getLastColumnNumber()) |
| 2264 | + ); |
2277 | 2265 | } |
2278 | 2266 | } |
2279 | 2267 | } |
@@ -4226,7 +4214,7 @@ public Expression visitElementValue(final ElementValueContext ctx) { |
4226 | 4214 |
|
4227 | 4215 | @Override |
4228 | 4216 | public ListExpression visitElementValueArrayInitializer(final ElementValueArrayInitializerContext ctx) { |
4229 | | - return configureAST(new ListExpression(ctx.elementValue().stream().map(this::visitElementValue).collect(Collectors.toList())), ctx); |
| 4217 | + return configureAST(listX(ctx.elementValue().stream().map(this::visitElementValue).toList()), ctx); |
4230 | 4218 | } |
4231 | 4219 |
|
4232 | 4220 | @Override |
|
0 commit comments