*/
public void transformCode(final Context context, final Klass klass) {
// loop over all the definitions
for (Iterator it = m_definitions.iterator(); it.hasNext();) {
AspectWerkzDefinition definition = (AspectWerkzDefinition)it.next();
definition.loadAspects(context.getLoader());
final ClassGen cg = klass.getClassGen();
ClassMetaData classMetaData = BcelMetaDataMaker.
createClassMetaData(context.getJavaClass(cg));
if (classFilter(definition, classMetaData, cg)) {
return;
}
final InstructionFactory factory = new InstructionFactory(cg);
final ConstantPoolGen cpg = cg.getConstantPool();
final Method[] methods = cg.getMethods();
// get the index for the <clinit> method (if there is one)
boolean noClinitMethod = true;
int indexClinit = -1;
for (int i = 0; i < methods.length; i++) {
if (methods[i].getName().equals("<clinit>")) {
indexClinit = i;
noClinitMethod = false;
break;
}
}
// build and sort the method lookup list
final List methodLookupList = new ArrayList();
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
MethodMetaData methodMetaData = BcelMetaDataMaker.createMethodMetaData(methods[i]);
if (methodFilter(definition, classMetaData, methodMetaData, method)) {
continue;
}
methodLookupList.add(methods[i]);
}
Collections.sort(methodLookupList, BCELMethodComparator.getInstance());
final Map methodSequences = new HashMap();
final List newMethods = new ArrayList();
Method clInitMethod = null;
boolean isClassAdvised = false;
for (int i = 0; i < methods.length; i++) {
Method method = methods[i];
MethodMetaData methodMetaData = BcelMetaDataMaker.createMethodMetaData(method);
if (methodFilter(definition, classMetaData, methodMetaData, method)
|| !method.isStatic()) {
continue;
}
isClassAdvised = true;
final MethodGen mg = new MethodGen(method, cg.getClassName(), cpg);
// take care of identification of overloaded methods by
// inserting a sequence number
if (methodSequences.containsKey(method.getName())) {
int sequence = ((Integer)methodSequences.get(methods[i].getName())).intValue();
methodSequences.remove(method.getName());
sequence++;
methodSequences.put(method.getName(), new Integer(sequence));
}
else {
methodSequences.put(method.getName(), new Integer(1));
}
final int methodLookupId = methodLookupList.indexOf(method);
final int methodSequence = ((Integer)methodSequences.get(method.getName())).intValue();
addStaticJoinPointField(cpg, cg, mg, methodSequence);
// get the join point controller
final String controllerClassName = definition.getJoinPointController(
classMetaData, methodMetaData
);
if (noClinitMethod) {
// no <clinit> method exists
if (clInitMethod == null) {
clInitMethod = createClInitMethodWithStaticJoinPointField(
cpg, cg,
method,
factory,
methodSequence
);
}
else {
clInitMethod = createStaticJoinPointField(
cpg, cg, clInitMethod,
method,
factory,
methodSequence
);
}
}
else {
// we have a <clinit> method
methods[indexClinit] = createStaticJoinPointField(
cpg, cg, methods[indexClinit],
method,
factory,
methodSequence
);
}
// create a proxy method for the original method
newMethods.add(createProxyMethod(
cpg, cg,
methodMetaData.getName(),
mg, factory,
methodLookupId,
methodSequence,
method.getAccessFlags(),
definition.getUuid(),
controllerClassName
));
// add a prefix to the original method
methods[i] = addPrefixToMethod(
mg, method, methodSequence, definition.getUuid()
);
mg.setMaxLocals();
mg.setMaxStack();
}