001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.aspectwerkz.aspect.container;
005:
006: import com.tc.asm.MethodVisitor;
007: import com.tc.asm.Label;
008:
009: /**
010: * Abstract class for lazy model that instantiate the aspect as soon as aspectOf is called
011: * (f.e. perThread, perClass)
012: *
013: * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
014: */
015: public abstract class LazyPerXFactoryCompiler extends
016: AbstractAspectFactoryCompiler {
017:
018: public LazyPerXFactoryCompiler(String uuid, String aspectClassName,
019: String aspectQualifiedName, String containerClassName,
020: String rawParameters, ClassLoader loader) {
021: super (uuid, aspectClassName, aspectQualifiedName,
022: containerClassName, rawParameters, loader);
023: }
024:
025: protected abstract String getXSignature();
026:
027: protected void createAspectOf() {
028: m_cw.visitField(ACC_PRIVATE + ACC_STATIC,
029: FACTORY_ASPECTS_FIELD_NAME, MAP_CLASS_SIGNATURE, null,
030: null);
031:
032: m_clinit.visitTypeInsn(NEW, "java/util/WeakHashMap");
033: m_clinit.visitInsn(DUP);
034: m_clinit.visitMethodInsn(INVOKESPECIAL,
035: "java/util/WeakHashMap", INIT_METHOD_NAME,
036: NO_PARAM_RETURN_VOID_SIGNATURE);
037: m_clinit.visitFieldInsn(PUTSTATIC, m_aspectFactoryClassName,
038: FACTORY_ASPECTS_FIELD_NAME, MAP_CLASS_SIGNATURE);
039:
040: MethodVisitor cv = m_cw.visitMethod(ACC_PUBLIC + ACC_STATIC
041: + ACC_FINAL, FACTORY_ASPECTOF_METHOD_NAME, "("
042: + getXSignature() + ")" + m_aspectClassSignature, null,
043: null);
044:
045: cv.visitFieldInsn(GETSTATIC, m_aspectFactoryClassName,
046: FACTORY_ASPECTS_FIELD_NAME, MAP_CLASS_SIGNATURE);
047: cv.visitVarInsn(ALOAD, 0);//Class
048: cv.visitMethodInsn(INVOKEINTERFACE, MAP_CLASS_NAME,
049: "getDefault", "(Ljava/lang/Object;)Ljava/lang/Object;");
050: cv.visitVarInsn(ASTORE, 1);
051: cv.visitVarInsn(ALOAD, 1);
052: Label ifBound = new Label();
053: cv.visitJumpInsn(IFNONNULL, ifBound);
054: if (m_hasAspectContainer) {
055: cv.visitFieldInsn(GETSTATIC, m_aspectFactoryClassName,
056: FACTORY_CONTAINER_FIELD_NAME,
057: ASPECT_CONTAINER_CLASS_SIGNATURE);
058: cv.visitMethodInsn(INVOKEINTERFACE,
059: ASPECT_CONTAINER_CLASS_NAME,
060: ASPECT_CONTAINER_ASPECTOF_METHOD_NAME, "("
061: + getXSignature() + ")Ljava/lang/Object;");
062: cv.visitTypeInsn(CHECKCAST, m_aspectClassName);
063: } else {
064: cv.visitTypeInsn(NEW, m_aspectClassName);
065: cv.visitInsn(DUP);
066: cv.visitMethodInsn(INVOKESPECIAL, m_aspectClassName,
067: INIT_METHOD_NAME, NO_PARAM_RETURN_VOID_SIGNATURE);
068: }
069: cv.visitVarInsn(ASTORE, 2);
070: cv.visitFieldInsn(GETSTATIC, m_aspectFactoryClassName,
071: FACTORY_ASPECTS_FIELD_NAME, MAP_CLASS_SIGNATURE);
072: cv.visitVarInsn(ALOAD, 0);
073: cv.visitVarInsn(ALOAD, 2);
074: cv
075: .visitMethodInsn(INVOKEINTERFACE, MAP_CLASS_NAME,
076: "put",
077: "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;");
078: cv.visitVarInsn(ALOAD, 2);
079: cv.visitInsn(ARETURN);
080:
081: cv.visitLabel(ifBound);
082: cv.visitVarInsn(ALOAD, 1);
083: cv.visitTypeInsn(CHECKCAST, m_aspectClassName);
084: cv.visitInsn(ARETURN);
085: cv.visitMaxs(0, 0);
086: }
087:
088: protected void createHasAspect() {
089: MethodVisitor cv = m_cw.visitMethod(ACC_STATIC + ACC_PUBLIC
090: + ACC_FINAL, FACTORY_HASASPECT_METHOD_NAME, "("
091: + getXSignature() + ")Z", null, null);
092:
093: cv.visitFieldInsn(GETSTATIC, m_aspectFactoryClassName,
094: FACTORY_ASPECTS_FIELD_NAME, MAP_CLASS_SIGNATURE);
095: cv.visitVarInsn(ALOAD, 0);
096: cv.visitMethodInsn(INVOKEINTERFACE, MAP_CLASS_NAME,
097: "containsKey", "(Ljava/lang/Object;)Z");
098: cv.visitInsn(IRETURN);
099: cv.visitMaxs(0, 0);
100: }
101:
102: protected void createOtherArtifacts() {
103: ;
104: }
105:
106: static class PerClassAspectFactoryCompiler extends
107: LazyPerXFactoryCompiler {
108: public PerClassAspectFactoryCompiler(String uuid,
109: String aspectClassName, String aspectQualifiedName,
110: String containerClassName, String rawParameters,
111: ClassLoader loader) {
112: super (uuid, aspectClassName, aspectQualifiedName,
113: containerClassName, rawParameters, loader);
114: }
115:
116: protected String getXSignature() {
117: return "Ljava/lang/Class;";
118: }
119: }
120:
121: static class PerThreadAspectFactoryCompiler extends
122: LazyPerXFactoryCompiler {
123: public PerThreadAspectFactoryCompiler(String uuid,
124: String aspectClassName, String aspectQualifiedName,
125: String containerClassName, String rawParameters,
126: ClassLoader loader) {
127: super (uuid, aspectClassName, aspectQualifiedName,
128: containerClassName, rawParameters, loader);
129: }
130:
131: protected String getXSignature() {
132: return "Ljava/lang/Thread;";
133: }
134: }
135:
136: }
|