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 org.codehaus.aspectwerkz.joinpoint.impl; 9 10 import org.codehaus.aspectwerkz.MethodTuple; 11 import org.codehaus.aspectwerkz.annotation.Annotation; 12 import org.codehaus.aspectwerkz.annotation.Annotations; 13 import org.codehaus.aspectwerkz.joinpoint.MethodSignature; 14 15 import java.lang.reflect.Method; 16 import java.util.List; 17 18 /*** 19 * Implementation for the method signature. 20 * 21 * @author <a href="mailto:jboner@codehaus.org">Jonas Bonér </a> 22 */ 23 public class MethodSignatureImpl implements MethodSignature { 24 private final Class m_declaringType; 25 26 private final MethodTuple m_methodTuple; 27 28 /*** 29 * @param declaringType 30 * @param methodTuple 31 */ 32 public MethodSignatureImpl(final Class declaringType, final MethodTuple methodTuple) { 33 m_declaringType = declaringType; 34 m_methodTuple = methodTuple; 35 } 36 37 /*** 38 * Returns the method tuple. 39 * 40 * @return the method tuple 41 */ 42 public MethodTuple getMethodTuple() { 43 return m_methodTuple; 44 } 45 46 /*** 47 * Returns the method. 48 * 49 * @return the method 50 */ 51 public Method getMethod() { 52 return m_methodTuple.getOriginalMethod(); 53 } 54 55 /*** 56 * Returns the declaring class. 57 * 58 * @return the declaring class 59 */ 60 public Class getDeclaringType() { 61 return m_declaringType; 62 } 63 64 /*** 65 * Returns the modifiers for the signature. <p/>Could be used like this: 66 * 67 * <pre> 68 * boolean isPublic = java.lang.reflect.Modifier.isPublic(signature.getModifiers()); 69 * </pre> 70 * 71 * @return the mofifiers 72 */ 73 public int getModifiers() { 74 return m_methodTuple.getOriginalMethod().getModifiers(); 75 } 76 77 /*** 78 * Returns the name (f.e. name of method of field). 79 * 80 * @return 81 */ 82 public String getName() { 83 return m_methodTuple.getName(); 84 } 85 86 /*** 87 * Returns the exception types declared by the code block. 88 * 89 * @return the exception types 90 */ 91 public Class[] getExceptionTypes() { 92 return m_methodTuple.getOriginalMethod().getExceptionTypes(); 93 } 94 95 /*** 96 * Returns the parameter types. 97 * 98 * @return the parameter types 99 */ 100 public Class[] getParameterTypes() { 101 return m_methodTuple.getOriginalMethod().getParameterTypes(); 102 } 103 104 /*** 105 * Returns the return type. 106 * 107 * @return the return type 108 */ 109 public Class getReturnType() { 110 return m_methodTuple.getOriginalMethod().getReturnType(); 111 } 112 113 /*** 114 * Return the annotation with a specific name. 115 * 116 * @param annotationName the annotation name 117 * @return the annotation or null 118 */ 119 public Annotation getAnnotation(final String annotationName) { 120 return Annotations.getAnnotation(annotationName, m_methodTuple.getWrapperMethod()); 121 } 122 123 /*** 124 * Return a list with the annotations with a specific name. 125 * 126 * @param annotationName the annotation name 127 * @return the annotations in a list (can be empty) 128 */ 129 public List getAnnotations(final String annotationName) { 130 return Annotations.getAnnotations(annotationName, m_methodTuple.getWrapperMethod()); 131 } 132 133 /*** 134 * Return all the annotations <p/>Each annotation is wrapped in 135 * {@link org.codehaus.aspectwerkz.annotation.AnnotationInfo}instance. 136 * 137 * @return a list with the annotations 138 */ 139 public List getAnnotationInfos() { 140 return Annotations.getAnnotationInfos(m_methodTuple.getWrapperMethod()); 141 } 142 143 /*** 144 * Returns a string representation of the signature. 145 * 146 * @return a string representation 147 * @TODO: implement toString to something meaningful 148 */ 149 public String toString() { 150 StringBuffer signature = new StringBuffer(); 151 signature.append(getReturnType().getName()).append(" "); 152 signature.append(getName()).append("("); 153 Class[] params = getParameterTypes(); 154 for (int i = 0; i < params.length; i++) { 155 signature.append(params[i].getName()); 156 if (i < params.length-1 ) { 157 signature.append(", "); 158 } 159 } 160 signature.append(")"); 161 return signature.toString(); 162 } 163 }