comparison graal/com.oracle.truffle.codegen.processor/src/com/oracle/truffle/codegen/processor/Utils.java @ 8248:c4c3f50fa9c2

Fixes for codegen builtins support.
author Christian Humer <christian.humer@gmail.com>
date Tue, 12 Mar 2013 11:37:32 +0100
parents 703c09f8640c
children cb70ed101b5f
comparison
equal deleted inserted replaced
8247:5b08b0f4d338 8248:c4c3f50fa9c2
53 boxedType = context.getEnvironment().getTypeUtils().boxedClass((PrimitiveType) boxedType).asType(); 53 boxedType = context.getEnvironment().getTypeUtils().boxedClass((PrimitiveType) boxedType).asType();
54 } 54 }
55 return boxedType; 55 return boxedType;
56 } 56 }
57 57
58 public static List<TypeMirror> asTypeMirrors(List<? extends Element> elements) {
59 List<TypeMirror> types = new ArrayList<>(elements.size());
60 for (Element element : elements) {
61 types.add(element.asType());
62 }
63 return types;
64 }
65
58 public static List<AnnotationMirror> collectAnnotations(ProcessorContext context, AnnotationMirror markerAnnotation, String elementName, Element element, 66 public static List<AnnotationMirror> collectAnnotations(ProcessorContext context, AnnotationMirror markerAnnotation, String elementName, Element element,
59 Class<? extends Annotation> annotationClass) { 67 Class<? extends Annotation> annotationClass) {
60 List<AnnotationMirror> result = Utils.getAnnotationValueList(markerAnnotation, elementName); 68 List<AnnotationMirror> result = Utils.getAnnotationValueList(AnnotationMirror.class, markerAnnotation, elementName);
61 AnnotationMirror explicit = Utils.findAnnotationMirror(context.getEnvironment(), element, annotationClass); 69 AnnotationMirror explicit = Utils.findAnnotationMirror(context.getEnvironment(), element, annotationClass);
62 if (explicit != null) { 70 if (explicit != null) {
63 result.add(explicit); 71 result.add(explicit);
64 } 72 }
65 73
466 return null; 474 return null;
467 } 475 }
468 } 476 }
469 477
470 @SuppressWarnings("unchecked") 478 @SuppressWarnings("unchecked")
471 public static <T> List<T> getAnnotationValueList(AnnotationMirror mirror, String name) { 479 public static <T> List<T> getAnnotationValueList(Class<T> expectedListType, AnnotationMirror mirror, String name) {
480 List<? extends AnnotationValue> values = getAnnotationValue(List.class, mirror, name);
472 List<T> result = new ArrayList<>(); 481 List<T> result = new ArrayList<>();
473 List<? extends AnnotationValue> values = (List<? extends AnnotationValue>) getAnnotationValue(mirror, name).getValue(); 482
474 for (AnnotationValue value : values) { 483 for (AnnotationValue value : values) {
475 result.add((T) value.getValue()); 484 result.add(resolveAnnotationValue(expectedListType, value));
476 } 485 }
477 return result; 486 return result;
478 } 487 }
479 488
480 public static TypeMirror getAnnotationValueType(AnnotationMirror mirror, String name) { 489 public static <T> T getAnnotationValue(Class<T> expectedType, AnnotationMirror mirror, String name) {
481 return (TypeMirror) getAnnotationValue(mirror, name).getValue(); 490 return resolveAnnotationValue(expectedType, getAnnotationValue(mirror, name));
482 } 491 }
483 492
484 public static TypeMirror getAnnotationValueTypeMirror(AnnotationMirror mirror, String name) { 493 @SuppressWarnings({"unchecked"})
485 return (TypeMirror) getAnnotationValue(mirror, name).getValue(); 494 private static <T> T resolveAnnotationValue(Class<T> expectedType, AnnotationValue value) {
486 } 495 Object unboxedValue = value.accept(new AnnotationValueVisitorImpl(), null);
487 496 if (unboxedValue != null) {
488 public static String getAnnotationValueString(AnnotationMirror mirror, String name) { 497 if (expectedType == TypeMirror.class && unboxedValue instanceof String) {
489 return (String) getAnnotationValue(mirror, name).getValue(); 498 return null;
490 } 499 }
491 500 if (!expectedType.isAssignableFrom(unboxedValue.getClass())) {
492 public static int getAnnotationValueInt(AnnotationMirror mirror, String name) { 501 throw new ClassCastException(unboxedValue.getClass().getName() + " not assignable from " + expectedType.getName());
493 return (int) getAnnotationValue(mirror, name).getValue(); 502 }
503 }
504 return (T) unboxedValue;
494 } 505 }
495 506
496 public static AnnotationValue getAnnotationValue(AnnotationMirror mirror, String name) { 507 public static AnnotationValue getAnnotationValue(AnnotationMirror mirror, String name) {
497 ExecutableElement valueMethod = null; 508 ExecutableElement valueMethod = null;
498 for (ExecutableElement method : ElementFilter.methodsIn(mirror.getAnnotationType().asElement().getEnclosedElements())) { 509 for (ExecutableElement method : ElementFilter.methodsIn(mirror.getAnnotationType().asElement().getEnclosedElements())) {
508 519
509 AnnotationValue value = mirror.getElementValues().get(valueMethod); 520 AnnotationValue value = mirror.getElementValues().get(valueMethod);
510 if (value == null) { 521 if (value == null) {
511 value = valueMethod.getDefaultValue(); 522 value = valueMethod.getDefaultValue();
512 } 523 }
524
513 return value; 525 return value;
526 }
527
528 private static class AnnotationValueVisitorImpl extends AbstractAnnotationValueVisitor7<Object, Void> {
529
530 @Override
531 public Object visitBoolean(boolean b, Void p) {
532 return Boolean.valueOf(b);
533 }
534
535 @Override
536 public Object visitByte(byte b, Void p) {
537 return Byte.valueOf(b);
538 }
539
540 @Override
541 public Object visitChar(char c, Void p) {
542 return c;
543 }
544
545 @Override
546 public Object visitDouble(double d, Void p) {
547 return d;
548 }
549
550 @Override
551 public Object visitFloat(float f, Void p) {
552 return f;
553 }
554
555 @Override
556 public Object visitInt(int i, Void p) {
557 return i;
558 }
559
560 @Override
561 public Object visitLong(long i, Void p) {
562 return i;
563 }
564
565 @Override
566 public Object visitShort(short s, Void p) {
567 return s;
568 }
569
570 @Override
571 public Object visitString(String s, Void p) {
572 return s;
573 }
574
575 @Override
576 public Object visitType(TypeMirror t, Void p) {
577 return t;
578 }
579
580 @Override
581 public Object visitEnumConstant(VariableElement c, Void p) {
582 return c.getConstantValue();
583 }
584
585 @Override
586 public Object visitAnnotation(AnnotationMirror a, Void p) {
587 return a;
588 }
589
590 @Override
591 public Object visitArray(List<? extends AnnotationValue> vals, Void p) {
592 return vals;
593 }
594
514 } 595 }
515 596
516 public static boolean getAnnotationValueBoolean(AnnotationMirror mirror, String name) { 597 public static boolean getAnnotationValueBoolean(AnnotationMirror mirror, String name) {
517 return (Boolean) getAnnotationValue(mirror, name).getValue(); 598 return (Boolean) getAnnotationValue(mirror, name).getValue();
518 } 599 }