Skip to content

Commit 93f5ceb

Browse files
committed
more methods implementations for #349
1 parent 07fa478 commit 93f5ceb

File tree

4 files changed

+116
-66
lines changed

4 files changed

+116
-66
lines changed

utbot-framework-api/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ dependencies {
1212
// TODO do we really need apache commons?
1313
implementation group: 'org.apache.commons', name: 'commons-lang3', version: commons_lang_version
1414
implementation group: 'io.github.microutils', name: 'kotlin-logging', version: kotlin_logging_version
15-
api 'com.github.UnitTestBot:java-compilation-database:aad83be780'
15+
api 'com.github.UnitTestBot:java-compilation-database:1dd5579be1'
16+
// api group: 'org.utbot', name: 'jcdb', version: '1.0-SNAPSHOT'
1617
}
1718

1819
shadowJar {

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/Api.kt

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,19 @@
88

99
package org.utbot.framework.plugin.api
1010

11+
import kotlinx.coroutines.runBlocking
1112
import org.utbot.common.isDefaultValue
1213
import org.utbot.common.withToStringThreadLocalReentrancyGuard
1314
import org.utbot.framework.UtSettings
1415
import org.utbot.framework.plugin.api.MockFramework.MOCKITO
1516
import org.utbot.framework.plugin.api.impl.FieldIdReflectionStrategy
1617
import org.utbot.framework.plugin.api.impl.FieldIdSootStrategy
17-
import org.utbot.framework.plugin.api.util.booleanClassId
18-
import org.utbot.framework.plugin.api.util.byteClassId
19-
import org.utbot.framework.plugin.api.util.charClassId
20-
import org.utbot.framework.plugin.api.util.constructor
21-
import org.utbot.framework.plugin.api.util.doubleClassId
18+
import org.utbot.framework.plugin.api.util.*
2219
import org.utbot.framework.plugin.api.util.executableId
23-
import org.utbot.framework.plugin.api.util.findFieldOrNull
24-
import org.utbot.framework.plugin.api.util.floatClassId
2520
import org.utbot.framework.plugin.api.util.id
26-
import org.utbot.framework.plugin.api.util.intClassId
27-
import org.utbot.framework.plugin.api.util.isArray
28-
import org.utbot.framework.plugin.api.util.isPrimitive
29-
import org.utbot.framework.plugin.api.util.jClass
30-
import org.utbot.framework.plugin.api.util.longClassId
31-
import org.utbot.framework.plugin.api.util.method
32-
import org.utbot.framework.plugin.api.util.primitiveTypeJvmNameOrNull
33-
import org.utbot.framework.plugin.api.util.shortClassId
34-
import org.utbot.framework.plugin.api.util.toReferenceTypeBytecodeSignature
35-
import org.utbot.framework.plugin.api.util.voidClassId
21+
import soot.*
22+
import soot.jimple.JimpleBody
23+
import soot.jimple.Stmt
3624
import java.io.File
3725
import java.lang.reflect.Modifier
3826
import java.nio.file.Path
@@ -43,21 +31,6 @@ import kotlin.reflect.KFunction
4331
import kotlin.reflect.full.instanceParameter
4432
import kotlin.reflect.jvm.javaConstructor
4533
import kotlin.reflect.jvm.javaType
46-
import soot.ArrayType
47-
import soot.BooleanType
48-
import soot.ByteType
49-
import soot.CharType
50-
import soot.DoubleType
51-
import soot.FloatType
52-
import soot.IntType
53-
import soot.LongType
54-
import soot.RefType
55-
import soot.ShortType
56-
import soot.SootClass
57-
import soot.Type
58-
import soot.VoidType
59-
import soot.jimple.JimpleBody
60-
import soot.jimple.Stmt
6134

6235
data class UtMethod<R>(
6336
val callable: KCallable<R>,
@@ -637,9 +610,9 @@ open class ClassId(
637610
val elementClassId: ClassId? = null
638611
) {
639612
open val canonicalName: String
640-
get() = jClass.canonicalName ?: error("ClassId $name does not have canonical name")
613+
get() = jClass.name ?: error("ClassId $name does not have canonical name")
641614

642-
open val simpleName: String get() = jClass.simpleName
615+
open val simpleName: String get() = jClass.name
643616

644617
/**
645618
* For regular classes this is just a simple name
@@ -651,43 +624,43 @@ open class ClassId(
651624
.replace(Regex("[^a-zA-Z0-9]"), "")
652625
.let { if (this.isArray) it + "Array" else it }
653626

654-
open val packageName: String get() = jClass.`package`?.name ?: "" // empty package for primitives
627+
open val packageName: String get() = jClass.name.replaceAfterLast(".") // empty package for primitives
655628

656629
open val isInDefaultPackage: Boolean
657630
get() = packageName.isEmpty()
658631

659632
open val isPublic: Boolean
660-
get() = Modifier.isPublic(jClass.modifiers)
633+
get() = jClass.isPublic
661634

662635
open val isProtected: Boolean
663-
get() = Modifier.isProtected(jClass.modifiers)
636+
get() = jClass.isProtected
664637

665638
open val isPrivate: Boolean
666-
get() = Modifier.isPrivate(jClass.modifiers)
639+
get() = jClass.isPrivate
667640

668641
val isPackagePrivate: Boolean
669642
get() = !(isPublic || isProtected || isPrivate)
670643

671644
open val isFinal: Boolean
672-
get() = Modifier.isFinal(jClass.modifiers)
645+
get() = jClass.isFinal
673646

674647
open val isStatic: Boolean
675-
get() = Modifier.isStatic(jClass.modifiers)
648+
get() = jClass.isStatic
676649

677650
open val isAbstract: Boolean
678-
get() = Modifier.isAbstract(jClass.modifiers)
651+
get() = jClass.isAbstract
679652

680653
open val isAnonymous: Boolean
681-
get() = jClass.isAnonymousClass
654+
get() = jClass.isAnonymous
682655

683656
open val isLocal: Boolean
684657
get() = jClass.isLocalClass
685658

686659
open val isInner: Boolean
687-
get() = jClass.isMemberClass && !isStatic
660+
get() = jClass.isInner
688661

689662
open val isNested: Boolean
690-
get() = jClass.enclosingClass != null
663+
get() = jClass.isNested
691664

692665
open val isSynthetic: Boolean
693666
get() = jClass.isSynthetic
@@ -701,16 +674,20 @@ open class ClassId(
701674
*/
702675
// TODO for now it duplicates overridden methods JIRA:1458
703676
open val allMethods: Sequence<MethodId>
704-
get() = generateSequence(jClass) { it.superclass }
705-
.mapNotNull { it.declaredMethods }
706-
.flatMap { it.toList() }
707-
.map { it.executableId }
677+
get() {
678+
return runBlocking {
679+
generateSequence(jClass) { it.superclass }
680+
.mapNotNull { it.methods }
681+
.flatten()
682+
.map { it.executableId }
683+
}
684+
}
708685

709686
/**
710687
* Collects all declared constructors (including private and protected) from class to sequence
711688
*/
712689
open val allConstructors: Sequence<ConstructorId>
713-
get() = jClass.declaredConstructors.asSequence().map { it.executableId }
690+
get() = jClass.methods.filtasSequence().map { it.executableId }
714691

715692
open val typeParameters: TypeParameters
716693
get() = TypeParameters()
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package org.utbot.framework.plugin.api
2+
3+
import kotlinx.coroutines.runBlocking
4+
import org.utbot.jcdb.api.*
5+
import org.utbot.jcdb.api.ClassId
6+
import org.utbot.jcdb.api.FieldId
7+
8+
open class JCDBClassId(private val jcdbClassId: ClassId) {
9+
10+
fun findField(name: String): FieldId? {
11+
return runBlocking {
12+
jcdbClassId.findFieldOrNull(name)
13+
}
14+
}
15+
16+
val name get() = jcdbClassId.name
17+
val simpleName get() = jcdbClassId.simpleName
18+
19+
open val isPublic: Boolean
20+
get() = runBlocking { jcdbClassId.isPublic() }
21+
22+
open val isProtected: Boolean
23+
get() = runBlocking { jcdbClassId.isProtected() }
24+
25+
open val isPrivate: Boolean
26+
get() = runBlocking { jcdbClassId.isPrivate() }
27+
28+
open val isFinal: Boolean
29+
get() = runBlocking { jcdbClassId.isFinal() }
30+
31+
open val isStatic: Boolean
32+
get() = runBlocking { jcdbClassId.isStatic() }
33+
34+
open val isAbstract: Boolean
35+
get() = runBlocking { jcdbClassId.isAbstract() }
36+
37+
open val isAnonymous: Boolean
38+
get() = runBlocking { jcdbClassId.isAnonymous() }
39+
40+
open val isLocalClass: Boolean
41+
get() = runBlocking { jcdbClassId.isLocal() }
42+
43+
open val isInner: Boolean
44+
get() = isNested && !isStatic
45+
46+
open val isNested: Boolean
47+
get() = runBlocking { jcdbClassId.outerClass() != null }
48+
49+
open val isSynthetic: Boolean
50+
get() = runBlocking { jcdbClassId.isSynthetic() }
51+
52+
open val isMemberClass: Boolean
53+
get() = runBlocking { jcdbClassId.isMemberClass() }
54+
55+
open val superclass: JCDBClassId?
56+
get() = runBlocking {
57+
jcdbClassId.superclass()?.let {
58+
JCDBClassId(it)
59+
}
60+
}
61+
62+
val methods: List<org.utbot.jcdb.api.MethodId> get() {
63+
return runBlocking {
64+
jcdbClassId.methods()
65+
}
66+
}
67+
68+
val interfaces: List<JCDBClassId> get() {
69+
return runBlocking {
70+
jcdbClassId.interfaces().map { JCDBClassId(it) }
71+
}
72+
}
73+
74+
75+
}

utbot-framework-api/src/main/kotlin/org/utbot/framework/plugin/api/util/IdUtil.kt

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
package org.utbot.framework.plugin.api.util
22

3+
import kotlinx.coroutines.runBlocking
34
import org.utbot.common.findFieldOrNull
4-
import org.utbot.framework.plugin.api.BuiltinClassId
5-
import org.utbot.framework.plugin.api.BuiltinMethodId
6-
import org.utbot.framework.plugin.api.ClassId
7-
import org.utbot.framework.plugin.api.ConstructorId
8-
import org.utbot.framework.plugin.api.ExecutableId
9-
import org.utbot.framework.plugin.api.FieldId
10-
import org.utbot.framework.plugin.api.MethodId
11-
import org.utbot.framework.plugin.api.UtModel
12-
import org.utbot.framework.plugin.api.UtNullModel
13-
import org.utbot.framework.plugin.api.UtPrimitiveModel
5+
import org.utbot.framework.plugin.api.*
6+
import org.utbot.jcdb.impl.types.PredefinedPrimitive
147
import java.lang.reflect.Constructor
158
import java.lang.reflect.Executable
169
import java.lang.reflect.Field
@@ -69,11 +62,15 @@ fun ClassId.primitiveTypeJvmNameOrNull(): String? {
6962
// TODO: Make jClass, method, constructor and field private for IdUtil and rewrite all the code depending on it.
7063
// TODO: This is needed to avoid accessing actual Class, Method, Constructor and Field instances for builtin ids.
7164
// TODO: All the properties of ids can be accessed via corresponding properties in the public API.
72-
val ClassId.jClass: Class<*>
73-
get() = when {
74-
isPrimitive -> idToPrimitive[this]!!
75-
isArray -> Class.forName(name, true, utContext.classLoader) // TODO: probably rewrite
76-
else -> utContext.classLoader.loadClass(name)
65+
val ClassId.jClass: JCDBClassId
66+
get() {
67+
return when {
68+
isPrimitive -> idToPrimitive[this]!!
69+
isArray -> Class.forName(name, true, utContext.classLoader) // TODO: probably rewrite
70+
else -> runBlocking { utContext.classpathSet.findClassOrNull(name) }?.let {
71+
JCDBClassId(it)
72+
} ?: throw IllegalStateException("can't find class $name")
73+
}
7774
}
7875

7976
/**
@@ -218,7 +215,7 @@ val primitiveToId: Map<Class<*>, ClassId> = mapOf(
218215

219216
@Suppress("RemoveRedundantQualifierName")
220217
val idToPrimitive: Map<ClassId, Class<*>> = mapOf(
221-
voidClassId to java.lang.Void.TYPE,
218+
voidClassId to PredefinedPrimitive.java.lang.Void.TYPE,
222219
byteClassId to java.lang.Byte.TYPE,
223220
shortClassId to java.lang.Short.TYPE,
224221
charClassId to java.lang.Character.TYPE,

0 commit comments

Comments
 (0)