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
5 changes: 1 addition & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,6 @@ allprojects {
tasks.withType(org.asciidoctor.gradle.AsciidoctorTask) {
outputs.cacheIf { true }
}

tasks.withType(GroovyCompile) {
groovyOptions.forkOptions.jvmArgs += ["-Dgroovy.antlr4.cache.threshold=100"]
}
}

task(copyTestResources, type: Copy)
Expand Down Expand Up @@ -403,6 +399,7 @@ allprojects {
}

tasks.withType(GroovyCompile) {
groovyOptions.forkOptions.jvmArgs += ["-Dgroovy.antlr4.cache.threshold=100"]
groovyOptions.fork(memoryMaximumSize: groovycMain_mx)
groovyOptions.encoding = 'UTF-8'
//options.compilerArgs << "-Xlint:unchecked" << "-Xlint:deprecation"
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/codehaus/groovy/ast/ClassHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ public class ClassHelper {

void_WRAPPER_TYPE = makeCached(Void.class), METACLASS_TYPE = makeCached(MetaClass.class),
Iterator_TYPE = makeCached(Iterator.class),
AUTOCLOSEABLE_TYPE = makeCached(AutoCloseable.class),

Enum_Type = makeWithoutCaching(Enum.class),
Annotation_TYPE = makeCached(Annotation.class),
Expand Down
20 changes: 15 additions & 5 deletions src/main/java/org/codehaus/groovy/ast/stmt/TryCatchStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
*/
package org.codehaus.groovy.ast.stmt;

import org.codehaus.groovy.GroovyBugError;
import org.codehaus.groovy.ast.GroovyCodeVisitor;
import org.codehaus.groovy.ast.expr.DeclarationExpression;
import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.ast.expr.VariableExpression;

import java.util.ArrayList;
Expand All @@ -29,10 +31,10 @@
* Represents a try { ... } catch () finally {} statement in Groovy
*/
public class TryCatchStatement extends Statement {

private static final String IS_RESOURCE = "_IS_RESOURCE";
private Statement tryStatement;
private List<ExpressionStatement> resourceStatements = new ArrayList<ExpressionStatement>();
private List<CatchStatement> catchStatements = new ArrayList<CatchStatement>();
private List<ExpressionStatement> resourceStatements = new ArrayList<>();
private List<CatchStatement> catchStatements = new ArrayList<>();
private Statement finallyStatement;


Expand Down Expand Up @@ -62,13 +64,21 @@ public Statement getTryStatement() {
}

public void addResource(ExpressionStatement resourceStatement) {
if (!(resourceStatement.getExpression() instanceof DeclarationExpression || resourceStatement.getExpression() instanceof VariableExpression)) {
throw new IllegalArgumentException("resourceStatement should be a variable declaration statement or a variable");
Expression resourceExpression = resourceStatement.getExpression();
if (!(resourceExpression instanceof DeclarationExpression || resourceExpression instanceof VariableExpression)) {
throw new GroovyBugError("resourceStatement should be a variable declaration statement or a variable");
}

resourceExpression.putNodeMetaData(IS_RESOURCE, Boolean.TRUE);

resourceStatements.add(resourceStatement);
}

public static boolean isResource(Expression expression) {
Boolean r = expression.getNodeMetaData(IS_RESOURCE);
return null != r && r;
}

public void addCatch(CatchStatement catchStatement) {
catchStatements.add(catchStatement);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ public void visitRangeExpression(final RangeExpression expression) {
}

@Override
public void visitBinaryExpression(BinaryExpression expression) {
public void visitBinaryExpression(final BinaryExpression expression) {
BinaryExpression enclosingBinaryExpression = typeCheckingContext.getEnclosingBinaryExpression();
typeCheckingContext.pushEnclosingBinaryExpression(expression);
try {
Expand Down Expand Up @@ -856,6 +856,7 @@ public void visitBinaryExpression(BinaryExpression expression) {
}

if (null == lType) lType = getType(leftExpression);

ClassNode rType = getType(rightExpression);
if (isNullConstant(rightExpression)) {
if (!isPrimitiveType(lType))
Expand Down Expand Up @@ -995,11 +996,23 @@ && isAssignment(enclosingBinaryExpression.getOperation().getType())
if (!isEmptyDeclaration) {
storeType(expression, resultType);
}

validateResourceInARM(expression, resultType);
} finally {
typeCheckingContext.popEnclosingBinaryExpression();
}
}

private void validateResourceInARM(BinaryExpression expression, ClassNode lType) {
if (expression instanceof DeclarationExpression) {
if (TryCatchStatement.isResource(expression)) {
if (!lType.implementsInterface(ClassHelper.AUTOCLOSEABLE_TYPE)) {
addError("Resource[" + lType.getName() + "] in ARM should be of type AutoCloseable", expression);
}
}
}
}

private void inferParameterAndReturnTypesOfClosureOnRHS(ClassNode lType, Expression rightExpression, int op) {
if (ASSIGN == op) {
if (rightExpression instanceof ClosureExpression && ClassHelper.isFunctionalInterface(lType)) {
Expand Down
80 changes: 80 additions & 0 deletions src/test/groovy/bugs/Groovy9261.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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 groovy.test.GroovyTestCase

class Groovy9261 extends GroovyTestCase {
void testInvalidResourceInARM() {
String errMsg = shouldFail '''\
@groovy.transform.CompileStatic
void test() {
try (String str = '123') {
}
}
test()
'''

assert errMsg.contains('Resource[java.lang.String] in ARM should be of type AutoCloseable')
assert errMsg.contains('@ line 3, column 22.')
}

void testInvalidResourceInARM2() {
String errMsg = shouldFail '''\
@groovy.transform.CompileStatic
void test() {
try (str = '123') {
}
}
test()
'''

assert errMsg.contains('Resource[java.lang.String] in ARM should be of type AutoCloseable')
assert errMsg.contains('@ line 3, column 22.')
}

void testInvalidResourceInARM3() {
String errMsg = shouldFail '''\
@groovy.transform.CompileStatic
void test() {
try (def sr = new StringReader(''); str = '123') {
}
}
test()
'''

assert errMsg.contains('Resource[java.lang.String] in ARM should be of type AutoCloseable')
assert errMsg.contains('@ line 3, column 53.')
}

void testInvalidResourceInEnhancedARM() {
String errMsg = shouldFail '''\
@groovy.transform.CompileStatic
void test() {
String str = '123'
try (str) {
}
}
test()
'''

assert errMsg.contains('Resource[java.lang.String] in ARM should be of type AutoCloseable')
assert errMsg.contains('@ line 4, column 22.')
}
}