001: /**************************************************************************************
002: * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
003: * http://aspectwerkz.codehaus.org *
004: * ---------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of the LGPL license *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: **************************************************************************************/package org.codehaus.aspectwerkz.joinpoint.management;
008:
009: import java.lang.reflect.Constructor;
010: import java.lang.reflect.Field;
011: import java.lang.reflect.Method;
012:
013: import org.codehaus.aspectwerkz.joinpoint.EnclosingStaticJoinPoint;
014: import org.codehaus.aspectwerkz.joinpoint.impl.CatchClauseSignatureImpl;
015: import org.codehaus.aspectwerkz.joinpoint.impl.ConstructorSignatureImpl;
016: import org.codehaus.aspectwerkz.joinpoint.impl.EnclosingStaticJoinPointImpl;
017: import org.codehaus.aspectwerkz.joinpoint.impl.FieldSignatureImpl;
018: import org.codehaus.aspectwerkz.joinpoint.impl.MethodSignatureImpl;
019: import org.codehaus.aspectwerkz.joinpoint.impl.StaticInitializerSignatureImpl;
020: import org.codehaus.aspectwerkz.reflect.ReflectHelper;
021: import org.codehaus.aspectwerkz.transform.TransformationConstants;
022: import org.codehaus.aspectwerkz.transform.inlining.AsmHelper;
023:
024: /**
025: * Factory class for the signature hierarchy.
026: * The helper methods here are called by the JIT jp.
027: *
028: * TODO may be worth having a cache
029: *
030: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
031: * @author <a href="mailto:the_mindstorm@evolva.ro">Alex Popescu</a>
032: */
033: public final class SignatureFactory {
034:
035: /**
036: * Method signature factory
037: *
038: * @param declaringClass
039: * @param joinPointHash
040: * @return
041: */
042: public static final MethodSignatureImpl newMethodSignature(
043: final Class declaringClass, final int joinPointHash) {
044: Method[] methods = declaringClass.getDeclaredMethods();
045: for (int i = 0; i < methods.length; i++) {
046: Method method = methods[i];
047: if (ReflectHelper.calculateHash(method) == joinPointHash) {
048: return new MethodSignatureImpl(declaringClass, method);
049: }
050: }
051: // lookup in the hierarchy
052: MethodSignatureImpl signature = null;
053: for (int i = 0; i < declaringClass.getInterfaces().length; i++) {
054: signature = newMethodSignature(declaringClass
055: .getInterfaces()[i], joinPointHash);
056: if (signature != null) {
057: return signature;
058: }
059: }
060: if (declaringClass.getSuperclass() != null) {
061: signature = newMethodSignature(declaringClass
062: .getSuperclass(), joinPointHash);
063: } else {
064: return null;
065: }
066: return signature;
067: }
068:
069: /**
070: * Field signature factory
071: *
072: * @param declaringClass
073: * @param joinPointHash
074: * @return
075: */
076: public static final FieldSignatureImpl newFieldSignature(
077: final Class declaringClass, final int joinPointHash) {
078: Field[] fields = declaringClass.getDeclaredFields();
079: for (int i = 0; i < fields.length; i++) {
080: Field field = fields[i];
081: if (ReflectHelper.calculateHash(field) == joinPointHash) {
082: return new FieldSignatureImpl(declaringClass, field);
083: }
084: }
085: // lookup in the hierarchy
086: if (declaringClass.getSuperclass() != null) {
087: return newFieldSignature(declaringClass.getSuperclass(),
088: joinPointHash);
089: } else {
090: return null;
091: }
092: }
093:
094: /**
095: * Constructor signature factory
096: *
097: * @param declaringClass
098: * @param joinPointHash
099: * @return
100: */
101: public static final ConstructorSignatureImpl newConstructorSignature(
102: final Class declaringClass, final int joinPointHash) {
103: Constructor constructor = null;
104: for (int i = 0; i < declaringClass.getDeclaredConstructors().length; i++) {
105: Constructor c = declaringClass.getDeclaredConstructors()[i];
106: if (ReflectHelper.calculateHash(c) == joinPointHash) {
107: return new ConstructorSignatureImpl(declaringClass, c);
108: }
109: }
110: // lookup in the hierarchy
111: if (declaringClass.getSuperclass() != null) {
112: return newConstructorSignature(declaringClass
113: .getSuperclass(), joinPointHash);
114: } else {
115: return null;
116: }
117: }
118:
119: /**
120: * Handler signature factory
121: *
122: * @param exceptionClass
123: * @return
124: */
125: public static final CatchClauseSignatureImpl newCatchClauseSignature(
126: final Class exceptionClass) {
127: return new CatchClauseSignatureImpl(exceptionClass);
128: }
129:
130: /**
131: * Enclosing signature factory, wrapped behind an EnclosingStaticJoinPoint for syntax consistency
132: *
133: * @param declaringClass
134: * @param name
135: * @param description
136: * @return
137: */
138: public static EnclosingStaticJoinPoint newEnclosingStaticJoinPoint(
139: final Class declaringClass, final String name,
140: final String description) {
141: if (TransformationConstants.CLINIT_METHOD_NAME.equals(name)) {
142: return new EnclosingStaticJoinPointImpl(
143: new StaticInitializerSignatureImpl(declaringClass),
144: JoinPointType.STATIC_INITIALIZATION);
145: } else if (TransformationConstants.INIT_METHOD_NAME
146: .equals(name)) {
147: return new EnclosingStaticJoinPointImpl(
148: newConstructorSignature(declaringClass, AsmHelper
149: .calculateConstructorHash(description)),
150: JoinPointType.CONSTRUCTOR_EXECUTION);
151: } else {
152: // regular method
153: return new EnclosingStaticJoinPointImpl(newMethodSignature(
154: declaringClass, AsmHelper.calculateMethodHash(name,
155: description)),
156: JoinPointType.METHOD_EXECUTION);
157: }
158: }
159:
160: /**
161: * Static initialization factory
162: *
163: * @param declaringClass
164: * @return
165: */
166: public static StaticInitializerSignatureImpl newStaticInitializationSignature(
167: final Class declaringClass) {
168: return new StaticInitializerSignatureImpl(declaringClass);
169: }
170:
171: }
|