1 6 7 package org.jfox.ejb.meta; 8 9 import java.lang.reflect.Method ; 10 import java.util.ArrayList ; 11 import java.util.List ; 12 13 import org.jfox.ioc.util.XMLUtils; 14 import org.w3c.dom.Node ; 15 16 19 20 public class MethodDescriptor extends Descriptor implements Comparable { 21 22 private String ejbName; 23 private String intf; 24 private String name; 25 private String paramTypeNames[]; 26 27 public MethodDescriptor() { 28 29 } 30 31 public void processXML(Node node) throws EJBDescriptionException { 32 super.processXML(node); 33 setEjbName(XMLUtils.getChildNodeValueOf(node, "ejb-name")); 34 setIntf(XMLUtils.getChildNodeValueOf(node, "method-intf")); 35 setName(XMLUtils.getChildNodeValueOf(node, "method-name")); 36 List params = new ArrayList (); 37 for(Node paramNode = node.getFirstChild(); paramNode != null; paramNode = paramNode.getNextSibling()) { 38 if(paramNode.getNodeType() == Node.ELEMENT_NODE && "method-param".equals(paramNode.getNodeName())) 39 params.add(XMLUtils.getValueOf(paramNode)); 40 } 41 setParamTypeNames((String []) params.toArray(new String [0])); 42 } 43 44 public String getEjbName() { 45 return ejbName; 46 } 47 48 public void setEjbName(String ejbName) { 49 this.ejbName = ejbName; 50 } 51 52 public String getIntf() { 53 return intf; 54 } 55 56 public void setIntf(String intf) { 57 this.intf = intf; 58 } 59 60 public String getName() { 61 return name; 62 } 63 64 public void setName(String name) { 65 this.name = name; 66 } 67 68 public String [] getParamTypeNames() { 69 return paramTypeNames; 70 } 71 72 public void setParamTypeNames(String [] paramTypeNames) { 73 this.paramTypeNames = paramTypeNames; 74 } 75 76 public String getPrettyParameterString() { 77 if(paramTypeNames == null) 78 return ""; 79 String readableString = "("; 80 for(int i = 0; i < paramTypeNames.length; i++) { 81 if(i > 0) readableString = readableString + ", "; 82 readableString = readableString + paramTypeNames[i]; 83 } 84 85 readableString = readableString + ")"; 86 return readableString; 87 } 88 89 public boolean isDefault() { 90 return name == null || name.equals("*"); 91 } 92 93 public String toString() { 94 return getName() + getPrettyParameterString(); 95 } 96 97 public int hashCode() { 98 return getName().hashCode() ^ getPrettyParameterString().hashCode(); 99 } 100 101 public boolean match(Method method) { 102 if(isDefault()) 103 return true; 104 if(!name.equals(method.getName())) 105 return false; 106 if(paramTypeNames == null) 107 return true; 108 Class _params[] = method.getParameterTypes(); 109 if(_params.length != paramTypeNames.length) 110 return false; 111 for(int i = 0; i < _params.length; i++) { 112 String paramType = getTypeName(_params[i]); 113 if(!paramType.equals(paramTypeNames[i])) 114 return false; 115 } 116 return true; 117 } 118 119 private static final String getTypeName(Class clz) { 120 String typeName = ""; 121 for(; clz.isArray(); clz = clz.getComponentType()) 122 typeName = typeName + "[]"; 123 124 return clz.getName() + typeName; 125 } 126 127 public int compareTo(Object methodDesc) { 128 if(!(methodDesc instanceof MethodDescriptor)) { 129 return 1; 130 } 131 String _intf = ((MethodDescriptor) methodDesc).getIntf(); 132 String _name = ((MethodDescriptor) methodDesc).getName(); 133 String [] _paramTypeNames = ((MethodDescriptor) methodDesc).getParamTypeNames(); 134 if(!intf.equals(_intf)) 135 return intf.compareTo(_intf); 136 if(name.equals("*")) 137 return _name.equals("*") ? 0 : 1; 138 if(_name.equals("*")) 139 return -1; 140 if(!name.equals(_name)) 141 return name.compareTo(_name); 142 if(paramTypeNames == null) 143 return _paramTypeNames != null ? 1 : 0; 144 if(_paramTypeNames == null) 145 return -1; 146 if(paramTypeNames.length != _paramTypeNames.length) 147 return paramTypeNames.length - _paramTypeNames.length; 148 for(int i = 0; i < paramTypeNames.length; i++) 149 if(!paramTypeNames[i].equals(_paramTypeNames[i])) 150 return paramTypeNames[i].compareTo(_paramTypeNames[i]); 151 152 return 0; 153 } 154 155 public boolean equals(Object obj) { 156 if(obj != null && (obj instanceof MethodDescriptor)) 157 return compareTo((MethodDescriptor) obj) == 0; 158 else 159 return false; 160 } 161 162 public static void main(String [] args) { 163 164 } 165 } 166 167 | Popular Tags |