1 18 package org.apache.geronimo.interop.util; 19 20 import java.lang.reflect.Method ; 21 import java.util.Iterator ; 22 import java.util.LinkedList ; 23 24 25 public class ParameterList extends LinkedList { 26 public ParameterList() { 27 } 28 29 public ParameterList(Class [] types) { 30 init(types); 31 } 32 33 public ParameterList(Method template) { 34 Class [] types = template.getParameterTypes(); 35 init(types); 36 } 37 38 private void init(Class [] types) { 39 int n = types.length; 40 for (int i = 0; i < n; i++) { 41 Class type = types[i]; 42 add(type, "p" + (i + 1)); 43 } 44 } 45 46 public ParameterList add(String type, String name) { 47 add(new MethodParameter(type, name)); 48 return this; 49 } 50 51 public ParameterList add(Class type, String name) { 52 return add(JavaType.getName(type), name); 53 } 54 55 public ParameterList add(String name) { 56 return add("?", name); 57 } 58 59 public MethodParameter find(String parameter) { 60 for (Iterator i = iterator(); i.hasNext();) { 61 MethodParameter mp = (MethodParameter) i.next(); 62 if (mp.name.equals(parameter)) { 63 return mp; 64 } 65 } 66 return null; 67 } 68 69 public MethodParameter getParameter(int index) { 70 return (MethodParameter) get(index); 71 } 72 73 public boolean hasSameTypes(ParameterList that) { 74 if (this.size() != that.size()) { 75 return false; 76 } 77 Iterator i = this.iterator(); 78 Iterator j = that.iterator(); 79 for (; i.hasNext();) { 80 MethodParameter mp1 = (MethodParameter) i.next(); 81 MethodParameter mp2 = (MethodParameter) j.next(); 82 if (!mp1.type.equals(mp2.type)) { 83 return false; 84 } 85 } 86 return true; 87 } 88 89 public String toString() { 90 StringBuffer sb = new StringBuffer ("("); 91 int comma = 0; 92 for (Iterator i = iterator(); i.hasNext(); comma++) { 93 MethodParameter mp = (MethodParameter) i.next(); 94 if (comma > 0) { 95 sb.append(", "); 96 } 97 sb.append(mp.name); 98 } 99 sb.append(")"); 100 return sb.toString(); 101 } 102 103 public String toStringWithNoSpaces() { 104 StringBuffer sb = new StringBuffer ("("); 105 int comma = 0; 106 for (Iterator i = iterator(); i.hasNext(); comma++) { 107 MethodParameter mp = (MethodParameter) i.next(); 108 if (comma > 0) { 109 sb.append(","); 110 } 111 sb.append(mp.name); 112 } 113 sb.append(")"); 114 return sb.toString(); 115 } 116 117 public String toStringWithTypesOnly() { 118 StringBuffer sb = new StringBuffer ("("); 119 int comma = 0; 120 for (Iterator i = iterator(); i.hasNext(); comma++) { 121 MethodParameter mp = (MethodParameter) i.next(); 122 if (comma > 0) { 123 sb.append(", "); 124 } 125 sb.append(mp.type); 126 } 127 sb.append(")"); 128 return sb.toString(); 129 } 130 131 public String toStringWithTypes() { 132 StringBuffer sb = new StringBuffer ("("); 133 int comma = 0; 134 for (Iterator i = iterator(); i.hasNext(); comma++) { 135 MethodParameter mp = (MethodParameter) i.next(); 136 if (comma > 0) { 137 sb.append(", "); 138 } 139 sb.append(mp.type); 140 sb.append(' '); 141 sb.append(mp.name); 142 } 143 sb.append(")"); 144 return sb.toString(); 145 } 146 147 public String toStringWithFinalTypes() { 148 StringBuffer sb = new StringBuffer ("("); 149 int comma = 0; 150 for (Iterator i = iterator(); i.hasNext(); comma++) { 151 MethodParameter mp = (MethodParameter) i.next(); 152 if (comma > 0) { 153 sb.append(", "); 154 } 155 sb.append("final "); 156 sb.append(mp.type); 157 sb.append(' '); 158 sb.append(mp.name); 159 } 160 sb.append(")"); 161 return sb.toString(); 162 } 163 164 public String getClassArray() { 165 if (isEmpty()) { 166 return "org.apache.geronimo.interop.util.ArrayUtil.EMPTY_CLASS_ARRAY"; 167 } 168 StringBuffer sb = new StringBuffer ("new java.lang.Class[] {"); 169 int comma = 0; 170 for (Iterator i = iterator(); i.hasNext(); comma++) { 171 MethodParameter mp = (MethodParameter) i.next(); 172 if (comma > 0) { 173 sb.append(", "); 174 } 175 sb.append(mp.type + ".class"); 176 } 177 sb.append("}"); 178 return sb.toString(); 179 } 180 181 public String getObjectArray() { 182 if (isEmpty()) { 183 return "org.apache.geronimo.interop.util.ArrayUtil.EMPTY_OBJECT_ARRAY"; 184 } 185 StringBuffer sb = new StringBuffer ("new java.lang.Object[] {"); 186 int comma = 0; 187 for (Iterator i = iterator(); i.hasNext(); comma++) { 188 MethodParameter mp = (MethodParameter) i.next(); 189 if (comma > 0) { 190 sb.append(", "); 191 } 192 sb.append(mp.getObject()); 193 } 194 sb.append("}"); 195 return sb.toString(); 196 } 197 198 public String wrapAll() { 199 StringBuffer sb = new StringBuffer (); 200 int comma = 0; 201 for (Iterator i = iterator(); i.hasNext(); comma++) { 202 MethodParameter mp = (MethodParameter) i.next(); 203 if (comma > 0) { 204 sb.append(", "); 205 } 206 sb.append(mp.getObject()); 207 } 208 return sb.toString(); 209 } 210 } 211 | Popular Tags |