diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatformConfigurator.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatformConfigurator.kt index f38f932c1a9..f16a2e5f9f2 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatformConfigurator.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/resolve/jvm/platform/JvmPlatformConfigurator.kt @@ -183,7 +183,7 @@ public class OverloadsAnnotationChecker: DeclarationChecker { if (descriptor is FunctionDescriptor && descriptor.getModality() == Modality.ABSTRACT) { diagnosticHolder.report(ErrorsJvm.OVERLOADS_ABSTRACT.on(declaration)) } - else if ((!descriptor.getVisibility().isPublicAPI() && descriptor.getVisibility() != Visibilities.INTERNAL) || + else if ((!descriptor.getVisibility().isPublicAPI && descriptor.getVisibility() != Visibilities.INTERNAL) || DescriptorUtils.isLocal(descriptor)) { diagnosticHolder.report(ErrorsJvm.OVERLOADS_PRIVATE.on(declaration)) } diff --git a/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/SamAdapterFunctionsScope.kt b/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/SamAdapterFunctionsScope.kt index 923cd63e1ec..76dd5a3fea7 100644 --- a/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/SamAdapterFunctionsScope.kt +++ b/compiler/frontend.java/src/org/jetbrains/kotlin/synthetic/SamAdapterFunctionsScope.kt @@ -27,6 +27,7 @@ import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.descriptorUtil.parentsWithSelf import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter import org.jetbrains.kotlin.resolve.scopes.JetScope +import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue import org.jetbrains.kotlin.storage.StorageManager import org.jetbrains.kotlin.types.DescriptorSubstitutor import org.jetbrains.kotlin.types.JetType @@ -45,7 +46,6 @@ class SamAdapterFunctionsScope(storageManager: StorageManager) : JetScope by Jet } private fun extensionForFunctionNotCached(function: FunctionDescriptor): FunctionDescriptor? { - //TODO! if (function.visibility == Visibilities.PRIVATE || function.visibility == Visibilities.PRIVATE_TO_THIS || function.visibility == Visibilities.INVISIBLE_FAKE) return null if (!function.hasJavaOriginInHierarchy()) return null //TODO: should we go into base at all? if (!SingleAbstractMethodUtils.isSamAdapterNecessary(function)) return null @@ -110,7 +110,27 @@ class SamAdapterFunctionsScope(storageManager: StorageManager) : JetScope by Jet val returnType = typeSubstitutor.safeSubstitute(originalFunction.returnType!!, Variance.OUT_VARIANCE) val receiverType = typeSubstitutor.safeSubstitute(ownerClass.defaultType, Variance.INVARIANT) val valueParameters = SingleAbstractMethodUtils.createValueParametersForSamAdapter(originalFunction, this, typeSubstitutor) - initialize(receiverType, null, typeParameters, valueParameters, returnType, Modality.FINAL, Visibilities.PUBLIC) + + val originalVisibility = originalFunction.visibility + val visibility = when (originalVisibility) { + Visibilities.PUBLIC -> Visibilities.PUBLIC + + else -> object : Visibility(originalVisibility.name, originalVisibility.isPublicAPI) { + override fun isVisible(receiver: ReceiverValue, what: DeclarationDescriptorWithVisibility, from: DeclarationDescriptor) + = originalVisibility.isVisible(receiver, originalFunction, from) + + override fun mustCheckInImports() + = throw UnsupportedOperationException("Should never be called for this visibility") + + override fun normalize() + = originalVisibility.normalize() + + override val displayName: String + get() = originalVisibility.displayName + " for synthetic extension" + } + } + + initialize(receiverType, null, typeParameters, valueParameters, returnType, Modality.FINAL, visibility) } override fun hasStableParameterNames() = originalFunction.hasStableParameterNames() diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.java index f2935f17087..c015f356694 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DeclarationsChecker.java @@ -339,7 +339,7 @@ public class DeclarationsChecker { JetFunction function = (JetFunction) member; hasDeferredType = function.getTypeReference() == null && function.hasBody() && !function.hasBlockBody(); } - if ((memberDescriptor.getVisibility().isPublicAPI()) && memberDescriptor.getOverriddenDescriptors().size() == 0 && hasDeferredType) { + if ((memberDescriptor.getVisibility().getIsPublicAPI()) && memberDescriptor.getOverriddenDescriptors().size() == 0 && hasDeferredType) { trace.report(PUBLIC_MEMBER_SHOULD_SPECIFY_TYPE.on(member)); } } diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DescriptorResolver.java b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DescriptorResolver.java index 6c08ca5cfe6..783ebc719b8 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/DescriptorResolver.java +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/DescriptorResolver.java @@ -27,10 +27,7 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns; import org.jetbrains.kotlin.descriptors.*; import org.jetbrains.kotlin.descriptors.annotations.Annotations; import org.jetbrains.kotlin.descriptors.impl.*; -import org.jetbrains.kotlin.diagnostics.DiagnosticFactory1; import org.jetbrains.kotlin.diagnostics.Errors; -import org.jetbrains.kotlin.lexer.JetKeywordToken; -import org.jetbrains.kotlin.lexer.JetModifierKeywordToken; import org.jetbrains.kotlin.lexer.JetTokens; import org.jetbrains.kotlin.name.FqName; import org.jetbrains.kotlin.name.Name; @@ -896,7 +893,7 @@ public class DescriptorResolver { boolean definedInClass = DescriptorUtils.getParentOfType(descriptor, ClassDescriptor.class) != null; boolean isLocal = DescriptorUtils.isLocal(descriptor); Visibility visibility = descriptor.getVisibility(); - boolean transformNeeded = !isLocal && !visibility.isPublicAPI() + boolean transformNeeded = !isLocal && !visibility.getIsPublicAPI() && !(definedInClass && Visibilities.isPrivate(visibility)); if (transformNeeded) { if (type.getConstructor().getSupertypes().size() == 1) { diff --git a/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/PackageLocal.kt b/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/PackageLocal.kt new file mode 100644 index 00000000000..7dfd4ec5669 --- /dev/null +++ b/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/PackageLocal.kt @@ -0,0 +1,13 @@ +// FILE: KotlinFile.kt +package k + +import JavaClass + +fun foo(javaClass: JavaClass) { + javaClass.doSomething { } +} + +// FILE: JavaClass.java +public class JavaClass { + void doSomething(Runnable runnable) { runnable.run(); } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/PackageLocal.txt b/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/PackageLocal.txt new file mode 100644 index 00000000000..c00a47cba5e --- /dev/null +++ b/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/PackageLocal.txt @@ -0,0 +1,13 @@ +package + +public open class JavaClass { + public constructor JavaClass() + public/*package*/ open fun doSomething(/*0*/ runnable: java.lang.Runnable!): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +package k { + internal fun foo(/*0*/ javaClass: JavaClass): kotlin.Unit +} diff --git a/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/Private.kt b/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/Private.kt new file mode 100644 index 00000000000..3ba01af925b --- /dev/null +++ b/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/Private.kt @@ -0,0 +1,13 @@ +// FILE: KotlinFile.kt +package k + +import JavaClass + +fun foo(javaClass: JavaClass) { + javaClass.doSomething { } +} + +// FILE: JavaClass.java +public class JavaClass { + private void doSomething(Runnable runnable) { runnable.run(); } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/Private.txt b/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/Private.txt new file mode 100644 index 00000000000..a4c3da552e9 --- /dev/null +++ b/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/Private.txt @@ -0,0 +1,13 @@ +package + +public open class JavaClass { + public constructor JavaClass() + private open fun doSomething(/*0*/ runnable: java.lang.Runnable!): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +package k { + internal fun foo(/*0*/ javaClass: JavaClass): kotlin.Unit +} diff --git a/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/Protected.kt b/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/Protected.kt new file mode 100644 index 00000000000..eb50ccfc8bc --- /dev/null +++ b/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/Protected.kt @@ -0,0 +1,24 @@ +// FILE: KotlinFile.kt +package k + +import JavaClass + +fun foo(javaClass: JavaClass) { + javaClass.doSomething { + bar() + } +} + +class X : JavaClass() { + fun foo(other: JavaClass) { + doSomething { bar() } + other.doSomething { bar() } // currently not flagged as error - see KT-8654 + } +} + +fun bar(){} + +// FILE: JavaClass.java +public class JavaClass { + protected void doSomething(Runnable runnable) { runnable.run(); } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/Protected.txt b/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/Protected.txt new file mode 100644 index 00000000000..d1bbf3a712c --- /dev/null +++ b/compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/Protected.txt @@ -0,0 +1,23 @@ +package + +public open class JavaClass { + public constructor JavaClass() + protected/*protected and package*/ open fun doSomething(/*0*/ runnable: java.lang.Runnable!): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String +} + +package k { + internal fun bar(): kotlin.Unit + internal fun foo(/*0*/ javaClass: JavaClass): kotlin.Unit + + internal final class X : JavaClass { + public constructor X() + protected/*protected and package*/ open override /*1*/ /*fake_override*/ fun doSomething(/*0*/ runnable: java.lang.Runnable!): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean + internal final fun foo(/*0*/ other: JavaClass): kotlin.Unit + public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int + public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String + } +} diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java index 4f2af6ddc20..eeacd4450d6 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/checkers/JetDiagnosticsTestGenerated.java @@ -14435,6 +14435,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { doTest(fileName); } + @TestMetadata("PackageLocal.kt") + public void testPackageLocal() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/PackageLocal.kt"); + doTest(fileName); + } + @TestMetadata("ParameterTypeAnnotation.kt") public void testParameterTypeAnnotation() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/ParameterTypeAnnotation.kt"); @@ -14447,6 +14453,18 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest { doTest(fileName); } + @TestMetadata("Private.kt") + public void testPrivate() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/Private.kt"); + doTest(fileName); + } + + @TestMetadata("Protected.kt") + public void testProtected() throws Exception { + String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/Protected.kt"); + doTest(fileName); + } + @TestMetadata("ReturnTypeAnnotation.kt") public void testReturnTypeAnnotation() throws Exception { String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/syntheticExtensions/samAdapters/ReturnTypeAnnotation.kt"); diff --git a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/JavaVisibilities.java b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/JavaVisibilities.java index 9d36a960676..61485c90cef 100644 --- a/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/JavaVisibilities.java +++ b/core/descriptor.loader.java/src/org/jetbrains/kotlin/load/java/JavaVisibilities.java @@ -27,7 +27,7 @@ public class JavaVisibilities { public static final Visibility PACKAGE_VISIBILITY = new Visibility("package", false) { @Override - protected boolean isVisible(@NotNull ReceiverValue receiver, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) { + public boolean isVisible(@NotNull ReceiverValue receiver, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) { return areInSamePackage(what, from); } @@ -43,8 +43,9 @@ public class JavaVisibilities { return -1; } + @NotNull @Override - public String toString() { + public String getDisplayName() { return "public/*package*/"; } @@ -57,7 +58,7 @@ public class JavaVisibilities { public static final Visibility PROTECTED_STATIC_VISIBILITY = new Visibility("protected_static", true) { @Override - protected boolean isVisible(@NotNull ReceiverValue receiver, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) { + public boolean isVisible(@NotNull ReceiverValue receiver, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) { if (areInSamePackage(what, from)) { return true; } @@ -80,8 +81,9 @@ public class JavaVisibilities { return false; } + @NotNull @Override - public String toString() { + public String getDisplayName() { return "protected/*protected static*/"; } @@ -94,7 +96,7 @@ public class JavaVisibilities { public static final Visibility PROTECTED_AND_PACKAGE = new Visibility("protected_and_package", true) { @Override - protected boolean isVisible(@NotNull ReceiverValue receiver, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) { + public boolean isVisible(@NotNull ReceiverValue receiver, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) { if (areInSamePackage(what, from)) { return true; } @@ -124,8 +126,9 @@ public class JavaVisibilities { return -1; } + @NotNull @Override - public String toString() { + public String getDisplayName() { return "protected/*protected and package*/"; } diff --git a/core/descriptors/src/org/jetbrains/kotlin/descriptors/Visibilities.java b/core/descriptors/src/org/jetbrains/kotlin/descriptors/Visibilities.java index 6a7c8c9c183..9671a506e3b 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/descriptors/Visibilities.java +++ b/core/descriptors/src/org/jetbrains/kotlin/descriptors/Visibilities.java @@ -36,7 +36,7 @@ public class Visibilities { } @Override - protected boolean isVisible(@NotNull ReceiverValue receiver, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) { + public boolean isVisible(@NotNull ReceiverValue receiver, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) { DeclarationDescriptor parent = what; while (parent != null) { parent = parent.getContainingDeclaration(); @@ -66,7 +66,7 @@ public class Visibilities { public static final Visibility PRIVATE_TO_THIS = new Visibility("private_to_this", false) { @Override - protected boolean isVisible(@NotNull ReceiverValue thisObject, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) { + public boolean isVisible(@NotNull ReceiverValue thisObject, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) { if (PRIVATE.isVisible(thisObject, what, from)) { DeclarationDescriptor classDescriptor = DescriptorUtils.getParentOfType(what, ClassDescriptor.class); @@ -82,8 +82,9 @@ public class Visibilities { return true; } + @NotNull @Override - public String toString() { + public String getDisplayName() { return "private/*private to this*/"; } }; @@ -95,7 +96,7 @@ public class Visibilities { } @Override - protected boolean isVisible(@NotNull ReceiverValue receiver, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) { + public boolean isVisible(@NotNull ReceiverValue receiver, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) { ClassDescriptor classDescriptor = DescriptorUtils.getParentOfType(what, ClassDescriptor.class); if (DescriptorUtils.isCompanionObject(classDescriptor)) { classDescriptor = DescriptorUtils.getParentOfType(classDescriptor, ClassDescriptor.class); @@ -118,7 +119,7 @@ public class Visibilities { } @Override - protected boolean isVisible(@NotNull ReceiverValue receiver, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) { + public boolean isVisible(@NotNull ReceiverValue receiver, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) { //NOTE: supposedly temporarily return PUBLIC.isVisible(receiver, what, from); } @@ -131,7 +132,7 @@ public class Visibilities { } @Override - protected boolean isVisible(@NotNull ReceiverValue receiver, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) { + public boolean isVisible(@NotNull ReceiverValue receiver, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) { return true; } }; @@ -143,7 +144,7 @@ public class Visibilities { } @Override - protected boolean isVisible(@NotNull ReceiverValue receiver, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) { + public boolean isVisible(@NotNull ReceiverValue receiver, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) { throw new IllegalStateException("This method shouldn't be invoked for LOCAL visibility"); } }; @@ -155,7 +156,7 @@ public class Visibilities { } @Override - protected boolean isVisible(@NotNull ReceiverValue receiver, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) { + public boolean isVisible(@NotNull ReceiverValue receiver, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) { throw new IllegalStateException("Visibility is unknown yet"); //This method shouldn't be invoked for INHERITED visibility } }; @@ -168,7 +169,7 @@ public class Visibilities { } @Override - protected boolean isVisible(@NotNull ReceiverValue receiver, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) { + public boolean isVisible(@NotNull ReceiverValue receiver, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from) { return false; } }; @@ -182,7 +183,7 @@ public class Visibilities { } @Override - protected boolean isVisible( + public boolean isVisible( @NotNull ReceiverValue receiver, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from ) { return false; diff --git a/core/descriptors/src/org/jetbrains/kotlin/descriptors/Visibility.java b/core/descriptors/src/org/jetbrains/kotlin/descriptors/Visibility.java deleted file mode 100644 index 50e2fecd0dc..00000000000 --- a/core/descriptors/src/org/jetbrains/kotlin/descriptors/Visibility.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright 2010-2015 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.jetbrains.kotlin.descriptors; - -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue; - -public abstract class Visibility { - private final boolean isPublicAPI; - private final String name; - - protected Visibility(@NotNull String name, boolean isPublicAPI) { - this.isPublicAPI = isPublicAPI; - this.name = name; - } - - public boolean isPublicAPI() { - return isPublicAPI; - } - - /** - * True, if it makes sense to check this visibility in imports and not import inaccessible declarations with such visibility. - * Hint: return true, if this visibility can be checked on file's level. - * Examples: - * it returns false for PROTECTED because protected members of classes can be imported to be used in subclasses of their containers, - * so when we are looking at the import, we don't know whether it is legal somewhere in this file or not. - * it returns true for INTERNAL, because an internal declaration is either visible everywhere in a file, or invisible everywhere in the same file. - * it returns true for PRIVATE, because there's no point in importing privates: they are inaccessible unless their short name is - * already available without an import - */ - public abstract boolean mustCheckInImports(); - - /** - * @return null if the answer is unknown - */ - protected Integer compareTo(@NotNull Visibility visibility) { - return Visibilities.compareLocal(this, visibility); - } - - @Override - public String toString() { - return name; - } - - @NotNull - public Visibility normalize() { - return this; - } - - protected abstract boolean isVisible(@NotNull ReceiverValue receiver, @NotNull DeclarationDescriptorWithVisibility what, @NotNull DeclarationDescriptor from); -} diff --git a/core/descriptors/src/org/jetbrains/kotlin/descriptors/Visibility.kt b/core/descriptors/src/org/jetbrains/kotlin/descriptors/Visibility.kt new file mode 100644 index 00000000000..4db61bd1756 --- /dev/null +++ b/core/descriptors/src/org/jetbrains/kotlin/descriptors/Visibility.kt @@ -0,0 +1,52 @@ +/* + * Copyright 2010-2015 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.descriptors + +import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue + +public abstract class Visibility protected constructor( + public val name: String, + public val isPublicAPI: Boolean +) { + public abstract fun isVisible(receiver: ReceiverValue, what: DeclarationDescriptorWithVisibility, from: DeclarationDescriptor): Boolean + + /** + * True, if it makes sense to check this visibility in imports and not import inaccessible declarations with such visibility. + * Hint: return true, if this visibility can be checked on file's level. + * Examples: + * it returns false for PROTECTED because protected members of classes can be imported to be used in subclasses of their containers, + * so when we are looking at the import, we don't know whether it is legal somewhere in this file or not. + * it returns true for INTERNAL, because an internal declaration is either visible everywhere in a file, or invisible everywhere in the same file. + * it returns true for PRIVATE, because there's no point in importing privates: they are inaccessible unless their short name is + * already available without an import + */ + public abstract fun mustCheckInImports(): Boolean + + /** + * @return null if the answer is unknown + */ + protected open fun compareTo(visibility: Visibility): Int? { + return Visibilities.compareLocal(this, visibility) + } + + public open val displayName: String + get() = name + + override final fun toString() = displayName + + public open fun normalize(): Visibility = this +} diff --git a/core/descriptors/src/org/jetbrains/kotlin/renderer/DescriptorRendererImpl.kt b/core/descriptors/src/org/jetbrains/kotlin/renderer/DescriptorRendererImpl.kt index d010d877312..d32eb48eb93 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/renderer/DescriptorRendererImpl.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/renderer/DescriptorRendererImpl.kt @@ -397,7 +397,7 @@ internal class DescriptorRendererImpl( visibility = visibility.normalize() } if (!showInternalKeyword && visibility == Visibilities.INTERNAL) return - builder.append(renderKeyword(visibility.toString())).append(" ") + builder.append(renderKeyword(visibility.displayName)).append(" ") } private fun renderModality(modality: Modality, builder: StringBuilder) { diff --git a/core/descriptors/src/org/jetbrains/kotlin/resolve/DescriptorUtils.kt b/core/descriptors/src/org/jetbrains/kotlin/resolve/DescriptorUtils.kt index 2dc41418ac1..3a1d3ca8c33 100644 --- a/core/descriptors/src/org/jetbrains/kotlin/resolve/DescriptorUtils.kt +++ b/core/descriptors/src/org/jetbrains/kotlin/resolve/DescriptorUtils.kt @@ -93,7 +93,7 @@ public val DeclarationDescriptorWithVisibility.isEffectivelyPublicApi: Boolean var parent: DeclarationDescriptorWithVisibility? = this while (parent != null) { - if (!parent.getVisibility().isPublicAPI()) return false + if (!parent.getVisibility().isPublicAPI) return false parent = DescriptorUtils.getParentOfType(parent, javaClass()) } diff --git a/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt b/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt index c45c637b2e6..b1659bf1266 100644 --- a/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt +++ b/idea/src/org/jetbrains/kotlin/idea/intentions/Utils.kt @@ -16,14 +16,12 @@ package org.jetbrains.kotlin.idea.intentions -import com.intellij.openapi.util.TextRange import com.intellij.psi.tree.IElementType import org.jetbrains.kotlin.JetNodeTypes import org.jetbrains.kotlin.descriptors.CallableDescriptor import org.jetbrains.kotlin.descriptors.DeclarationDescriptorWithVisibility import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor import org.jetbrains.kotlin.idea.caches.resolve.analyze -import org.jetbrains.kotlin.idea.references.JetReference import org.jetbrains.kotlin.idea.references.mainReference import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers import org.jetbrains.kotlin.idea.util.ShortenReferences @@ -76,7 +74,7 @@ fun JetCallableDeclaration.canRemoveTypeSpecificationByVisibility(): Boolean { if (isOverride) return true val descriptor = analyze()[BindingContext.DECLARATION_TO_DESCRIPTOR, this] - return descriptor !is DeclarationDescriptorWithVisibility || !descriptor.getVisibility().isPublicAPI() + return descriptor !is DeclarationDescriptorWithVisibility || !descriptor.getVisibility().isPublicAPI } // returns assignment which replaces initializer diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/ManglingUtils.java b/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/ManglingUtils.java index 49a0193ebeb..b4a1469861f 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/ManglingUtils.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/utils/ManglingUtils.java @@ -82,7 +82,7 @@ public class ManglingUtils { DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration(); if (containingDeclaration instanceof PackageFragmentDescriptor) { - return descriptor.getVisibility().isPublicAPI(); + return descriptor.getVisibility().getIsPublicAPI(); } else if (containingDeclaration instanceof ClassDescriptor) { ClassDescriptor classDescriptor = (ClassDescriptor) containingDeclaration; @@ -98,7 +98,7 @@ public class ManglingUtils { } // Don't use stable mangling when it inside a non-public API declaration. - if (!classDescriptor.getVisibility().isPublicAPI()) { + if (!classDescriptor.getVisibility().getIsPublicAPI()) { return false; }