- Timestamp:
- 07/02/13 16:20:07 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/params/ParamCandidate.java
r88 r90 10 10 import java.lang.reflect.ParameterizedType; 11 11 import java.lang.reflect.Type; 12 import java.util.Arrays; 12 13 import java.util.Collection; 14 import java.util.Collections; 15 import java.util.Comparator; 13 16 import java.util.HashMap; 17 import java.util.LinkedList; 18 import java.util.List; 14 19 import java.util.Map; 15 20 21 import com.framsticks.params.annotations.FramsClassAnnotation; 16 22 import com.framsticks.params.annotations.ParamAnnotation; 23 import com.framsticks.params.types.ProcedureParam; 17 24 18 25 public class ParamCandidate { … … 65 72 protected final OneTime<Method> setter = new OneTime<>("setter"); 66 73 protected final OneTime<Method> getter = new OneTime<>("getter"); 74 protected final OneTime<Method> caller = new OneTime<>("caller"); 75 76 protected final List<ParamAnnotation> annotations = new LinkedList<>(); 67 77 68 78 /** … … 124 134 } 125 135 136 /** 137 * @return the getter 138 */ 139 public Method getCaller() { 140 return caller.get(); 141 } 142 143 /** 144 * @return the annotations 145 */ 146 public List<ParamAnnotation> getAnnotations() { 147 return Collections.unmodifiableList(annotations); 148 } 149 126 150 void validate() throws ConstructionException { 127 151 try { 152 if (caller.has()) { 153 if (!isPublic(caller)) { 154 throw new ConstructionException().msg("method is not public"); 155 } 156 if (getter.has() || setter.has()) { 157 throw new ConstructionException().msg("getter or setter coexist with caller"); 158 } 159 return; 160 } 128 161 if (isPublic(field)) { 129 162 if (getter.has()) { … … 147 180 148 181 boolean isFinal() { 182 if (caller.has()) { 183 return false; 184 } 149 185 if (Collection.class.isAssignableFrom(getRawType())) { 150 186 return false; … … 160 196 161 197 boolean isReadOnly() { 198 if (caller.has()) { 199 return false; 200 } 162 201 if (Collection.class.isAssignableFrom(getRawType())) { 163 202 return false; … … 172 211 } 173 212 174 void add( Member member, String name) {213 void add(ParamAnnotation paramAnnotation, Member member, String name) { 175 214 this.name.set(name); 215 annotations.add(paramAnnotation); 176 216 if (member instanceof Field) { 177 this.field.set((Field) member);217 field.set((Field) member); 178 218 setType(field.get().getGenericType()); 179 219 return; … … 181 221 if (member instanceof Method) { 182 222 Method m = (Method) member; 223 if (!paramAnnotation.paramType().equals(Param.class)) { 224 if (paramAnnotation.paramType().equals(ProcedureParam.class)) { 225 caller.set(m); 226 return; 227 } 228 throw new ConstructionException().msg("explicit set of paramType different than ProcedureParam is not yet supported").arg("name", name).arg("method", m).arg("in", this); 229 } 183 230 Type[] ps = m.getGenericParameterTypes(); 184 231 if (ps.length == 0) { … … 222 269 } 223 270 224 public static <M extends Member & AnnotatedElement> void filterParamsCandidates( Map<String, ParamCandidate> params, M[] members) {271 public static <M extends Member & AnnotatedElement> void filterParamsCandidates(Set set, M[] members) { 225 272 for (M m : members) { 226 273 ParamAnnotation pa = m.getAnnotation(ParamAnnotation.class); … … 228 275 continue; 229 276 } 230 // if (!isPublic(m)) {231 // throw new ConstructionException().msg("field is not public").arg("field", m);232 // }233 277 String id = FramsClassBuilder.getId(pa, m); 234 278 ParamCandidate pc = null; 235 if ( params.containsKey(id)) {236 pc = params.get(id);279 if (set.getCandidates().containsKey(id)) { 280 pc = set.getCandidates().get(id); 237 281 } else { 238 282 pc = new ParamCandidate(id); 239 params.put(id, pc); 240 } 241 pc.add(m, FramsClassBuilder.getName(pa, m)); 242 243 } 244 } 245 246 public static Map<String, ParamCandidate> getAllCandidates(Class<?> javaClass) throws ConstructionException { 247 Map<String, ParamCandidate> candidates = new HashMap<>(); 248 283 set.getCandidates().put(id, pc); 284 set.getOrder().add(pc); 285 } 286 pc.add(pa, m, FramsClassBuilder.getName(pa, m)); 287 } 288 } 289 290 public static class Set { 291 protected final Map<String, ParamCandidate> candidates; 292 protected final List<ParamCandidate> order; 293 294 /** 295 * @param candidates 296 * @param order 297 */ 298 public Set(Map<String, ParamCandidate> candidates, List<ParamCandidate> order) { 299 this.candidates = candidates; 300 this.order = order; 301 } 302 303 /** 304 * @return the candidates 305 */ 306 public Map<String, ParamCandidate> getCandidates() { 307 return candidates; 308 } 309 310 /** 311 * @return the order 312 */ 313 public List<ParamCandidate> getOrder() { 314 return order; 315 } 316 } 317 318 public static Set getAllCandidates(Class<?> javaClass) throws ConstructionException { 319 320 List<Class<?>> javaClasses = new LinkedList<>(); 249 321 while (javaClass != null) { 250 filterParamsCandidates(candidates, javaClass.getDeclaredFields()); 251 filterParamsCandidates(candidates, javaClass.getDeclaredMethods()); 252 322 javaClasses.add(0, javaClass); 253 323 javaClass = javaClass.getSuperclass(); 254 324 } 255 325 256 for (ParamCandidate pc : candidates.values()) { 326 Set resultSet = new Set(new HashMap<String, ParamCandidate>(), new LinkedList<ParamCandidate>()); 327 328 for (Class<?> j : javaClasses) { 329 Set set = new Set(resultSet.getCandidates(), new LinkedList<ParamCandidate>()); 330 filterParamsCandidates(set, j.getDeclaredFields()); 331 filterParamsCandidates(set, j.getDeclaredMethods()); 332 333 FramsClassAnnotation fa = j.getAnnotation(FramsClassAnnotation.class); 334 if (fa != null) { 335 final List<String> order = Arrays.asList(fa.order()); 336 Collections.sort(set.getOrder(), new Comparator<ParamCandidate>() { 337 @Override 338 public int compare(ParamCandidate pc0, ParamCandidate pc1) { 339 int u0 = order.indexOf(pc0.getId()); 340 int u1 = order.indexOf(pc1.getId()); 341 if (u0 == -1 || u1 == -1) { 342 return 0; 343 } 344 return u0 - u1; 345 } 346 }); 347 } 348 resultSet.getOrder().addAll(0, set.getOrder()); 349 } 350 351 for (ParamCandidate pc : resultSet.getOrder()) { 257 352 pc.validate(); 258 353 } 259 354 260 return candidates;355 return resultSet; 261 356 } 262 357 263 358 public static Class<?> getRawClass(final Type type) { 359 if (type == null) { 360 throw new IllegalArgumentException(); 361 } 264 362 if (Class.class.isInstance(type)) { 265 363 return Class.class.cast(type);
Note: See TracChangeset
for help on using the changeset viewer.