47 import com.sun.tools.javac.code.Type.WildcardType; |
47 import com.sun.tools.javac.code.Type.WildcardType; |
48 import com.sun.tools.javac.code.TypeAnnotationPosition.TypePathEntry; |
48 import com.sun.tools.javac.code.TypeAnnotationPosition.TypePathEntry; |
49 import com.sun.tools.javac.code.TypeAnnotationPosition.TypePathEntryKind; |
49 import com.sun.tools.javac.code.TypeAnnotationPosition.TypePathEntryKind; |
50 import com.sun.tools.javac.code.TypeTag; |
50 import com.sun.tools.javac.code.TypeTag; |
51 import com.sun.tools.javac.code.Symbol.VarSymbol; |
51 import com.sun.tools.javac.code.Symbol.VarSymbol; |
|
52 import com.sun.tools.javac.code.Symbol.MethodSymbol; |
|
53 import com.sun.tools.javac.comp.Annotate; |
52 import com.sun.tools.javac.comp.Annotate.Annotator; |
54 import com.sun.tools.javac.comp.Annotate.Annotator; |
53 import com.sun.tools.javac.tree.JCTree; |
55 import com.sun.tools.javac.tree.JCTree; |
54 import com.sun.tools.javac.tree.JCTree.JCBlock; |
56 import com.sun.tools.javac.tree.JCTree.JCBlock; |
55 import com.sun.tools.javac.tree.JCTree.JCClassDecl; |
57 import com.sun.tools.javac.tree.JCTree.JCClassDecl; |
56 import com.sun.tools.javac.tree.JCTree.JCExpression; |
58 import com.sun.tools.javac.tree.JCTree.JCExpression; |
|
59 import com.sun.tools.javac.tree.JCTree.JCLambda; |
57 import com.sun.tools.javac.tree.JCTree.JCMethodDecl; |
60 import com.sun.tools.javac.tree.JCTree.JCMethodDecl; |
|
61 import com.sun.tools.javac.tree.JCTree.JCNewClass; |
58 import com.sun.tools.javac.tree.JCTree.JCTypeApply; |
62 import com.sun.tools.javac.tree.JCTree.JCTypeApply; |
59 import com.sun.tools.javac.tree.JCTree.JCVariableDecl; |
63 import com.sun.tools.javac.tree.JCTree.JCVariableDecl; |
60 import com.sun.tools.javac.tree.TreeScanner; |
64 import com.sun.tools.javac.tree.TreeScanner; |
61 import com.sun.tools.javac.tree.JCTree.*; |
65 import com.sun.tools.javac.tree.JCTree.*; |
62 import com.sun.tools.javac.util.Assert; |
66 import com.sun.tools.javac.util.Assert; |
79 /** |
83 /** |
80 * Separate type annotations from declaration annotations and |
84 * Separate type annotations from declaration annotations and |
81 * determine the correct positions for type annotations. |
85 * determine the correct positions for type annotations. |
82 * This version only visits types in signatures and should be |
86 * This version only visits types in signatures and should be |
83 * called from MemberEnter. |
87 * called from MemberEnter. |
84 * The method returns the Annotator object that should be added |
88 * The method takes the Annotate object as parameter and |
85 * to the correct Annotate queue for later processing. |
89 * adds an Annotator to the correct Annotate queue for |
|
90 * later processing. |
86 */ |
91 */ |
87 public static Annotator organizeTypeAnnotationsSignatures(final Symtab syms, final Names names, |
92 public static void organizeTypeAnnotationsSignatures(final Symtab syms, final Names names, |
88 final Log log, final JCClassDecl tree) { |
93 final Log log, final JCClassDecl tree, Annotate annotate) { |
89 return new Annotator() { |
94 annotate.afterRepeated( new Annotator() { |
90 @Override |
95 @Override |
91 public void enterAnnotation() { |
96 public void enterAnnotation() { |
92 new TypeAnnotationPositions(syms, names, log, true).scan(tree); |
97 new TypeAnnotationPositions(syms, names, log, true).scan(tree); |
93 } |
98 } |
94 }; |
99 } ); |
95 } |
100 } |
96 |
101 |
97 /** |
102 /** |
98 * This version only visits types in bodies, that is, field initializers, |
103 * This version only visits types in bodies, that is, field initializers, |
99 * top-level blocks, and method bodies, and should be called from Attr. |
104 * top-level blocks, and method bodies, and should be called from Attr. |
100 */ |
105 */ |
101 public static void organizeTypeAnnotationsBodies(Symtab syms, Names names, Log log, JCClassDecl tree) { |
106 public static void organizeTypeAnnotationsBodies(Symtab syms, Names names, Log log, JCClassDecl tree) { |
102 new TypeAnnotationPositions(syms, names, log, false).scan(tree); |
107 new TypeAnnotationPositions(syms, names, log, false).scan(tree); |
103 } |
108 } |
104 |
109 |
|
110 public enum AnnotationType { DECLARATION, TYPE, BOTH }; |
|
111 |
|
112 /** |
|
113 * Determine whether an annotation is a declaration annotation, |
|
114 * a type annotation, or both. |
|
115 */ |
|
116 public static AnnotationType annotationType(Symtab syms, Names names, |
|
117 Attribute.Compound a, Symbol s) { |
|
118 Attribute.Compound atTarget = |
|
119 a.type.tsym.attribute(syms.annotationTargetType.tsym); |
|
120 if (atTarget == null) { |
|
121 return inferTargetMetaInfo(a, s); |
|
122 } |
|
123 Attribute atValue = atTarget.member(names.value); |
|
124 if (!(atValue instanceof Attribute.Array)) { |
|
125 Assert.error("annotationType(): bad @Target argument " + atValue + |
|
126 " (" + atValue.getClass() + ")"); |
|
127 return AnnotationType.DECLARATION; // error recovery |
|
128 } |
|
129 Attribute.Array arr = (Attribute.Array) atValue; |
|
130 boolean isDecl = false, isType = false; |
|
131 for (Attribute app : arr.values) { |
|
132 if (!(app instanceof Attribute.Enum)) { |
|
133 Assert.error("annotationType(): unrecognized Attribute kind " + app + |
|
134 " (" + app.getClass() + ")"); |
|
135 isDecl = true; |
|
136 continue; |
|
137 } |
|
138 Attribute.Enum e = (Attribute.Enum) app; |
|
139 if (e.value.name == names.TYPE) { |
|
140 if (s.kind == Kinds.TYP) |
|
141 isDecl = true; |
|
142 } else if (e.value.name == names.FIELD) { |
|
143 if (s.kind == Kinds.VAR && |
|
144 s.owner.kind != Kinds.MTH) |
|
145 isDecl = true; |
|
146 } else if (e.value.name == names.METHOD) { |
|
147 if (s.kind == Kinds.MTH && |
|
148 !s.isConstructor()) |
|
149 isDecl = true; |
|
150 } else if (e.value.name == names.PARAMETER) { |
|
151 if (s.kind == Kinds.VAR && |
|
152 s.owner.kind == Kinds.MTH && |
|
153 (s.flags() & Flags.PARAMETER) != 0) |
|
154 isDecl = true; |
|
155 } else if (e.value.name == names.CONSTRUCTOR) { |
|
156 if (s.kind == Kinds.MTH && |
|
157 s.isConstructor()) |
|
158 isDecl = true; |
|
159 } else if (e.value.name == names.LOCAL_VARIABLE) { |
|
160 if (s.kind == Kinds.VAR && |
|
161 s.owner.kind == Kinds.MTH && |
|
162 (s.flags() & Flags.PARAMETER) == 0) |
|
163 isDecl = true; |
|
164 } else if (e.value.name == names.ANNOTATION_TYPE) { |
|
165 if (s.kind == Kinds.TYP && |
|
166 (s.flags() & Flags.ANNOTATION) != 0) |
|
167 isDecl = true; |
|
168 } else if (e.value.name == names.PACKAGE) { |
|
169 if (s.kind == Kinds.PCK) |
|
170 isDecl = true; |
|
171 } else if (e.value.name == names.TYPE_USE) { |
|
172 if (s.kind == Kinds.TYP || |
|
173 s.kind == Kinds.VAR || |
|
174 (s.kind == Kinds.MTH && !s.isConstructor() && |
|
175 !s.type.getReturnType().hasTag(TypeTag.VOID)) || |
|
176 (s.kind == Kinds.MTH && s.isConstructor())) |
|
177 isType = true; |
|
178 } else if (e.value.name == names.TYPE_PARAMETER) { |
|
179 /* Irrelevant in this case */ |
|
180 // TYPE_PARAMETER doesn't aid in distinguishing between |
|
181 // Type annotations and declaration annotations on an |
|
182 // Element |
|
183 } else { |
|
184 Assert.error("annotationType(): unrecognized Attribute name " + e.value.name + |
|
185 " (" + e.value.name.getClass() + ")"); |
|
186 isDecl = true; |
|
187 } |
|
188 } |
|
189 if (isDecl && isType) { |
|
190 return AnnotationType.BOTH; |
|
191 } else if (isType) { |
|
192 return AnnotationType.TYPE; |
|
193 } else { |
|
194 return AnnotationType.DECLARATION; |
|
195 } |
|
196 } |
|
197 |
|
198 /** Infer the target annotation kind, if none is give. |
|
199 * We only infer declaration annotations. |
|
200 */ |
|
201 private static AnnotationType inferTargetMetaInfo(Attribute.Compound a, Symbol s) { |
|
202 return AnnotationType.DECLARATION; |
|
203 } |
|
204 |
|
205 |
105 private static class TypeAnnotationPositions extends TreeScanner { |
206 private static class TypeAnnotationPositions extends TreeScanner { |
106 |
|
107 private enum AnnotationType { DECLARATION, TYPE, BOTH }; |
|
108 |
207 |
109 private final Symtab syms; |
208 private final Symtab syms; |
110 private final Names names; |
209 private final Names names; |
111 private final Log log; |
210 private final Log log; |
112 private final boolean sigOnly; |
211 private final boolean sigOnly; |
188 // type is non-null and annotations are added to that type |
291 // type is non-null and annotations are added to that type |
189 type = typeWithAnnotations(typetree, type, typeAnnotations, log); |
292 type = typeWithAnnotations(typetree, type, typeAnnotations, log); |
190 |
293 |
191 if (sym.getKind() == ElementKind.METHOD) { |
294 if (sym.getKind() == ElementKind.METHOD) { |
192 sym.type.asMethodType().restype = type; |
295 sym.type.asMethodType().restype = type; |
|
296 } else if (sym.getKind() == ElementKind.PARAMETER) { |
|
297 sym.type = type; |
|
298 if (sym.getQualifiedName().equals(names._this)) { |
|
299 sym.owner.type.asMethodType().recvtype = type; |
|
300 // note that the typeAnnotations will also be added to the owner below. |
|
301 } else { |
|
302 MethodType methType = sym.owner.type.asMethodType(); |
|
303 List<VarSymbol> params = ((MethodSymbol)sym.owner).params; |
|
304 List<Type> oldArgs = methType.argtypes; |
|
305 ListBuffer<Type> newArgs = new ListBuffer<Type>(); |
|
306 while (params.nonEmpty()) { |
|
307 if (params.head == sym) { |
|
308 newArgs.add(type); |
|
309 } else { |
|
310 newArgs.add(oldArgs.head); |
|
311 } |
|
312 oldArgs = oldArgs.tail; |
|
313 params = params.tail; |
|
314 } |
|
315 methType.argtypes = newArgs.toList(); |
|
316 } |
193 } else { |
317 } else { |
194 sym.type = type; |
318 sym.type = type; |
195 } |
319 } |
196 |
320 |
197 sym.annotations.appendUniqueTypes(typeAnnotations); |
321 sym.annotations.appendUniqueTypes(typeAnnotations); |
198 if (sym.getKind() == ElementKind.PARAMETER && |
322 |
199 sym.getQualifiedName().equals(names._this)) { |
|
200 sym.owner.type.asMethodType().recvtype = type; |
|
201 // note that the typeAnnotations will also be added to the owner below. |
|
202 } |
|
203 if (sym.getKind() == ElementKind.PARAMETER || |
323 if (sym.getKind() == ElementKind.PARAMETER || |
204 sym.getKind() == ElementKind.LOCAL_VARIABLE || |
324 sym.getKind() == ElementKind.LOCAL_VARIABLE || |
205 sym.getKind() == ElementKind.RESOURCE_VARIABLE || |
325 sym.getKind() == ElementKind.RESOURCE_VARIABLE || |
206 sym.getKind() == ElementKind.EXCEPTION_PARAMETER) { |
326 sym.getKind() == ElementKind.EXCEPTION_PARAMETER) { |
207 // Make sure all type annotations from the symbol are also |
327 // Make sure all type annotations from the symbol are also |
274 // All annotations share the same position; modify the first one. |
394 // All annotations share the same position; modify the first one. |
275 Attribute.TypeCompound a = annotations.get(0); |
395 Attribute.TypeCompound a = annotations.get(0); |
276 TypeAnnotationPosition p = a.position; |
396 TypeAnnotationPosition p = a.position; |
277 p.location = p.location.prependList(depth.toList()); |
397 p.location = p.location.prependList(depth.toList()); |
278 } |
398 } |
|
399 typetree.type = toreturn; |
279 return toreturn; |
400 return toreturn; |
280 } else if (type.hasTag(TypeTag.TYPEVAR)) { |
401 } else if (type.hasTag(TypeTag.TYPEVAR)) { |
281 // Nothing to do for type variables. |
402 // Nothing to do for type variables. |
|
403 return type; |
|
404 } else if (type.getKind() == TypeKind.UNION) { |
|
405 // There is a TypeKind, but no TypeTag. |
|
406 JCTypeUnion tutree = (JCTypeUnion) typetree; |
|
407 JCExpression fst = tutree.alternatives.get(0); |
|
408 Type res = typeWithAnnotations(fst, fst.type, annotations, log); |
|
409 fst.type = res; |
|
410 // TODO: do we want to set res as first element in uct.alternatives? |
|
411 // UnionClassType uct = (com.sun.tools.javac.code.Type.UnionClassType)type; |
|
412 // Return the un-annotated union-type. |
282 return type; |
413 return type; |
283 } else { |
414 } else { |
284 Type enclTy = type; |
415 Type enclTy = type; |
285 Element enclEl = type.asElement(); |
416 Element enclEl = type.asElement(); |
286 JCTree enclTr = typetree; |
417 JCTree enclTr = typetree; |
478 private static Attribute.TypeCompound toTypeCompound(Attribute.Compound a, TypeAnnotationPosition p) { |
610 private static Attribute.TypeCompound toTypeCompound(Attribute.Compound a, TypeAnnotationPosition p) { |
479 // It is safe to alias the position. |
611 // It is safe to alias the position. |
480 return new Attribute.TypeCompound(a, p); |
612 return new Attribute.TypeCompound(a, p); |
481 } |
613 } |
482 |
614 |
483 private AnnotationType annotationType(Attribute.Compound a, Symbol s) { |
|
484 Attribute.Compound atTarget = |
|
485 a.type.tsym.attribute(syms.annotationTargetType.tsym); |
|
486 if (atTarget == null) { |
|
487 return inferTargetMetaInfo(a, s); |
|
488 } |
|
489 Attribute atValue = atTarget.member(names.value); |
|
490 if (!(atValue instanceof Attribute.Array)) { |
|
491 Assert.error("annotationType(): bad @Target argument " + atValue + |
|
492 " (" + atValue.getClass() + ")"); |
|
493 return AnnotationType.DECLARATION; // error recovery |
|
494 } |
|
495 Attribute.Array arr = (Attribute.Array) atValue; |
|
496 boolean isDecl = false, isType = false; |
|
497 for (Attribute app : arr.values) { |
|
498 if (!(app instanceof Attribute.Enum)) { |
|
499 Assert.error("annotationType(): unrecognized Attribute kind " + app + |
|
500 " (" + app.getClass() + ")"); |
|
501 isDecl = true; |
|
502 continue; |
|
503 } |
|
504 Attribute.Enum e = (Attribute.Enum) app; |
|
505 if (e.value.name == names.TYPE) { |
|
506 if (s.kind == Kinds.TYP) |
|
507 isDecl = true; |
|
508 } else if (e.value.name == names.FIELD) { |
|
509 if (s.kind == Kinds.VAR && |
|
510 s.owner.kind != Kinds.MTH) |
|
511 isDecl = true; |
|
512 } else if (e.value.name == names.METHOD) { |
|
513 if (s.kind == Kinds.MTH && |
|
514 !s.isConstructor()) |
|
515 isDecl = true; |
|
516 } else if (e.value.name == names.PARAMETER) { |
|
517 if (s.kind == Kinds.VAR && |
|
518 s.owner.kind == Kinds.MTH && |
|
519 (s.flags() & Flags.PARAMETER) != 0) |
|
520 isDecl = true; |
|
521 } else if (e.value.name == names.CONSTRUCTOR) { |
|
522 if (s.kind == Kinds.MTH && |
|
523 s.isConstructor()) |
|
524 isDecl = true; |
|
525 } else if (e.value.name == names.LOCAL_VARIABLE) { |
|
526 if (s.kind == Kinds.VAR && |
|
527 s.owner.kind == Kinds.MTH && |
|
528 (s.flags() & Flags.PARAMETER) == 0) |
|
529 isDecl = true; |
|
530 } else if (e.value.name == names.ANNOTATION_TYPE) { |
|
531 if (s.kind == Kinds.TYP && |
|
532 (s.flags() & Flags.ANNOTATION) != 0) |
|
533 isDecl = true; |
|
534 } else if (e.value.name == names.PACKAGE) { |
|
535 if (s.kind == Kinds.PCK) |
|
536 isDecl = true; |
|
537 } else if (e.value.name == names.TYPE_USE) { |
|
538 if (s.kind == Kinds.TYP || |
|
539 s.kind == Kinds.VAR || |
|
540 (s.kind == Kinds.MTH && !s.isConstructor() && |
|
541 !s.type.getReturnType().hasTag(TypeTag.VOID)) || |
|
542 (s.kind == Kinds.MTH && s.isConstructor())) |
|
543 isType = true; |
|
544 } else if (e.value.name == names.TYPE_PARAMETER) { |
|
545 /* Irrelevant in this case */ |
|
546 // TYPE_PARAMETER doesn't aid in distinguishing between |
|
547 // Type annotations and declaration annotations on an |
|
548 // Element |
|
549 } else { |
|
550 Assert.error("annotationType(): unrecognized Attribute name " + e.value.name + |
|
551 " (" + e.value.name.getClass() + ")"); |
|
552 isDecl = true; |
|
553 } |
|
554 } |
|
555 if (isDecl && isType) { |
|
556 return AnnotationType.BOTH; |
|
557 } else if (isType) { |
|
558 return AnnotationType.TYPE; |
|
559 } else { |
|
560 return AnnotationType.DECLARATION; |
|
561 } |
|
562 } |
|
563 |
|
564 /** Infer the target annotation kind, if none is give. |
|
565 * We only infer declaration annotations. |
|
566 */ |
|
567 private static AnnotationType inferTargetMetaInfo(Attribute.Compound a, Symbol s) { |
|
568 return AnnotationType.DECLARATION; |
|
569 } |
|
570 |
|
571 |
615 |
572 /* This is the beginning of the second part of organizing |
616 /* This is the beginning of the second part of organizing |
573 * type annotations: determine the type annotation positions. |
617 * type annotations: determine the type annotation positions. |
574 */ |
618 */ |
575 |
619 |
583 // Note that p.offset is set in |
627 // Note that p.offset is set in |
584 // com.sun.tools.javac.jvm.Gen.setTypeAnnotationPositions(int) |
628 // com.sun.tools.javac.jvm.Gen.setTypeAnnotationPositions(int) |
585 |
629 |
586 switch (frame.getKind()) { |
630 switch (frame.getKind()) { |
587 case TYPE_CAST: |
631 case TYPE_CAST: |
|
632 JCTypeCast frameTC = (JCTypeCast) frame; |
588 p.type = TargetType.CAST; |
633 p.type = TargetType.CAST; |
|
634 if (frameTC.clazz.hasTag(Tag.TYPEINTERSECTION)) { |
|
635 // This case was already handled by INTERSECTION_TYPE |
|
636 } else { |
|
637 p.type_index = 0; |
|
638 } |
589 p.pos = frame.pos; |
639 p.pos = frame.pos; |
590 return; |
640 return; |
591 |
641 |
592 case INSTANCE_OF: |
642 case INSTANCE_OF: |
593 p.type = TargetType.INSTANCEOF; |
643 p.type = TargetType.INSTANCEOF; |
594 p.pos = frame.pos; |
644 p.pos = frame.pos; |
595 return; |
645 return; |
596 |
646 |
597 case NEW_CLASS: |
647 case NEW_CLASS: |
598 JCNewClass frameNewClass = (JCNewClass)frame; |
648 JCNewClass frameNewClass = (JCNewClass) frame; |
599 if (frameNewClass.typeargs.contains(tree)) { |
649 if (frameNewClass.def != null) { |
|
650 // Special handling for anonymous class instantiations |
|
651 JCClassDecl frameClassDecl = frameNewClass.def; |
|
652 if (frameClassDecl.extending == tree) { |
|
653 p.type = TargetType.CLASS_EXTENDS; |
|
654 p.type_index = -1; |
|
655 } else if (frameClassDecl.implementing.contains(tree)) { |
|
656 p.type = TargetType.CLASS_EXTENDS; |
|
657 p.type_index = frameClassDecl.implementing.indexOf(tree); |
|
658 } else { |
|
659 // In contrast to CLASS below, typarams cannot occur here. |
|
660 Assert.error("Could not determine position of tree " + tree + |
|
661 " within frame " + frame); |
|
662 } |
|
663 } else if (frameNewClass.typeargs.contains(tree)) { |
600 p.type = TargetType.CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT; |
664 p.type = TargetType.CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT; |
601 p.type_index = frameNewClass.typeargs.indexOf(tree); |
665 p.type_index = frameNewClass.typeargs.indexOf(tree); |
602 } else { |
666 } else { |
603 p.type = TargetType.NEW; |
667 p.type = TargetType.NEW; |
604 } |
668 } |
647 } |
711 } |
648 return; |
712 return; |
649 } |
713 } |
650 |
714 |
651 case PARAMETERIZED_TYPE: { |
715 case PARAMETERIZED_TYPE: { |
|
716 List<JCTree> newPath = path.tail; |
|
717 |
652 if (((JCTypeApply)frame).clazz == tree) { |
718 if (((JCTypeApply)frame).clazz == tree) { |
653 // generic: RAW; noop |
719 // generic: RAW; noop |
654 } else if (((JCTypeApply)frame).arguments.contains(tree)) { |
720 } else if (((JCTypeApply)frame).arguments.contains(tree)) { |
655 JCTypeApply taframe = (JCTypeApply) frame; |
721 JCTypeApply taframe = (JCTypeApply) frame; |
656 int arg = taframe.arguments.indexOf(tree); |
722 int arg = taframe.arguments.indexOf(tree); |
657 p.location = p.location.prepend(new TypePathEntry(TypePathEntryKind.TYPE_ARGUMENT, arg)); |
723 p.location = p.location.prepend(new TypePathEntry(TypePathEntryKind.TYPE_ARGUMENT, arg)); |
658 |
724 |
659 locateNestedTypes(taframe.type, p); |
725 Type typeToUse; |
|
726 if (newPath.tail != null && newPath.tail.head.hasTag(Tag.NEWCLASS)) { |
|
727 // If we are within an anonymous class instantiation, use its type, |
|
728 // because it contains a correctly nested type. |
|
729 typeToUse = newPath.tail.head.type; |
|
730 } else { |
|
731 typeToUse = taframe.type; |
|
732 } |
|
733 |
|
734 locateNestedTypes(typeToUse, p); |
660 } else { |
735 } else { |
661 Assert.error("Could not determine type argument position of tree " + tree + |
736 Assert.error("Could not determine type argument position of tree " + tree + |
662 " within frame " + frame); |
737 " within frame " + frame); |
663 } |
738 } |
664 |
739 |
665 List<JCTree> newPath = path.tail; |
|
666 resolveFrame(newPath.head, newPath.tail.head, newPath, p); |
740 resolveFrame(newPath.head, newPath.tail.head, newPath, p); |
667 return; |
741 return; |
668 } |
742 } |
669 |
743 |
670 case MEMBER_REFERENCE: { |
744 case MEMBER_REFERENCE: { |
778 p.type = TargetType.RESOURCE_VARIABLE; |
852 p.type = TargetType.RESOURCE_VARIABLE; |
779 break; |
853 break; |
780 default: |
854 default: |
781 Assert.error("Found unexpected type annotation for variable: " + v + " with kind: " + v.getKind()); |
855 Assert.error("Found unexpected type annotation for variable: " + v + " with kind: " + v.getKind()); |
782 } |
856 } |
|
857 if (v.getKind() != ElementKind.FIELD) { |
|
858 v.owner.annotations.appendUniqueTypes(v.getRawTypeAttributes()); |
|
859 } |
783 return; |
860 return; |
784 |
861 |
785 case ANNOTATED_TYPE: { |
862 case ANNOTATED_TYPE: { |
786 if (frame == tree) { |
863 if (frame == tree) { |
787 // This is only true for the first annotated type we see. |
864 // This is only true for the first annotated type we see. |
788 // For any other annotated types along the path, we do |
865 // For any other annotated types along the path, we do |
789 // not care about inner types. |
866 // not care about inner types. |
790 JCAnnotatedType atypetree = (JCAnnotatedType) frame; |
867 JCAnnotatedType atypetree = (JCAnnotatedType) frame; |
791 final Type utype = atypetree.underlyingType.type; |
868 final Type utype = atypetree.underlyingType.type; |
|
869 if (utype == null) { |
|
870 // This might happen during DeferredAttr; |
|
871 // we will be back later. |
|
872 return; |
|
873 } |
792 Symbol tsym = utype.tsym; |
874 Symbol tsym = utype.tsym; |
793 if (tsym.getKind().equals(ElementKind.TYPE_PARAMETER) || |
875 if (tsym.getKind().equals(ElementKind.TYPE_PARAMETER) || |
794 utype.getKind().equals(TypeKind.WILDCARD) || |
876 utype.getKind().equals(TypeKind.WILDCARD) || |
795 utype.getKind().equals(TypeKind.ARRAY)) { |
877 utype.getKind().equals(TypeKind.ARRAY)) { |
796 // Type parameters, wildcards, and arrays have the declaring |
878 // Type parameters, wildcards, and arrays have the declaring |
921 pos.pos = tree.restype.pos; |
1013 pos.pos = tree.restype.pos; |
922 separateAnnotationsKinds(tree.restype, tree.sym.type.getReturnType(), |
1014 separateAnnotationsKinds(tree.restype, tree.sym.type.getReturnType(), |
923 tree.sym, pos); |
1015 tree.sym, pos); |
924 } |
1016 } |
925 } |
1017 } |
926 if (tree.recvparam != null && tree.recvparam.sym != null) { |
1018 if (tree.recvparam != null && tree.recvparam.sym != null && |
|
1019 !tree.recvparam.mods.annotations.isEmpty()) { |
|
1020 // Nothing to do for separateAnnotationsKinds if |
|
1021 // there are no annotations of either kind. |
927 // TODO: make sure there are no declaration annotations. |
1022 // TODO: make sure there are no declaration annotations. |
928 TypeAnnotationPosition pos = new TypeAnnotationPosition(); |
1023 TypeAnnotationPosition pos = new TypeAnnotationPosition(); |
929 pos.type = TargetType.METHOD_RECEIVER; |
1024 pos.type = TargetType.METHOD_RECEIVER; |
930 pos.pos = tree.recvparam.vartype.pos; |
1025 pos.pos = tree.recvparam.vartype.pos; |
931 separateAnnotationsKinds(tree.recvparam.vartype, tree.recvparam.sym.type, |
1026 separateAnnotationsKinds(tree.recvparam.vartype, tree.recvparam.sym.type, |
932 tree.recvparam.sym, pos); |
1027 tree.recvparam.sym, pos); |
933 } |
1028 } |
934 int i = 0; |
1029 int i = 0; |
935 for (JCVariableDecl param : tree.params) { |
1030 for (JCVariableDecl param : tree.params) { |
936 TypeAnnotationPosition pos = new TypeAnnotationPosition(); |
1031 if (!param.mods.annotations.isEmpty()) { |
937 pos.type = TargetType.METHOD_FORMAL_PARAMETER; |
1032 // Nothing to do for separateAnnotationsKinds if |
938 pos.parameter_index = i; |
1033 // there are no annotations of either kind. |
939 pos.pos = param.vartype.pos; |
1034 TypeAnnotationPosition pos = new TypeAnnotationPosition(); |
940 separateAnnotationsKinds(param.vartype, param.sym.type, param.sym, pos); |
1035 pos.type = TargetType.METHOD_FORMAL_PARAMETER; |
|
1036 pos.parameter_index = i; |
|
1037 pos.pos = param.vartype.pos; |
|
1038 separateAnnotationsKinds(param.vartype, param.sym.type, param.sym, pos); |
|
1039 } |
941 ++i; |
1040 ++i; |
942 } |
1041 } |
943 } |
1042 } |
944 |
1043 |
945 push(tree); |
1044 push(tree); |
956 scan(tree.body); |
1055 scan(tree.body); |
957 } |
1056 } |
958 pop(); |
1057 pop(); |
959 } |
1058 } |
960 |
1059 |
|
1060 /* Store a reference to the current lambda expression, to |
|
1061 * be used by all type annotations within this expression. |
|
1062 */ |
|
1063 private JCLambda currentLambda = null; |
|
1064 |
|
1065 public void visitLambda(JCLambda tree) { |
|
1066 JCLambda prevLambda = currentLambda; |
|
1067 try { |
|
1068 currentLambda = tree; |
|
1069 |
|
1070 int i = 0; |
|
1071 for (JCVariableDecl param : tree.params) { |
|
1072 if (!param.mods.annotations.isEmpty()) { |
|
1073 // Nothing to do for separateAnnotationsKinds if |
|
1074 // there are no annotations of either kind. |
|
1075 TypeAnnotationPosition pos = new TypeAnnotationPosition(); |
|
1076 pos.type = TargetType.METHOD_FORMAL_PARAMETER; |
|
1077 pos.parameter_index = i; |
|
1078 pos.pos = param.vartype.pos; |
|
1079 pos.onLambda = tree; |
|
1080 separateAnnotationsKinds(param.vartype, param.sym.type, param.sym, pos); |
|
1081 } |
|
1082 ++i; |
|
1083 } |
|
1084 |
|
1085 push(tree); |
|
1086 scan(tree.body); |
|
1087 scan(tree.params); |
|
1088 pop(); |
|
1089 } finally { |
|
1090 currentLambda = prevLambda; |
|
1091 } |
|
1092 } |
|
1093 |
961 /** |
1094 /** |
962 * Resolve declaration vs. type annotations in variable declarations and |
1095 * Resolve declaration vs. type annotations in variable declarations and |
963 * then determine the positions. |
1096 * then determine the positions. |
964 */ |
1097 */ |
965 @Override |
1098 @Override |
966 public void visitVarDef(final JCVariableDecl tree) { |
1099 public void visitVarDef(final JCVariableDecl tree) { |
967 if (tree.sym == null) { |
1100 if (tree.mods.annotations.isEmpty()) { |
|
1101 // Nothing to do for separateAnnotationsKinds if |
|
1102 // there are no annotations of either kind. |
|
1103 } else if (tree.sym == null) { |
968 // Something is wrong already. Quietly ignore. |
1104 // Something is wrong already. Quietly ignore. |
969 } else if (tree.sym.getKind() == ElementKind.PARAMETER) { |
1105 } else if (tree.sym.getKind() == ElementKind.PARAMETER) { |
970 // Parameters are handled in visitMethodDef above. |
1106 // Parameters are handled in visitMethodDef or visitLambda. |
971 } else if (tree.sym.getKind() == ElementKind.FIELD) { |
1107 } else if (tree.sym.getKind() == ElementKind.FIELD) { |
972 if (sigOnly) { |
1108 if (sigOnly) { |
973 TypeAnnotationPosition pos = new TypeAnnotationPosition(); |
1109 TypeAnnotationPosition pos = new TypeAnnotationPosition(); |
974 pos.type = TargetType.FIELD; |
1110 pos.type = TargetType.FIELD; |
975 pos.pos = tree.pos; |
1111 pos.pos = tree.pos; |
977 } |
1113 } |
978 } else if (tree.sym.getKind() == ElementKind.LOCAL_VARIABLE) { |
1114 } else if (tree.sym.getKind() == ElementKind.LOCAL_VARIABLE) { |
979 TypeAnnotationPosition pos = new TypeAnnotationPosition(); |
1115 TypeAnnotationPosition pos = new TypeAnnotationPosition(); |
980 pos.type = TargetType.LOCAL_VARIABLE; |
1116 pos.type = TargetType.LOCAL_VARIABLE; |
981 pos.pos = tree.pos; |
1117 pos.pos = tree.pos; |
|
1118 pos.onLambda = currentLambda; |
982 separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos); |
1119 separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos); |
983 } else if (tree.sym.getKind() == ElementKind.EXCEPTION_PARAMETER) { |
1120 } else if (tree.sym.getKind() == ElementKind.EXCEPTION_PARAMETER) { |
984 TypeAnnotationPosition pos = new TypeAnnotationPosition(); |
1121 TypeAnnotationPosition pos = new TypeAnnotationPosition(); |
985 pos.type = TargetType.EXCEPTION_PARAMETER; |
1122 pos.type = TargetType.EXCEPTION_PARAMETER; |
986 pos.pos = tree.pos; |
1123 pos.pos = tree.pos; |
|
1124 pos.onLambda = currentLambda; |
987 separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos); |
1125 separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos); |
988 } else if (tree.sym.getKind() == ElementKind.RESOURCE_VARIABLE) { |
1126 } else if (tree.sym.getKind() == ElementKind.RESOURCE_VARIABLE) { |
989 TypeAnnotationPosition pos = new TypeAnnotationPosition(); |
1127 TypeAnnotationPosition pos = new TypeAnnotationPosition(); |
990 pos.type = TargetType.RESOURCE_VARIABLE; |
1128 pos.type = TargetType.RESOURCE_VARIABLE; |
991 pos.pos = tree.pos; |
1129 pos.pos = tree.pos; |
|
1130 pos.onLambda = currentLambda; |
992 separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos); |
1131 separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos); |
993 } else if (tree.sym.getKind() == ElementKind.ENUM_CONSTANT) { |
1132 } else if (tree.sym.getKind() == ElementKind.ENUM_CONSTANT) { |
994 // No type annotations can occur here. |
1133 // No type annotations can occur here. |
995 } else { |
1134 } else { |
996 // There is nothing else in a variable declaration that needs separation. |
1135 // There is nothing else in a variable declaration that needs separation. |
1029 findPosition(tree, peek2(), tree.annotations); |
1168 findPosition(tree, peek2(), tree.annotations); |
1030 super.visitTypeParameter(tree); |
1169 super.visitTypeParameter(tree); |
1031 } |
1170 } |
1032 |
1171 |
1033 @Override |
1172 @Override |
|
1173 public void visitNewClass(JCNewClass tree) { |
|
1174 if (tree.def != null && |
|
1175 !tree.def.mods.annotations.isEmpty()) { |
|
1176 JCClassDecl classdecl = tree.def; |
|
1177 TypeAnnotationPosition pos = new TypeAnnotationPosition(); |
|
1178 pos.type = TargetType.CLASS_EXTENDS; |
|
1179 pos.pos = tree.pos; |
|
1180 if (classdecl.extending == tree.clazz) { |
|
1181 pos.type_index = -1; |
|
1182 } else if (classdecl.implementing.contains(tree.clazz)) { |
|
1183 pos.type_index = classdecl.implementing.indexOf(tree.clazz); |
|
1184 } else { |
|
1185 // In contrast to CLASS elsewhere, typarams cannot occur here. |
|
1186 Assert.error("Could not determine position of tree " + tree); |
|
1187 } |
|
1188 Type before = classdecl.sym.type; |
|
1189 separateAnnotationsKinds(classdecl, tree.clazz.type, classdecl.sym, pos); |
|
1190 |
|
1191 // classdecl.sym.type now contains an annotated type, which |
|
1192 // is not what we want there. |
|
1193 // TODO: should we put this type somewhere in the superclass/interface? |
|
1194 classdecl.sym.type = before; |
|
1195 } |
|
1196 |
|
1197 scan(tree.encl); |
|
1198 scan(tree.typeargs); |
|
1199 scan(tree.clazz); |
|
1200 scan(tree.args); |
|
1201 |
|
1202 // The class body will already be scanned. |
|
1203 // scan(tree.def); |
|
1204 } |
|
1205 |
|
1206 @Override |
1034 public void visitNewArray(JCNewArray tree) { |
1207 public void visitNewArray(JCNewArray tree) { |
1035 findPosition(tree, tree, tree.annotations); |
1208 findPosition(tree, tree, tree.annotations); |
1036 int dimAnnosCount = tree.dimAnnotations.size(); |
1209 int dimAnnosCount = tree.dimAnnotations.size(); |
1037 ListBuffer<TypePathEntry> depth = ListBuffer.lb(); |
1210 ListBuffer<TypePathEntry> depth = ListBuffer.lb(); |
1038 |
1211 |
1039 // handle annotations associated with dimensions |
1212 // handle annotations associated with dimensions |
1040 for (int i = 0; i < dimAnnosCount; ++i) { |
1213 for (int i = 0; i < dimAnnosCount; ++i) { |
1041 TypeAnnotationPosition p = new TypeAnnotationPosition(); |
1214 TypeAnnotationPosition p = new TypeAnnotationPosition(); |
1042 p.pos = tree.pos; |
1215 p.pos = tree.pos; |
|
1216 p.onLambda = currentLambda; |
1043 p.type = TargetType.NEW; |
1217 p.type = TargetType.NEW; |
1044 if (i != 0) { |
1218 if (i != 0) { |
1045 depth = depth.append(TypePathEntry.ARRAY); |
1219 depth = depth.append(TypePathEntry.ARRAY); |
1046 p.location = p.location.appendList(depth.toList()); |
1220 p.location = p.location.appendList(depth.toList()); |
1047 } |
1221 } |
1051 |
1225 |
1052 // handle "free" annotations |
1226 // handle "free" annotations |
1053 // int i = dimAnnosCount == 0 ? 0 : dimAnnosCount - 1; |
1227 // int i = dimAnnosCount == 0 ? 0 : dimAnnosCount - 1; |
1054 // TODO: is depth.size == i here? |
1228 // TODO: is depth.size == i here? |
1055 JCExpression elemType = tree.elemtype; |
1229 JCExpression elemType = tree.elemtype; |
|
1230 depth = depth.append(TypePathEntry.ARRAY); |
1056 while (elemType != null) { |
1231 while (elemType != null) { |
1057 if (elemType.hasTag(JCTree.Tag.ANNOTATED_TYPE)) { |
1232 if (elemType.hasTag(JCTree.Tag.ANNOTATED_TYPE)) { |
1058 JCAnnotatedType at = (JCAnnotatedType)elemType; |
1233 JCAnnotatedType at = (JCAnnotatedType)elemType; |
1059 TypeAnnotationPosition p = new TypeAnnotationPosition(); |
1234 TypeAnnotationPosition p = new TypeAnnotationPosition(); |
1060 p.type = TargetType.NEW; |
1235 p.type = TargetType.NEW; |
1061 p.pos = tree.pos; |
1236 p.pos = tree.pos; |
1062 p.location = p.location.appendList(depth.toList()); |
1237 p.onLambda = currentLambda; |
|
1238 locateNestedTypes(elemType.type, p); |
|
1239 p.location = p.location.prependList(depth.toList()); |
1063 setTypeAnnotationPos(at.annotations, p); |
1240 setTypeAnnotationPos(at.annotations, p); |
1064 elemType = at.underlyingType; |
1241 elemType = at.underlyingType; |
1065 } else if (elemType.hasTag(JCTree.Tag.TYPEARRAY)) { |
1242 } else if (elemType.hasTag(JCTree.Tag.TYPEARRAY)) { |
1066 depth = depth.append(TypePathEntry.ARRAY); |
1243 depth = depth.append(TypePathEntry.ARRAY); |
1067 elemType = ((JCArrayTypeTree)elemType).elemtype; |
1244 elemType = ((JCArrayTypeTree)elemType).elemtype; |
|
1245 } else if (elemType.hasTag(JCTree.Tag.SELECT)) { |
|
1246 elemType = ((JCFieldAccess)elemType).selected; |
1068 } else { |
1247 } else { |
1069 break; |
1248 break; |
1070 } |
1249 } |
1071 } |
1250 } |
1072 scan(tree.elems); |
1251 scan(tree.elems); |
1074 |
1253 |
1075 private void findPosition(JCTree tree, JCTree frame, List<JCAnnotation> annotations) { |
1254 private void findPosition(JCTree tree, JCTree frame, List<JCAnnotation> annotations) { |
1076 if (!annotations.isEmpty()) { |
1255 if (!annotations.isEmpty()) { |
1077 /* |
1256 /* |
1078 System.out.println("Finding pos for: " + annotations); |
1257 System.out.println("Finding pos for: " + annotations); |
1079 System.out.println(" tree: " + tree); |
1258 System.out.println(" tree: " + tree + " kind: " + tree.getKind()); |
1080 System.out.println(" frame: " + frame); |
1259 System.out.println(" frame: " + frame + " kind: " + frame.getKind()); |
1081 */ |
1260 */ |
1082 TypeAnnotationPosition p = new TypeAnnotationPosition(); |
1261 TypeAnnotationPosition p = new TypeAnnotationPosition(); |
|
1262 p.onLambda = currentLambda; |
1083 resolveFrame(tree, frame, frames.toList(), p); |
1263 resolveFrame(tree, frame, frames.toList(), p); |
1084 setTypeAnnotationPos(annotations, p); |
1264 setTypeAnnotationPos(annotations, p); |
1085 } |
1265 } |
1086 } |
1266 } |
1087 |
1267 |
1088 private static void setTypeAnnotationPos(List<JCAnnotation> annotations, |
1268 private static void setTypeAnnotationPos(List<JCAnnotation> annotations, |
1089 TypeAnnotationPosition position) { |
1269 TypeAnnotationPosition position) { |
1090 for (JCAnnotation anno : annotations) { |
1270 for (JCAnnotation anno : annotations) { |
1091 ((Attribute.TypeCompound) anno.attribute).position = position; |
1271 // attribute might be null during DeferredAttr; |
1092 } |
1272 // we will be back later. |
|
1273 if (anno.attribute != null) { |
|
1274 ((Attribute.TypeCompound) anno.attribute).position = position; |
|
1275 } |
|
1276 } |
|
1277 } |
|
1278 |
|
1279 @Override |
|
1280 public String toString() { |
|
1281 return super.toString() + ": sigOnly: " + sigOnly; |
1093 } |
1282 } |
1094 } |
1283 } |
1095 } |
1284 } |