Modify a method body so that instantiation of the specified class is replaced with a call to the specified static method. For example,
replaceNew(ctPoint, ctSingleton, "createPoint")
(where
ctPoint
and
ctSingleton
are compile-time classes for class
Point
and class
Singleton
, respectively) replaces all occurrences of:
in the method body with:
Singleton.createPoint(x, y)
This enables to intercept instantiation of Point
and change the samentics. For example, the following createPoint()
implements the singleton pattern:
public static Point createPoint(int x, int y) { if (aPoint == null) aPoint = new Point(x, y); return aPoint; }
The static method call substituted for the original new
expression must be able to receive the same set of parameters as the original constructor. If there are multiple constructors with different parameter types, then there must be multiple static methods with the same name but different parameter types.
The return type of the substituted static method must be the exactly same as the type of the instantiated class specified by newClass
.
@param newClass the instantiated class.
@param calledClass the class in which the static method isdeclared.
@param calledMethod the name of the static method.