Skip to content

Commit

Permalink
Added MultiCatchType, no more IllegalArgumentException when trying to…
Browse files Browse the repository at this point in the history
… determine type of multi-catch statement
  • Loading branch information
jkschneider committed Jun 16, 2017
1 parent 90889a8 commit 259ea6b
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 29 deletions.
12 changes: 6 additions & 6 deletions rewrite-core/src/main/kotlin/com/netflix/rewrite/ast/Cursor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ data class Cursor(val path: List<Tree>) {
val Empty = Cursor(emptyList())
}

override fun equals(other: Any?): Boolean = if(other is Cursor && path.size == other.path.size) {
path.forEachIndexed { i, _ ->
if(other.path[i] != path[i])
return@equals false
override fun equals(other: Any?): Boolean = other is Cursor && path.size == other.path.size && path.let { p ->
for(i in p.indices) {
if(p[i] != other.path[i])
return false
}
true
} else false
return true
}

override fun hashCode(): Int = path.hashCode()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ sealed class Tr : Serializable, Tree {
override val formatting: Formatting = Formatting.Empty,
override val id: Long = id()): TypeTree, Tr() {
@get:JsonIgnore
override val type: Type by lazy { throw IllegalArgumentException("Multi-catch does not represent a single type") }
override val type: Type by lazy { Type.MultiCatchType(alternatives.map { it.type }.filterNotNull()) }

override fun <R> accept(v: AstVisitor<R>): R = v.visitMultiCatch(this)
}
Expand Down
40 changes: 18 additions & 22 deletions rewrite-core/src/main/kotlin/com/netflix/rewrite/ast/Type.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.koloboke.collect.map.hash.HashObjObjMap
import com.koloboke.collect.map.hash.HashObjObjMaps
import com.koloboke.collect.set.hash.HashObjSet
import com.koloboke.collect.set.hash.HashObjSets
import com.netflix.rewrite.ast.Type.Companion.deepEquals
import java.io.Serializable

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator::class, property = "@ref")
Expand All @@ -37,9 +38,13 @@ sealed class Type: Serializable {
is Primitive -> t is Primitive && this == t
is Var -> t is Var && this.deepEquals(t)
is ShallowClass -> t is ShallowClass && fullyQualifiedName == t.fullyQualifiedName
is MultiCatchType -> t is MultiCatchType && throwableTypes.size == t.throwableTypes.size &&
throwableTypes.indices.all { i -> throwableTypes[i].deepEquals(t.throwableTypes[i]) }
}
}

data class MultiCatchType(val throwableTypes: List<Type>): Type()

/**
* Reduces memory and CPU footprint when deep class insight isn't necessary, such as
* for the type parameters of a Type.Class
Expand Down Expand Up @@ -77,31 +82,22 @@ sealed class Type: Serializable {
if(c == null || fullyQualifiedName != c.fullyQualifiedName)
return false

val membersEqual = if(members.size == c.members.size) {
var equal = true
members.forEachIndexed { i, m ->
if(!c.members[i].deepEquals(m)) {
equal = false
return@forEachIndexed
}
}
equal
} else false
if(members.size != c.members.size)
return false
members.indices.forEach {
if(!members[it].deepEquals(c.members[it])) return@deepEquals false
}

val supertypeEqual = supertype.deepEquals(c.supertype)
if(!supertype.deepEquals(c.supertype))
return false

val typeParametersEqual = if(typeParameters.size == c.typeParameters.size) {
var equal = true
typeParameters.forEachIndexed { i, param ->
if(!c.typeParameters[i].deepEquals(param)) {
equal = false
return@forEachIndexed
}
}
equal
} else false
if(typeParameters.size != c.typeParameters.size)
return false
typeParameters.indices.forEach {
if(!typeParameters[it].deepEquals(c.typeParameters[it])) return@deepEquals false
}

return membersEqual && supertypeEqual && typeParametersEqual
return true
}

companion object {
Expand Down

0 comments on commit 259ea6b

Please sign in to comment.