From 855715cc5cb341343bee4b63367e73f8de2f05aa Mon Sep 17 00:00:00 2001 From: Michael Nedzelsky Date: Tue, 9 Jun 2015 18:58:21 +0300 Subject: [PATCH] extract common logic to KotlinAbiVersionIndexBase --- .../idea/versions/KotlinAbiVersionIndex.kt | 24 ++-------- .../versions/KotlinAbiVersionIndexBase.kt | 46 +++++++++++++++++++ .../KotlinJavaScriptAbiVersionIndex.kt | 24 ++-------- 3 files changed, 56 insertions(+), 38 deletions(-) create mode 100644 idea/src/org/jetbrains/kotlin/idea/versions/KotlinAbiVersionIndexBase.kt diff --git a/idea/src/org/jetbrains/kotlin/idea/versions/KotlinAbiVersionIndex.kt b/idea/src/org/jetbrains/kotlin/idea/versions/KotlinAbiVersionIndex.kt index e9d8a7253a0..1224d265317 100644 --- a/idea/src/org/jetbrains/kotlin/idea/versions/KotlinAbiVersionIndex.kt +++ b/idea/src/org/jetbrains/kotlin/idea/versions/KotlinAbiVersionIndex.kt @@ -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() { - - override fun getName() = ID.create(javaClass().getCanonicalName()) +public object KotlinAbiVersionIndex : KotlinAbiVersionIndexBase(javaClass()) { 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()) - private val kotlinAnnotationsDesc = setOf( OLD_JET_CLASS_ANNOTATION, OLD_JET_PACKAGE_CLASS_ANNOTATION, @@ -62,7 +51,7 @@ public object KotlinAbiVersionIndex : ScalarIndexExtension() { 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() { } }, 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 diff --git a/idea/src/org/jetbrains/kotlin/idea/versions/KotlinAbiVersionIndexBase.kt b/idea/src/org/jetbrains/kotlin/idea/versions/KotlinAbiVersionIndexBase.kt new file mode 100644 index 00000000000..70544d99fd4 --- /dev/null +++ b/idea/src/org/jetbrains/kotlin/idea/versions/KotlinAbiVersionIndexBase.kt @@ -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(private val classOfIndex: Class) : ScalarIndexExtension() { + + override fun getName() = ID.create(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()) + } + } +} diff --git a/idea/src/org/jetbrains/kotlin/idea/versions/KotlinJavaScriptAbiVersionIndex.kt b/idea/src/org/jetbrains/kotlin/idea/versions/KotlinJavaScriptAbiVersionIndex.kt index 79b132155a5..7b710bd60bf 100644 --- a/idea/src/org/jetbrains/kotlin/idea/versions/KotlinJavaScriptAbiVersionIndex.kt +++ b/idea/src/org/jetbrains/kotlin/idea/versions/KotlinJavaScriptAbiVersionIndex.kt @@ -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() { - - override fun getName() = ID.create(javaClass().getCanonicalName()) +public object KotlinJavaScriptAbiVersionIndex : KotlinAbiVersionIndexBase(javaClass()) { 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()) - private val INDEXER = DataIndexer() { inputData: FileContent -> val result = Maps.newHashMap() - try { + tryBlock(inputData) { val text = VfsUtilCore.loadText(inputData.getFile()) val metadataList = ArrayList() KotlinJavascriptMetadataUtils.parseMetadata(text, metadataList) @@ -65,9 +54,6 @@ public object KotlinJavaScriptAbiVersionIndex : ScalarIndexExtension() { } } } - catch (e: Throwable) { - LOG.warn("Could not index ABI version for file " + inputData.getFile() + ": " + e.getMessage()) - } result }