Implement package protected visibility

This commit is contained in:
Natalia.Ukhorskaya
2013-02-06 14:13:17 +04:00
parent 88e3b9e190
commit 11273f3035
23 changed files with 202 additions and 9 deletions
@@ -70,6 +70,7 @@ public class AsmUtil {
.put(Visibilities.PRIVATE, ACC_PRIVATE)
.put(Visibilities.PROTECTED, ACC_PROTECTED)
.put(JavaDescriptorResolver.PROTECTED_STATIC_VISIBILITY, ACC_PROTECTED)
.put(JavaDescriptorResolver.PROTECTED_AND_PACKAGE, ACC_PROTECTED)
.put(Visibilities.PUBLIC, ACC_PUBLIC)
.put(Visibilities.INTERNAL, ACC_PUBLIC)
.put(Visibilities.LOCAL, NO_FLAG_LOCAL)
@@ -95,7 +95,7 @@ public final class DescriptorResolverUtils {
if (modifierListOwner.hasModifierProperty(PsiModifier.STATIC)) {
return JavaDescriptorResolver.PROTECTED_STATIC_VISIBILITY;
}
return Visibilities.PROTECTED;
return JavaDescriptorResolver.PROTECTED_AND_PACKAGE;
}
return JavaDescriptorResolver.PACKAGE_VISIBILITY;
}
@@ -121,7 +121,7 @@ public final class DescriptorResolverUtils {
public static Visibility getConstructorVisibility(ClassDescriptor classDescriptor) {
Visibility containingClassVisibility = classDescriptor.getVisibility();
if (containingClassVisibility == JavaDescriptorResolver.PROTECTED_STATIC_VISIBILITY) {
return Visibilities.PROTECTED;
return JavaDescriptorResolver.PROTECTED_AND_PACKAGE;
}
return containingClassVisibility;
}
@@ -41,10 +41,7 @@ public class JavaDescriptorResolver implements DependencyClassByQualifiedNameRes
public static Visibility PACKAGE_VISIBILITY = new Visibility("package", false) {
@Override
protected boolean isVisible(@NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) {
NamespaceDescriptor parentPackage = DescriptorUtils.getParentOfType(what, NamespaceDescriptor.class);
NamespaceDescriptor fromPackage = DescriptorUtils.getParentOfType(from, NamespaceDescriptor.class, false);
assert parentPackage != null;
return parentPackage.equals(fromPackage);
return DescriptorUtils.isInSameNamespace(what, from);
}
@Override
@@ -84,6 +81,34 @@ public class JavaDescriptorResolver implements DependencyClassByQualifiedNameRes
}
};
public static final Visibility PROTECTED_AND_PACKAGE = new Visibility("protected_and_package", false) {
@Override
protected boolean isVisible(@NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) {
if (DescriptorUtils.isInSameNamespace(what, from)) {
return true;
}
ClassDescriptor whatClass = DescriptorUtils.getParentOfType(what, ClassDescriptor.class, false);
if (whatClass == null) return false;
ClassDescriptor fromClass = DescriptorUtils.getParentOfType(from, ClassDescriptor.class, false);
if (fromClass == null) return false;
if (DescriptorUtils.isSubclass(fromClass, whatClass)) {
return true;
}
return isVisible(what, fromClass.getContainingDeclaration());
}
@Override
protected Integer compareTo(@NotNull Visibility visibility) {
if (this == visibility) return 0;
if (visibility == Visibilities.INTERNAL) return null;
if (visibility == Visibilities.PRIVATE) return 1;
return -1;
}
};
private JavaPropertyResolver propertiesResolver;
private JavaClassResolver classResolver;
private JavaConstructorResolver constructorResolver;
@@ -135,6 +135,12 @@ public class DescriptorUtils {
return descriptor.getContainingDeclaration() instanceof NamespaceDescriptor;
}
public static boolean isInSameNamespace(@NotNull DeclarationDescriptor first, @NotNull DeclarationDescriptor second) {
NamespaceDescriptor whatPackage = DescriptorUtils.getParentOfType(first, NamespaceDescriptor.class, false);
NamespaceDescriptor fromPackage = DescriptorUtils.getParentOfType(second, NamespaceDescriptor.class, false);
return fromPackage != null && whatPackage != null && whatPackage.equals(fromPackage);
}
@Nullable
public static DeclarationDescriptor findTopLevelParent(@NotNull DeclarationDescriptor declarationDescriptor) {
DeclarationDescriptor descriptor = declarationDescriptor;
@@ -317,6 +317,10 @@ public class DescriptorRendererImpl implements DescriptorRenderer {
else if ("protected_static".equals(visibility.toString())) {
builder.append("protected/*protected static*/ ");
}
else if ("protected_and_package".equals(visibility.toString())) {
builder.append("protected/*protected and package*/ ");
}
else{
builder.append(renderKeyword(visibility.toString())).append(" ");
}
@@ -0,0 +1,9 @@
package protectedPack;
import java.lang.String;
public class overrideProtectedFunInPackage {
protected String foo() {
return "fail";
}
}
@@ -0,0 +1,17 @@
package protectedPackKotlin
import protectedPack.overrideProtectedFunInPackage
class Derived: overrideProtectedFunInPackage() {
protected override fun foo(): String? {
return "OK"
}
fun test(): String {
return foo()!!
}
}
fun box(): String {
return Derived().test()
}
@@ -0,0 +1,9 @@
package protectedPack;
import java.lang.String;
public class protectedFunInPackage {
protected String foo() {
return "OK";
}
}
@@ -0,0 +1,5 @@
package protectedPack
fun box(): String {
return protectedFunInPackage().foo()!!
}
@@ -0,0 +1,7 @@
package protectedPack;
import java.lang.String;
public class protectedPropertyInPackage {
protected String foo = "OK";
}
@@ -0,0 +1,5 @@
package protectedPack
fun box(): String {
return protectedPropertyInPackage().foo!!
}
@@ -0,0 +1,11 @@
package protectedPack;
import java.lang.String;
public class protectedStaticClass {
protected static class Inner {
public String foo() {
return "OK";
}
}
}
@@ -0,0 +1,11 @@
package protectedPack
class Derived(): protectedStaticClass() {
fun test(): String {
return protectedStaticClass.Inner().foo()!!
}
}
fun box(): String {
return Derived().test()
}
@@ -2,5 +2,5 @@ package test
public open class JFrame : awt.Frame {
public constructor JFrame()
protected final var accessibleContext : jet.String?
protected/*protected and package*/ final var accessibleContext : jet.String?
}
@@ -0,0 +1,5 @@
package test;
public class ProtectedPackageConstructor {
protected static class Foo { }
}
@@ -0,0 +1,9 @@
package test
public open class ProtectedPackageConstructor : java.lang.Object {
public constructor ProtectedPackageConstructor()
protected/*protected static*/ open class Foo : java.lang.Object {
protected/*protected and package*/ constructor Foo()
}
}
@@ -0,0 +1,5 @@
package test;
public class ProtectedPackageFun {
protected void foo() {}
}
@@ -0,0 +1,6 @@
package test
public open class ProtectedPackageFun : java.lang.Object {
public constructor ProtectedPackageFun()
protected/*protected and package*/ open fun foo() : Unit
}
@@ -0,0 +1,5 @@
package test;
public class ProtectedPackageProperty {
protected int foo = 1;
}
@@ -0,0 +1,6 @@
package test
public open class ProtectedPackageProperty : java.lang.Object {
public constructor ProtectedPackageProperty()
protected/*protected and package*/ final var foo : jet.Int
}
@@ -4,6 +4,6 @@ public open class ConstructorInProtectedStaticNestedClass : java.lang.Object {
public constructor ConstructorInProtectedStaticNestedClass()
protected/*protected static*/ open class Inner : java.lang.Object {
protected constructor Inner()
protected/*protected and package*/ constructor Inner()
}
}
@@ -30,7 +30,7 @@ import org.jetbrains.jet.codegen.AbstractJavaVisibilityTest;
/** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */
@SuppressWarnings("all")
@TestMetadata("compiler/testData/codegen/visibility")
@InnerTestClasses({JavaVisibilityTestGenerated.Package.class})
@InnerTestClasses({JavaVisibilityTestGenerated.Package.class, JavaVisibilityTestGenerated.Protected_and_package.class})
public class JavaVisibilityTestGenerated extends AbstractJavaVisibilityTest {
public void testAllFilesPresentInVisibility() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/codegen/visibility"), "kt", true);
@@ -59,10 +59,39 @@ public class JavaVisibilityTestGenerated extends AbstractJavaVisibilityTest {
}
@TestMetadata("compiler/testData/codegen/visibility/protected_and_package")
public static class Protected_and_package extends AbstractJavaVisibilityTest {
public void testAllFilesPresentInProtected_and_package() throws Exception {
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), "org.jetbrains.jet.generators.tests.GenerateTests", new File("compiler/testData/codegen/visibility/protected_and_package"), "kt", true);
}
@TestMetadata("overrideProtectedFunInPackage.kt")
public void testOverrideProtectedFunInPackage() throws Exception {
blackBoxFileWithJavaByFullPath("compiler/testData/codegen/visibility/protected_and_package/overrideProtectedFunInPackage.kt");
}
@TestMetadata("protectedFunInPackage.kt")
public void testProtectedFunInPackage() throws Exception {
blackBoxFileWithJavaByFullPath("compiler/testData/codegen/visibility/protected_and_package/protectedFunInPackage.kt");
}
@TestMetadata("protectedPropertyInPackage.kt")
public void testProtectedPropertyInPackage() throws Exception {
blackBoxFileWithJavaByFullPath("compiler/testData/codegen/visibility/protected_and_package/protectedPropertyInPackage.kt");
}
@TestMetadata("protectedStaticClass.kt")
public void testProtectedStaticClass() throws Exception {
blackBoxFileWithJavaByFullPath("compiler/testData/codegen/visibility/protected_and_package/protectedStaticClass.kt");
}
}
public static Test suite() {
TestSuite suite = new TestSuite("JavaVisibilityTestGenerated");
suite.addTestSuite(JavaVisibilityTestGenerated.class);
suite.addTestSuite(Package.class);
suite.addTestSuite(Protected_and_package.class);
return suite;
}
}
@@ -100,6 +100,24 @@ public final class LoadJavaCustomTest extends KotlinTestWithEnvironment {
dir + "ConstructorInProtectedStaticNestedClass.java");
}
public void testProtectedPackageFun() throws Exception {
String dir = PATH + "/protectedPackageVisibility/";
doTest(dir + "ProtectedPackageFun.txt",
dir + "ProtectedPackageFun.java");
}
public void testProtectedPackageConstructor() throws Exception {
String dir = PATH + "/protectedPackageVisibility/";
doTest(dir + "ProtectedPackageConstructor.txt",
dir + "ProtectedPackageConstructor.java");
}
public void testProtectedPackageProperty() throws Exception {
String dir = PATH + "/protectedPackageVisibility/";
doTest(dir + "ProtectedPackageProperty.txt",
dir + "ProtectedPackageProperty.java");
}
public void testStaticFinal() throws Exception {
String dir = "/staticFinal/";
doTest(PATH + dir + "expected.txt",