Generate field for const-val with same visibility as desciptor
Also add test checking that constant static final fields generated for them
This commit is contained in:
@@ -365,6 +365,8 @@ public class AsmUtil {
|
||||
}
|
||||
}
|
||||
|
||||
if (memberDescriptor instanceof PropertyDescriptor && ((PropertyDescriptor) memberDescriptor).isConst()) return null;
|
||||
|
||||
if (containingDeclaration instanceof PackageFragmentDescriptor) {
|
||||
return ACC_PUBLIC;
|
||||
}
|
||||
|
||||
@@ -300,10 +300,11 @@ public class PropertyCodegen {
|
||||
boolean hasPublicFieldAnnotation = AnnotationsPackage.findPublicFieldAnnotation(propertyDescriptor) != null;
|
||||
|
||||
FieldOwnerContext backingFieldContext = context;
|
||||
boolean takeVisibilityFromDescriptor = propertyDescriptor.isLateInit() || propertyDescriptor.isConst();
|
||||
if (AsmUtil.isInstancePropertyWithStaticBackingField(propertyDescriptor) ) {
|
||||
modifiers |= ACC_STATIC;
|
||||
|
||||
if (propertyDescriptor.isLateInit()) {
|
||||
if (takeVisibilityFromDescriptor) {
|
||||
modifiers |= getVisibilityAccessFlag(propertyDescriptor);
|
||||
}
|
||||
else if (hasPublicFieldAnnotation && !isDelegate) {
|
||||
@@ -320,7 +321,7 @@ public class PropertyCodegen {
|
||||
v.getSerializationBindings().put(STATIC_FIELD_IN_OUTER_CLASS, propertyDescriptor);
|
||||
}
|
||||
}
|
||||
else if (propertyDescriptor.isLateInit()) {
|
||||
else if (takeVisibilityFromDescriptor) {
|
||||
modifiers |= getVisibilityAccessFlag(propertyDescriptor);
|
||||
}
|
||||
else if (!isDelegate && hasPublicFieldAnnotation) {
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
@file:JvmName("XYZ")
|
||||
import java.lang.reflect.Modifier
|
||||
|
||||
private const val privateConst: Int = 1
|
||||
public const val publicConst: Int = 3
|
||||
|
||||
public object A {
|
||||
private const val privateConst: Int = 1
|
||||
protected const val protectedConst: Int = 2
|
||||
public const val publicConst: Int = 3
|
||||
}
|
||||
|
||||
public class B {
|
||||
companion object {
|
||||
private const val privateConst: Int = 1
|
||||
protected const val protectedConst: Int = 2
|
||||
public const val publicConst: Int = 3
|
||||
}
|
||||
}
|
||||
|
||||
fun check(clazz: Class<*>, expectProtected: Boolean = true) {
|
||||
val fields = clazz.declaredFields.filter { it.name.contains("Const") }
|
||||
|
||||
assert(fields.all { Modifier.isStatic(it.modifiers) }) { "`$clazz` contains non-static fields" }
|
||||
|
||||
assert(Modifier.isPrivate(fields.single { it.name.contains("private") }.modifiers)) {
|
||||
"`$clazz`.privateConst is not private"
|
||||
}
|
||||
|
||||
assert(Modifier.isPublic(fields.single { it.name.contains("public") }.modifiers)) {
|
||||
"`$clazz`.publicConst is not public"
|
||||
}
|
||||
|
||||
if (expectProtected) {
|
||||
assert(Modifier.isProtected(fields.single { it.name.contains("protected") }.modifiers)) {
|
||||
"`$clazz`.protectedConst is not protected"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun box(): String {
|
||||
check(A::class.java)
|
||||
check(B::class.java)
|
||||
check(Class.forName("XYZ"), false)
|
||||
|
||||
return "OK"
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package test;
|
||||
|
||||
@Ann(ABC.TOP_LEVEL + A.OBJECT + B.COMPANION)
|
||||
class Java {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
@file:JvmName("ABC")
|
||||
package test;
|
||||
|
||||
public const val TOP_LEVEL = "O"
|
||||
|
||||
public object A {
|
||||
public const val OBJECT = "K"
|
||||
}
|
||||
|
||||
public class B {
|
||||
companion object {
|
||||
public const val COMPANION = "56"
|
||||
}
|
||||
}
|
||||
|
||||
annotation class Ann(val value: String)
|
||||
@@ -0,0 +1,26 @@
|
||||
package test
|
||||
|
||||
public const val TOP_LEVEL: kotlin.String
|
||||
|
||||
public object A {
|
||||
private constructor A()
|
||||
public const final val OBJECT: kotlin.String
|
||||
}
|
||||
|
||||
@kotlin.annotation.annotation() public final class Ann : kotlin.Annotation {
|
||||
public constructor Ann(/*0*/ kotlin.String)
|
||||
public final val value: kotlin.String
|
||||
}
|
||||
|
||||
public final class B {
|
||||
public constructor B()
|
||||
|
||||
public companion object Companion {
|
||||
private constructor Companion()
|
||||
public const final val COMPANION: kotlin.String
|
||||
}
|
||||
}
|
||||
|
||||
@test.Ann(value = "OK56") public/*package*/ open class Java {
|
||||
public/*package*/ constructor Java()
|
||||
}
|
||||
+6
@@ -1483,6 +1483,12 @@ public class BlackBoxWithStdlibCodegenTestGenerated extends AbstractBlackBoxCode
|
||||
doTestWithStdlib(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("constFlags.kt")
|
||||
public void testConstFlags() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/fullJdk/constFlags.kt");
|
||||
doTestWithStdlib(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("genericBackingFieldSignature.kt")
|
||||
public void testGenericBackingFieldSignature() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/codegen/boxWithStdlib/fullJdk/genericBackingFieldSignature.kt");
|
||||
|
||||
+6
@@ -466,6 +466,12 @@ public class CompileJavaAgainstKotlinTestGenerated extends AbstractCompileJavaAg
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/compileJavaAgainstKotlin/property"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("ConstVal.kt")
|
||||
public void testConstVal() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/compileJavaAgainstKotlin/property/ConstVal.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("Extensions.kt")
|
||||
public void testExtensions() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/compileJavaAgainstKotlin/property/Extensions.kt");
|
||||
|
||||
Reference in New Issue
Block a user