Correct visibility for non-public SAM-adapters

This commit is contained in:
Valentin Kipyatkov
2015-07-29 17:58:42 +03:00
parent 2eb04db95a
commit d5f95cf126
19 changed files with 219 additions and 96 deletions
@@ -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))
}
@@ -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()
@@ -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));
}
}
@@ -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) {
@@ -0,0 +1,13 @@
// FILE: KotlinFile.kt
package k
import JavaClass
fun foo(javaClass: JavaClass) {
javaClass.<!INVISIBLE_MEMBER!>doSomething<!> <!TYPE_MISMATCH!>{ }<!>
}
// FILE: JavaClass.java
public class JavaClass {
void doSomething(Runnable runnable) { runnable.run(); }
}
@@ -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
}
@@ -0,0 +1,13 @@
// FILE: KotlinFile.kt
package k
import JavaClass
fun foo(javaClass: JavaClass) {
javaClass.<!INVISIBLE_MEMBER!>doSomething<!> <!TYPE_MISMATCH!>{ }<!>
}
// FILE: JavaClass.java
public class JavaClass {
private void doSomething(Runnable runnable) { runnable.run(); }
}
@@ -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
}
@@ -0,0 +1,24 @@
// FILE: KotlinFile.kt
package k
import JavaClass
fun foo(javaClass: JavaClass) {
javaClass.<!INVISIBLE_MEMBER!>doSomething<!> <!TYPE_MISMATCH!>{
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(); }
}
@@ -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
}
}
@@ -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");
@@ -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*/";
}
@@ -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;
@@ -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);
}
@@ -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
}
@@ -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) {
@@ -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<DeclarationDescriptorWithVisibility>())
}
@@ -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
@@ -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;
}