if (onTypeBinding.isInterface()) {
+ classScope.problemReporter().signalError(sourceStart, sourceEnd,
+ "can't define constructors on interfaces");
ignoreFurtherInvestigation = true;
return null;
}
<compile files="AfterReturningInterfaceConstructor.java"/>
<run class="AfterReturningInterfaceConstructor"/>
</ajc-test>
+
+ <ajc-test dir="new" pr="889"
+ title="after returning advice on interface constructor - error">
+ <compile files="AfterReturningInterfaceConstructorCE.java">
+ <message kind="error" line="26"/>
+ </compile>
+ </ajc-test>
<ajc-test dir="bugs" pr="900"
title="after advice on static call join point">
</compile>
</ajc-test>
+ <ajc-test dir="bugs"
+ pr="49295"
+ title="declare warning on subtype constructor">
+ <compile files="SubtypeConstructorCW.java" >
+ <message kind="warning" line="5" text="String as first"/>
+ <message kind="warning" line="10" text="String as first"/>
+ </compile>
+ <run class="SubtypeConstructorCW"/>
+ </ajc-test>
+
</suite>
<run class="AfterReturningParamMatching"/>
</ajc-test>
- <ajc-test dir="bugs"
- pr="49295"
- title="declare warning on subtype constructor">
- <compile files="SubtypeConstructorCW.java" >
- <message kind="warning" line="5" text="String as first"/>
- <message kind="warning" line="10" text="String as first"/>
- </compile>
- <run class="SubtypeConstructorCW"/>
- </ajc-test>
-
<ajc-test dir="bugs/interAbstract"
pr="49784"
title="aspect declares interface method (abstract decl, default impl)">
<run class="Main"/>
</ajc-test>
+ <ajc-test dir="bugs" pr="36787"
+ title="interface initialization order">
+ <compile files="InterfaceInitializerOrder.java"/>
+ <run class="InterfaceInitializerOrder"/>
+ </ajc-test>
+
</suite>
--- /dev/null
+import org.aspectj.testing.Tester;
+
+public class InterfaceInitializerOrder {
+ public static void main(String[] args) {
+ Base base = new Base();
+ Tester.checkEqual(InitAspect.inits.toString(), "Super1,Super2,SuperInterface,Base,");
+ }
+}
+
+class Super1 {}
+
+class Super2 extends Super1 {}
+
+interface SuperInterface {}
+
+class Base extends Super2 implements SuperInterface { }
+
+aspect InitAspect {
+ public static StringBuffer inits = new StringBuffer();
+
+ pointcut outerMatch() : initialization(new(..)) && !within(InitAspect);
+ before() : outerMatch() {
+ inits.append(thisJoinPoint.getSignature().getDeclaringType().getName());
+ inits.append(",");
+ }
+}
}
}
class F implements Runnable {
- F(int i) {// CW 10
- }
+ F(int i) {}// CW 10
+
public void run() {
}
}
}
static {
Tester.clearEvents();
- // new(..) for both class and interface
- Tester.expectEventsInString("after-c,after-c,c,after-d,after-d,d");
+ // new(..) for just class
+ Tester.expectEventsInString("after-c,c,after-d,d");
}
}
static final String afterThrowing = "after() throwing(): ";
static final String c = "C()";
static final String i = "I()";
- static final String cjp = "execution(" + c + ")";
- static final String ijp = "execution(" + i + ")";
+ static final String cjp = "initialization(" + c + ")";
+ static final String ijp = "initialization(" + i + ")";
static void e(String event) {
//System.err.println("act event: " + event); // XXX
}
aspect A {
- /** must pick out both interface and implementor constructor execution */
- pointcut pc(): execution(new(..)) && !within(A);
+ /** must pick out both interface and implementor initializers */
+ pointcut pc(): initialization(new(..)) && !within(A);
before(): pc() {
U.e(U.before + thisJoinPoint);
public static void main (String[] args) {
Tester.expectEvent("constructor");
Tester.expectEvent("advice");
- new C();
+ I i = new C();
+
+ Tester.checkEqual(i.i, 2, "i.i");
+
Tester.checkAllEvents();
}
}
aspect A {
int I.i;
- I.new() {
- i = 2;
+ after(I i) returning: this(i) && initialization(I.new(..)) {
+ i.i = 2;
}
- after() returning: execution(I.new()) {
+ after() returning: initialization(I.new(..)) {
Tester.event("advice");
}
}
--- /dev/null
+
+import org.aspectj.testing.Tester;
+
+/** @testcase PR#889 after returning advice on interface constructor */
+public class AfterReturningInterfaceConstructorCE {
+ public static void main (String[] args) {
+ Tester.expectEvent("constructor");
+ Tester.expectEvent("advice");
+ I i = new C();
+ System.out.println("i.i: " + i.i);
+
+ Tester.checkAllEvents();
+ }
+}
+
+interface I {}
+
+class C implements I {
+ C() {
+ Tester.event("constructor");
+ }
+}
+
+aspect A {
+ int I.i;
+ I.new() {
+ i = 2;
+ System.out.println("running I.new()");
+ }
+ after() returning: execution(I.new()) { // ERR: can't define constructor on interface
+ Tester.event("advice");
+ }
+}
interface LoggedType {
}
declare parents: C implements LoggedType;
- after() : within(LoggedType+)
+ after(): within(LoggedType+)
//&& !initialization(new(..))
//&& !preinitialization(new(..)) // 1.1 only
{
before(I i): initialization(I.new()) && this(i) {
Tester.checkEqual(((C)i).state, "C-constructed", thisJoinPoint.toString());
}
- before(I i): execution(I.new()) && this(i) {
- Tester.checkEqual(((C)i).state, "C-constructed", thisJoinPoint.toString());
- Tester.note("constructed I");
- }
+// before(I i): execution(I.new()) && this(i) {
+// Tester.checkEqual(((C)i).state, "C-constructed", thisJoinPoint.toString());
+// Tester.note("constructed I");
+// }
after(I i): initialization(I.new()) && this(i) {
Tester.checkEqual(((C)i).state, "C-constructed", thisJoinPoint.toString());
Tester.note("initialized I");
Tester.check("constructed SubC");
Tester.check("initialized I");
- Tester.check("constructed I");
+ //Tester.check("constructed I");
Tester.check("static initialized C");
Tester.check("static initialized SubC");
, "before AllTargetJoinPoints set(String TargetClass.staticString)"
, "before AllTargetJoinPoints preinitialization(TargetClass())"
, "before AllTargetJoinPoints initialization(java.lang.Runnable())"
- , "before AllTargetJoinPoints execution(java.lang.Runnable())"
+ //, "before AllTargetJoinPoints execution(java.lang.Runnable())"
, "before AllTargetJoinPoints initialization(TargetClass())"
//, "before AllTargetJoinPoints execution(TargetClass.<init>)"
, "before AllTargetJoinPoints set(String TargetClass.string)"
, "before AllTargetJoinPoints preinitialization(TargetClass())"
, "before AllTargetJoinPoints initialization(TargetClass())"
, "before AllTargetJoinPoints initialization(java.lang.Runnable())"
- , "before AllTargetJoinPoints execution(java.lang.Runnable())"
+ //, "before AllTargetJoinPoints execution(java.lang.Runnable())"
//, "before AllTargetJoinPoints execution(TargetClass.<init>)"
, "before AllTargetJoinPoints set(String TargetClass.string)"
, "before AllTargetJoinPoints execution(TargetClass())"
Range.genStart(body, body.getStart().getNext()),
Range.genEnd(body, call.getPrev()));
} else {
- // assert s.getKind() == Shadow.Initialization
+ // assert s.getKind() == Shadow.Initialization
r.associateWithTargets(
Range.genStart(body, call.getNext()),
Range.genEnd(body));
// walk the body
boolean beforeSuperOrThisCall = true;
- if (shouldWeaveBody(mg)) { //!mg.isAjSynthetic()) {
- for (InstructionHandle h = mg.getBody().getStart();
- h != null;
- h = h.getNext()) {
- if (h == superOrThisCall) {
- beforeSuperOrThisCall = false;
- continue;
+ if (shouldWeaveBody(mg)) {
+ if (canMatchBodyShadows) {
+ for (InstructionHandle h = mg.getBody().getStart();
+ h != null;
+ h = h.getNext()) {
+ if (h == superOrThisCall) {
+ beforeSuperOrThisCall = false;
+ continue;
+ }
+ match(mg, h, beforeSuperOrThisCall ? null : enclosingShadow, shadowAccumulator);
}
- match(mg, h, beforeSuperOrThisCall ? null : enclosingShadow, shadowAccumulator);
}
match(enclosingShadow, shadowAccumulator);
}
// XXX we don't do pre-inits of interfaces
- // now add interface inits and cexecs
+ // now add interface inits
if (superOrThisCall != null && ! isThisCall(superOrThisCall)) {
InstructionHandle curr = enclosingShadow.getRange().getStart();
for (Iterator i = addedSuperInitializersAsList.iterator(); i.hasNext(); ) {
IfaceInitList l = (IfaceInitList) i.next();
- // generate the cexec jp
+
Member ifaceInitSig = AjcMemberMaker.interfaceConstructor(l.onType);
- BcelShadow cexecShadow =
- BcelShadow.makeIfaceConstructorExecution(
- world,
- mg,
- curr,
- ifaceInitSig);
- if (match(cexecShadow, shadowAccumulator)) {
- cexecShadow.getRange().getBody().append(
- cexecShadow.getRange().getStart(),
- InstructionConstants.NOP);
- }
- // generate the init jp around it
+
BcelShadow initShadow =
- BcelShadow.makeIfaceInitialization(
- world,
- mg,
- cexecShadow,
- ifaceInitSig);
- match(initShadow, shadowAccumulator);
+ BcelShadow.makeIfaceInitialization(world, mg, ifaceInitSig);
+
// insert code in place
InstructionList inits = genInitInstructions(l.list, false);
- initShadow.getRange().insert(inits, Range.InsideAfter);
+ if (match(initShadow, shadowAccumulator) || !inits.isEmpty()) {
+ initShadow.initIfaceInitializer(curr);
+ initShadow.getRange().insert(inits, Range.OutsideBefore);
+ }
}
// now we add our initialization code
public static BcelShadow makeIfaceInitialization(
BcelWorld world,
LazyMethodGen constructor,
- BcelShadow ifaceCExecShadow,
Member interfaceConstructorSignature)
{
InstructionList body = constructor.getBody();
constructor,
null);
s.fallsThrough = true;
- ShadowRange r = new ShadowRange(body);
- r.associateWithShadow(s);
- InstructionHandle start = Range.genStart(body, ifaceCExecShadow.getRange().getStart());
- InstructionHandle end = Range.genEnd(body, ifaceCExecShadow.getRange().getEnd());
-
- r.associateWithTargets(start, end);
+// ShadowRange r = new ShadowRange(body);
+// r.associateWithShadow(s);
+// InstructionHandle start = Range.genStart(body, handle);
+// InstructionHandle end = Range.genEnd(body, handle);
+//
+// r.associateWithTargets(start, end);
return s;
}
-
- public static BcelShadow makeIfaceConstructorExecution(
- BcelWorld world,
- LazyMethodGen constructor,
- InstructionHandle next,
- Member interfaceConstructorSignature)
- {
- // final InstructionFactory fact = constructor.getEnclosingClass().getFactory();
- InstructionList body = constructor.getBody();
- // TypeX inType = constructor.getEnclosingClass().getType();
- BcelShadow s =
- new BcelShadow(
- world,
- ConstructorExecution,
- interfaceConstructorSignature,
- constructor,
- null);
- s.fallsThrough = true;
- ShadowRange r = new ShadowRange(body);
- r.associateWithShadow(s);
- // ??? this may or may not work
- InstructionHandle start = Range.genStart(body, next);
- //InstructionHandle end = Range.genEnd(body, body.append(start, fact.NOP));
- InstructionHandle end = Range.genStart(body, next);
- //body.append(start, fact.NOP);
-
- r.associateWithTargets(start, end);
- return s;
+
+ public void initIfaceInitializer(InstructionHandle end) {
+ final InstructionList body = enclosingMethod.getBody();
+ ShadowRange r = new ShadowRange(body);
+ r.associateWithShadow(this);
+ InstructionHandle nop = body.insert(end, InstructionConstants.NOP);
+
+ r.associateWithTargets(
+ Range.genStart(body, nop),
+ Range.genEnd(body, nop));
}
+// public static BcelShadow makeIfaceConstructorExecution(
+// BcelWorld world,
+// LazyMethodGen constructor,
+// InstructionHandle next,
+// Member interfaceConstructorSignature)
+// {
+// // final InstructionFactory fact = constructor.getEnclosingClass().getFactory();
+// InstructionList body = constructor.getBody();
+// // TypeX inType = constructor.getEnclosingClass().getType();
+// BcelShadow s =
+// new BcelShadow(
+// world,
+// ConstructorExecution,
+// interfaceConstructorSignature,
+// constructor,
+// null);
+// s.fallsThrough = true;
+// ShadowRange r = new ShadowRange(body);
+// r.associateWithShadow(s);
+// // ??? this may or may not work
+// InstructionHandle start = Range.genStart(body, next);
+// //InstructionHandle end = Range.genEnd(body, body.append(start, fact.NOP));
+// InstructionHandle end = Range.genStart(body, next);
+// //body.append(start, fact.NOP);
+//
+// r.associateWithTargets(start, end);
+// return s;
+// }
+
/** Create an initialization join point associated with a constructor, but not
* with any body of code yet. If this is actually matched, it's range will be set