diff --git a/src/main/kotlin/com/netflix/java/refactor/find/HasType.kt b/src/main/kotlin/com/netflix/java/refactor/find/HasType.kt index f65b383..2707fea 100644 --- a/src/main/kotlin/com/netflix/java/refactor/find/HasType.kt +++ b/src/main/kotlin/com/netflix/java/refactor/find/HasType.kt @@ -1,7 +1,7 @@ package com.netflix.java.refactor.find -import com.netflix.java.refactor.ast.SingleCompilationUnitAstScanner import com.netflix.java.refactor.ast.AstScannerBuilder +import com.netflix.java.refactor.ast.SingleCompilationUnitAstScanner import com.sun.source.tree.IdentifierTree import com.sun.source.tree.MethodInvocationTree import com.sun.tools.javac.code.Symbol @@ -26,7 +26,7 @@ class HasTypeScanner(val op: HasType): SingleCompilationUnitAstScanner( val invocation = node as JCTree.JCMethodInvocation if(invocation.meth is JCTree.JCIdent) { // statically imported type - return (invocation.meth as JCTree.JCIdent)?.sym?.owner?.toString() == op.clazz + return (invocation.meth as JCTree.JCIdent).sym?.owner?.toString() == op.clazz } return super.visitMethodInvocation(node, context) } diff --git a/src/main/kotlin/com/netflix/java/refactor/fix/ChangeMethodInvocation.kt b/src/main/kotlin/com/netflix/java/refactor/fix/ChangeMethodInvocation.kt index caa340f..1e51d2d 100644 --- a/src/main/kotlin/com/netflix/java/refactor/fix/ChangeMethodInvocation.kt +++ b/src/main/kotlin/com/netflix/java/refactor/fix/ChangeMethodInvocation.kt @@ -38,7 +38,7 @@ class ChangeMethodInvocation(signature: String, val tx: RefactorTransaction) : R refactorName = name return this } - + fun changeName(nameBuilder: (Method) -> String): ChangeMethodInvocation { refactorNameBuilder = nameBuilder return this @@ -93,7 +93,7 @@ class RefactorArguments(val op: ChangeMethodInvocation) { individualArgumentRefactors.add(arg) return arg } - + fun whereArgNamesAre(vararg name: String): RefactorArguments { this.argumentNames = name.toList() return this @@ -103,12 +103,12 @@ class RefactorArguments(val op: ChangeMethodInvocation) { reorderArguments = name.toList() return this } - + fun insert(pos: Int, value: String): RefactorArguments { insertions.add(InsertArgument(pos, value)) return this } - + fun delete(pos: Int): RefactorArguments { deletions.add(DeleteArgument(pos)) return this @@ -137,10 +137,10 @@ open class RefactorArgument(val op: RefactorArguments, class ChangeMethodInvocationScanner(val op: ChangeMethodInvocation) : FixingScanner() { override fun visitMethodInvocation(node: MethodInvocationTree, context: Context): List? { val invocation = node as JCTree.JCMethodInvocation - if (op.matcher.matches(invocation)) { - return refactorMethod(invocation) - } - return null + val nestedFixes = super.visitMethodInvocation(node, context) ?: emptyList() + return nestedFixes + if (op.matcher.matches(invocation)) { + refactorMethod(invocation) + } else emptyList() } fun refactorMethod(invocation: JCTree.JCMethodInvocation): List { @@ -152,18 +152,16 @@ class ChangeMethodInvocationScanner(val op: ChangeMethodInvocation) : FixingScan else -> null } - val newName = - if(op.refactorName is String) { + val newName = + if (op.refactorName is String) { op.refactorName - } - else if(op.refactorNameBuilder != null) { + } else if (op.refactorNameBuilder != null) { op.refactorNameBuilder!!(Method(invocation.meth.toString(), invocation.source(), invocation.args.map { Argument(it.source()) })) - } - else { + } else { null } - - if(newName is String) { + + if (newName is String) { when (meth) { is JCTree.JCFieldAccess -> { val nameStart = meth.selected.getEndPosition(cu.endPositions) + 1 @@ -178,12 +176,12 @@ class ChangeMethodInvocationScanner(val op: ChangeMethodInvocation) : FixingScan if (op.refactorArguments is RefactorArguments) { if (op.refactorArguments?.reorderArguments != null) { val reorders = op.refactorArguments!!.reorderArguments!! - val paramNames = when(methSym) { + val paramNames = when (methSym) { is Symbol.MethodSymbol -> methSym.params().map { it.name.toString() } else -> null } - if(paramNames != null) { + if (paramNames != null) { var argPos = 0 reorders.forEachIndexed { paramPos, reorder -> if (invocation.arguments.size <= argPos) { @@ -193,13 +191,13 @@ class ChangeMethodInvocationScanner(val op: ChangeMethodInvocation) : FixingScan } if (paramNames[paramPos] != reorder) { - var swaps = invocation.arguments.filterIndexed { j, swap -> paramNames[Math.min(j, paramNames.size-1)] == reorder } + var swaps = invocation.arguments.filterIndexed { j, swap -> paramNames[Math.min(j, paramNames.size - 1)] == reorder } // when no source is attached, we must define names first - if(swaps.isEmpty()) { + if (swaps.isEmpty()) { val pos = op.refactorArguments?.argumentNames?.indexOf(reorder) ?: -1 if (pos >= 0 && pos < invocation.arguments.size) { - swaps = if(pos < op.refactorArguments!!.argumentNames!!.size - 1) { + swaps = if (pos < op.refactorArguments!!.argumentNames!!.size - 1) { listOf(invocation.args[pos]) } else { // this is a varargs argument, grab them all @@ -207,16 +205,14 @@ class ChangeMethodInvocationScanner(val op: ChangeMethodInvocation) : FixingScan } } } - + swaps.forEach { swap -> fixes.add(invocation.arguments[argPos].replace(swap.changesToArgument(argPos) ?: swap.source())) argPos++ } - } - else argPos++ + } else argPos++ } - } - else { + } else { // TODO what do we do when the method symbol is not present? } } else { @@ -226,20 +222,18 @@ class ChangeMethodInvocationScanner(val op: ChangeMethodInvocation) : FixingScan } } } - + op.refactorArguments?.insertions?.forEach { insertion -> - if(invocation.arguments.isEmpty()) { + if (invocation.arguments.isEmpty()) { val argStart = sourceText.indexOf('(', invocation.methodSelect.getEndPosition(cu.endPositions)) + 1 - fixes.add(insertAt(argStart, "${if(insertion.pos > 0) ", " else ""}${insertion.value}")) - } - else if(invocation.arguments.size <= insertion.pos) { + fixes.add(insertAt(argStart, "${if (insertion.pos > 0) ", " else ""}${insertion.value}")) + } else if (invocation.arguments.size <= insertion.pos) { fixes.add(insertAt(invocation.arguments.last().getEndPosition(cu.endPositions), ", ${insertion.value}")) - } - else { + } else { fixes.add(insertAt(invocation.arguments[insertion.pos].startPosition, "${insertion.value}, ")) } } - + op.refactorArguments?.deletions?.forEach { deletion -> fixes.add(invocation.arguments[deletion.pos].delete()) } @@ -274,7 +268,7 @@ class ChangeMethodInvocationScanner(val op: ChangeMethodInvocation) : FixingScan // prefix and suffix hold the special characters surrounding the values of primitive-ish types, // e.g. the "" around String, the L at the end of a long, etc. val valueMatcher = "(.*)${Pattern.quote(value.toString())}(.*)".toRegex().find(node.toString().replace("\\", "")) - return when(valueMatcher) { + return when (valueMatcher) { is MatchResult -> { val (prefix, suffix) = valueMatcher.groupValues.drop(1)