1 /* |
1 /* |
2 * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
5 * This code is free software; you can redistribute it and/or modify it |
6 * under the terms of the GNU General Public License version 2 only, as |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. Oracle designates this |
7 * published by the Free Software Foundation. Oracle designates this |
451 case TYPETEST: |
451 case TYPETEST: |
452 return getStartPos(((JCInstanceOf) tree).expr); |
452 return getStartPos(((JCInstanceOf) tree).expr); |
453 case POSTINC: |
453 case POSTINC: |
454 case POSTDEC: |
454 case POSTDEC: |
455 return getStartPos(((JCUnary) tree).arg); |
455 return getStartPos(((JCUnary) tree).arg); |
|
456 case ANNOTATED_TYPE: { |
|
457 JCAnnotatedType node = (JCAnnotatedType) tree; |
|
458 if (node.annotations.nonEmpty()) { |
|
459 if (node.underlyingType.hasTag(TYPEARRAY) || |
|
460 node.underlyingType.hasTag(SELECT)) { |
|
461 return getStartPos(node.underlyingType); |
|
462 } else { |
|
463 return getStartPos(node.annotations.head); |
|
464 } |
|
465 } else { |
|
466 return getStartPos(node.underlyingType); |
|
467 } |
|
468 } |
456 case NEWCLASS: { |
469 case NEWCLASS: { |
457 JCNewClass node = (JCNewClass)tree; |
470 JCNewClass node = (JCNewClass)tree; |
458 if (node.encl != null) |
471 if (node.encl != null) |
459 return getStartPos(node.encl); |
472 return getStartPos(node.encl); |
460 break; |
473 break; |
558 case PREINC: |
571 case PREINC: |
559 case PREDEC: |
572 case PREDEC: |
560 return getEndPos(((JCUnary) tree).arg, endPosTable); |
573 return getEndPos(((JCUnary) tree).arg, endPosTable); |
561 case WHILELOOP: |
574 case WHILELOOP: |
562 return getEndPos(((JCWhileLoop) tree).body, endPosTable); |
575 return getEndPos(((JCWhileLoop) tree).body, endPosTable); |
|
576 case ANNOTATED_TYPE: |
|
577 return getEndPos(((JCAnnotatedType) tree).underlyingType, endPosTable); |
563 case ERRONEOUS: { |
578 case ERRONEOUS: { |
564 JCErroneous node = (JCErroneous)tree; |
579 JCErroneous node = (JCErroneous)tree; |
565 if (node.errs != null && node.errs.nonEmpty()) |
580 if (node.errs != null && node.errs.nonEmpty()) |
566 return getEndPos(node.errs.last(), endPosTable); |
581 return getEndPos(node.errs.last(), endPosTable); |
567 } |
582 } |
1034 |
1051 |
1035 // Null check (implementation detail), for example, __.getClass() |
1052 // Null check (implementation detail), for example, __.getClass() |
1036 case NULLCHK: |
1053 case NULLCHK: |
1037 return Tree.Kind.OTHER; |
1054 return Tree.Kind.OTHER; |
1038 |
1055 |
|
1056 case ANNOTATION: |
|
1057 return Tree.Kind.ANNOTATION; |
|
1058 case TYPE_ANNOTATION: |
|
1059 return Tree.Kind.TYPE_ANNOTATION; |
|
1060 |
1039 default: |
1061 default: |
1040 return null; |
1062 return null; |
1041 } |
1063 } |
1042 } |
1064 } |
1043 |
1065 |
1044 /** |
1066 /** |
1045 * Returns the underlying type of the tree if it is annotated type, |
1067 * Returns the underlying type of the tree if it is an annotated type, |
1046 * or the tree itself otherwise |
1068 * or the tree itself otherwise. |
1047 */ |
1069 */ |
1048 public static JCExpression typeIn(JCExpression tree) { |
1070 public static JCExpression typeIn(JCExpression tree) { |
1049 switch (tree.getTag()) { |
1071 switch (tree.getTag()) { |
|
1072 case ANNOTATED_TYPE: |
|
1073 return ((JCAnnotatedType)tree).underlyingType; |
1050 case IDENT: /* simple names */ |
1074 case IDENT: /* simple names */ |
1051 case TYPEIDENT: /* primitive name */ |
1075 case TYPEIDENT: /* primitive name */ |
1052 case SELECT: /* qualified name */ |
1076 case SELECT: /* qualified name */ |
1053 case TYPEARRAY: /* array types */ |
1077 case TYPEARRAY: /* array types */ |
1054 case WILDCARD: /* wild cards */ |
1078 case WILDCARD: /* wild cards */ |
1055 case TYPEPARAMETER: /* type parameters */ |
1079 case TYPEPARAMETER: /* type parameters */ |
1056 case TYPEAPPLY: /* parameterized types */ |
1080 case TYPEAPPLY: /* parameterized types */ |
|
1081 case ERRONEOUS: /* error tree TODO: needed for BadCast JSR308 test case. Better way? */ |
1057 return tree; |
1082 return tree; |
1058 default: |
1083 default: |
1059 throw new AssertionError("Unexpected type tree: " + tree); |
1084 throw new AssertionError("Unexpected type tree: " + tree); |
1060 } |
1085 } |
1061 } |
1086 } |
1062 |
1087 |
|
1088 /* Return the inner-most type of a type tree. |
|
1089 * For an array that contains an annotated type, return that annotated type. |
|
1090 * TODO: currently only used by Pretty. Describe behavior better. |
|
1091 */ |
1063 public static JCTree innermostType(JCTree type) { |
1092 public static JCTree innermostType(JCTree type) { |
1064 switch (type.getTag()) { |
1093 JCTree lastAnnotatedType = null; |
1065 case TYPEARRAY: |
1094 JCTree cur = type; |
1066 return innermostType(((JCArrayTypeTree)type).elemtype); |
1095 loop: while (true) { |
1067 case WILDCARD: |
1096 switch (cur.getTag()) { |
1068 return innermostType(((JCWildcard)type).inner); |
1097 case TYPEARRAY: |
1069 default: |
1098 lastAnnotatedType = null; |
1070 return type; |
1099 cur = ((JCArrayTypeTree)cur).elemtype; |
1071 } |
1100 break; |
|
1101 case WILDCARD: |
|
1102 lastAnnotatedType = null; |
|
1103 cur = ((JCWildcard)cur).inner; |
|
1104 break; |
|
1105 case ANNOTATED_TYPE: |
|
1106 lastAnnotatedType = cur; |
|
1107 cur = ((JCAnnotatedType)cur).underlyingType; |
|
1108 break; |
|
1109 default: |
|
1110 break loop; |
|
1111 } |
|
1112 } |
|
1113 if (lastAnnotatedType!=null) { |
|
1114 return lastAnnotatedType; |
|
1115 } else { |
|
1116 return cur; |
|
1117 } |
|
1118 } |
|
1119 |
|
1120 private static class TypeAnnotationFinder extends TreeScanner { |
|
1121 public boolean foundTypeAnno = false; |
|
1122 public void visitAnnotation(JCAnnotation tree) { |
|
1123 foundTypeAnno = foundTypeAnno || tree.hasTag(TYPE_ANNOTATION); |
|
1124 } |
|
1125 } |
|
1126 |
|
1127 public static boolean containsTypeAnnotation(JCTree e) { |
|
1128 TypeAnnotationFinder finder = new TypeAnnotationFinder(); |
|
1129 finder.scan(e); |
|
1130 return finder.foundTypeAnno; |
1072 } |
1131 } |
1073 } |
1132 } |