*/
JMethod newMethod = new JMethod(program, x.getSourceInfo(), newName,
enclosingType, oldReturnType, false, true, true, x.isPrivate());
// Setup all params and locals; map from the old method to the new method
JParameter thisParam = program.createParameter(null,
"this$static".toCharArray(), enclosingType, true, newMethod);
Map/* <JVariable, JVariable> */varMap = new IdentityHashMap();
for (int i = 0; i < x.params.size(); ++i) {
JParameter oldVar = (JParameter) x.params.get(i);
JParameter newVar = program.createParameter(oldVar.getSourceInfo(),
oldVar.getName().toCharArray(), oldVar.getType(), oldVar.isFinal(),
newMethod);
varMap.put(oldVar, newVar);
}
newMethod.freezeParamTypes();
// Copy all locals over to the new method
for (int i = 0; i < x.locals.size(); ++i) {
JLocal oldVar = (JLocal) x.locals.get(i);
JLocal newVar = program.createLocal(oldVar.getSourceInfo(),
oldVar.getName().toCharArray(), oldVar.getType(), oldVar.isFinal(),
newMethod);
varMap.put(oldVar, newVar);
}
x.locals.clear();
// Move the body of the instance method to the static method
newMethod.body.statements.addAll(x.body.statements);
x.body.statements.clear();
/*
* Rewrite the method body. Update all thisRefs to paramrefs. Update
* paramRefs and localRefs to target the params/locals in the new method.
*/
RewriteMethodBody rewriter = new RewriteMethodBody(thisParam, varMap);
rewriter.accept(newMethod);
SourceInfo bodyInfo = x.body.getSourceInfo();
// delegate from the instance method to the static method
JMethodCall newCall = new JMethodCall(program, bodyInfo, null, newMethod);
newCall.getArgs().add(program.getExprThisRef(bodyInfo, enclosingType));
for (int i = 0; i < x.params.size(); ++i) {
JParameter param = (JParameter) x.params.get(i);
newCall.getArgs().add(new JParameterRef(program, bodyInfo, param));
}
JStatement statement;
if (oldReturnType == program.getTypeVoid()) {
statement = newCall.makeStatement();