Skip to content

Commit e2bd57c

Browse files
authored
An exception null cannot be cast to non-null type org.jacodb.api.JcClassType occurs #200 (#201)
1 parent 587d560 commit e2bd57c

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

jacodb-api/src/main/kotlin/org/jacodb/api/Exceptions.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ package org.jacodb.api
2121
*/
2222
class NoClassInClasspathException(val className: String) : Exception("Class $className not found in classpath")
2323

24+
/**
25+
* This exception should be thrown when classpath is incomplete
26+
*/
27+
class TypeNotFoundException(val typeName: String) : Exception("Type $typeName not found in classpath")
28+
29+
class MethodNotFoundException(msg: String) : Exception(msg)
2430

2531
fun String.throwClassNotFound(): Nothing {
2632
throw NoClassInClasspathException(this)

jacodb-api/src/main/kotlin/org/jacodb/api/ext/JcTypes.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,8 @@ val JcTypedMethod.humanReadableSignature: String get() {
193193
it.joinToString(prefix = "<", separator = ",", postfix = ">") { it.symbol }
194194
} ?: ""
195195
return "${enclosingType.typeName}#$generics$name($params):${returnType.typeName}"
196+
}
197+
198+
fun JcClasspath.findType(name: String): JcType {
199+
return findTypeOrNull(name) ?: throw TypeNotFoundException(name)
196200
}

jacodb-core/src/main/kotlin/org/jacodb/impl/cfg/TypedMethodRefImpl.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package org.jacodb.impl.cfg
1818

1919
import org.jacodb.api.*
2020
import org.jacodb.api.cfg.*
21+
import org.jacodb.api.ext.findType
2122
import org.jacodb.api.ext.jvmName
2223
import org.jacodb.impl.cfg.util.typeName
2324
import org.jacodb.impl.softLazy
@@ -76,7 +77,7 @@ abstract class MethodSignatureRef(
7677
}
7778

7879
fun JcType.throwNotFoundException(): Nothing {
79-
throw IllegalStateException(this.methodNotFoundMessage)
80+
throw MethodNotFoundException(this.methodNotFoundMessage)
8081
}
8182

8283
}
@@ -89,14 +90,14 @@ class TypedStaticMethodRefImpl(
8990
) : MethodSignatureRef(type, name, argTypes, returnType) {
9091

9192
constructor(classpath: JcClasspath, raw: JcRawStaticCallExpr) : this(
92-
classpath.findTypeOrNull(raw.declaringClass.typeName) as JcClassType,
93+
classpath.findType(raw.declaringClass.typeName) as JcClassType,
9394
raw.methodName,
9495
raw.argumentTypes,
9596
raw.returnType
9697
)
9798

9899
override val method: JcTypedMethod by weakLazy {
99-
type.lookup.staticMethod(name, description) ?: throw IllegalStateException(methodNotFoundMessage)
100+
type.lookup.staticMethod(name, description) ?: type.throwNotFoundException()
100101
}
101102
}
102103

@@ -108,7 +109,7 @@ class TypedSpecialMethodRefImpl(
108109
) : MethodSignatureRef(type, name, argTypes, returnType) {
109110

110111
constructor(classpath: JcClasspath, raw: JcRawSpecialCallExpr) : this(
111-
classpath.findTypeOrNull(raw.declaringClass.typeName) as JcClassType,
112+
classpath.findType(raw.declaringClass.typeName) as JcClassType,
112113
raw.methodName,
113114
raw.argumentTypes,
114115
raw.returnType
@@ -130,7 +131,7 @@ class VirtualMethodRefImpl(
130131

131132
companion object {
132133
private fun JcRawCallExpr.resolvedType(classpath: JcClasspath): Pair<JcClassType, JcClassType> {
133-
val declared = classpath.findTypeOrNull(declaringClass.typeName) as JcClassType
134+
val declared = classpath.findType(declaringClass.typeName) as JcClassType
134135
if (this is JcRawInstanceExpr) {
135136
val instance = instance
136137
if (instance is JcRawLocal) {
@@ -182,7 +183,7 @@ class TypedMethodRefImpl(
182183
) : MethodSignatureRef(type, name, argTypes, returnType) {
183184

184185
constructor(classpath: JcClasspath, raw: JcRawCallExpr) : this(
185-
classpath.findTypeOrNull(raw.declaringClass.typeName) as JcClassType,
186+
classpath.findType(raw.declaringClass.typeName) as JcClassType,
186187
raw.methodName,
187188
raw.argumentTypes,
188189
raw.returnType

0 commit comments

Comments
 (0)