mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-79920 Compile-time constants defined in Groovy have default values in the Java code which uses them
This commit is contained in:
+11
-2
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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 }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user