- Timestamp:
- 07/14/13 23:20:04 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
java/main/src/main/java/com/framsticks/params/ParamCandidate.java
r100 r101 10 10 import java.lang.reflect.ParameterizedType; 11 11 import java.lang.reflect.Type; 12 import java.util.ArrayList; 12 13 import java.util.Arrays; 13 14 import java.util.Collection; … … 15 16 import java.util.Comparator; 16 17 import java.util.HashMap; 18 import java.util.HashSet; 19 import java.util.IdentityHashMap; 17 20 import java.util.LinkedList; 18 21 import java.util.List; … … 75 78 protected final OneTime<Method> adder = new OneTime<>("adder"); 76 79 protected final OneTime<Method> remover = new OneTime<>("remover"); 80 protected int flags = 0; 77 81 78 82 protected final List<ParamAnnotation> annotations = new LinkedList<>(); … … 162 166 public List<ParamAnnotation> getAnnotations() { 163 167 return Collections.unmodifiableList(annotations); 168 } 169 170 protected final java.util.Set<Class<?>> dependantClasses = new HashSet<>(); 171 172 // public void addDependantClass(Class<?> javaClass) { 173 // dependantClasses.add(javaClass); 174 // } 175 176 /** 177 * @return the dependantClasses 178 */ 179 public java.util.Set<Class<?>> getDependantClasses() { 180 return Collections.unmodifiableSet(dependantClasses); 164 181 } 165 182 … … 247 264 this.name.set(name); 248 265 annotations.add(paramAnnotation); 266 flags |= paramAnnotation.flags(); 249 267 if (member instanceof Field) { 250 268 field.set((Field) member); … … 299 317 300 318 public int getFlags() { 301 int f = 0;319 int f = flags; 302 320 if (isReadOnly()) { 303 321 f |= ParamFlags.READONLY; … … 341 359 protected final Map<String, ParamCandidate> candidates; 342 360 protected final List<ParamCandidate> order; 361 protected final java.util.Set<Class<?>> dependantClasses = new HashSet<>(); 343 362 344 363 /** … … 364 383 return order; 365 384 } 366 } 385 386 public java.util.Set<Class<?>> getDependentClasses() { 387 return dependantClasses; 388 } 389 } 390 391 protected static final Map<Class<?>, Set> setsCache = Collections.synchronizedMap(new IdentityHashMap<Class<?>, Set>()); 367 392 368 393 public static Set getAllCandidates(Class<?> javaClass) throws ConstructionException { 394 Set result = setsCache.get(javaClass); 395 if (result != null) { 396 return result; 397 } 369 398 370 399 List<Class<?>> javaClasses = new LinkedList<>(); … … 374 403 } 375 404 376 Set resultSet = new Set(new HashMap<String, ParamCandidate>(), new LinkedList<ParamCandidate>());405 result = new Set(new HashMap<String, ParamCandidate>(), new LinkedList<ParamCandidate>()); 377 406 378 407 for (Class<?> j : javaClasses) { 379 Set set = new Set(resultSet.getCandidates(), new LinkedList<ParamCandidate>()); 408 Set set = new Set(result.getCandidates(), new LinkedList<ParamCandidate>()); 409 380 410 filterParamsCandidates(set, j.getDeclaredFields()); 381 411 filterParamsCandidates(set, j.getDeclaredMethods()); … … 383 413 FramsClassAnnotation fa = j.getAnnotation(FramsClassAnnotation.class); 384 414 if (fa != null) { 415 416 if (j != javaClass) { 417 result.dependantClasses.add(j); 418 } 419 for (Class<?> r : fa.register()) { 420 result.dependantClasses.add(r); 421 } 422 423 385 424 final List<String> order = Arrays.asList(fa.order()); 386 425 Collections.sort(set.getOrder(), new Comparator<ParamCandidate>() { … … 396 435 }); 397 436 } 398 result Set.getOrder().addAll(0, set.getOrder());399 } 400 401 for (ParamCandidate pc : result Set.getOrder()) {437 result.getOrder().addAll(0, set.getOrder()); 438 } 439 440 for (ParamCandidate pc : result.getOrder()) { 402 441 pc.validate(); 403 } 404 405 return resultSet; 442 pc.induceParamType(Param.build()); 443 result.dependantClasses.addAll(pc.getDependantClasses()); 444 } 445 446 setsCache.put(javaClass, result); 447 448 return result; 406 449 } 407 450 … … 425 468 } 426 469 470 protected ParamBuilder induceParamType(ParamBuilder builder, Type type) { 471 // if (type.equals(Void.TYPE)) { 472 // throw new ConstructionException().msg("void is not a valid type"); 473 // } 474 475 if (type instanceof ParameterizedType) { 476 ParameterizedType p = (ParameterizedType) type; 477 Type rawType = p.getRawType(); 478 Type containedType = null; 479 //TODO make implementation here 480 boolean map = false; 481 StringBuilder b = new StringBuilder(); 482 if (rawType.equals(Map.class)) { 483 containedType = p.getActualTypeArguments()[1]; 484 map = true; 485 b.append("l"); 486 } else if (rawType.equals(List.class)) { 487 containedType = p.getActualTypeArguments()[0]; 488 b.append("l"); 489 } else if (rawType.equals(EventListener.class)) { 490 containedType = p.getActualTypeArguments()[0]; 491 b.append("e"); 492 } 493 if (!(containedType instanceof Class)) { 494 return builder; 495 } 496 b.append(" "); 497 498 Class<?> containedClass = (Class<?>) containedType; 499 FramsClassAnnotation fca = containedClass.getAnnotation(FramsClassAnnotation.class); 500 if (fca == null) { 501 throw new ConstructionException().msg("the contained class is not annotated").arg("class", containedClass); 502 } 503 dependantClasses.add(containedClass); 504 b.append(FramsClassBuilder.getName(fca, containedClass)); 505 //TODO parameterize this 506 if (map) { 507 b.append(" uid"); 508 } 509 510 builder.type(b.toString()); 511 return builder; 512 } 513 514 if (type instanceof Class) { 515 516 Class<?> cl = (Class<?>) type; 517 518 // TODO: future support for enum 519 // if (cl.isEnum()) { 520 // Class<? extends Enum<?>> enumType = (Class<? extends Enum<?>>) cl; 521 // Enum<?>[] enums = enumType.getEnumConstants(); 522 // StringBuilder b = new StringBuilder(); 523 524 // b.append("d 0 ").append(enums.length - 1).append(" 0 "); 525 // for (Enum<?> e : enums) { 526 // b.append("~").append(e.name()); 527 // } 528 // return b.toString(); 529 // } 530 if (cl.equals(Integer.class) || cl.equals(int.class)) { 531 builder.type("d"); 532 return builder; 533 } 534 if (cl.equals(String.class)) { 535 builder.type("s"); 536 return builder; 537 } 538 if (cl.equals(Double.class) || cl.equals(double.class)) { 539 builder.type("f"); 540 return builder; 541 } 542 if (cl.equals(Boolean.class) || cl.equals(boolean.class)) { 543 builder.type( "d 0 1"); 544 return builder; 545 } 546 if (cl.equals(Object.class)) { 547 builder.type("x"); 548 return builder; 549 } 550 551 552 // builder.type("o " + (cl).getCanonicalName()); 553 builder.type("o " + cl.getSimpleName()); 554 dependantClasses.add(cl); 555 builder.fillStorageType(cl); 556 return builder; 557 } 558 559 throw new ConstructionException().msg("failed to find framsticks for native type").arg("type", type); 560 } 561 562 563 public ParamBuilder induceParamType(ParamBuilder builder) { 564 Method method = getCaller(); 565 if (method == null) { 566 return induceParamType(builder, getType()); 567 } 568 569 if (!method.getReturnType().equals(Void.TYPE)) { 570 builder.resultType(induceParamType(Param.build(), method.getGenericReturnType()).finish(ValueParam.class)); 571 } 572 573 List<ValueParam> arguments = new ArrayList<>(); 574 int number = 0; 575 for (Type arg : method.getGenericParameterTypes()) { 576 arguments.add(induceParamType(Param.build(), arg).idAndName("arg" + (number++)).finish(ValueParam.class)); 577 } 578 builder.argumentsType(arguments); 579 580 return builder; 581 } 582 427 583 };
Note: See TracChangeset
for help on using the changeset viewer.