Show warning on usages of javaClass<T>() in annotations loaded from Java

This commit is contained in:
Denis Zharkov
2015-04-17 17:34:44 +03:00
parent 482b4e3688
commit f53baebf89
13 changed files with 272 additions and 13 deletions
@@ -16,15 +16,23 @@
package org.jetbrains.kotlin.load.kotlin
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor
import org.jetbrains.kotlin.diagnostics.DiagnosticFactory0
import org.jetbrains.kotlin.load.java.JvmAnnotationNames
import org.jetbrains.kotlin.load.java.descriptors.JavaConstructorDescriptor
import org.jetbrains.kotlin.psi.JetExpression
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.calls.checkers.CallChecker
import org.jetbrains.kotlin.resolve.calls.context.BasicCallResolutionContext
import org.jetbrains.kotlin.resolve.calls.model.ExpressionValueArgument
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.ResolvedValueArgument
import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm
import org.jetbrains.kotlin.types.JetType
public class JavaAnnotationCallChecker : CallChecker {
override fun <F : CallableDescriptor?> check(resolvedCall: ResolvedCall<F>, context: BasicCallResolutionContext) {
@@ -32,14 +40,56 @@ public class JavaAnnotationCallChecker : CallChecker {
if (resultingDescriptor !is JavaConstructorDescriptor ||
resultingDescriptor.getContainingDeclaration().getKind() != ClassKind.ANNOTATION_CLASS) return
reportErrorsOnPositionedArguments(resolvedCall, context)
reportWarningOnJavaClassUsages(resolvedCall, context)
}
private fun reportWarningOnJavaClassUsages(
resolvedCall: ResolvedCall<*>,
context: BasicCallResolutionContext
) {
resolvedCall.getValueArguments().filter { it.getKey().getType().isJavaLangClassOrArray() }.forEach {
reportOnValueArgument(context, it, ErrorsJvm.JAVA_LANG_CLASS_ARGUMENT_IN_ANNOTATION)
}
}
private fun JetType.isJavaLangClassOrArray() = isJavaLangClass() ||
(KotlinBuiltIns.isArray(this) && getArguments().first().getType().isJavaLangClass())
private fun JetType.isJavaLangClass(): Boolean {
val classifier = getConstructor().getDeclarationDescriptor()
if (classifier !is ClassDescriptor) return false
return DescriptorUtils.isJavaLangClass(classifier)
}
private fun reportErrorsOnPositionedArguments(
resolvedCall: ResolvedCall<*>,
context: BasicCallResolutionContext
) {
resolvedCall.getValueArguments().filter {
p -> p.key.getName() != JvmAnnotationNames.DEFAULT_ANNOTATION_MEMBER_NAME &&
p.value is ExpressionValueArgument &&
!((p.value as ExpressionValueArgument).getValueArgument()?.isNamed() ?: true)
p ->
p.key.getName() != JvmAnnotationNames.DEFAULT_ANNOTATION_MEMBER_NAME &&
p.value is ExpressionValueArgument &&
!((p.value as ExpressionValueArgument).getValueArgument()?.isNamed() ?: true)
}.forEach {
context.trace.report(
ErrorsJvm.POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION.on(
it.getValue().getArguments().first().getArgumentExpression()))
reportOnValueArgument(context, it, ErrorsJvm.POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION)
}
}
private fun reportOnValueArgument(
context: BasicCallResolutionContext,
argument: Map.Entry<ValueParameterDescriptor, ResolvedValueArgument>,
diagnostic: DiagnosticFactory0<JetExpression>
) {
argument.getValue().getArguments().forEach {
if (it.getArgumentExpression() != null) {
context.trace.report(
diagnostic.on(
it.getArgumentExpression()
)
)
}
}
}
}
@@ -57,6 +57,7 @@ public class DefaultErrorMessagesJvm implements DefaultErrorMessages.Extension {
MAP.put(ErrorsJvm.NATIVE_DECLARATION_CANNOT_BE_INLINED, "Members of traits can not be inlined");
MAP.put(ErrorsJvm.POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION, "Only named arguments are available for Java annotations");
MAP.put(ErrorsJvm.JAVA_LANG_CLASS_ARGUMENT_IN_ANNOTATION, "Usage of `javaClass<T>()` in annotations is deprecated. Use T::class instead");
MAP.put(ErrorsJvm.NO_REFLECTION_IN_CLASS_PATH, "Expression ''{0}'' uses reflection which is not found in compilation classpath. Make sure you have kotlin-reflect.jar in the classpath", Renderers.ELEMENT_TEXT);
@@ -52,6 +52,7 @@ public interface ErrorsJvm {
DiagnosticFactory0<JetDeclaration> NATIVE_DECLARATION_CANNOT_BE_INLINED = DiagnosticFactory0.create(ERROR, DECLARATION_SIGNATURE);
DiagnosticFactory0<JetExpression> POSITIONED_VALUE_ARGUMENT_FOR_JAVA_ANNOTATION = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<JetExpression> JAVA_LANG_CLASS_ARGUMENT_IN_ANNOTATION = DiagnosticFactory0.create(WARNING);
// TODO: make this a warning
DiagnosticFactory1<JetExpression, JetExpression> NO_REFLECTION_IN_CLASS_PATH = DiagnosticFactory1.create(ERROR);