Implement Java 9 module visibility checks
In this commit, only IDE tests are added, because we look for module declarations in the IDE across the whole project, whereas in the compiler we should do this on the module path only and that requires separate work (KT-18599) which is done in the following commits. (The change in Cache.kt is needed so that JvmModuleAccessibilityChecker.ClassifierUsage, which is an inner class, would be injected properly.) #KT-18598 In Progress #KT-18599 In Progress
This commit is contained in:
+134
@@ -0,0 +1,134 @@
|
||||
/*
|
||||
* Copyright 2010-2017 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.checkers
|
||||
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.config.LanguageVersionSettings
|
||||
import org.jetbrains.kotlin.descriptors.*
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.load.java.sources.JavaSourceElement
|
||||
import org.jetbrains.kotlin.load.java.structure.impl.VirtualFileBoundJavaClass
|
||||
import org.jetbrains.kotlin.load.kotlin.KotlinJvmBinaryPackageSourceElement
|
||||
import org.jetbrains.kotlin.load.kotlin.KotlinJvmBinarySourceElement
|
||||
import org.jetbrains.kotlin.load.kotlin.VirtualFileKotlinClass
|
||||
import org.jetbrains.kotlin.resolve.BindingTrace
|
||||
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.resolve.calls.checkers.CallChecker
|
||||
import org.jetbrains.kotlin.resolve.calls.checkers.CallCheckerContext
|
||||
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
|
||||
import org.jetbrains.kotlin.resolve.checkers.ClassifierUsageChecker
|
||||
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
|
||||
import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm
|
||||
import org.jetbrains.kotlin.resolve.jvm.modules.JavaModuleResolver
|
||||
import org.jetbrains.kotlin.resolve.source.getPsi
|
||||
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedMemberDescriptor
|
||||
|
||||
class JvmModuleAccessibilityChecker(project: Project) : CallChecker {
|
||||
private val moduleResolver = JavaModuleResolver.getInstance(project)
|
||||
|
||||
override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) {
|
||||
val descriptor = resolvedCall.resultingDescriptor
|
||||
|
||||
// javac seems to check only the containing class of the member being called. Note that it's fine to call, for example,
|
||||
// members with parameter types or return type from an unexported package
|
||||
val targetDescriptor = DescriptorUtils.getParentOfType(descriptor, ClassOrPackageFragmentDescriptor::class.java) ?: return
|
||||
|
||||
val fileFromOurModule = DescriptorToSourceUtils.getContainingFile(context.scope.ownerDescriptor)?.virtualFile
|
||||
diagnosticFor(targetDescriptor, descriptor, fileFromOurModule, reportOn)?.let(context.trace::report)
|
||||
}
|
||||
|
||||
private fun diagnosticFor(
|
||||
targetClassOrPackage: ClassOrPackageFragmentDescriptor,
|
||||
originalDescriptor: DeclarationDescriptorWithSource?,
|
||||
fileFromOurModule: VirtualFile?,
|
||||
reportOn: PsiElement
|
||||
): Diagnostic? {
|
||||
val referencedFile = findVirtualFile(targetClassOrPackage, originalDescriptor) ?: return null
|
||||
|
||||
// TODO: do not resolve our JavaModule every time, invent a way to obtain it from ModuleDescriptor and do it once in constructor
|
||||
val ourModule = fileFromOurModule?.let(moduleResolver::findJavaModule)
|
||||
val theirModule = moduleResolver.findJavaModule(referencedFile)
|
||||
|
||||
// If we're both in the unnamed module, it's OK, no error should be reported
|
||||
if (ourModule == null && theirModule == null) return null
|
||||
|
||||
if (theirModule == null) {
|
||||
// We should probably prohibit this usage according to JPMS (named module cannot use types from unnamed module),
|
||||
// but we cannot be sure that a module without module-info.java is going to be actually used as an unnamed module.
|
||||
// It could also be an automatic module, in which case it would be read by every module.
|
||||
return null
|
||||
}
|
||||
|
||||
val containingPackage = DescriptorUtils.getParentOfType(targetClassOrPackage, PackageFragmentDescriptor::class.java, false)
|
||||
val fqName = containingPackage?.fqNameUnsafe?.toSafe() ?: return null
|
||||
if (theirModule.exports(fqName)) return null
|
||||
|
||||
if (ourModule != null) {
|
||||
if (ourModule.name == theirModule.name) return null
|
||||
|
||||
if (!moduleResolver.moduleGraph.reads(ourModule.name, theirModule.name)) {
|
||||
return ErrorsJvm.JAVA_MODULE_DOES_NOT_DEPEND_ON_MODULE.on(reportOn, theirModule.name)
|
||||
}
|
||||
|
||||
if (theirModule.exportsTo(fqName, ourModule.name)) return null
|
||||
}
|
||||
|
||||
return ErrorsJvm.JAVA_MODULE_DOES_NOT_EXPORT_PACKAGE.on(reportOn, theirModule.name, fqName.asString())
|
||||
}
|
||||
|
||||
private fun findVirtualFile(
|
||||
descriptor: ClassOrPackageFragmentDescriptor,
|
||||
originalDescriptor: DeclarationDescriptorWithSource?
|
||||
): VirtualFile? {
|
||||
val source = descriptor.source
|
||||
when (source) {
|
||||
is KotlinJvmBinarySourceElement -> (source.binaryClass as? VirtualFileKotlinClass)?.file?.let { return it }
|
||||
is JavaSourceElement -> (source.javaElement as? VirtualFileBoundJavaClass)?.virtualFile?.let { return it }
|
||||
is KotlinJvmBinaryPackageSourceElement -> {
|
||||
if (originalDescriptor is DeserializedMemberDescriptor) {
|
||||
(source.getContainingBinaryClass(originalDescriptor) as? VirtualFileKotlinClass)?.file?.let { return it }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
source.getPsi()?.containingFile?.virtualFile?.let { return it }
|
||||
|
||||
return originalDescriptor?.source?.getPsi()?.containingFile?.virtualFile
|
||||
}
|
||||
|
||||
inner class ClassifierUsage : ClassifierUsageChecker {
|
||||
override fun check(
|
||||
targetDescriptor: ClassifierDescriptor,
|
||||
trace: BindingTrace,
|
||||
element: PsiElement,
|
||||
languageVersionSettings: LanguageVersionSettings
|
||||
) {
|
||||
val targetClassOrPackage = when (targetDescriptor) {
|
||||
is ClassDescriptor -> targetDescriptor
|
||||
is TypeAliasDescriptor -> {
|
||||
DescriptorUtils.getParentOfType(targetDescriptor, ClassOrPackageFragmentDescriptor::class.java) ?: return
|
||||
}
|
||||
else -> return
|
||||
}
|
||||
|
||||
diagnosticFor(targetClassOrPackage, targetDescriptor, element.containingFile.virtualFile, element)?.let(trace::report)
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
-1
@@ -112,7 +112,10 @@ public class DefaultErrorMessagesJvm implements DefaultErrorMessages.Extension {
|
||||
MAP.put(INTERFACE_STATIC_METHOD_CALL_FROM_JAVA6_TARGET, "Calls to static methods in Java interfaces are deprecated in JVM target 1.6. Recompile with '-jvm-target 1.8'");
|
||||
|
||||
MAP.put(INLINE_FROM_HIGHER_PLATFORM, "Cannot inline bytecode built with {0} into bytecode that is being built with {1}. Please specify proper ''-jvm-target'' option", STRING, STRING);
|
||||
MAP.put(ErrorsJvm.OBSOLETE_SUSPEND_INLINE_FUNCTIONS_ABI, "Cannot inline suspend function built with compiler version less than 1.1.4/1.2-M1");
|
||||
MAP.put(OBSOLETE_SUSPEND_INLINE_FUNCTIONS_ABI, "Cannot inline suspend function built with compiler version less than 1.1.4/1.2-M1");
|
||||
|
||||
MAP.put(JAVA_MODULE_DOES_NOT_DEPEND_ON_MODULE, "Symbol is declared in module ''{0}'' which current module does not depend on", STRING);
|
||||
MAP.put(JAVA_MODULE_DOES_NOT_EXPORT_PACKAGE, "Symbol is declared in module ''{0}'' which does not export package ''{1}''", STRING, STRING);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -95,6 +95,9 @@ public interface ErrorsJvm {
|
||||
DiagnosticFactory2<PsiElement, String, String> INLINE_FROM_HIGHER_PLATFORM = DiagnosticFactory2.create(ERROR);
|
||||
DiagnosticFactory0<PsiElement> OBSOLETE_SUSPEND_INLINE_FUNCTIONS_ABI = DiagnosticFactory0.create(ERROR);
|
||||
|
||||
DiagnosticFactory1<PsiElement, String> JAVA_MODULE_DOES_NOT_DEPEND_ON_MODULE = DiagnosticFactory1.create(ERROR);
|
||||
DiagnosticFactory2<PsiElement, String, String> JAVA_MODULE_DOES_NOT_EXPORT_PACKAGE = DiagnosticFactory2.create(ERROR);
|
||||
|
||||
@SuppressWarnings("UnusedDeclaration")
|
||||
Object _initializer = new Object() {
|
||||
{
|
||||
|
||||
+27
@@ -29,6 +29,7 @@ class JavaModuleGraph(finder: JavaModuleFinder) {
|
||||
visited += "java.base"
|
||||
|
||||
fun dfs(moduleName: String) {
|
||||
// Automatic modules have no transitive exports, so we only consider explicit modules here
|
||||
val moduleInfo = (module(moduleName) as? JavaModule.Explicit)?.moduleInfo ?: return
|
||||
for ((dependencyModuleName, isTransitive) in moduleInfo.requires) {
|
||||
if (!visited.add(dependencyModuleName) && isTransitive) {
|
||||
@@ -40,4 +41,30 @@ class JavaModuleGraph(finder: JavaModuleFinder) {
|
||||
moduleNames.forEach(::dfs)
|
||||
return visited.toList()
|
||||
}
|
||||
|
||||
fun reads(moduleName: String, dependencyName: String): Boolean {
|
||||
if (moduleName == dependencyName || dependencyName == "java.base") return true
|
||||
|
||||
val visited = linkedSetOf<String>()
|
||||
|
||||
fun dfs(name: String): Boolean {
|
||||
if (!visited.add(name)) return false
|
||||
|
||||
val module = module(name)
|
||||
when (module) {
|
||||
is JavaModule.Automatic -> return true
|
||||
is JavaModule.Explicit -> {
|
||||
for ((dependencyModuleName, isTransitive) in module.moduleInfo.requires) {
|
||||
if (dependencyModuleName == dependencyName) return true
|
||||
if (isTransitive && dfs(dependencyName)) return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
null -> return false
|
||||
else -> error("Unsupported module type: $module")
|
||||
}
|
||||
}
|
||||
|
||||
return dfs(moduleName)
|
||||
}
|
||||
}
|
||||
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 2010-2017 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.modules
|
||||
|
||||
import com.intellij.openapi.components.ServiceManager
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
|
||||
interface JavaModuleResolver {
|
||||
val moduleGraph: JavaModuleGraph
|
||||
|
||||
// For any .java, .kt or .class file in the project, returns the corresponding module or null if there's none
|
||||
fun findJavaModule(file: VirtualFile): JavaModule?
|
||||
|
||||
companion object SERVICE {
|
||||
fun getInstance(project: Project): JavaModuleResolver =
|
||||
ServiceManager.getService(project, JavaModuleResolver::class.java)
|
||||
}
|
||||
}
|
||||
+2
@@ -86,6 +86,8 @@ object JvmPlatformConfigurator : PlatformConfigurator(
|
||||
container.useImpl<JavaSyntheticScopes>()
|
||||
container.useImpl<InterfaceDefaultMethodCallChecker>()
|
||||
container.useImpl<InlinePlatformCompatibilityChecker>()
|
||||
container.useImpl<JvmModuleAccessibilityChecker>()
|
||||
container.useImpl<JvmModuleAccessibilityChecker.ClassifierUsage>()
|
||||
container.useInstance(JvmTypeSpecificityComparator)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user