add SerializedResourcePaths, extract base code from builtins package fragment, data finder
This commit is contained in:
+43
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* 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.serialization
|
||||
|
||||
import com.google.protobuf.ExtensionRegistryLite
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
|
||||
public abstract class SerializedResourcePaths {
|
||||
public abstract val EXTENSION_REGISTRY: ExtensionRegistryLite
|
||||
|
||||
public abstract fun getClassMetadataPath(classId: ClassId): String
|
||||
|
||||
public abstract fun getPackageFilePath(fqName: FqName): String
|
||||
|
||||
public abstract fun getStringTableFilePath(fqName: FqName): String
|
||||
|
||||
// TODO: remove this after M12
|
||||
public object FallbackPaths {
|
||||
public fun getPackageFilePath(fqName: FqName): String =
|
||||
fqName.asString().replace('.', '/') + "/.kotlin_package"
|
||||
|
||||
public fun getStringTableFilePath(fqName: FqName): String =
|
||||
fqName.asString().replace('.', '/') + "/.kotlin_string_table"
|
||||
}
|
||||
|
||||
public val fallbackPaths: FallbackPaths = FallbackPaths
|
||||
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* 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.serialization.deserialization
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.impl.PackageFragmentDescriptorImpl
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.resolve.scopes.JetScope
|
||||
import org.jetbrains.kotlin.serialization.ProtoBuf
|
||||
import org.jetbrains.kotlin.serialization.SerializedResourcePaths
|
||||
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedPackageMemberScope
|
||||
import org.jetbrains.kotlin.storage.NotNullLazyValue
|
||||
import org.jetbrains.kotlin.storage.StorageManager
|
||||
import java.io.InputStream
|
||||
import javax.inject.Inject
|
||||
import kotlin.properties.Delegates
|
||||
|
||||
public abstract class DeserializedPackageFragment(
|
||||
fqName: FqName,
|
||||
protected val storageManager: StorageManager,
|
||||
module: ModuleDescriptor,
|
||||
protected val serializedResourcePaths: SerializedResourcePaths,
|
||||
private val loadResource: (path: String) -> InputStream?
|
||||
) : PackageFragmentDescriptorImpl(module, fqName) {
|
||||
|
||||
val nameResolver = NameResolver.read(
|
||||
loadResource(serializedResourcePaths.getStringTableFilePath(fqName))
|
||||
?: loadResourceSure(serializedResourcePaths.fallbackPaths.getStringTableFilePath(fqName))
|
||||
)
|
||||
|
||||
protected var components: DeserializationComponents by Delegates.notNull()
|
||||
|
||||
Inject
|
||||
public fun setDeserializationComponents(components: DeserializationComponents) {
|
||||
this.components = components
|
||||
}
|
||||
|
||||
private val memberScopeLazyValue: NotNullLazyValue<JetScope> = storageManager.createLazyValue {
|
||||
val packageStream = loadResourceSure(serializedResourcePaths.getPackageFilePath(fqName))
|
||||
val packageProto = ProtoBuf.Package.parseFrom(packageStream, serializedResourcePaths.EXTENSION_REGISTRY)
|
||||
DeserializedPackageMemberScope(this, packageProto, nameResolver, components, classNames = { loadClassNames(fqName, packageProto) })
|
||||
}
|
||||
|
||||
override fun getMemberScope() = memberScopeLazyValue()
|
||||
|
||||
protected abstract fun loadClassNames(fqName: FqName, packageProto: ProtoBuf.Package): Collection<Name>
|
||||
|
||||
protected fun loadResourceSure(path: String): InputStream =
|
||||
loadResource(path) ?: throw IllegalStateException("Resource not found in classpath: $path")
|
||||
}
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* 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.serialization.deserialization
|
||||
|
||||
import org.jetbrains.kotlin.serialization.deserialization.DeserializedPackageFragment
|
||||
import org.jetbrains.kotlin.descriptors.PackageFragmentProvider
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.serialization.ClassData
|
||||
import org.jetbrains.kotlin.serialization.ProtoBuf
|
||||
import org.jetbrains.kotlin.serialization.SerializedResourcePaths
|
||||
import java.io.InputStream
|
||||
|
||||
public open class ResourceLoadingClassDataFinder(
|
||||
private val packageFragmentProvider: PackageFragmentProvider,
|
||||
private val serializedResourcePaths: SerializedResourcePaths,
|
||||
private val loadResource: (path: String) -> InputStream?
|
||||
) : ClassDataFinder {
|
||||
override fun findClassData(classId: ClassId): ClassData? {
|
||||
val packageFragment = packageFragmentProvider.getPackageFragments(classId.getPackageFqName()).singleOrNull() ?: return null
|
||||
|
||||
val stream = loadResource(serializedResourcePaths.getClassMetadataPath(classId)) ?: return null
|
||||
|
||||
val classProto = ProtoBuf.Class.parseFrom(stream, serializedResourcePaths.EXTENSION_REGISTRY)
|
||||
val nameResolver =
|
||||
(packageFragment as? DeserializedPackageFragment ?: error("Not a deserialized package fragment: $packageFragment")).nameResolver
|
||||
|
||||
val expectedShortName = classId.getShortClassName()
|
||||
val actualShortName = nameResolver.getClassId(classProto.getFqName()).getShortClassName()
|
||||
if (!actualShortName.isSpecial() && actualShortName != expectedShortName) {
|
||||
// Workaround for case-insensitive file systems,
|
||||
// otherwise we'd find "Collection" for "collection" etc
|
||||
return null
|
||||
}
|
||||
|
||||
return ClassData(nameResolver, classProto)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user