emptyConstructor = false
)
public void replaceInitializer(final Object o, Map<String, String> attributes) throws CannotCompileException, NotFoundException {
final String field = attributes.get("field");
CtClass ctClass = o instanceof CtClass ? (CtClass) o : null;
CtBehavior ctBehavior = null;
if (ctClass == null) {
ctBehavior = (CtBehavior) o;
ctClass = ctBehavior.getDeclaringClass();
}
String ctFieldClass = attributes.get("fieldClass");
if (ctFieldClass != null) {
if (ctClass == o) {
PatchLog.warning("Must set methods to run on if using fieldClass.");
return;
}
ctClass = classPool.get(ctFieldClass);
}
final CtField ctField = ctClass.getDeclaredField(field);
String code = attributes.get("code");
String clazz = attributes.get("class");
if (code == null && clazz == null) {
throw new NullPointerException("Must give code or class");
}
final String newInitialiser = code == null ? "$_ = new " + clazz + "();" : code;
Set<CtBehavior> allBehaviours = new HashSet<CtBehavior>();
if (ctBehavior == null) {
Collections.addAll(allBehaviours, ctClass.getDeclaredConstructors());
CtBehavior initialiser = ctClass.getClassInitializer();
if (initialiser != null) {
allBehaviours.add(initialiser);
}
} else {
allBehaviours.add(ctBehavior);