JMethod newMethod = new JMethod(sourceInfo.makeChild(
CreateStaticImplsVisitor.class, "Devirtualized function"), newName,
enclosingType, returnType, false, true, true, x.isPrivate());
// Setup parameters; map from the old params to the new params
JParameter thisParam = JParameter.create(sourceInfo.makeChild(
CreateStaticImplsVisitor.class, "Instance parameter"), "this$static",
enclosingType, true, true, newMethod);
Map<JParameter, JParameter> varMap = new IdentityHashMap<JParameter, JParameter>();
for (int i = 0; i < x.getParams().size(); ++i) {
JParameter oldVar = x.getParams().get(i);
JParameter newVar = JParameter.create(oldVar.getSourceInfo(),
oldVar.getName(), oldVar.getType(), oldVar.isFinal(), false,
newMethod);
varMap.put(oldVar, newVar);
}
// Set the new original param types based on the old original param types
List<JType> originalParamTypes = new ArrayList<JType>();
originalParamTypes.add(enclosingType);
originalParamTypes.addAll(x.getOriginalParamTypes());
newMethod.setOriginalTypes(x.getOriginalReturnType(), originalParamTypes);
// Move the body of the instance method to the static method
JAbstractMethodBody movedBody = x.getBody();
newMethod.setBody(movedBody);
// Create a new body for the instance method that delegates to the static
SourceInfo delegateCallSourceInfo = sourceInfo.makeChild(
CreateStaticImplsVisitor.class, "Degelgating to devirtualized method");
JMethodBody newBody = new JMethodBody(delegateCallSourceInfo);
x.setBody(newBody);
JMethodCall newCall = new JMethodCall(delegateCallSourceInfo, null,
newMethod);
newCall.addArg(new JThisRef(delegateCallSourceInfo, enclosingType));
for (int i = 0; i < x.getParams().size(); ++i) {
JParameter param = x.getParams().get(i);
newCall.addArg(new JParameterRef(delegateCallSourceInfo, param));
}
JStatement statement;
if (returnType == program.getTypeVoid()) {
statement = newCall.makeStatement();