Skip to content

Commit

Permalink
An exception null cannot be cast to non-null type org.jacodb.api.JcCl…
Browse files Browse the repository at this point in the history
…assType occurs #200 (#201)
  • Loading branch information
lehvolk authored Nov 14, 2023
1 parent 587d560 commit e2bd57c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
6 changes: 6 additions & 0 deletions jacodb-api/src/main/kotlin/org/jacodb/api/Exceptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ package org.jacodb.api
*/
class NoClassInClasspathException(val className: String) : Exception("Class $className not found in classpath")

/**
* This exception should be thrown when classpath is incomplete
*/
class TypeNotFoundException(val typeName: String) : Exception("Type $typeName not found in classpath")

class MethodNotFoundException(msg: String) : Exception(msg)

fun String.throwClassNotFound(): Nothing {
throw NoClassInClasspathException(this)
Expand Down
4 changes: 4 additions & 0 deletions jacodb-api/src/main/kotlin/org/jacodb/api/ext/JcTypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,8 @@ val JcTypedMethod.humanReadableSignature: String get() {
it.joinToString(prefix = "<", separator = ",", postfix = ">") { it.symbol }
} ?: ""
return "${enclosingType.typeName}#$generics$name($params):${returnType.typeName}"
}

fun JcClasspath.findType(name: String): JcType {
return findTypeOrNull(name) ?: throw TypeNotFoundException(name)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package org.jacodb.impl.cfg

import org.jacodb.api.*
import org.jacodb.api.cfg.*
import org.jacodb.api.ext.findType
import org.jacodb.api.ext.jvmName
import org.jacodb.impl.cfg.util.typeName
import org.jacodb.impl.softLazy
Expand Down Expand Up @@ -76,7 +77,7 @@ abstract class MethodSignatureRef(
}

fun JcType.throwNotFoundException(): Nothing {
throw IllegalStateException(this.methodNotFoundMessage)
throw MethodNotFoundException(this.methodNotFoundMessage)
}

}
Expand All @@ -89,14 +90,14 @@ class TypedStaticMethodRefImpl(
) : MethodSignatureRef(type, name, argTypes, returnType) {

constructor(classpath: JcClasspath, raw: JcRawStaticCallExpr) : this(
classpath.findTypeOrNull(raw.declaringClass.typeName) as JcClassType,
classpath.findType(raw.declaringClass.typeName) as JcClassType,
raw.methodName,
raw.argumentTypes,
raw.returnType
)

override val method: JcTypedMethod by weakLazy {
type.lookup.staticMethod(name, description) ?: throw IllegalStateException(methodNotFoundMessage)
type.lookup.staticMethod(name, description) ?: type.throwNotFoundException()
}
}

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

constructor(classpath: JcClasspath, raw: JcRawSpecialCallExpr) : this(
classpath.findTypeOrNull(raw.declaringClass.typeName) as JcClassType,
classpath.findType(raw.declaringClass.typeName) as JcClassType,
raw.methodName,
raw.argumentTypes,
raw.returnType
Expand All @@ -130,7 +131,7 @@ class VirtualMethodRefImpl(

companion object {
private fun JcRawCallExpr.resolvedType(classpath: JcClasspath): Pair<JcClassType, JcClassType> {
val declared = classpath.findTypeOrNull(declaringClass.typeName) as JcClassType
val declared = classpath.findType(declaringClass.typeName) as JcClassType
if (this is JcRawInstanceExpr) {
val instance = instance
if (instance is JcRawLocal) {
Expand Down Expand Up @@ -182,7 +183,7 @@ class TypedMethodRefImpl(
) : MethodSignatureRef(type, name, argTypes, returnType) {

constructor(classpath: JcClasspath, raw: JcRawCallExpr) : this(
classpath.findTypeOrNull(raw.declaringClass.typeName) as JcClassType,
classpath.findType(raw.declaringClass.typeName) as JcClassType,
raw.methodName,
raw.argumentTypes,
raw.returnType
Expand Down

0 comments on commit e2bd57c

Please sign in to comment.