extract common logic to KotlinAbiVersionIndexBase

This commit is contained in:
Michael Nedzelsky
2015-06-09 18:58:21 +03:00
parent dac10944e0
commit 855715cc5c
3 changed files with 56 additions and 38 deletions
@@ -16,10 +16,10 @@
package org.jetbrains.kotlin.idea.versions
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.fileTypes.StdFileTypes
import com.intellij.util.indexing.*
import com.intellij.util.io.ExternalIntegerKeyDescriptor
import com.intellij.util.indexing.DataIndexer
import com.intellij.util.indexing.FileBasedIndex
import com.intellij.util.indexing.FileContent
import org.jetbrains.kotlin.codegen.AsmUtil.asmDescByFqNameWithoutInnerClasses
import org.jetbrains.kotlin.load.java.AbiVersionUtil
import org.jetbrains.kotlin.load.java.JvmAnnotationNames.*
@@ -28,27 +28,16 @@ import org.jetbrains.org.objectweb.asm.ClassReader
import org.jetbrains.org.objectweb.asm.ClassVisitor
import org.jetbrains.org.objectweb.asm.Opcodes
/**
* Important! This is not a stub-based index. And it has its own version
*/
public object KotlinAbiVersionIndex : ScalarIndexExtension<Int>() {
override fun getName() = ID.create<Int, Void>(javaClass<KotlinAbiVersionIndex>().getCanonicalName())
public object KotlinAbiVersionIndex : KotlinAbiVersionIndexBase<KotlinAbiVersionIndex>(javaClass<KotlinAbiVersionIndex>()) {
override fun getIndexer() = INDEXER
override fun getKeyDescriptor() = ExternalIntegerKeyDescriptor()
override fun getInputFilter() = FileBasedIndex.InputFilter() { file -> file.getFileType() == StdFileTypes.CLASS }
override fun dependsOnFileContent() = true
override fun getVersion() = VERSION
private val VERSION = 1
private val LOG = Logger.getInstance(javaClass<KotlinAbiVersionIndex>())
private val kotlinAnnotationsDesc = setOf(
OLD_JET_CLASS_ANNOTATION,
OLD_JET_PACKAGE_CLASS_ANNOTATION,
@@ -62,7 +51,7 @@ public object KotlinAbiVersionIndex : ScalarIndexExtension<Int>() {
var version: Int? = null
var annotationPresent = false
try {
tryBlock(inputData) {
val classReader = ClassReader(inputData.getContent())
classReader.accept(object : ClassVisitor(Opcodes.ASM5) {
override fun visitAnnotation(desc: String, visible: Boolean): AnnotationVisitor? {
@@ -86,9 +75,6 @@ public object KotlinAbiVersionIndex : ScalarIndexExtension<Int>() {
}
}, ClassReader.SKIP_CODE or ClassReader.SKIP_DEBUG or ClassReader.SKIP_FRAMES)
}
catch (e: Throwable) {
LOG.warn("Could not index ABI version for file " + inputData.getFile() + ": " + e.getMessage())
}
if (annotationPresent && version == null) {
// No version at all: the class is too old
@@ -0,0 +1,46 @@
/*
* 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.idea.versions
import com.intellij.openapi.diagnostic.Logger
import com.intellij.util.indexing.FileContent
import com.intellij.util.indexing.ID
import com.intellij.util.indexing.ScalarIndexExtension
import com.intellij.util.io.ExternalIntegerKeyDescriptor
/**
* Important! This is not a stub-based index. And it has its own version
*/
abstract class KotlinAbiVersionIndexBase<T>(private val classOfIndex: Class<T>) : ScalarIndexExtension<Int>() {
override fun getName() = ID.create<Int, Void>(classOfIndex.getCanonicalName())
override fun getKeyDescriptor() = ExternalIntegerKeyDescriptor()
override fun dependsOnFileContent() = true
private val LOG = Logger.getInstance(classOfIndex)
protected inline fun tryBlock(inputData: FileContent, body: () -> Unit) {
try {
body()
}
catch (e: Throwable) {
LOG.warn("Could not index ABI version for file " + inputData.getFile() + ": " + e.getMessage())
}
}
}
@@ -17,41 +17,30 @@
package org.jetbrains.kotlin.idea.versions
import com.google.common.collect.Maps
import com.intellij.openapi.diagnostic.Logger
import com.intellij.openapi.vfs.VfsUtilCore
import com.intellij.util.indexing.*
import com.intellij.util.io.ExternalIntegerKeyDescriptor
import com.intellij.util.indexing.DataIndexer
import com.intellij.util.indexing.FileBasedIndex
import com.intellij.util.indexing.FileContent
import org.jetbrains.kotlin.js.JavaScript
import org.jetbrains.kotlin.load.java.AbiVersionUtil
import org.jetbrains.kotlin.utils.KotlinJavascriptMetadata
import org.jetbrains.kotlin.utils.KotlinJavascriptMetadataUtils
import java.util.ArrayList
/**
* Important! This is not a stub-based index. And it has its own version
*/
public object KotlinJavaScriptAbiVersionIndex : ScalarIndexExtension<Int>() {
override fun getName() = ID.create<Int, Void>(javaClass<KotlinJavaScriptAbiVersionIndex>().getCanonicalName())
public object KotlinJavaScriptAbiVersionIndex : KotlinAbiVersionIndexBase<KotlinJavaScriptAbiVersionIndex>(javaClass<KotlinJavaScriptAbiVersionIndex>()) {
override fun getIndexer() = INDEXER
override fun getKeyDescriptor() = ExternalIntegerKeyDescriptor()
override fun getInputFilter() = FileBasedIndex.InputFilter() { file -> JavaScript.EXTENSION == file.getExtension() }
override fun dependsOnFileContent() = true
override fun getVersion() = VERSION
private val VERSION = 1
private val LOG = Logger.getInstance(javaClass<KotlinJavaScriptAbiVersionIndex>())
private val INDEXER = DataIndexer() { inputData: FileContent ->
val result = Maps.newHashMap<Int, Void>()
try {
tryBlock(inputData) {
val text = VfsUtilCore.loadText(inputData.getFile())
val metadataList = ArrayList<KotlinJavascriptMetadata>()
KotlinJavascriptMetadataUtils.parseMetadata(text, metadataList)
@@ -65,9 +54,6 @@ public object KotlinJavaScriptAbiVersionIndex : ScalarIndexExtension<Int>() {
}
}
}
catch (e: Throwable) {
LOG.warn("Could not index ABI version for file " + inputData.getFile() + ": " + e.getMessage())
}
result
}