From 481afeb9149f190c5567e2c4dd41fbae7d03e5ec Mon Sep 17 00:00:00 2001 From: "Natalia.Ukhorskaya" Date: Thu, 1 Nov 2012 15:50:58 +0400 Subject: [PATCH] Use getfield or putfield instructions for private properties instead of invokevirtual getA or setA --- .../jet/codegen/ExpressionCodegen.java | 2 + .../jet/codegen/PropertyCodegen.java | 6 +-- .../jet/lang/resolve/DescriptorUtils.java | 5 ++ .../properties/accessToPrivateProperty.kt | 52 +++++++++++++++++++ .../properties/accessToPrivateSetter.kt | 13 +++++ .../jet/codegen/PropertyGenTest.java | 10 ++++ 6 files changed, 83 insertions(+), 5 deletions(-) create mode 100644 compiler/testData/codegen/properties/accessToPrivateProperty.kt create mode 100644 compiler/testData/codegen/properties/accessToPrivateSetter.kt diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java index a652b640920..0d34f87cffc 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/ExpressionCodegen.java @@ -1537,6 +1537,7 @@ public class ExpressionCodegen extends JetVisitor implem //noinspection ConstantConditions if (isInsideClass && (propertyDescriptor.getGetter() == null || + !DescriptorUtils.isExternallyAccessible(propertyDescriptor) || propertyDescriptor.getGetter().isDefault() && propertyDescriptor.getGetter().getModality() == Modality.FINAL)) { getter = null; } @@ -1573,6 +1574,7 @@ public class ExpressionCodegen extends JetVisitor implem //noinspection ConstantConditions if (!propertyDescriptor.isVar() || isInsideClass && (propertyDescriptor.getSetter() == null || + !DescriptorUtils.isExternallyAccessible(propertyDescriptor) || propertyDescriptor.getSetter().isDefault() && propertyDescriptor.getSetter().getModality() == Modality.FINAL)) { setter = null; diff --git a/compiler/backend/src/org/jetbrains/jet/codegen/PropertyCodegen.java b/compiler/backend/src/org/jetbrains/jet/codegen/PropertyCodegen.java index 944677ad8bf..94ebbedc247 100644 --- a/compiler/backend/src/org/jetbrains/jet/codegen/PropertyCodegen.java +++ b/compiler/backend/src/org/jetbrains/jet/codegen/PropertyCodegen.java @@ -43,6 +43,7 @@ import static org.jetbrains.asm4.Opcodes.*; import static org.jetbrains.jet.codegen.AsmUtil.*; import static org.jetbrains.jet.codegen.CodegenUtil.*; import static org.jetbrains.jet.lang.resolve.BindingContextUtils.descriptorToDeclaration; +import static org.jetbrains.jet.lang.resolve.DescriptorUtils.isExternallyAccessible; import static org.jetbrains.jet.lang.resolve.java.AsmTypeConstants.OBJECT_TYPE; /** @@ -140,11 +141,6 @@ public class PropertyCodegen extends GenerationStateAware { } } - private static boolean isExternallyAccessible(PropertyDescriptor p) { - return p.getVisibility() != Visibilities.PRIVATE || DescriptorUtils.isClassObject(p.getContainingDeclaration()) - || p.getContainingDeclaration() instanceof NamespaceDescriptor; - } - private void generateSetter(JetProperty p, PropertyDescriptor propertyDescriptor) { JetPropertyAccessor setter = p.getSetter(); if (setter != null && setter.getBodyExpression() != null) { diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DescriptorUtils.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DescriptorUtils.java index 9a048b25895..0550a792f63 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DescriptorUtils.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DescriptorUtils.java @@ -386,4 +386,9 @@ public class DescriptorUtils { return receiverParameterDescriptor.getValue(); } + + public static boolean isExternallyAccessible(PropertyDescriptor propertyDescriptor) { + return propertyDescriptor.getVisibility() != Visibilities.PRIVATE || isClassObject(propertyDescriptor.getContainingDeclaration()) + || propertyDescriptor.getContainingDeclaration() instanceof NamespaceDescriptor; + } } diff --git a/compiler/testData/codegen/properties/accessToPrivateProperty.kt b/compiler/testData/codegen/properties/accessToPrivateProperty.kt new file mode 100644 index 00000000000..5d2ef0419a6 --- /dev/null +++ b/compiler/testData/codegen/properties/accessToPrivateProperty.kt @@ -0,0 +1,52 @@ +class A { + private var foo = 1 + get() { + return 1 + } + + fun foo() { + foo = 5 + foo + } +} + +class B { + private val foo = 1 + get + + fun foo() { + foo + } +} + +class C { + private var foo = 1 + get + set + + fun foo() { + foo = 2 + foo + } +} + +class D { + private var foo = 1 + set(i: Int) { + foo = i + 1 + } + + fun foo() { + foo = 5 + foo + } +} + +fun box(): String { + A().foo() + B().foo() + C().foo() + D().foo() + return "OK" +} + diff --git a/compiler/testData/codegen/properties/accessToPrivateSetter.kt b/compiler/testData/codegen/properties/accessToPrivateSetter.kt new file mode 100644 index 00000000000..2e2738f2c84 --- /dev/null +++ b/compiler/testData/codegen/properties/accessToPrivateSetter.kt @@ -0,0 +1,13 @@ +class D { + var foo = 1 + private set + + fun foo() { + foo = 2 + } +} + +fun box(): String { + D().foo() + return "OK" +} diff --git a/compiler/tests/org/jetbrains/jet/codegen/PropertyGenTest.java b/compiler/tests/org/jetbrains/jet/codegen/PropertyGenTest.java index 25b4737f392..9f518a6498f 100644 --- a/compiler/tests/org/jetbrains/jet/codegen/PropertyGenTest.java +++ b/compiler/tests/org/jetbrains/jet/codegen/PropertyGenTest.java @@ -366,4 +366,14 @@ public class PropertyGenTest extends CodegenTestCase { blackBoxFile("properties/kt2892.kt"); } + public void testAccessToPrivateProperty() throws Exception { + createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY); + blackBoxFile("properties/accessToPrivateProperty.kt"); + } + + public void testAccessToPrivateSetter() throws Exception { + createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.JDK_ONLY); + blackBoxFile("properties/accessToPrivateSetter.kt"); + } + }