Skip to content

Commit 7ff9638

Browse files
committed
GROOVY-9855
1 parent 807f93f commit 7ff9638

File tree

4 files changed

+60
-2
lines changed

4 files changed

+60
-2
lines changed

base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/StaticCompilationTests.java

+23
Original file line numberDiff line numberDiff line change
@@ -5344,6 +5344,29 @@ public void testCompileStatic9799() {
53445344
runConformTest(sources, "works");
53455345
}
53465346

5347+
@Test
5348+
public void testCompileStatic9855() {
5349+
//@formatter:off
5350+
String[] sources = {
5351+
"Main.groovy",
5352+
"@groovy.transform.CompileStatic\n" +
5353+
"@SuppressWarnings(C.PREFIX + 'checked')\n" + // not 'un'.plus('checked')
5354+
"class C {\n" +
5355+
" public static final String PREFIX = 'un'\n" +
5356+
"}\n" +
5357+
"new C()\n",
5358+
};
5359+
//@formatter:on
5360+
5361+
runNegativeTest(sources,
5362+
"----------\n" +
5363+
"1. WARNING in Main.groovy (at line 2)\n" +
5364+
"\t@SuppressWarnings(C.PREFIX + 'checked')\n" +
5365+
"\t ^^^^^^^^^^^^^^^^^^^^\n" +
5366+
"Unnecessary @SuppressWarnings(\"unchecked\")\n" +
5367+
"----------\n");
5368+
}
5369+
53475370
@Test
53485371
public void testCompileStatic9860() {
53495372
//@formatter:off

base/org.codehaus.groovy25/src/org/apache/groovy/ast/tools/ExpressionUtils.java

+12
Original file line numberDiff line numberDiff line change
@@ -350,9 +350,21 @@ public static Expression transformInlineConstants(final Expression exp) {
350350
}
351351
} else if (exp instanceof BinaryExpression) {
352352
BinaryExpression be = (BinaryExpression) exp;
353+
/* GRECLIPSE edit -- GROOVY-9855: inline string concat sooner
353354
be.setLeftExpression(transformInlineConstants(be.getLeftExpression()));
354355
be.setRightExpression(transformInlineConstants(be.getRightExpression()));
355356
return be;
357+
*/
358+
Expression lhs = transformInlineConstants(be.getLeftExpression());
359+
Expression rhs = transformInlineConstants(be.getRightExpression());
360+
if (be.getOperation().getType() == PLUS && lhs instanceof ConstantExpression && rhs instanceof ConstantExpression &&
361+
lhs.getType().equals(ClassHelper.STRING_TYPE) && rhs.getType().equals(ClassHelper.STRING_TYPE)) {
362+
return configure(be, new ConstantExpression(lhs.getText() + rhs.getText()));
363+
}
364+
be.setLeftExpression(lhs);
365+
be.setRightExpression(rhs);
366+
return be;
367+
// GRECLIPSE end
356368
} else if (exp instanceof ListExpression) {
357369
ListExpression origList = (ListExpression) exp;
358370
ListExpression newList = new ListExpression();

base/org.codehaus.groovy30/src/org/apache/groovy/ast/tools/ExpressionUtils.java

+12
Original file line numberDiff line numberDiff line change
@@ -337,9 +337,21 @@ public static Expression transformInlineConstants(final Expression exp) {
337337
}
338338
} else if (exp instanceof BinaryExpression) {
339339
BinaryExpression be = (BinaryExpression) exp;
340+
/* GRECLIPSE edit -- GROOVY-9855: inline string concat sooner
340341
be.setLeftExpression(transformInlineConstants(be.getLeftExpression()));
341342
be.setRightExpression(transformInlineConstants(be.getRightExpression()));
342343
return be;
344+
*/
345+
Expression lhs = transformInlineConstants(be.getLeftExpression());
346+
Expression rhs = transformInlineConstants(be.getRightExpression());
347+
if (be.getOperation().getType() == PLUS && lhs instanceof ConstantExpression && rhs instanceof ConstantExpression &&
348+
lhs.getType().equals(ClassHelper.STRING_TYPE) && rhs.getType().equals(ClassHelper.STRING_TYPE)) {
349+
return configure(be, new ConstantExpression(lhs.getText() + rhs.getText()));
350+
}
351+
be.setLeftExpression(lhs);
352+
be.setRightExpression(rhs);
353+
return be;
354+
// GRECLIPSE end
343355
} else if (exp instanceof ListExpression) {
344356
ListExpression origList = (ListExpression) exp;
345357
ListExpression newList = new ListExpression();

base/org.eclipse.jdt.groovy.core/src/org/codehaus/jdt/groovy/internal/compiler/ast/GroovyCompilationUnitDeclaration.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
import org.codehaus.groovy.syntax.PreciseSyntaxException;
9090
import org.codehaus.groovy.syntax.SyntaxException;
9191
import org.codehaus.groovy.syntax.Token;
92+
import org.codehaus.groovy.syntax.Types;
9293
import org.codehaus.groovy.tools.GroovyClass;
9394
import org.codehaus.jdt.groovy.control.EclipseSourceUnit;
9495
import org.codehaus.jdt.groovy.core.dom.GroovyCompilationUnit;
@@ -1655,8 +1656,18 @@ private org.eclipse.jdt.internal.compiler.ast.Expression createAnnotationMemberE
16551656
return new ClassLiteralAccess(expr.getStart() - 1, new Wildcard(Wildcard.UNBOUND));
16561657

16571658
} else if (expr instanceof BinaryExpression) {
1658-
// annotation may be something like "@Tag(value = List<String)" (incomplete generics specification)
1659-
1659+
BinaryExpression be = (BinaryExpression) expr;
1660+
if (be.getOperation().getType() == Types.PLUS) {
1661+
org.eclipse.jdt.internal.compiler.ast.Expression expression = new org.eclipse.jdt.internal.compiler.ast.BinaryExpression(
1662+
createAnnotationMemberExpression(be.getLeftExpression(), type),
1663+
createAnnotationMemberExpression(be.getRightExpression(), type),
1664+
org.eclipse.jdt.internal.compiler.ast.OperatorIds.PLUS
1665+
);
1666+
expression.sourceStart = expr.getStart();
1667+
expression.sourceEnd = expr.getEnd() - 1;
1668+
return expression;
1669+
}
1670+
// or annotation may be something like "@Tag(value = List<String)" (incomplete generics specification)
16601671
} else {
16611672
org.eclipse.jdt.internal.compiler.ast.Expression expression = createInitializationExpression(expr, type);
16621673
if (expression != null) {

0 commit comments

Comments
 (0)