KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > transform > inlining > weaver > AfterObjectInitializationCodeAdapter


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.transform.inlining.weaver;
5
6 import com.tc.asm.MethodAdapter;
7 import com.tc.asm.MethodVisitor;
8
9 import com.tc.aspectwerkz.transform.TransformationConstants;
10
11 /**
12  * A visitor that keeps track of NEW and INVOKESPECIAL when within a constructor
13  * to flag when the object initialization has been reached (after this/super call).
14  * <p/>
15  * No regular weaving should occur before it since this(XXJP.invoke(this)) is not allowed Java code
16  *
17  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
18  */

19 public class AfterObjectInitializationCodeAdapter extends MethodAdapter implements TransformationConstants {
20
21   private String JavaDoc m_callerMemberName;
22   private int m_newCount = 0;
23   private int m_invokeSpecialCount = 0;
24   protected boolean m_isObjectInitialized = false;
25
26   public AfterObjectInitializationCodeAdapter(MethodVisitor cv, String JavaDoc callerMemberName) {
27     super(cv);
28     m_callerMemberName = callerMemberName;
29     // object initialization matters within constructors only
30
if (!m_callerMemberName.equals(INIT_METHOD_NAME)) {
31       m_isObjectInitialized = true;
32     }
33   }
34
35   public void visitTypeInsn(int opcode, String JavaDoc desc) {
36     if (opcode == NEW) {
37       m_newCount++;
38     }
39     super.visitTypeInsn(opcode, desc);
40   }
41
42   protected boolean queryCurrentMethodInsn(final int opcode,
43                                            final String JavaDoc calleeClassName,
44                                            final String JavaDoc calleeMethodName,
45                                            final String JavaDoc calleeMethodDesc) {
46     int localInvokeSpecialCount = m_invokeSpecialCount;
47     int localNewCount = m_newCount;
48
49     if (opcode == INVOKESPECIAL) {
50       localInvokeSpecialCount++;
51     }
52
53     if (m_callerMemberName.equals(INIT_METHOD_NAME)) {
54       // in ctor
55
// make sure we are after object initialization ie after
56
// the INVOKESPECIAL for this(..) / super(..)
57
// that is we have seen an INVOKESPECIAL while newCount == 0
58
// or while newCount == invokeSpecialCount - 1
59
// [ ie same with numberOfInvokeSpecialCount = 1 ]
60
if (opcode == INVOKESPECIAL) {
61         if (localNewCount == localInvokeSpecialCount - 1) {
62           return true;
63         }
64       }
65       return false;
66     }
67     return false;
68   }
69
70   public void visitMethodInsn(final int opcode,
71                               final String JavaDoc calleeClassName,
72                               final String JavaDoc calleeMethodName,
73                               final String JavaDoc calleeMethodDesc) {
74     if (opcode == INVOKESPECIAL) {
75       m_invokeSpecialCount++;
76     }
77
78     if (m_callerMemberName.equals(INIT_METHOD_NAME)) {
79       // in ctor
80
// make sure we are after object initialization ie after
81
// the INVOKESPECIAL for this(..) / super(..)
82
// that is we have seen an INVOKESPECIAL while newCount == 0
83
// or while newCount == invokeSpecialCount - 1
84
// [ ie same with numberOfInvokeSpecialCount = 1 ]
85
if (opcode == INVOKESPECIAL) {
86         if (m_newCount == m_invokeSpecialCount - 1) {
87           m_isObjectInitialized = true;
88         }
89       }
90     }
91     super.visitMethodInsn(opcode, calleeClassName, calleeMethodName, calleeMethodDesc);
92   }
93 }
94
Popular Tags