Load Java declarations which reference FunctionN as Deprecated.Error

#KT-25855 Fixed
This commit is contained in:
Denis Zharkov
2018-08-29 15:01:15 +03:00
parent bcf1b9c804
commit 24a905293f
16 changed files with 196 additions and 22 deletions
@@ -32,7 +32,7 @@ object JavaToKotlinClassMap : PlatformToKotlinClassMap {
FunctionClassDescriptor.Kind.KSuspendFunction.packageFqName.toString() + "." + FunctionClassDescriptor.Kind.KSuspendFunction.classNamePrefix
private val FUNCTION_N_CLASS_ID = ClassId.topLevel(FqName("kotlin.jvm.functions.FunctionN"))
private val FUNCTION_N_FQ_NAME = FUNCTION_N_CLASS_ID.asSingleFqName()
val FUNCTION_N_FQ_NAME = FUNCTION_N_CLASS_ID.asSingleFqName()
private val K_FUNCTION_CLASS_ID = ClassId.topLevel(FqName("kotlin.reflect.KFunction"))
private val javaToKotlin = HashMap<FqNameUnsafe, ClassId>()
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.load.java.descriptors;
import kotlin.Pair;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor;
@@ -28,6 +29,7 @@ public interface JavaCallableMemberDescriptor extends CallableMemberDescriptor {
JavaCallableMemberDescriptor enhance(
@Nullable KotlinType enhancedReceiverType,
@NotNull List<ValueParameterData> enhancedValueParametersData,
@NotNull KotlinType enhancedReturnType
@NotNull KotlinType enhancedReturnType,
@Nullable Pair<UserDataKey<?>, ?> additionalUserData
);
}
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.load.java.descriptors;
import kotlin.Pair;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.descriptors.*;
@@ -119,8 +120,10 @@ public class JavaClassConstructorDescriptor extends ClassConstructorDescriptorIm
public JavaClassConstructorDescriptor enhance(
@Nullable KotlinType enhancedReceiverType,
@NotNull List<ValueParameterData> enhancedValueParametersData,
@NotNull KotlinType enhancedReturnType
@NotNull KotlinType enhancedReturnType,
@Nullable Pair<UserDataKey<?>, ?> additionalUserData
) {
JavaClassConstructorDescriptor enhanced = createSubstitutedCopy(
getContainingDeclaration(), /* original = */ null, getKind(), null, getAnnotations(), getSource());
ReceiverParameterDescriptor enhancedReceiver =
@@ -138,6 +141,10 @@ public class JavaClassConstructorDescriptor extends ClassConstructorDescriptorIm
getVisibility()
);
if (additionalUserData != null) {
enhanced.putInUserDataMap(additionalUserData.getFirst(), additionalUserData.getSecond());
}
return enhanced;
}
}
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.load.java.descriptors;
import kotlin.Pair;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.descriptors.*;
@@ -144,7 +145,8 @@ public class JavaMethodDescriptor extends SimpleFunctionDescriptorImpl implement
public JavaMethodDescriptor enhance(
@Nullable KotlinType enhancedReceiverType,
@NotNull List<ValueParameterData> enhancedValueParametersData,
@NotNull KotlinType enhancedReturnType
@NotNull KotlinType enhancedReturnType,
@Nullable Pair<UserDataKey<?>, ?> additionalUserData
) {
List<ValueParameterDescriptor> enhancedValueParameters =
UtilKt.copyValueParameters(enhancedValueParametersData, getValueParameters(), this);
@@ -164,6 +166,11 @@ public class JavaMethodDescriptor extends SimpleFunctionDescriptorImpl implement
.build();
assert enhancedMethod != null : "null after substitution while enhancing " + toString();
if (additionalUserData != null) {
enhancedMethod.putInUserDataMap(additionalUserData.getFirst(), additionalUserData.getSecond());
}
return enhancedMethod;
}
}
@@ -16,6 +16,7 @@
package org.jetbrains.kotlin.load.java.descriptors;
import kotlin.Pair;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
@@ -33,6 +34,8 @@ import java.util.List;
public class JavaPropertyDescriptor extends PropertyDescriptorImpl implements JavaCallableMemberDescriptor {
private final boolean isStaticFinal;
@Nullable
private final Pair<UserDataKey<?>, ?> singleUserData;
private JavaPropertyDescriptor(
@NotNull DeclarationDescriptor containingDeclaration,
@@ -44,12 +47,14 @@ public class JavaPropertyDescriptor extends PropertyDescriptorImpl implements Ja
@NotNull SourceElement source,
@Nullable PropertyDescriptor original,
@NotNull Kind kind,
boolean isStaticFinal
boolean isStaticFinal,
@Nullable Pair<UserDataKey<?>, ?> singleUserData
) {
super(containingDeclaration, original, annotations, modality, visibility, isVar, name, kind, source,
false, false, false, false, false, false);
this.isStaticFinal = isStaticFinal;
this.singleUserData = singleUserData;
}
@NotNull
@@ -64,8 +69,8 @@ public class JavaPropertyDescriptor extends PropertyDescriptorImpl implements Ja
boolean isStaticFinal
) {
return new JavaPropertyDescriptor(
containingDeclaration, annotations, modality, visibility, isVar, name, source, null, Kind.DECLARATION, isStaticFinal
);
containingDeclaration, annotations, modality, visibility, isVar, name, source, null, Kind.DECLARATION, isStaticFinal,
null);
}
@NotNull
@@ -80,8 +85,8 @@ public class JavaPropertyDescriptor extends PropertyDescriptorImpl implements Ja
) {
return new JavaPropertyDescriptor(
newOwner, getAnnotations(), newModality, newVisibility, isVar(), newName, SourceElement.NO_SOURCE, original,
kind, isStaticFinal
);
kind, isStaticFinal,
singleUserData);
}
@Override
@@ -94,7 +99,8 @@ public class JavaPropertyDescriptor extends PropertyDescriptorImpl implements Ja
public JavaCallableMemberDescriptor enhance(
@Nullable KotlinType enhancedReceiverType,
@NotNull List<ValueParameterData> enhancedValueParametersData,
@NotNull KotlinType enhancedReturnType
@NotNull KotlinType enhancedReturnType,
@Nullable Pair<UserDataKey<?>, ?> additionalUserData
) {
JavaPropertyDescriptor enhanced = new JavaPropertyDescriptor(
getContainingDeclaration(),
@@ -104,10 +110,10 @@ public class JavaPropertyDescriptor extends PropertyDescriptorImpl implements Ja
isVar(),
getName(),
getSource(),
getOriginal(),
getOriginal() == this ? null : getOriginal(),
getKind(),
isStaticFinal
);
isStaticFinal,
additionalUserData);
PropertyGetterDescriptorImpl newGetter = null;
PropertyGetterDescriptorImpl getter = getGetter();
@@ -159,4 +165,15 @@ public class JavaPropertyDescriptor extends PropertyDescriptorImpl implements Ja
return isStaticFinal && ConstUtil.canBeUsedForConstVal(type) &&
(!TypeEnhancementKt.hasEnhancedNullability(type) || KotlinBuiltIns.isString(type));
}
@Nullable
@Override
public <V> V getUserData(UserDataKey<V> key) {
if (singleUserData != null && singleUserData.getFirst().equals(key)) {
//noinspection unchecked
return (V) singleUserData.getSecond();
}
return null;
}
}
@@ -30,8 +30,10 @@ import org.jetbrains.kotlin.load.java.lazy.descriptors.isJavaField
import org.jetbrains.kotlin.load.kotlin.SignatureBuildingComponents
import org.jetbrains.kotlin.load.kotlin.computeJvmDescriptor
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.resolve.DEPRECATED_FUNCTION_KEY
import org.jetbrains.kotlin.resolve.constants.EnumValue
import org.jetbrains.kotlin.resolve.descriptorUtil.firstArgument
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
import org.jetbrains.kotlin.types.typeUtil.isTypeParameter
@@ -160,7 +162,7 @@ class SignatureEnhancement(
val hasDefaultValue = p.hasDefaultValueInAnnotation(actualType)
val wereChanges = enhancementResult.wereChanges || (hasDefaultValue != p.declaresDefaultValue())
ValueParameterEnhancementResult(enhancementResult.type, hasDefaultValue, wereChanges)
ValueParameterEnhancementResult(enhancementResult.type, hasDefaultValue, wereChanges, enhancementResult.containsFunctionN)
}
val returnTypeEnhancement =
@@ -174,14 +176,24 @@ class SignatureEnhancement(
AnnotationTypeQualifierResolver.QualifierApplicabilityType.METHOD_RETURN_TYPE
) { it.returnType!! }.enhance(predefinedEnhancementInfo?.returnTypeInfo)
val containsFunctionN = receiverTypeEnhancement?.containsFunctionN == true || returnTypeEnhancement.containsFunctionN ||
valueParameterEnhancements.any { it.containsFunctionN }
if ((receiverTypeEnhancement?.wereChanges == true)
|| returnTypeEnhancement.wereChanges || valueParameterEnhancements.any { it.wereChanges }
|| returnTypeEnhancement.wereChanges || valueParameterEnhancements.any { it.wereChanges } || containsFunctionN
) {
val additionalUserData =
if (containsFunctionN)
DEPRECATED_FUNCTION_KEY to DeprecationCausedByFunctionN(this)
else
null
@Suppress("UNCHECKED_CAST")
return this.enhance(
receiverTypeEnhancement?.type,
valueParameterEnhancements.map { ValueParameterData(it.type, it.hasDefaultValue) },
returnTypeEnhancement.type
returnTypeEnhancement.type,
additionalUserData
) as D
}
@@ -218,9 +230,16 @@ class SignatureEnhancement(
}
}
val containsFunctionN = TypeUtils.contains(fromOverride) {
val classifier = it.constructor.declarationDescriptor ?: return@contains false
classifier.name == JavaToKotlinClassMap.FUNCTION_N_FQ_NAME.shortName() &&
classifier.fqNameOrNull() == JavaToKotlinClassMap.FUNCTION_N_FQ_NAME
}
return fromOverride.enhance(qualifiersWithPredefined ?: qualifiers)?.let { enhanced ->
PartEnhancementResult(enhanced, wereChanges = true)
} ?: PartEnhancementResult(fromOverride, wereChanges = false)
PartEnhancementResult(enhanced, wereChanges = true, containsFunctionN = containsFunctionN)
} ?: PartEnhancementResult(fromOverride, wereChanges = false, containsFunctionN = containsFunctionN)
}
private fun KotlinType.extractQualifiers(): JavaTypeQualifiers {
@@ -397,12 +416,18 @@ class SignatureEnhancement(
}
private open class PartEnhancementResult(val type: KotlinType, val wereChanges: Boolean)
private open class PartEnhancementResult(
val type: KotlinType,
val wereChanges: Boolean,
val containsFunctionN: Boolean
)
private class ValueParameterEnhancementResult(
type: KotlinType,
val hasDefaultValue: Boolean,
wereChanges: Boolean
) : PartEnhancementResult(type, wereChanges)
wereChanges: Boolean,
containsFunctionN: Boolean
) : PartEnhancementResult(type, wereChanges, containsFunctionN)
private fun CallableMemberDescriptor.partsForValueParameter(
// TODO: investigate if it's really can be a null (check properties' with extension overrides in Java)
@@ -17,10 +17,14 @@
package org.jetbrains.kotlin.load.java
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.Deprecation
import org.jetbrains.kotlin.resolve.DeprecationLevelValue
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.typeUtil.makeNotNullable
import org.jetbrains.kotlin.utils.extractRadix
@@ -60,4 +64,11 @@ fun KotlinType.lexicalCastFrom(value: String): JavaDefaultValue? {
}
return if (result != null) Constant(result) else null
}
}
class DeprecationCausedByFunctionN(override val target: DeclarationDescriptor) : Deprecation {
override val deprecationLevel: DeprecationLevelValue
get() = DeprecationLevelValue.ERROR
override val message: String?
get() = "Java members containing references to ${JavaToKotlinClassMap.FUNCTION_N_FQ_NAME} are not supported"
}
@@ -5,6 +5,7 @@
package org.jetbrains.kotlin.resolve
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
interface Deprecation {
@@ -18,3 +19,4 @@ enum class DeprecationLevelValue {
WARNING, ERROR, HIDDEN
}
val DEPRECATED_FUNCTION_KEY = object : CallableDescriptor.UserDataKey<Deprecation> {}