From 9affbb63d108f907bf673d1c6795c9f0fe4a0734 Mon Sep 17 00:00:00 2001 From: peter Date: Tue, 6 Mar 2012 16:23:17 +0400 Subject: [PATCH] IDEA-79920 Compile-time constants defined in Groovy have default values in the Java code which uses them --- .../convertToJava/StubGenerator.java | 13 +++++++++++-- .../groovy/compiler/GroovyCompilerTest.groovy | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/convertToJava/StubGenerator.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/convertToJava/StubGenerator.java index 2f0ee33e22ab..a0d21ba0581b 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/convertToJava/StubGenerator.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/refactoring/convertToJava/StubGenerator.java @@ -41,6 +41,7 @@ import org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrRe import org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement; import org.jetbrains.plugins.groovy.lang.psi.impl.PsiImplUtil; import org.jetbrains.plugins.groovy.lang.psi.impl.statements.expressions.TypesUtil; +import org.jetbrains.plugins.groovy.lang.psi.util.GroovyConstantExpressionEvaluator; import java.util.*; @@ -365,12 +366,20 @@ public class StubGenerator implements ClassItemGenerator { //type PsiType declaredType = typeElement == null ? PsiType.getJavaLangObject(variable.getManager(), variable.getResolveScope()) : typeElement.getType(); - final String initializer = GroovyToJavaGenerator.getDefaultValueText(declaredType.getCanonicalText()); writeType(text, declaredType, variableDeclaration, classNameProvider); - text.append(' ').append(name).append(" = ").append(initializer); + text.append(' ').append(name).append(" = ").append(getVariableInitializer(variable, declaredType)); text.append(";\n"); } } + private static String getVariableInitializer(GrVariable variable, PsiType declaredType) { + if (declaredType instanceof PsiPrimitiveType) { + Object eval = GroovyConstantExpressionEvaluator.evaluate(variable.getInitializerGroovy()); + if (eval instanceof Number || eval instanceof Boolean) { + return eval.toString(); + } + } + return GroovyToJavaGenerator.getDefaultValueText(declaredType.getCanonicalText()); + } } diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/compiler/GroovyCompilerTest.groovy b/plugins/groovy/test/org/jetbrains/plugins/groovy/compiler/GroovyCompilerTest.groovy index f2dcfbf91b6a..3aa64fada2ba 100644 --- a/plugins/groovy/test/org/jetbrains/plugins/groovy/compiler/GroovyCompilerTest.groovy +++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/compiler/GroovyCompilerTest.groovy @@ -561,6 +561,25 @@ class Main { assertEmpty make() } + public void testCompileTimeConstants() { + myFixture.addFileToProject 'Gr.groovy', ''' +interface Gr { + String HELLO = "Hello" + int MAGIC = 239 + Boolean BOOL = true + boolean bool = true +}''' + myFixture.addFileToProject 'Main.java', ''' +public class Main { + public static void main(String[] args) { + System.out.println(Gr.HELLO + ", " + Gr.BOOL + Gr.bool + Gr.MAGIC); + } +} +''' + make() + assertOutput 'Main', 'Hello, truetrue239' + } + public static class IdeaModeTest extends GroovyCompilerTest { @Override protected boolean useJps() { false } }