KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > CtorExecution


1 /**************************************************************************************
2  * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

8 package test;
9
10 import junit.framework.TestCase;
11 import org.codehaus.aspectwerkz.annotation.Before;
12 import org.codehaus.aspectwerkz.annotation.Around;
13 import org.codehaus.aspectwerkz.joinpoint.StaticJoinPoint;
14 import org.codehaus.aspectwerkz.transform.inlining.weaver.SerialVersionUidVisitor;
15
16 import java.io.Serializable JavaDoc;
17 import java.lang.reflect.Field JavaDoc;
18
19 /**
20  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
21  */

22 public class CtorExecution extends TestCase implements Serializable JavaDoc {
23
24     static int s_count = 0;
25
26     public CtorExecution m_ref;
27
28     public int m_i;// = 1;
29

30     public CtorExecution(CtorExecution ref) {
31         postInit(this);
32         //m_ref = ref;
33
}
34
35     static void postInit(CtorExecution target) {
36         ;
37     }
38
39     public CtorExecution() {
40         // tricky INVOKESPECIAL indexing
41
this(new CtorExecution((CtorExecution)null));
42         new CtorExecution((CtorExecution)null);
43         postInit(this);
44     }
45
46     public CtorExecution(String JavaDoc s) {
47         // tricky INVOKESPECIAL indexing
48
// and tricky new CtorExecution() call before instance initialization
49
// and tricky method call before instance initialization
50
super((new CtorExecution()).string(s));
51         (new CtorExecution()).string(s);
52     }
53
54     public CtorExecution(int i) {
55         // tricky field get and set before instance initialization
56
super(""+(new CtorExecution()).m_i++);
57         (new CtorExecution()).m_i++;
58     }
59
60     public String JavaDoc string(String JavaDoc s) {
61         return s;
62     }
63
64     public void testSome() {
65         s_count = 0;
66         CtorExecution me = new CtorExecution();
67         me = new CtorExecution(me);
68         me = new CtorExecution("foo");
69         me = new CtorExecution(2);
70         assertEquals(116, s_count);// don't know if it is the right number but decompiled seems ok..
71
}
72
73     public void testSerialVer() throws Throwable JavaDoc {
74         Class JavaDoc x = CtorExecution.class;
75         long l = SerialVersionUidVisitor.calculateSerialVersionUID(x);
76         // uncomment me and turn off weaver to compute the expected serialVerUID
77
//System.out.println(l);
78

79         Field f = x.getDeclaredField("serialVersionUID");
80         long uid = ((Long JavaDoc)f.get(null)).longValue();
81         //System.out.println(uid);
82
assertEquals(3813928159352352835L, uid);
83     }
84
85     public static class Aspect {
86         @Before("within(test.CtorExecution)")
87         void before(StaticJoinPoint sjp) {
88             s_count++;
89             //System.err.println(sjp.getSignature());
90
}
91
92         @Around("execution(test.CtorExecution.new(..))" +
93                 " || (call(test.CtorExecution.new(..)) && within(test.CtorExecution))")
94         Object JavaDoc around(StaticJoinPoint sjp) throws Throwable JavaDoc {
95             s_count++;
96             //System.out.println(sjp.getSignature());
97
return sjp.proceed();
98         }
99
100     }
101
102     public static void main(String JavaDoc[] args) {
103         junit.textui.TestRunner.run(suite());
104     }
105
106     public static junit.framework.Test suite() {
107         return new junit.framework.TestSuite(CtorExecution.class);
108     }
109
110 }
111
Popular Tags