@Override
public Change createChange(IProgressMonitor pm)
throws CoreException,
OperationCanceledException
{
ICompilationUnit unit = element.getCompilationUnit();
CompilationUnit astCompUnit = parse(unit, pm);
ASTNode astElement = NodeFinder.perform(astCompUnit, element.getNameRange());
MethodDeclaration astMethod = (MethodDeclaration)getParent(astElement, MethodDeclaration.class);
String newName = getArguments().getNewName();
List<TraceFnFixOperation> ops = new ArrayList<TraceFnFixOperation>(1);
TraceFnCtorFinder finder = new TraceFnCtorFinder(newName, ops);
astMethod.accept(finder);
if (ops.size() == 0)
return null;
return new TraceChange("Fix Trace", unit, ops);
}
public static class TraceFnCtorFinder extends ASTVisitor
{
private final String methodName;
private final List<TraceFnFixOperation> workingops;
public TraceFnCtorFinder(String methodName, List<TraceFnFixOperation> workingops)
{
this.methodName = methodName;
this.workingops = workingops;
}
@Override
public boolean visit(ClassInstanceCreation ctorClass)
{
Type type = ctorClass.getType();
// Only examine simple types
if (type.isSimpleType())
{
SimpleType simpleType = (SimpleType)type;
String typeName = simpleType.getName().getFullyQualifiedName();
// Check type has correct name
if ("Trace".equals(typeName))
{
List<?> arguments = ctorClass.arguments();
// Only check a single argument
if ((arguments != null) &&
(arguments.size() == 1))
{
Object arg = arguments.get(0);
// Only check a string literal argument
if (arg instanceof StringLiteral)
{
StringLiteral literal = (StringLiteral) arg;
String currentArg = literal.getLiteralValue();
// Check whether argument value is valid
if (!methodName.equals(currentArg))
{
workingops.add(new TraceFnFixOperation(literal.getStartPosition(),
literal.getLength(),
methodName));
}
}
}
}
}
return false;
}
}
public static class TraceChange extends CompilationUnitChange
{
public TraceChange(String name,
ICompilationUnit cunit,
List<TraceFnFixOperation> ops)
{
super(name, cunit);
MultiTextEdit multiTextEdit= new MultiTextEdit();
setEdit(multiTextEdit);
for (TraceFnFixOperation op : ops)
{
addEdit(new ReplaceEdit(op.startPosition,
op.length,
"\"" + op.methodName + "\""));
}
}
}