diff --git a/idea/src/org/jetbrains/kotlin/idea/versions/KotlinAbiVersionIndex.kt b/idea/src/org/jetbrains/kotlin/idea/versions/KotlinAbiVersionIndex.kt index 0e3cf9f5de5..685d3432a86 100644 --- a/idea/src/org/jetbrains/kotlin/idea/versions/KotlinAbiVersionIndex.kt +++ b/idea/src/org/jetbrains/kotlin/idea/versions/KotlinAbiVersionIndex.kt @@ -14,139 +14,115 @@ * limitations under the License. */ -package org.jetbrains.kotlin.idea.versions; +package org.jetbrains.kotlin.idea.versions -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.Maps; -import com.intellij.openapi.diagnostic.Logger; -import com.intellij.openapi.fileTypes.StdFileTypes; -import com.intellij.openapi.util.Ref; -import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.util.indexing.*; -import com.intellij.util.io.ExternalIntegerKeyDescriptor; -import com.intellij.util.io.KeyDescriptor; -import org.jetbrains.annotations.NotNull; -import org.jetbrains.kotlin.load.java.AbiVersionUtil; -import org.jetbrains.org.objectweb.asm.AnnotationVisitor; -import org.jetbrains.org.objectweb.asm.ClassReader; -import org.jetbrains.org.objectweb.asm.ClassVisitor; -import org.jetbrains.org.objectweb.asm.Opcodes; +import com.google.common.collect.ImmutableSet +import com.google.common.collect.Maps +import com.intellij.openapi.diagnostic.Logger +import com.intellij.openapi.fileTypes.StdFileTypes +import com.intellij.openapi.util.Ref +import com.intellij.openapi.vfs.VirtualFile +import com.intellij.util.indexing.* +import com.intellij.util.io.ExternalIntegerKeyDescriptor +import com.intellij.util.io.KeyDescriptor +import org.jetbrains.kotlin.load.java.AbiVersionUtil +import org.jetbrains.org.objectweb.asm.AnnotationVisitor +import org.jetbrains.org.objectweb.asm.ClassReader +import org.jetbrains.org.objectweb.asm.ClassVisitor +import org.jetbrains.org.objectweb.asm.Opcodes -import java.util.Map; -import java.util.Set; - -import static org.jetbrains.kotlin.codegen.AsmUtil.asmDescByFqNameWithoutInnerClasses; -import static org.jetbrains.kotlin.load.java.JvmAnnotationNames.*; +import org.jetbrains.kotlin.codegen.AsmUtil.asmDescByFqNameWithoutInnerClasses +import org.jetbrains.kotlin.load.java.JvmAnnotationNames.* /** * Important! This is not a stub-based index. And it has its own version */ -public class KotlinAbiVersionIndex extends ScalarIndexExtension constructor { - private static final Logger LOG = Logger.getInstance(KotlinAbiVersionIndex.class); +public class KotlinAbiVersionIndex private constructor() : ScalarIndexExtension() { - public static final KotlinAbiVersionIndex INSTANCE = new KotlinAbiVersionIndex(); + override fun getName(): ID { + return NAME + } - private static final int VERSION = 1; + override fun getIndexer(): DataIndexer { + return INDEXER + } - private static final ID NAME = ID.create(KotlinAbiVersionIndex.class.getCanonicalName()); - private static final ExternalIntegerKeyDescriptor KEY_DESCRIPTOR = new ExternalIntegerKeyDescriptor(); + override fun getKeyDescriptor(): KeyDescriptor { + return KEY_DESCRIPTOR + } - private static final FileBasedIndex.InputFilter INPUT_FILTER = new FileBasedIndex.InputFilter() { - @Override - public boolean acceptInput(VirtualFile file) { - return file.getFileType() == StdFileTypes.CLASS; + override fun getInputFilter(): FileBasedIndex.InputFilter { + return INPUT_FILTER + } + + override fun dependsOnFileContent(): Boolean { + return true + } + + override fun getVersion(): Int { + return VERSION + } + + companion object { + private val LOG = Logger.getInstance(javaClass()) + + public val INSTANCE: KotlinAbiVersionIndex = KotlinAbiVersionIndex() + + private val VERSION = 1 + + private val NAME = ID.create(javaClass().getCanonicalName()) + private val KEY_DESCRIPTOR = ExternalIntegerKeyDescriptor() + + private val INPUT_FILTER = object : FileBasedIndex.InputFilter { + override fun acceptInput(file: VirtualFile): Boolean { + return file.getFileType() == StdFileTypes.CLASS + } } - }; - private static final DataIndexer INDEXER = new DataIndexer() { - @SuppressWarnings("deprecation") - private final Set kotlinAnnotationsDesc = new ImmutableSet.Builder() - .add(asmDescByFqNameWithoutInnerClasses(OLD_JET_CLASS_ANNOTATION)) - .add(asmDescByFqNameWithoutInnerClasses(OLD_JET_PACKAGE_CLASS_ANNOTATION)) - .add(asmDescByFqNameWithoutInnerClasses(OLD_KOTLIN_CLASS)) - .add(asmDescByFqNameWithoutInnerClasses(OLD_KOTLIN_PACKAGE)) - .add(asmDescByFqNameWithoutInnerClasses(KOTLIN_CLASS)) - .add(asmDescByFqNameWithoutInnerClasses(KOTLIN_PACKAGE)) - .build(); + private val INDEXER = object : DataIndexer { + SuppressWarnings("deprecation") + private val kotlinAnnotationsDesc = ImmutableSet.Builder().add(asmDescByFqNameWithoutInnerClasses(OLD_JET_CLASS_ANNOTATION)).add(asmDescByFqNameWithoutInnerClasses(OLD_JET_PACKAGE_CLASS_ANNOTATION)).add(asmDescByFqNameWithoutInnerClasses(OLD_KOTLIN_CLASS)).add(asmDescByFqNameWithoutInnerClasses(OLD_KOTLIN_PACKAGE)).add(asmDescByFqNameWithoutInnerClasses(KOTLIN_CLASS)).add(asmDescByFqNameWithoutInnerClasses(KOTLIN_PACKAGE)).build() - @NotNull - @Override - public Map map(FileContent inputData) { - final Map result = Maps.newHashMap(); - final Ref annotationPresent = new Ref(false); + override fun map(inputData: FileContent): Map { + val result = Maps.newHashMap() + val annotationPresent = Ref(false) - try { - ClassReader classReader = new ClassReader(inputData.getContent()); - classReader.accept(new ClassVisitor(Opcodes.ASM5) { - @Override - public AnnotationVisitor visitAnnotation(String desc, boolean visible) { - if (!kotlinAnnotationsDesc.contains(desc)) { - return null; - } - annotationPresent.set(true); - return new AnnotationVisitor(Opcodes.ASM5) { - @Override - public void visit(String name, Object value) { - if (ABI_VERSION_FIELD_NAME.equals(name)) { - if (value instanceof Integer) { - Integer abiVersion = (Integer) value; - result.put(abiVersion, null); - } - else { - // Version is set to something weird - result.put(AbiVersionUtil.INVALID_VERSION, null); + try { + val classReader = ClassReader(inputData.getContent()) + classReader.accept(object : ClassVisitor(Opcodes.ASM5) { + override fun visitAnnotation(desc: String, visible: Boolean): AnnotationVisitor? { + if (!kotlinAnnotationsDesc.contains(desc)) { + return null + } + annotationPresent.set(true) + return object : AnnotationVisitor(Opcodes.ASM5) { + override fun visit(name: String, value: Any) { + if (ABI_VERSION_FIELD_NAME == name) { + if (value is Int) { + result.put(value, null) + } + else { + // Version is set to something weird + result.put(AbiVersionUtil.INVALID_VERSION, null) + } } } } - }; - } - }, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES); - } - catch (Throwable e) { - LOG.warn("Could not index ABI version for file " + inputData.getFile() + ": " + e.getMessage()); - } + } + }, 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.get() && result.isEmpty()) { - // No version at all: the class is too old - result.put(AbiVersionUtil.INVALID_VERSION, null); - } - return result; + if (annotationPresent.get() && result.isEmpty()) { + // No version at all: the class is too old + result.put(AbiVersionUtil.INVALID_VERSION, null) + } + + return result + } } - }; - - private KotlinAbiVersionIndex() { - } - - @NotNull - @Override - public ID getName() { - return NAME; - } - - @NotNull - @Override - public DataIndexer getIndexer() { - return INDEXER; - } - - @Override - public KeyDescriptor getKeyDescriptor() { - return KEY_DESCRIPTOR; - } - - @Override - public FileBasedIndex.InputFilter getInputFilter() { - return INPUT_FILTER; - } - - @Override - public boolean dependsOnFileContent() { - return true; - } - - @Override - public int getVersion() { - return VERSION; } }