parentName = parent.getName().replace('.', '/');
}
}
// @Aspect public class ...
// TODO AV - we could point to the aop.xml that defines it and use JSR-45
LazyClassGen cg = new LazyClassGen(concreteAspect.name.replace('.', '/'), parentName, null, Modifier.PUBLIC
+ Constants.ACC_SUPER, EMPTY_STRINGS, world);
if (parent != null && parent.isParameterizedType()) {
cg.setSuperClass(parent);
}
if (perclauseString == null) {
AnnotationGen ag = new AnnotationGen(new ObjectType("org/aspectj/lang/annotation/Aspect"),
Collections.<NameValuePair> emptyList(), true, cg.getConstantPool());
cg.addAnnotation(ag);
} else {
// List elems = new ArrayList();
List<NameValuePair> elems = new ArrayList<NameValuePair>();
elems.add(new NameValuePair("value",
new SimpleElementValue(ElementValue.STRING, cg.getConstantPool(), perclauseString), cg.getConstantPool()));
AnnotationGen ag = new AnnotationGen(new ObjectType("org/aspectj/lang/annotation/Aspect"), elems, true,
cg.getConstantPool());
cg.addAnnotation(ag);
}
if (concreteAspect.precedence != null) {
SimpleElementValue svg = new SimpleElementValue(ElementValue.STRING, cg.getConstantPool(), concreteAspect.precedence);
List<NameValuePair> elems = new ArrayList<NameValuePair>();
elems.add(new NameValuePair("value", svg, cg.getConstantPool()));
AnnotationGen agprec = new AnnotationGen(new ObjectType("org/aspectj/lang/annotation/DeclarePrecedence"), elems, true,
cg.getConstantPool());
cg.addAnnotation(agprec);
}
// default constructor
LazyMethodGen init = new LazyMethodGen(Modifier.PUBLIC, Type.VOID, "<init>", EMPTY_TYPES, EMPTY_STRINGS, cg);
InstructionList cbody = init.getBody();
cbody.append(InstructionConstants.ALOAD_0);
cbody.append(cg.getFactory().createInvoke(parentName, "<init>", Type.VOID, EMPTY_TYPES, Constants.INVOKESPECIAL));
cbody.append(InstructionConstants.RETURN);
cg.addMethodGen(init);
for (Iterator<Definition.Pointcut> it = concreteAspect.pointcuts.iterator(); it.hasNext();) {
Definition.Pointcut abstractPc = (Definition.Pointcut) it.next();
// TODO AV - respect visibility instead of opening up as public?
LazyMethodGen mg = new LazyMethodGen(Modifier.PUBLIC, Type.VOID, abstractPc.name, EMPTY_TYPES, EMPTY_STRINGS, cg);
SimpleElementValue svg = new SimpleElementValue(ElementValue.STRING, cg.getConstantPool(), abstractPc.expression);
List<NameValuePair> elems = new ArrayList<NameValuePair>();
elems.add(new NameValuePair("value", svg, cg.getConstantPool()));
AnnotationGen mag = new AnnotationGen(new ObjectType("org/aspectj/lang/annotation/Pointcut"), elems, true,
cg.getConstantPool());
AnnotationAJ max = new BcelAnnotation(mag, world);
mg.addAnnotation(max);
InstructionList body = mg.getBody();
body.append(InstructionConstants.RETURN);
cg.addMethodGen(mg);
}
// Construct any defined declare error/warnings
if (concreteAspect.deows.size() > 0) {
int counter = 1;
for (Definition.DeclareErrorOrWarning deow : concreteAspect.deows) {
// Building this:
// @DeclareWarning("call(* javax.sql..*(..)) && !within(org.xyz.daos..*)")
// static final String aMessage = "Only DAOs should be calling JDBC.";
FieldGen field = new FieldGen(Modifier.FINAL, ObjectType.STRING, "rule" + (counter++), cg.getConstantPool());
SimpleElementValue svg = new SimpleElementValue(ElementValue.STRING, cg.getConstantPool(), deow.pointcut);
List<NameValuePair> elems = new ArrayList<NameValuePair>();
elems.add(new NameValuePair("value", svg, cg.getConstantPool()));
AnnotationGen mag = new AnnotationGen(new ObjectType("org/aspectj/lang/annotation/Declare"
+ (deow.isError ? "Error" : "Warning")), elems, true, cg.getConstantPool());
field.addAnnotation(mag);
field.setValue(deow.message);
cg.addField(field, null);
}
}
if (concreteAspect.pointcutsAndAdvice.size() > 0) {
int adviceCounter = 1;
for (PointcutAndAdvice paa : concreteAspect.pointcutsAndAdvice) {
generateAdviceMethod(paa, adviceCounter, cg);
adviceCounter++;
}
}
// handle the perClause
ReferenceType rt = new ReferenceType(ResolvedType.forName(concreteAspect.name).getSignature(), world);
GeneratedReferenceTypeDelegate grtd = new GeneratedReferenceTypeDelegate(rt);
grtd.setSuperclass(parent);
rt.setDelegate(grtd);
BcelPerClauseAspectAdder perClauseMunger = new BcelPerClauseAspectAdder(rt, perclauseKind);
perClauseMunger.forceMunge(cg, false);
// TODO AV - unsafe cast
// register the fresh new class into the world repository as it does not
// exist on the classpath anywhere
JavaClass jc = cg.getJavaClass((BcelWorld) world);
((BcelWorld) world).addSourceObjectType(jc, true);
bytes = jc.getBytes();
return bytes;
}