Skip to content

Commit

Permalink
GROOVY-9855
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Dec 22, 2020
1 parent 807f93f commit 7ff9638
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5344,6 +5344,29 @@ public void testCompileStatic9799() {
runConformTest(sources, "works");
}

@Test
public void testCompileStatic9855() {
//@formatter:off
String[] sources = {
"Main.groovy",
"@groovy.transform.CompileStatic\n" +
"@SuppressWarnings(C.PREFIX + 'checked')\n" + // not 'un'.plus('checked')
"class C {\n" +
" public static final String PREFIX = 'un'\n" +
"}\n" +
"new C()\n",
};
//@formatter:on

runNegativeTest(sources,
"----------\n" +
"1. WARNING in Main.groovy (at line 2)\n" +
"\t@SuppressWarnings(C.PREFIX + 'checked')\n" +
"\t ^^^^^^^^^^^^^^^^^^^^\n" +
"Unnecessary @SuppressWarnings(\"unchecked\")\n" +
"----------\n");
}

@Test
public void testCompileStatic9860() {
//@formatter:off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,21 @@ public static Expression transformInlineConstants(final Expression exp) {
}
} else if (exp instanceof BinaryExpression) {
BinaryExpression be = (BinaryExpression) exp;
/* GRECLIPSE edit -- GROOVY-9855: inline string concat sooner
be.setLeftExpression(transformInlineConstants(be.getLeftExpression()));
be.setRightExpression(transformInlineConstants(be.getRightExpression()));
return be;
*/
Expression lhs = transformInlineConstants(be.getLeftExpression());
Expression rhs = transformInlineConstants(be.getRightExpression());
if (be.getOperation().getType() == PLUS && lhs instanceof ConstantExpression && rhs instanceof ConstantExpression &&
lhs.getType().equals(ClassHelper.STRING_TYPE) && rhs.getType().equals(ClassHelper.STRING_TYPE)) {
return configure(be, new ConstantExpression(lhs.getText() + rhs.getText()));
}
be.setLeftExpression(lhs);
be.setRightExpression(rhs);
return be;
// GRECLIPSE end
} else if (exp instanceof ListExpression) {
ListExpression origList = (ListExpression) exp;
ListExpression newList = new ListExpression();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,21 @@ public static Expression transformInlineConstants(final Expression exp) {
}
} else if (exp instanceof BinaryExpression) {
BinaryExpression be = (BinaryExpression) exp;
/* GRECLIPSE edit -- GROOVY-9855: inline string concat sooner
be.setLeftExpression(transformInlineConstants(be.getLeftExpression()));
be.setRightExpression(transformInlineConstants(be.getRightExpression()));
return be;
*/
Expression lhs = transformInlineConstants(be.getLeftExpression());
Expression rhs = transformInlineConstants(be.getRightExpression());
if (be.getOperation().getType() == PLUS && lhs instanceof ConstantExpression && rhs instanceof ConstantExpression &&
lhs.getType().equals(ClassHelper.STRING_TYPE) && rhs.getType().equals(ClassHelper.STRING_TYPE)) {
return configure(be, new ConstantExpression(lhs.getText() + rhs.getText()));
}
be.setLeftExpression(lhs);
be.setRightExpression(rhs);
return be;
// GRECLIPSE end
} else if (exp instanceof ListExpression) {
ListExpression origList = (ListExpression) exp;
ListExpression newList = new ListExpression();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
import org.codehaus.groovy.syntax.PreciseSyntaxException;
import org.codehaus.groovy.syntax.SyntaxException;
import org.codehaus.groovy.syntax.Token;
import org.codehaus.groovy.syntax.Types;
import org.codehaus.groovy.tools.GroovyClass;
import org.codehaus.jdt.groovy.control.EclipseSourceUnit;
import org.codehaus.jdt.groovy.core.dom.GroovyCompilationUnit;
Expand Down Expand Up @@ -1655,8 +1656,18 @@ private org.eclipse.jdt.internal.compiler.ast.Expression createAnnotationMemberE
return new ClassLiteralAccess(expr.getStart() - 1, new Wildcard(Wildcard.UNBOUND));

} else if (expr instanceof BinaryExpression) {
// annotation may be something like "@Tag(value = List<String)" (incomplete generics specification)

BinaryExpression be = (BinaryExpression) expr;
if (be.getOperation().getType() == Types.PLUS) {
org.eclipse.jdt.internal.compiler.ast.Expression expression = new org.eclipse.jdt.internal.compiler.ast.BinaryExpression(
createAnnotationMemberExpression(be.getLeftExpression(), type),
createAnnotationMemberExpression(be.getRightExpression(), type),
org.eclipse.jdt.internal.compiler.ast.OperatorIds.PLUS
);
expression.sourceStart = expr.getStart();
expression.sourceEnd = expr.getEnd() - 1;
return expression;
}
// or annotation may be something like "@Tag(value = List<String)" (incomplete generics specification)
} else {
org.eclipse.jdt.internal.compiler.ast.Expression expression = createInitializationExpression(expr, type);
if (expression != null) {
Expand Down

0 comments on commit 7ff9638

Please sign in to comment.