Skip to content

Commit

Permalink
Fix nested method invocation changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jkschneider committed Oct 4, 2016
1 parent 1cfa416 commit cffabee
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 37 deletions.
4 changes: 2 additions & 2 deletions src/main/kotlin/com/netflix/java/refactor/find/HasType.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -26,7 +26,7 @@ class HasTypeScanner(val op: HasType): SingleCompilationUnitAstScanner<Boolean>(
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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -137,10 +137,10 @@ open class RefactorArgument(val op: RefactorArguments,
class ChangeMethodInvocationScanner(val op: ChangeMethodInvocation) : FixingScanner() {
override fun visitMethodInvocation(node: MethodInvocationTree, context: Context): List<RefactorFix>? {
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<RefactorFix> {
Expand All @@ -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
Expand All @@ -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) {
Expand All @@ -193,30 +191,28 @@ 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
invocation.arguments.drop(pos)
}
}
}

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 {
Expand All @@ -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())
}
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit cffabee

Please sign in to comment.