Skip to content

Commit

Permalink
Fix ClassCastException in FindFields
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Aug 28, 2016
1 parent 53791d3 commit 444ec02
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ fun fullyQualifiedName(sourceStr: String): String? {
val pkgMatcher = Pattern.compile("\\s*package\\s+([\\w\\.]+)").matcher(sourceStr)
val pkg = if (pkgMatcher.find()) pkgMatcher.group(1) + "." else ""

val classMatcher = Pattern.compile("\\s*(class|interface|enum)\\s+(\\w+)").matcher(sourceStr)
val classMatcher = Pattern.compile("\\s*(class|interface|enum)\\s*(<[^>]*>?)\\s+(\\w+)").matcher(sourceStr)
return if (classMatcher.find()) pkg + classMatcher.group(2) else null
}
7 changes: 5 additions & 2 deletions src/main/kotlin/com/netflix/java/refactor/find/FindFields.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ class FindFields(val clazz: String, val includeInherited: Boolean) : AstScannerB
class FindFieldScanner(val op: FindFields) : SingleCompilationUnitAstScanner<List<Field>>() {
override fun visitCompilationUnit(node: CompilationUnitTree?, p: Context?): List<Field> {
super.visitCompilationUnit(node, p)
return cu.defs.filterIsInstance<JCTree.JCClassDecl>().flatMap { superFields(it.type as Type.ClassType) }
return cu.defs.filterIsInstance<JCTree.JCClassDecl>().flatMap { superFields(it.type as Type.ClassType?) }
}

private fun superFields(type: Type.ClassType, inHierarchy: Boolean = false): List<Field> {
private fun superFields(type: Type.ClassType?, inHierarchy: Boolean = false): List<Field> {
if(type == null)
return emptyList()

if (type.supertype_field == Type.noType)
return emptyList()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.junit.Assert.assertEquals
import org.junit.Test
import kotlin.test.assertTrue

class FindFieldTest: AbstractRefactorTest() {
class FindFieldsTest : AbstractRefactorTest() {

@Test
fun findField() {
Expand Down Expand Up @@ -50,4 +50,18 @@ class FindFieldTest: AbstractRefactorTest() {
assertEquals("list", parseJava(b, a).findFieldsIncludingInherited(List::class.java).firstOrNull()?.name)
assertTrue(parseJava(b, a).findFieldsIncludingInherited(Set::class.java).isEmpty())
}

// FIXME this is intended to test the case where there is something that satisfies cu.defs.find { it.type == null }, but
// doesn't currently
@Test
fun unresolvableTypeSymbol() {
val b = java("""
import java.util.List;
public class <T extends A> B<T> {
List unresolvable;
}
""")

parseJava(b).findFields(List::class.java)
}
}

0 comments on commit 444ec02

Please sign in to comment.