From 444ec02bee6f3069fba8e552effd6724f21e651a Mon Sep 17 00:00:00 2001 From: Jon Schneider Date: Sat, 27 Aug 2016 23:39:01 -0700 Subject: [PATCH] Fix ClassCastException in FindFields --- .../java/refactor/AbstractRefactorTest.kt | 2 +- .../com/netflix/java/refactor/find/FindFields.kt | 7 +++++-- .../find/{FindFieldTest.kt => FindFieldsTest.kt} | 16 +++++++++++++++- 3 files changed, 21 insertions(+), 4 deletions(-) rename src/test/kotlin/com/netflix/java/refactor/find/{FindFieldTest.kt => FindFieldsTest.kt} (75%) diff --git a/src/main/kotlin/com/netflix/java/refactor/AbstractRefactorTest.kt b/src/main/kotlin/com/netflix/java/refactor/AbstractRefactorTest.kt index 0bb2a18..fd381e9 100644 --- a/src/main/kotlin/com/netflix/java/refactor/AbstractRefactorTest.kt +++ b/src/main/kotlin/com/netflix/java/refactor/AbstractRefactorTest.kt @@ -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 } diff --git a/src/main/kotlin/com/netflix/java/refactor/find/FindFields.kt b/src/main/kotlin/com/netflix/java/refactor/find/FindFields.kt index b892759..82f0853 100644 --- a/src/main/kotlin/com/netflix/java/refactor/find/FindFields.kt +++ b/src/main/kotlin/com/netflix/java/refactor/find/FindFields.kt @@ -18,10 +18,13 @@ class FindFields(val clazz: String, val includeInherited: Boolean) : AstScannerB class FindFieldScanner(val op: FindFields) : SingleCompilationUnitAstScanner>() { override fun visitCompilationUnit(node: CompilationUnitTree?, p: Context?): List { super.visitCompilationUnit(node, p) - return cu.defs.filterIsInstance().flatMap { superFields(it.type as Type.ClassType) } + return cu.defs.filterIsInstance().flatMap { superFields(it.type as Type.ClassType?) } } - private fun superFields(type: Type.ClassType, inHierarchy: Boolean = false): List { + private fun superFields(type: Type.ClassType?, inHierarchy: Boolean = false): List { + if(type == null) + return emptyList() + if (type.supertype_field == Type.noType) return emptyList() diff --git a/src/test/kotlin/com/netflix/java/refactor/find/FindFieldTest.kt b/src/test/kotlin/com/netflix/java/refactor/find/FindFieldsTest.kt similarity index 75% rename from src/test/kotlin/com/netflix/java/refactor/find/FindFieldTest.kt rename to src/test/kotlin/com/netflix/java/refactor/find/FindFieldsTest.kt index f1a4920..57f6e36 100644 --- a/src/test/kotlin/com/netflix/java/refactor/find/FindFieldTest.kt +++ b/src/test/kotlin/com/netflix/java/refactor/find/FindFieldsTest.kt @@ -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() { @@ -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 B { + List unresolvable; + } + """) + + parseJava(b).findFields(List::class.java) + } } \ No newline at end of file