1 8 package org.codehaus.aspectwerkz.transform.inlining.weaver; 9 10 11 import org.codehaus.aspectwerkz.joinpoint.management.JoinPointType; 12 import org.codehaus.aspectwerkz.reflect.ClassInfo; 13 import org.codehaus.aspectwerkz.transform.Context; 14 import org.codehaus.aspectwerkz.transform.TransformationConstants; 15 import org.codehaus.aspectwerkz.transform.TransformationUtil; 16 import org.codehaus.aspectwerkz.transform.inlining.AsmHelper; 17 import org.codehaus.aspectwerkz.transform.inlining.ContextImpl; 18 import org.codehaus.aspectwerkz.transform.inlining.EmittedJoinPoint; 19 import org.objectweb.asm.Attribute; 20 import org.objectweb.asm.ClassAdapter; 21 import org.objectweb.asm.ClassVisitor; 22 import org.objectweb.asm.CodeVisitor; 23 import org.objectweb.asm.Type; 24 25 import java.util.Set ; 26 27 35 public class StaticInitializationVisitor extends ClassAdapter implements TransformationConstants { 36 37 private final ContextImpl m_ctx; 38 private String m_declaringTypeName; 39 private final Set m_addedMethods; 40 41 48 public StaticInitializationVisitor( final ClassVisitor cv, 49 final Context ctx, 50 final Set addedMethods) { 51 super(cv); 52 m_ctx = (ContextImpl) ctx; 53 m_addedMethods = addedMethods; 54 } 55 56 65 public void visit( final int version, 66 final int access, 67 final String name, 68 final String superName, 69 final String [] interfaces, 70 final String sourceFile) { 71 m_declaringTypeName = name; 72 super.visit(version, access, name, superName, interfaces, sourceFile); 73 } 74 75 85 public CodeVisitor visitMethod( final int access, 86 final String name, 87 final String desc, 88 final String [] exceptions, 89 final Attribute attrs) { 90 if(!CLINIT_METHOD_NAME.equals(name)) { 91 return super.visitMethod(access, name, desc, exceptions, attrs); 92 } 93 94 String prefixedOriginalName = TransformationUtil.getPrefixedOriginalClinitName(m_declaringTypeName); 95 if (m_addedMethods.contains(AlreadyAddedMethodAdapter.getMethodKey(prefixedOriginalName, CLINIT_METHOD_SIGNATURE))) { 96 return super.visitMethod(access, name, desc, exceptions, attrs); 97 } 98 99 m_ctx.markAsAdvised(); 100 101 createProxyMethod(access, name, desc, exceptions, attrs); 103 104 return cv.visitMethod(access + ACC_PUBLIC, prefixedOriginalName, desc, exceptions, attrs); 106 } 107 108 118 private void createProxyMethod( final int access, 119 final String name, 120 final String desc, 121 final String [] exceptions, 122 final Attribute attrs) { 123 CodeVisitor mv = cv.visitMethod(access, name, desc, exceptions, attrs); 124 125 mv.visitInsn(ACONST_NULL); 127 128 int joinPointHash = AsmHelper.calculateMethodHash(name, desc); 129 String joinPointClassName = TransformationUtil 130 .getJoinPointClassName( m_declaringTypeName, 131 name, 132 desc, 133 m_declaringTypeName, 134 JoinPointType.STATIC_INITIALIZATION_INT, 135 joinPointHash); 136 137 mv.visitMethodInsn(INVOKESTATIC, 138 joinPointClassName, 139 INVOKE_METHOD_NAME, 140 TransformationUtil.getInvokeSignatureForCodeJoinPoints( access, 141 desc, 142 m_declaringTypeName, 143 m_declaringTypeName)); 144 145 AsmHelper.addReturnStatement(mv, Type.VOID_TYPE); 146 mv.visitMaxs(0, 0); 147 148 m_ctx.addEmittedJoinPoint( 150 new EmittedJoinPoint(JoinPointType.STATIC_INITIALIZATION_INT, 151 m_declaringTypeName, 152 name, 153 desc, 154 access, 155 m_declaringTypeName, 156 name, 157 desc, 158 access, 159 joinPointHash, 160 joinPointClassName, 161 EmittedJoinPoint.NO_LINE_NUMBER 162 ) 163 ); 164 } 165 } 166 | Popular Tags |