From 155cbbfc02edd71639bf6bfecc0a13ed660f105d Mon Sep 17 00:00:00 2001 From: Natalia Ukhorskaya Date: Thu, 21 Nov 2013 18:26:08 +0400 Subject: [PATCH] Add evaluator for java property initializer --- .../resolver/TraceBasedJavaResolverCache.java | 8 +++--- ...NegativePropertyAsAnnotationParameter.java | 8 ++++++ ...vaNegativePropertyAsAnnotationParameter.kt | 26 +++++++++++++++++++ .../BlackBoxWithJavaCodegenTestGenerated.java | 5 ++++ 4 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 compiler/testData/codegen/boxWithJava/annotations/javaNegativePropertyAsAnnotationParameter.java create mode 100644 compiler/testData/codegen/boxWithJava/annotations/javaNegativePropertyAsAnnotationParameter.kt diff --git a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/resolver/TraceBasedJavaResolverCache.java b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/resolver/TraceBasedJavaResolverCache.java index 6e0a6347d34..32f5c9a4366 100644 --- a/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/resolver/TraceBasedJavaResolverCache.java +++ b/compiler/frontend.java/src/org/jetbrains/jet/lang/resolve/java/resolver/TraceBasedJavaResolverCache.java @@ -20,7 +20,7 @@ import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.diagnostic.Logger; import com.intellij.psi.PsiExpression; import com.intellij.psi.PsiField; -import com.intellij.psi.PsiLiteralExpression; +import com.intellij.psi.impl.JavaConstantExpressionEvaluator; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.jet.lang.descriptors.*; @@ -116,9 +116,9 @@ public class TraceBasedJavaResolverCache implements JavaResolverCache { if (AnnotationUtils.isPropertyCompileTimeConstant(descriptor)) { PsiExpression initializer = psiField.getInitializer(); - if (initializer instanceof PsiLiteralExpression) { - CompileTimeConstant constant = JavaAnnotationArgumentResolver - .resolveCompileTimeConstantValue(((PsiLiteralExpression) initializer).getValue(), descriptor.getType()); + Object evaluatedExpression = JavaConstantExpressionEvaluator.computeConstantExpression(initializer, false); + if (evaluatedExpression != null) { + CompileTimeConstant constant = JavaAnnotationArgumentResolver.resolveCompileTimeConstantValue(evaluatedExpression, descriptor.getType()); if (constant != null) { trace.record(COMPILE_TIME_INITIALIZER, descriptor, constant); } diff --git a/compiler/testData/codegen/boxWithJava/annotations/javaNegativePropertyAsAnnotationParameter.java b/compiler/testData/codegen/boxWithJava/annotations/javaNegativePropertyAsAnnotationParameter.java new file mode 100644 index 00000000000..bf4066634b1 --- /dev/null +++ b/compiler/testData/codegen/boxWithJava/annotations/javaNegativePropertyAsAnnotationParameter.java @@ -0,0 +1,8 @@ +class Foo { + public static final int i = -2; + public static final short s = -2; + public static final float f = -2f; + public static final double d = -2.0; + public static final long l = -2L; + public static final byte b = -2; +} \ No newline at end of file diff --git a/compiler/testData/codegen/boxWithJava/annotations/javaNegativePropertyAsAnnotationParameter.kt b/compiler/testData/codegen/boxWithJava/annotations/javaNegativePropertyAsAnnotationParameter.kt new file mode 100644 index 00000000000..2e0af867684 --- /dev/null +++ b/compiler/testData/codegen/boxWithJava/annotations/javaNegativePropertyAsAnnotationParameter.kt @@ -0,0 +1,26 @@ +import java.lang.annotation.Retention +import java.lang.annotation.RetentionPolicy + +Ann(Foo.i, Foo.s, Foo.f, Foo.d, Foo.l, Foo.b) class MyClass + +fun box(): String { + val ann = javaClass().getAnnotation(javaClass()) + if (ann == null) return "fail: cannot find Ann on MyClass}" + if (ann.i != -2) return "fail: annotation parameter i should be -2, but was ${ann.i}" + if (ann.s != -2.toShort()) return "fail: annotation parameter i should be -2, but was ${ann.i}" + if (ann.f != -2.toFloat()) return "fail: annotation parameter i should be -2, but was ${ann.i}" + if (ann.d != -2.toDouble()) return "fail: annotation parameter i should be -2, but was ${ann.i}" + if (ann.l != -2.toLong()) return "fail: annotation parameter i should be -2, but was ${ann.i}" + if (ann.b != -2.toByte()) return "fail: annotation parameter i should be -2, but was ${ann.i}" + return "OK" +} + +Retention(RetentionPolicy.RUNTIME) +annotation class Ann( + val i: Int, + val s: Short, + val f: Float, + val d: Double, + val l: Long, + val b: Byte +) \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxWithJavaCodegenTestGenerated.java b/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxWithJavaCodegenTestGenerated.java index 0448e11e665..96f5bac029d 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxWithJavaCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/codegen/generated/BlackBoxWithJavaCodegenTestGenerated.java @@ -43,6 +43,11 @@ public class BlackBoxWithJavaCodegenTestGenerated extends AbstractBlackBoxCodege JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/codegen/boxWithJava/annotations"), Pattern.compile("^(.+)\\.kt$"), true); } + @TestMetadata("javaNegativePropertyAsAnnotationParameter.kt") + public void testJavaNegativePropertyAsAnnotationParameter() throws Exception { + doTestWithJava("compiler/testData/codegen/boxWithJava/annotations/javaNegativePropertyAsAnnotationParameter.kt"); + } + @TestMetadata("javaPropertyAsAnnotationParameter.kt") public void testJavaPropertyAsAnnotationParameter() throws Exception { doTestWithJava("compiler/testData/codegen/boxWithJava/annotations/javaPropertyAsAnnotationParameter.kt");