src/share/classes/com/sun/tools/javac/code/TypeAnnotations.java

changeset 1571
af8417e590f4
parent 1563
bc456436c613
child 1644
40adaf938847
equal deleted inserted replaced
1570:f91144b7da75 1571:af8417e590f4
215 // We found a type annotation in a declaration annotation position, 215 // We found a type annotation in a declaration annotation position,
216 // for example, on the return type. 216 // for example, on the return type.
217 // Such an annotation is _not_ part of an JCAnnotatedType tree and we therefore 217 // Such an annotation is _not_ part of an JCAnnotatedType tree and we therefore
218 // need to set its position explicitly. 218 // need to set its position explicitly.
219 // The method returns a copy of type that contains these annotations. 219 // The method returns a copy of type that contains these annotations.
220 //
221 // As a side effect the method sets the type annotation position of "annotations".
222 // Note that it is assumed that all annotations share the same position.
220 private static Type typeWithAnnotations(final JCTree typetree, final Type type, 223 private static Type typeWithAnnotations(final JCTree typetree, final Type type,
221 final List<Attribute.TypeCompound> annotations, Log log) { 224 final List<Attribute.TypeCompound> annotations, Log log) {
222 // System.out.printf("typeWithAnnotations(typetree: %s, type: %s, annotations: %s)%n", 225 // System.out.printf("typeWithAnnotations(typetree: %s, type: %s, annotations: %s)%n",
223 // typetree, type, annotations); 226 // typetree, type, annotations);
224 if (annotations.isEmpty()) { 227 if (annotations.isEmpty()) {
265 arTree = arrayTypeTree(arTree.elemtype); 268 arTree = arrayTypeTree(arTree.elemtype);
266 depth = depth.append(TypePathEntry.ARRAY); 269 depth = depth.append(TypePathEntry.ARRAY);
267 } 270 }
268 Type arelemType = typeWithAnnotations(arTree.elemtype, arType.elemtype, annotations, log); 271 Type arelemType = typeWithAnnotations(arTree.elemtype, arType.elemtype, annotations, log);
269 tomodify.elemtype = arelemType; 272 tomodify.elemtype = arelemType;
270 for (Attribute.TypeCompound a : annotations) { 273 {
274 // All annotations share the same position; modify the first one.
275 Attribute.TypeCompound a = annotations.get(0);
271 TypeAnnotationPosition p = a.position; 276 TypeAnnotationPosition p = a.position;
272 p.location = p.location.prependList(depth.toList()); 277 p.location = p.location.prependList(depth.toList());
273 } 278 }
274 return toreturn; 279 return toreturn;
275 } else if (type.hasTag(TypeTag.TYPEVAR)) { 280 } else if (type.hasTag(TypeTag.TYPEVAR)) {
343 } 348 }
344 349
345 if (depth.nonEmpty()) { 350 if (depth.nonEmpty()) {
346 // Only need to change the annotation positions 351 // Only need to change the annotation positions
347 // if they are on an enclosed type. 352 // if they are on an enclosed type.
348 for (Attribute.TypeCompound a : annotations) { 353 // All annotations share the same position; modify the first one.
349 TypeAnnotationPosition p = a.position; 354 Attribute.TypeCompound a = annotations.get(0);
350 p.location = p.location.appendList(depth.toList()); 355 TypeAnnotationPosition p = a.position;
351 } 356 p.location = p.location.appendList(depth.toList());
352 } 357 }
353 358
354 Type ret = typeWithAnnotations(type, enclTy, annotations); 359 Type ret = typeWithAnnotations(type, enclTy, annotations);
355 return ret; 360 return ret;
356 } 361 }
461 return new AnnotatedType(s, t); 466 return new AnnotatedType(s, t);
462 } 467 }
463 468
464 @Override 469 @Override
465 public Type visitType(Type t, List<TypeCompound> s) { 470 public Type visitType(Type t, List<TypeCompound> s) {
466 // Error? 471 return new AnnotatedType(s, t);
467 return t;
468 } 472 }
469 }; 473 };
470 474
471 return type.accept(visitor, annotations); 475 return type.accept(visitor, annotations);
472 } 476 }
573 List<JCTree> path, TypeAnnotationPosition p) { 577 List<JCTree> path, TypeAnnotationPosition p) {
574 /* 578 /*
575 System.out.println("Resolving tree: " + tree + " kind: " + tree.getKind()); 579 System.out.println("Resolving tree: " + tree + " kind: " + tree.getKind());
576 System.out.println(" Framing tree: " + frame + " kind: " + frame.getKind()); 580 System.out.println(" Framing tree: " + frame + " kind: " + frame.getKind());
577 */ 581 */
582
583 // Note that p.offset is set in
584 // com.sun.tools.javac.jvm.Gen.setTypeAnnotationPositions(int)
585
578 switch (frame.getKind()) { 586 switch (frame.getKind()) {
579 case TYPE_CAST: 587 case TYPE_CAST:
580 p.type = TargetType.CAST; 588 p.type = TargetType.CAST;
581 p.pos = frame.pos; 589 p.pos = frame.pos;
582 return; 590 return;
654 " within frame " + frame); 662 " within frame " + frame);
655 } 663 }
656 664
657 List<JCTree> newPath = path.tail; 665 List<JCTree> newPath = path.tail;
658 resolveFrame(newPath.head, newPath.tail.head, newPath, p); 666 resolveFrame(newPath.head, newPath.tail.head, newPath, p);
667 return;
668 }
669
670 case MEMBER_REFERENCE: {
671 JCMemberReference mrframe = (JCMemberReference) frame;
672
673 if (mrframe.expr == tree) {
674 switch (mrframe.mode) {
675 case INVOKE:
676 p.type = TargetType.METHOD_REFERENCE;
677 break;
678 case NEW:
679 p.type = TargetType.CONSTRUCTOR_REFERENCE;
680 break;
681 default:
682 Assert.error("Unknown method reference mode " + mrframe.mode +
683 " for tree " + tree + " within frame " + frame);
684 }
685 p.pos = frame.pos;
686 } else if (mrframe.typeargs != null &&
687 mrframe.typeargs.contains(tree)) {
688 int arg = mrframe.typeargs.indexOf(tree);
689 p.type_index = arg;
690 switch (mrframe.mode) {
691 case INVOKE:
692 p.type = TargetType.METHOD_REFERENCE_TYPE_ARGUMENT;
693 break;
694 case NEW:
695 p.type = TargetType.CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT;
696 break;
697 default:
698 Assert.error("Unknown method reference mode " + mrframe.mode +
699 " for tree " + tree + " within frame " + frame);
700 }
701 p.pos = frame.pos;
702 } else {
703 Assert.error("Could not determine type argument position of tree " + tree +
704 " within frame " + frame);
705 }
659 return; 706 return;
660 } 707 }
661 708
662 case ARRAY_TYPE: { 709 case ARRAY_TYPE: {
663 ListBuffer<TypePathEntry> index = ListBuffer.lb(); 710 ListBuffer<TypePathEntry> index = ListBuffer.lb();
764 List<JCTree> newPath = path.tail; 811 List<JCTree> newPath = path.tail;
765 resolveFrame(newPath.head, newPath.tail.head, newPath, p); 812 resolveFrame(newPath.head, newPath.tail.head, newPath, p);
766 return; 813 return;
767 } 814 }
768 815
816 case INTERSECTION_TYPE: {
817 JCTypeIntersection isect = (JCTypeIntersection)frame;
818 p.type_index = isect.bounds.indexOf(tree);
819 List<JCTree> newPath = path.tail;
820 resolveFrame(newPath.head, newPath.tail.head, newPath, p);
821 return;
822 }
823
769 case METHOD_INVOCATION: { 824 case METHOD_INVOCATION: {
770 JCMethodInvocation invocation = (JCMethodInvocation)frame; 825 JCMethodInvocation invocation = (JCMethodInvocation)frame;
771 if (!invocation.typeargs.contains(tree)) { 826 if (!invocation.typeargs.contains(tree)) {
772 Assert.error("{" + tree + "} is not an argument in the invocation: " + invocation); 827 Assert.error("{" + tree + "} is not an argument in the invocation: " + invocation);
773 } 828 }
909 */ 964 */
910 @Override 965 @Override
911 public void visitVarDef(final JCVariableDecl tree) { 966 public void visitVarDef(final JCVariableDecl tree) {
912 if (tree.sym == null) { 967 if (tree.sym == null) {
913 // Something is wrong already. Quietly ignore. 968 // Something is wrong already. Quietly ignore.
969 } else if (tree.sym.getKind() == ElementKind.PARAMETER) {
970 // Parameters are handled in visitMethodDef above.
914 } else if (tree.sym.getKind() == ElementKind.FIELD) { 971 } else if (tree.sym.getKind() == ElementKind.FIELD) {
915 if (sigOnly) { 972 if (sigOnly) {
916 TypeAnnotationPosition pos = new TypeAnnotationPosition(); 973 TypeAnnotationPosition pos = new TypeAnnotationPosition();
917 pos.type = TargetType.FIELD; 974 pos.type = TargetType.FIELD;
918 pos.pos = tree.pos; 975 pos.pos = tree.pos;
922 TypeAnnotationPosition pos = new TypeAnnotationPosition(); 979 TypeAnnotationPosition pos = new TypeAnnotationPosition();
923 pos.type = TargetType.LOCAL_VARIABLE; 980 pos.type = TargetType.LOCAL_VARIABLE;
924 pos.pos = tree.pos; 981 pos.pos = tree.pos;
925 separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos); 982 separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos);
926 } else if (tree.sym.getKind() == ElementKind.EXCEPTION_PARAMETER) { 983 } else if (tree.sym.getKind() == ElementKind.EXCEPTION_PARAMETER) {
927 // System.out.println("Found exception param: " + tree);
928 TypeAnnotationPosition pos = new TypeAnnotationPosition(); 984 TypeAnnotationPosition pos = new TypeAnnotationPosition();
929 pos.type = TargetType.EXCEPTION_PARAMETER; 985 pos.type = TargetType.EXCEPTION_PARAMETER;
930 pos.pos = tree.pos; 986 pos.pos = tree.pos;
931 separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos); 987 separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos);
932 } else if (tree.sym.getKind() == ElementKind.RESOURCE_VARIABLE) { 988 } else if (tree.sym.getKind() == ElementKind.RESOURCE_VARIABLE) {
933 TypeAnnotationPosition pos = new TypeAnnotationPosition(); 989 TypeAnnotationPosition pos = new TypeAnnotationPosition();
934 pos.type = TargetType.RESOURCE_VARIABLE; 990 pos.type = TargetType.RESOURCE_VARIABLE;
935 pos.pos = tree.pos; 991 pos.pos = tree.pos;
936 separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos); 992 separateAnnotationsKinds(tree.vartype, tree.sym.type, tree.sym, pos);
993 } else if (tree.sym.getKind() == ElementKind.ENUM_CONSTANT) {
994 // No type annotations can occur here.
937 } else { 995 } else {
938 // There is nothing else in a variable declaration that needs separation. 996 // There is nothing else in a variable declaration that needs separation.
939 // System.out.println("We found a: " + tree); 997 Assert.error("Unhandled variable kind: " + tree + " of kind: " + tree.sym.getKind());
940 } 998 }
941 999
942 push(tree); 1000 push(tree);
943 // super.visitVarDef(tree); 1001 // super.visitVarDef(tree);
944 scan(tree.mods); 1002 scan(tree.mods);

mercurial