1 22 package org.jboss.aop.metadata; 23 24 import javassist.CtClass; 25 import javassist.CtMethod; 26 import javassist.NotFoundException; 27 import org.jboss.aop.util.XmlHelper; 28 import org.w3c.dom.Element ; 29 30 import java.lang.reflect.Method ; 31 import java.util.Iterator ; 32 33 52 public class MethodConfig 53 { 54 String methodName = null; 55 String signature = null; 56 public MethodConfig() 58 { 59 } 60 61 63 public boolean patternMatches(Method method) 64 { 65 if (methodName.equals("*")) 67 { 68 return true; 69 } 70 71 if ( methodName.equals(method.getName()) == false ) 72 { 73 return false; 75 } 76 if (signature == null) return true; 77 78 String sig = getSignature(method.getParameterTypes()); 79 return sig.equals(signature); 80 } 81 82 public boolean patternMatches(CtMethod method) throws NotFoundException 83 { 84 if (methodName.equals("*")) 86 { 87 return true; 88 } 89 90 if ( methodName.equals(method.getName()) == false ) 91 { 92 return false; 94 } 95 if (signature == null) return true; 96 97 String sig = getSignature(method.getParameterTypes()); 98 return sig.equals(signature); 99 } 100 101 public void importXml(Element element) 102 throws Exception 103 { 104 methodName = XmlHelper.getElementContent(XmlHelper.getUniqueChild(element, "method-name")); 105 signature = "("; 106 Element paramsElement = XmlHelper.getOptionalChild(element, "method-params"); 107 if (paramsElement != null) 108 { 109 Iterator paramsIterator = XmlHelper.getChildrenByTagName(paramsElement, 110 "method-param"); 111 while (paramsIterator.hasNext()) 112 { 113 signature += XmlHelper.getElementContent((Element )paramsIterator.next()); 114 signature += " "; 115 } 116 } 117 signature += ")"; 118 } 119 120 public static String toGenericString(Method method) 121 { 122 StringBuffer buf = new StringBuffer (); 123 buf.append(method.getName()); 124 buf.append("("); 125 getSignature(method.getParameterTypes()); 126 buf.append(")"); 127 return buf.toString(); 128 } 129 130 public static String getSignature(Class [] source) 131 { 132 StringBuffer sig = new StringBuffer ("("); 133 for( int i=0; i < source.length; i++ ) 134 { 135 String brackets = ""; 136 Class cls = source[i]; 137 while(cls.isArray()) 138 { 139 brackets += "[]"; 140 cls = cls.getComponentType(); 141 } 142 sig.append(cls.getName()); 143 sig.append(brackets); 144 sig.append(" "); 145 } 146 sig.append(")"); 147 return sig.toString(); 148 } 149 150 public static String getSignature(CtClass[] source) 151 { 152 StringBuffer sig = new StringBuffer ("("); 153 for( int i=0; i < source.length; i++ ) 154 { 155 CtClass cls = source[i]; 156 sig.append(cls.getName()); 157 sig.append(" "); 158 } 159 sig.append(")"); 160 return sig.toString(); 161 } 162 163 } 165 | Popular Tags |