com.jetbrains.pluginverifier.verifiers.InheritanceUtil.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of verifier-core Show documentation
Show all versions of verifier-core Show documentation
Core classes of JetBrains Plugin Verifier with verification rules, general usage detection and bytecode verification engine
/*
* Copyright 2000-2020 JetBrains s.r.o. and other contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
*/
package com.jetbrains.pluginverifier.verifiers
import com.jetbrains.plugin.structure.classes.resolvers.Resolver
import com.jetbrains.pluginverifier.verifiers.resolution.ClassFile
import com.jetbrains.pluginverifier.verifiers.resolution.resolveClassOrNull
import java.util.*
private fun Resolver.resolveAllDirectParents(classFile: ClassFile): List {
val parents = listOfNotNull(classFile.superName) + classFile.interfaces
return parents.mapNotNull { resolveClassOrNull(it) }
}
fun Resolver.isSubclassOrSelf(childClassName: String, possibleParentName: String): Boolean {
if (childClassName == possibleParentName) {
return true
}
return isSubclassOf(childClassName, possibleParentName)
}
fun Resolver.isSubclassOf(childClassName: String, possibleParentName: String): Boolean {
val childClass = resolveClassOrNull(childClassName) ?: return false
return isSubclassOf(childClass, possibleParentName)
}
fun Resolver.isSubclassOf(child: ClassFile, parentName: String): Boolean {
if (parentName == "java/lang/Object") {
return true
}
val directParents = resolveAllDirectParents(child)
val queue = LinkedList()
queue.addAll(directParents)
val visited = hashSetOf()
visited.addAll(directParents.map { it.name })
while (queue.isNotEmpty()) {
val node = queue.poll()
if (node.name == parentName) {
return true
}
resolveAllDirectParents(node).filterNot { it.name in visited }.forEach {
visited.add(it.name)
queue.addLast(it)
}
}
return false
}