Report warning on 'Singleton.javaClass'

As a temporary measure before 'javaClass' property is deprecated and removed.
Getting the Java class of a singleton this way is confusing and most of the
time is a failed attempt to get the Java class of a class with a companion
object (e.g. 'Int.javaClass')
This commit is contained in:
Alexander Udalov
2015-08-31 16:01:46 +03:00
parent 3f1bf46756
commit f7a178f978
7 changed files with 159 additions and 1 deletions
@@ -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.resolve.jvm.calls.checkers
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.resolve.calls.checkers.CallChecker
import org.jetbrains.kotlin.resolve.calls.context.BasicCallResolutionContext
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm
import org.jetbrains.kotlin.types.JetTypeImpl
import org.jetbrains.kotlin.types.TypeProjectionImpl
public class JavaClassOnCompanionChecker : CallChecker {
override fun <F : CallableDescriptor> check(resolvedCall: ResolvedCall<F>, context: BasicCallResolutionContext) {
val descriptor = resolvedCall.resultingDescriptor
if (descriptor !is PropertyDescriptor || descriptor.name.asString() != "javaClass") return
val container = descriptor.containingDeclaration
if (container !is PackageFragmentDescriptor || container.fqName.asString() != "kotlin") return
val actualType = descriptor.type
val companionObject = actualType.arguments.singleOrNull()?.type?.constructor?.declarationDescriptor as? ClassDescriptor ?: return
if (companionObject.isCompanionObject) {
val containingClass = companionObject.containingDeclaration as ClassDescriptor
val javaLangClass = actualType.constructor.declarationDescriptor as? ClassDescriptor ?: return
val expectedType = JetTypeImpl.create(
Annotations.EMPTY, javaLangClass, actualType.isMarkedNullable,
listOf(TypeProjectionImpl(containingClass.defaultType))
)
context.trace.report(ErrorsJvm.JAVA_CLASS_ON_COMPANION.on(resolvedCall.call.callElement, actualType, expectedType))
}
}
}
@@ -75,6 +75,12 @@ public class DefaultErrorMessagesJvm implements DefaultErrorMessages.Extension {
MAP.put(ErrorsJvm.WHEN_ENUM_CAN_BE_NULL_IN_JAVA, "Enum argument can be null in Java, but exhaustive when contains no null branch");
MAP.put(ErrorsJvm.INAPPLICABLE_PUBLIC_FIELD, "publicField annotation is not applicable to this declaration");
MAP.put(ErrorsJvm.JAVA_CLASS_ON_COMPANION,
"The resulting type of this ''javaClass'' call is {0} and not {1}. " +
"Please use the more clear ''::class.java'' syntax to avoid confusion",
Renderers.RENDER_TYPE, Renderers.RENDER_TYPE
);
}
@NotNull
@@ -27,6 +27,7 @@ import org.jetbrains.kotlin.psi.JetAnnotationEntry;
import org.jetbrains.kotlin.psi.JetDeclaration;
import org.jetbrains.kotlin.psi.JetElement;
import org.jetbrains.kotlin.psi.JetExpression;
import org.jetbrains.kotlin.types.JetType;
import static org.jetbrains.kotlin.diagnostics.PositioningStrategies.*;
import static org.jetbrains.kotlin.diagnostics.Severity.ERROR;
@@ -65,6 +66,8 @@ public interface ErrorsJvm {
DiagnosticFactory0<JetElement> NO_REFLECTION_IN_CLASS_PATH = DiagnosticFactory0.create(WARNING);
DiagnosticFactory2<JetElement, JetType, JetType> JAVA_CLASS_ON_COMPANION = DiagnosticFactory2.create(WARNING);
enum NullabilityInformationSource {
KOTLIN {
@NotNull
@@ -50,6 +50,7 @@ import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory
import org.jetbrains.kotlin.resolve.calls.smartcasts.Nullability
import org.jetbrains.kotlin.resolve.descriptorUtil.getAnnotationRetention
import org.jetbrains.kotlin.resolve.descriptorUtil.isRepeatableAnnotation
import org.jetbrains.kotlin.resolve.jvm.calls.checkers.JavaClassOnCompanionChecker
import org.jetbrains.kotlin.resolve.jvm.calls.checkers.NeedSyntheticChecker
import org.jetbrains.kotlin.resolve.jvm.calls.checkers.ReflectionAPICallChecker
import org.jetbrains.kotlin.resolve.jvm.calls.checkers.TraitDefaultMethodCallChecker
@@ -76,7 +77,8 @@ public object JvmPlatformConfigurator : PlatformConfigurator(
NeedSyntheticChecker(),
JavaAnnotationCallChecker(),
JavaAnnotationMethodCallChecker(),
TraitDefaultMethodCallChecker()
TraitDefaultMethodCallChecker(),
JavaClassOnCompanionChecker()
),
additionalTypeCheckers = listOf(