1 28 29 package com.caucho.config.types; 30 31 import com.caucho.config.ConfigException; 32 import com.caucho.util.CharBuffer; 33 import com.caucho.util.L10N; 34 35 import java.lang.reflect.Method ; 36 import java.util.ArrayList ; 37 38 public class SignaturePattern { 39 private static L10N L = new L10N(SignaturePattern.class); 40 41 private String _methodName; 42 43 private ArrayList <String > _paramTypes; 44 45 public void addText(String methodName) 46 throws ConfigException 47 { 48 if (methodName.indexOf('(') < 0) { 49 _methodName = methodName; 50 return; 51 } 52 53 Signature sig = new Signature(); 54 sig.addText(methodName); 55 sig.init(); 56 57 _methodName = sig.getName(); 58 59 String []params = sig.getParameterTypes(); 60 if (params != null) { 61 _paramTypes = new ArrayList <String >(); 62 63 for (int i = 0; i < params.length; i++) 64 _paramTypes.add(params[i]); 65 } 66 } 67 68 71 public void addParam(String typeName) 72 { 73 if (_paramTypes == null) 74 _paramTypes = new ArrayList <String >(); 75 76 _paramTypes.add(typeName); 77 } 78 79 84 public void setHasParams() 85 { 86 if (_paramTypes == null) 87 _paramTypes = new ArrayList <String >(); 88 } 89 90 public boolean isMatch(Method method) 91 { 92 return isMatch(method.getName(), method.getParameterTypes()); 93 } 94 95 public boolean isMatch(String methodName, Class []params) 96 { 97 if (_methodName == null) 98 return false; 99 else if (_methodName.equals("*")) 100 return true; 101 else if (! _methodName.equals(methodName)) 102 return false; 103 else if (_paramTypes == null) 104 return true; 105 106 if (params.length != _paramTypes.size()) 107 return false; 108 109 for (int i = 0; i < params.length; i++) { 110 String name = params[i].getName(); 111 String param = (String ) _paramTypes.get(i); 112 113 if (! name.equals(param) && ! name.endsWith("." + param)) 114 return false; 115 } 116 117 return true; 118 } 119 120 public int hashCode() 121 { 122 return _methodName.hashCode(); 123 } 124 125 public boolean equals(Object o) 126 { 127 if (! (o instanceof SignaturePattern)) 128 return false; 129 130 SignaturePattern cfg = (SignaturePattern) o; 131 132 if (! _methodName.equals(cfg._methodName)) 133 return false; 134 135 if (_paramTypes == null || cfg._paramTypes == null) 136 return _paramTypes == cfg._paramTypes; 137 138 if (_paramTypes.size() != cfg._paramTypes.size()) 139 return false; 140 141 for (int i = 0; i < _paramTypes.size(); i++) 142 if (! _paramTypes.get(i).equals(cfg._paramTypes.get(i))) 143 return false; 144 145 return true; 146 } 147 148 public String toString() 149 { 150 CharBuffer cb = new CharBuffer(); 151 152 cb.append("SignaturePattern["); 153 cb.append(_methodName); 154 cb.append("("); 155 for (int i = 0; _paramTypes != null && i < _paramTypes.size(); i++) { 156 if (i != 0) 157 cb.append(", "); 158 cb.append(_paramTypes.get(i)); 159 } 160 cb.append(")]"); 161 return cb.toString(); 162 } 163 } 164 | Popular Tags |