TypeAnnotationPosition.java revision 3666:90dd93e668a5
1/*
2 * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
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
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package com.sun.tools.javac.code;
27
28import java.util.Iterator;
29
30import com.sun.tools.javac.tree.JCTree.JCLambda;
31import com.sun.tools.javac.util.*;
32
33/** A type annotation position.
34*
35*  <p><b>This is NOT part of any supported API.
36*  If you write code that depends on this, you do so at your own risk.
37*  This code and its internal interfaces are subject to change or
38*  deletion without notice.</b>
39*/
40// Code duplicated in com.sun.tools.classfile.TypeAnnotation.Position
41public class TypeAnnotationPosition {
42
43    public enum TypePathEntryKind {
44        ARRAY(0),
45        INNER_TYPE(1),
46        WILDCARD(2),
47        TYPE_ARGUMENT(3);
48
49        public final int tag;
50
51        private TypePathEntryKind(int tag) {
52            this.tag = tag;
53        }
54    }
55
56    public static class TypePathEntry {
57        /** The fixed number of bytes per TypePathEntry. */
58        public static final int bytesPerEntry = 2;
59
60        public final TypePathEntryKind tag;
61        public final int arg;
62
63        public static final TypePathEntry ARRAY = new TypePathEntry(TypePathEntryKind.ARRAY);
64        public static final TypePathEntry INNER_TYPE = new TypePathEntry(TypePathEntryKind.INNER_TYPE);
65        public static final TypePathEntry WILDCARD = new TypePathEntry(TypePathEntryKind.WILDCARD);
66
67        private TypePathEntry(TypePathEntryKind tag) {
68            Assert.check(tag == TypePathEntryKind.ARRAY ||
69                    tag == TypePathEntryKind.INNER_TYPE ||
70                    tag == TypePathEntryKind.WILDCARD);
71            this.tag = tag;
72            this.arg = 0;
73        }
74
75        public TypePathEntry(TypePathEntryKind tag, int arg) {
76            Assert.check(tag == TypePathEntryKind.TYPE_ARGUMENT);
77            this.tag = tag;
78            this.arg = arg;
79        }
80
81        public static TypePathEntry fromBinary(int tag, int arg) {
82            Assert.check(arg == 0 || tag == TypePathEntryKind.TYPE_ARGUMENT.tag);
83            switch (tag) {
84            case 0:
85                return ARRAY;
86            case 1:
87                return INNER_TYPE;
88            case 2:
89                return WILDCARD;
90            case 3:
91                return new TypePathEntry(TypePathEntryKind.TYPE_ARGUMENT, arg);
92            default:
93                Assert.error("Invalid TypePathEntryKind tag: " + tag);
94                return null;
95            }
96        }
97
98        @Override
99        public String toString() {
100            return tag.toString() +
101                    (tag == TypePathEntryKind.TYPE_ARGUMENT ? ("(" + arg + ")") : "");
102        }
103
104        @Override
105        public boolean equals(Object other) {
106            if (! (other instanceof TypePathEntry)) {
107                return false;
108            }
109            TypePathEntry tpe = (TypePathEntry) other;
110            return this.tag == tpe.tag && this.arg == tpe.arg;
111        }
112
113        @Override
114        public int hashCode() {
115            return this.tag.hashCode() * 17 + this.arg;
116        }
117    }
118
119    public static final List<TypePathEntry> emptyPath = List.nil();
120
121    public final TargetType type;
122
123    // For generic/array types.
124
125    public List<TypePathEntry> location;
126
127    // Tree position.
128    public final int pos;
129
130    // For type casts, type tests, new, locals (as start_pc),
131    // and method and constructor reference type arguments.
132    public boolean isValidOffset = false;
133    public int offset = -1;
134
135    // For locals. arrays same length
136    public int[] lvarOffset = null;
137    public int[] lvarLength = null;
138    public int[] lvarIndex = null;
139
140    // For type parameter bound
141    public final int bound_index;
142
143    // For type parameter and method parameter
144    public int parameter_index;
145
146    // For class extends, implements, and throws clauses
147    public final int type_index;
148
149    // For exception parameters, index into exception table.  In
150    // com.sun.tools.javac.jvm.Gen.genCatch, we first use this to hold
151    // the catch type's constant pool entry index.  Then in
152    // com.sun.tools.javac.jvm.Code.fillExceptionParameterPositions we
153    // use that value to determine the exception table index.
154    // When read from class file, this holds
155    private int exception_index = Integer.MIN_VALUE;
156
157    // If this type annotation is within a lambda expression,
158    // store a pointer to the lambda expression tree in order
159    // to allow a later translation to the right method.
160    public final JCLambda onLambda;
161
162    // NOTE: This constructor will eventually go away, and be replaced
163    // by static builder methods.
164
165    @Override
166    public String toString() {
167        StringBuilder sb = new StringBuilder();
168        sb.append('[');
169        sb.append(type);
170
171        switch (type) {
172        // instanceof
173        case INSTANCEOF:
174        // new expression
175        case NEW:
176        // constructor/method reference receiver
177        case CONSTRUCTOR_REFERENCE:
178        case METHOD_REFERENCE:
179            sb.append(", offset = ");
180            sb.append(offset);
181            break;
182        // local variable
183        case LOCAL_VARIABLE:
184        // resource variable
185        case RESOURCE_VARIABLE:
186            if (lvarOffset == null) {
187                sb.append(", lvarOffset is null!");
188                break;
189            }
190            sb.append(", {");
191            for (int i = 0; i < lvarOffset.length; ++i) {
192                if (i != 0) sb.append("; ");
193                sb.append("start_pc = ");
194                sb.append(lvarOffset[i]);
195                sb.append(", length = ");
196                sb.append(lvarLength[i]);
197                sb.append(", index = ");
198                sb.append(lvarIndex[i]);
199            }
200            sb.append("}");
201            break;
202        // method receiver
203        case METHOD_RECEIVER:
204            // Do nothing
205            break;
206        // type parameter
207        case CLASS_TYPE_PARAMETER:
208        case METHOD_TYPE_PARAMETER:
209            sb.append(", param_index = ");
210            sb.append(parameter_index);
211            break;
212        // type parameter bound
213        case CLASS_TYPE_PARAMETER_BOUND:
214        case METHOD_TYPE_PARAMETER_BOUND:
215            sb.append(", param_index = ");
216            sb.append(parameter_index);
217            sb.append(", bound_index = ");
218            sb.append(bound_index);
219            break;
220        // class extends or implements clause
221        case CLASS_EXTENDS:
222            sb.append(", type_index = ");
223            sb.append(type_index);
224            break;
225        // throws
226        case THROWS:
227            sb.append(", type_index = ");
228            sb.append(type_index);
229            break;
230        // exception parameter
231        case EXCEPTION_PARAMETER:
232            sb.append(", exception_index = ");
233            sb.append(exception_index);
234            break;
235        // method parameter
236        case METHOD_FORMAL_PARAMETER:
237            sb.append(", param_index = ");
238            sb.append(parameter_index);
239            break;
240        // type cast
241        case CAST:
242        // method/constructor/reference type argument
243        case CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT:
244        case METHOD_INVOCATION_TYPE_ARGUMENT:
245        case CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT:
246        case METHOD_REFERENCE_TYPE_ARGUMENT:
247            sb.append(", offset = ");
248            sb.append(offset);
249            sb.append(", type_index = ");
250            sb.append(type_index);
251            break;
252        // We don't need to worry about these
253        case METHOD_RETURN:
254        case FIELD:
255            break;
256        case UNKNOWN:
257            sb.append(", position UNKNOWN!");
258            break;
259        default:
260            Assert.error("Unknown target type: " + type);
261        }
262
263        // Append location data for generics/arrays.
264        if (!location.isEmpty()) {
265            sb.append(", location = (");
266            sb.append(location);
267            sb.append(")");
268        }
269
270        sb.append(", pos = ");
271        sb.append(pos);
272
273        if (onLambda != null) {
274            sb.append(", onLambda hash = ");
275            sb.append(onLambda.hashCode());
276        }
277
278        sb.append(']');
279        return sb.toString();
280    }
281
282    /**
283     * Indicates whether the target tree of the annotation has been optimized
284     * away from classfile or not.
285     * @return true if the target has not been optimized away
286     */
287    public boolean emitToClassfile() {
288        return !type.isLocal() || isValidOffset;
289    }
290
291
292    public boolean matchesPos(int pos) {
293        return this.pos == pos;
294    }
295
296    public void updatePosOffset(int to) {
297        offset = to;
298        lvarOffset = new int[]{to};
299        isValidOffset = true;
300    }
301
302    public boolean hasExceptionIndex() {
303        return exception_index >= 0;
304    }
305
306    public int getExceptionIndex() {
307        Assert.check(exception_index >= 0, "exception_index is not set");
308        return exception_index;
309    }
310
311    public void setExceptionIndex(final int exception_index) {
312        Assert.check(!hasExceptionIndex(), "exception_index already set");
313        Assert.check(exception_index >= 0, "Expected a valid index into exception table");
314        this.exception_index = exception_index;
315    }
316
317    public boolean hasCatchType() {
318        return exception_index < 0 && exception_index != Integer.MIN_VALUE;
319    }
320
321    public int getCatchType() {
322        Assert.check(hasCatchType(),
323                     "exception_index does not contain valid catch info");
324        return ((-this.exception_index) - 1) & 0xff ;
325    }
326
327    public int getStartPos() {
328        Assert.check(hasCatchType(),
329                     "exception_index does not contain valid catch info");
330        return ((-this.exception_index) - 1) >> 8 ;
331    }
332
333    public void setCatchInfo(final int catchType, final int startPos) {
334        Assert.check(!hasExceptionIndex(),
335                     "exception_index is already set");
336        Assert.check(catchType >= 0, "Expected a valid catch type");
337        this.exception_index = -((catchType | startPos << 8) + 1);
338    }
339
340    /**
341     * Decode the binary representation for a type path and set
342     * the {@code location} field.
343     *
344     * @param list The bytecode representation of the type path.
345     */
346    public static List<TypePathEntry> getTypePathFromBinary(java.util.List<Integer> list) {
347        ListBuffer<TypePathEntry> loc = new ListBuffer<>();
348        Iterator<Integer> iter = list.iterator();
349        while (iter.hasNext()) {
350            Integer fst = iter.next();
351            Assert.check(iter.hasNext());
352            Integer snd = iter.next();
353            loc = loc.append(TypePathEntry.fromBinary(fst, snd));
354        }
355        return loc.toList();
356    }
357
358    public static List<Integer> getBinaryFromTypePath(java.util.List<TypePathEntry> locs) {
359        ListBuffer<Integer> loc = new ListBuffer<>();
360        for (TypePathEntry tpe : locs) {
361            loc = loc.append(tpe.tag.tag);
362            loc = loc.append(tpe.arg);
363        }
364        return loc.toList();
365    }
366
367    // These methods are the new preferred way to create
368    // TypeAnnotationPositions
369
370    // Never make this a public constructor without creating a builder.
371    private TypeAnnotationPosition(final TargetType ttype,
372                                   final int pos,
373                                   final int parameter_index,
374                                   final JCLambda onLambda,
375                                   final int type_index,
376                                   final int bound_index,
377                                   final List<TypePathEntry> location) {
378        Assert.checkNonNull(location);
379        this.type = ttype;
380        this.pos = pos;
381        this.parameter_index = parameter_index;
382        this.onLambda = onLambda;
383        this.type_index = type_index;
384        this.bound_index = bound_index;
385        this.location = location;
386    }
387
388    /**
389     * Create a {@code TypeAnnotationPosition} for a method return.
390     *
391     * @param location The type path.
392     * @param onLambda The lambda for this parameter.
393     * @param pos The position from the associated tree node.
394     */
395    public static TypeAnnotationPosition
396        methodReturn(final List<TypePathEntry> location,
397                     final JCLambda onLambda,
398                     final int pos) {
399        return new TypeAnnotationPosition(TargetType.METHOD_RETURN, pos,
400                                          Integer.MIN_VALUE, onLambda,
401                                          Integer.MIN_VALUE, Integer.MIN_VALUE,
402                                          location);
403    }
404
405    /**
406     * Create a {@code TypeAnnotationPosition} for a method return.
407     *
408     * @param location The type path.
409     */
410    public static TypeAnnotationPosition
411        methodReturn(final List<TypePathEntry> location) {
412        return methodReturn(location, null, -1);
413    }
414
415    /**
416     * Create a {@code TypeAnnotationPosition} for a method return.
417     *
418     * @param pos The position from the associated tree node.
419     */
420    public static TypeAnnotationPosition methodReturn(final int pos) {
421        return methodReturn(emptyPath, null, pos);
422    }
423
424    /**
425     * Create a {@code TypeAnnotationPosition} for a method receiver parameter.
426     *
427     * @param location The type path.
428     * @param onLambda The lambda for this parameter.
429     * @param pos The position from the associated tree node.
430     */
431    public static TypeAnnotationPosition
432        methodReceiver(final List<TypePathEntry> location,
433                     final JCLambda onLambda,
434                     final int pos) {
435        return new TypeAnnotationPosition(TargetType.METHOD_RECEIVER, pos,
436                                          Integer.MIN_VALUE, onLambda,
437                                          Integer.MIN_VALUE, Integer.MIN_VALUE,
438                                          location);
439    }
440
441    /**
442     * Create a {@code TypeAnnotationPosition} for a method receiver parameter.
443     *
444     * @param location The type path.
445     */
446    public static TypeAnnotationPosition
447        methodReceiver(final List<TypePathEntry> location) {
448        return methodReceiver(location, null, -1);
449    }
450
451    /**
452     * Create a {@code TypeAnnotationPosition} for a method receiver parameter.
453     *
454     * @param pos The position from the associated tree node.
455     */
456    public static TypeAnnotationPosition methodReceiver(final int pos) {
457        return methodReceiver(emptyPath, null, pos);
458    }
459
460    /**
461     * Create a {@code TypeAnnotationPosition} for a method formal parameter.
462     *
463     * @param location The type path.
464     * @param onLambda The lambda for this parameter.
465     * @param parameter_index The index of the parameter.
466     * @param pos The position from the associated tree node.
467     */
468    public static TypeAnnotationPosition
469        methodParameter(final List<TypePathEntry> location,
470                        final JCLambda onLambda,
471                        final int parameter_index,
472                        final int pos) {
473        return new TypeAnnotationPosition(TargetType.METHOD_FORMAL_PARAMETER,
474                                          pos, parameter_index, onLambda,
475                                          Integer.MIN_VALUE, Integer.MIN_VALUE,
476                                          location);
477    }
478
479    /**
480     * Create a {@code TypeAnnotationPosition} for a method formal parameter.
481     *
482     * @param onLambda The lambda for this parameter.
483     * @param parameter_index The index of the parameter.
484     * @param pos The position from the associated tree node.
485     */
486    public static TypeAnnotationPosition
487        methodParameter(final JCLambda onLambda,
488                        final int parameter_index,
489                        final int pos) {
490        return methodParameter(emptyPath, onLambda,
491                               parameter_index, pos);
492    }
493
494    /**
495     * Create a {@code TypeAnnotationPosition} for a method formal parameter.
496     *
497     * @param parameter_index The index of the parameter.
498     * @param pos The position from the associated tree node.
499     */
500    public static TypeAnnotationPosition
501        methodParameter(final int parameter_index,
502                        final int pos) {
503        return methodParameter(null, parameter_index, pos);
504    }
505
506    /**
507     * Create a {@code TypeAnnotationPosition} for a method formal parameter.
508     *
509     * @param location The type path.
510     * @param parameter_index The index of the parameter.
511     */
512    public static TypeAnnotationPosition
513        methodParameter(final List<TypePathEntry> location,
514                        final int parameter_index) {
515        return methodParameter(location, null, parameter_index, -1);
516    }
517
518    /**
519     * Create a {@code TypeAnnotationPosition} for a method reference.
520     *
521     * @param location The type path.
522     * @param onLambda The lambda for this method reference.
523     * @param pos The position from the associated tree node.
524     */
525    public static TypeAnnotationPosition
526        methodRef(final List<TypePathEntry> location,
527                  final JCLambda onLambda,
528                  final int pos) {
529        return new TypeAnnotationPosition(TargetType.METHOD_REFERENCE, pos,
530                                          Integer.MIN_VALUE, onLambda,
531                                          Integer.MIN_VALUE, Integer.MIN_VALUE,
532                                          location);
533    }
534
535    /**
536     * Create a {@code TypeAnnotationPosition} for a method reference.
537     *
538     * @param location The type path.
539     */
540    public static TypeAnnotationPosition
541        methodRef(final List<TypePathEntry> location) {
542        return methodRef(location, null, -1);
543    }
544
545    /**
546     * Create a {@code TypeAnnotationPosition} for a constructor reference.
547     *
548     * @param location The type path.
549     * @param onLambda The lambda for this constructor reference.
550     * @param pos The position from the associated tree node.
551     */
552    public static TypeAnnotationPosition
553        constructorRef(final List<TypePathEntry> location,
554                       final JCLambda onLambda,
555                       final int pos) {
556        return new TypeAnnotationPosition(TargetType.CONSTRUCTOR_REFERENCE, pos,
557                                          Integer.MIN_VALUE, onLambda,
558                                          Integer.MIN_VALUE, Integer.MIN_VALUE,
559                                          location);
560    }
561
562    /**
563     * Create a {@code TypeAnnotationPosition} for a constructor reference.
564     *
565     * @param location The type path.
566     */
567    public static TypeAnnotationPosition
568        constructorRef(final List<TypePathEntry> location) {
569        return constructorRef(location, null, -1);
570    }
571
572    /**
573     * Create a {@code TypeAnnotationPosition} for a field.
574     *
575     * @param location The type path.
576     * @param onLambda The lambda for this variable.
577     * @param pos The position from the associated tree node.
578     */
579    public static TypeAnnotationPosition
580        field(final List<TypePathEntry> location,
581              final JCLambda onLambda,
582              final int pos) {
583        return new TypeAnnotationPosition(TargetType.FIELD, pos,
584                                          Integer.MIN_VALUE, onLambda,
585                                          Integer.MIN_VALUE, Integer.MIN_VALUE,
586                                          location);
587    }
588
589    /**
590     * Create a {@code TypeAnnotationPosition} for a field.
591     *
592     * @param location The type path.
593     */
594    public static TypeAnnotationPosition
595        field(final List<TypePathEntry> location) {
596        return field(location, null, -1);
597    }
598
599    /**
600     * Create a {@code TypeAnnotationPosition} for a field.
601     *
602     * @param pos The position from the associated tree node.
603     */
604    public static TypeAnnotationPosition field(final int pos) {
605        return field(emptyPath, null, pos);
606    }
607
608    /**
609     * Create a {@code TypeAnnotationPosition} for a local variable.
610     *
611     * @param location The type path.
612     * @param onLambda The lambda for this variable.
613     * @param pos The position from the associated tree node.
614     */
615    public static TypeAnnotationPosition
616        localVariable(final List<TypePathEntry> location,
617                      final JCLambda onLambda,
618                      final int pos) {
619        return new TypeAnnotationPosition(TargetType.LOCAL_VARIABLE, pos,
620                                          Integer.MIN_VALUE, onLambda,
621                                          Integer.MIN_VALUE, Integer.MIN_VALUE,
622                                          location);
623    }
624
625    /**
626     * Create a {@code TypeAnnotationPosition} for a local variable.
627     *
628     * @param onLambda The lambda for this variable.
629     * @param pos The position from the associated tree node.
630     */
631    public static TypeAnnotationPosition
632        localVariable(final JCLambda onLambda,
633                      final int pos) {
634        return localVariable(emptyPath, onLambda, pos);
635    }
636
637    /**
638     * Create a {@code TypeAnnotationPosition} for a local variable.
639     *
640     * @param location The type path.
641     */
642    public static TypeAnnotationPosition
643        localVariable(final List<TypePathEntry> location) {
644        return localVariable(location, null, -1);
645    }
646
647    /**
648     * Create a {@code TypeAnnotationPosition} for an exception parameter.
649     *
650     * @param location The type path.
651     * @param onLambda The lambda for this parameter.
652     * @param pos The position from the associated tree node.
653     */
654    public static TypeAnnotationPosition
655        exceptionParameter(final List<TypePathEntry> location,
656                           final JCLambda onLambda,
657                           final int pos) {
658        return new TypeAnnotationPosition(TargetType.EXCEPTION_PARAMETER, pos,
659                                          Integer.MIN_VALUE, onLambda,
660                                          Integer.MIN_VALUE, Integer.MIN_VALUE,
661                                          location);
662    }
663
664    /**
665     * Create a {@code TypeAnnotationPosition} for an exception parameter.
666     *
667     * @param onLambda The lambda for this parameter.
668     * @param pos The position from the associated tree node.
669     */
670    public static TypeAnnotationPosition
671        exceptionParameter(final JCLambda onLambda,
672                           final int pos) {
673        return exceptionParameter(emptyPath, onLambda, pos);
674    }
675
676    /**
677     * Create a {@code TypeAnnotationPosition} for an exception parameter.
678     *
679     * @param location The type path.
680     */
681    public static TypeAnnotationPosition
682        exceptionParameter(final List<TypePathEntry> location) {
683        return exceptionParameter(location, null, -1);
684    }
685
686
687    /**
688     * Create a {@code TypeAnnotationPosition} for a resource variable.
689     *
690     * @param location The type path.
691     * @param onLambda The lambda for this variable.
692     * @param pos The position from the associated tree node.
693     */
694    public static TypeAnnotationPosition
695        resourceVariable(final List<TypePathEntry> location,
696                         final JCLambda onLambda,
697                         final int pos) {
698        return new TypeAnnotationPosition(TargetType.RESOURCE_VARIABLE, pos,
699                                          Integer.MIN_VALUE, onLambda,
700                                          Integer.MIN_VALUE, Integer.MIN_VALUE,
701                                          location);
702    }
703
704    /**
705     * Create a {@code TypeAnnotationPosition} for a resource variable.
706     *
707     * @param onLambda The lambda for this variable.
708     * @param pos The position from the associated tree node.
709     */
710    public static TypeAnnotationPosition
711        resourceVariable(final JCLambda onLambda,
712                         final int pos) {
713        return resourceVariable(emptyPath, onLambda, pos);
714    }
715
716    /**
717     * Create a {@code TypeAnnotationPosition} for a resource variable.
718     *
719     * @param location The type path.
720     */
721    public static TypeAnnotationPosition
722        resourceVariable(final List<TypePathEntry> location) {
723        return resourceVariable(location, null, -1);
724    }
725
726    /**
727     * Create a {@code TypeAnnotationPosition} for a new.
728     *
729     * @param location The type path.
730     * @param onLambda The lambda for this variable.
731     * @param pos The position from the associated tree node.
732     */
733    public static TypeAnnotationPosition
734        newObj(final List<TypePathEntry> location,
735               final JCLambda onLambda,
736               final int pos) {
737        return new TypeAnnotationPosition(TargetType.NEW, pos,
738                                          Integer.MIN_VALUE, onLambda,
739                                          Integer.MIN_VALUE, Integer.MIN_VALUE,
740                                          location);
741    }
742
743    /**
744     * Create a {@code TypeAnnotationPosition} for a new.
745     *
746     * @param pos The position from the associated tree node.
747     */
748    public static TypeAnnotationPosition newObj(final int pos) {
749        return newObj(emptyPath, null, pos);
750    }
751
752    /**
753     * Create a {@code TypeAnnotationPosition} for a new.
754     *
755     * @param location The type path.
756     */
757    public static TypeAnnotationPosition
758        newObj(final List<TypePathEntry> location) {
759        return newObj(location, null, -1);
760    }
761
762    /**
763     * Create a {@code TypeAnnotationPosition} for a class extension.
764     *
765     * @param location The type path.
766     * @param onLambda The lambda for this variable.
767     * @param type_index The index of the interface.
768     * @param pos The position from the associated tree node.
769     */
770    public static TypeAnnotationPosition
771        classExtends(final List<TypePathEntry> location,
772                     final JCLambda onLambda,
773                     final int type_index,
774                     final int pos) {
775        return new TypeAnnotationPosition(TargetType.CLASS_EXTENDS, pos,
776                                          Integer.MIN_VALUE, onLambda,
777                                          type_index, Integer.MIN_VALUE,
778                                          location);
779    }
780
781    /**
782     * Create a {@code TypeAnnotationPosition} for a class extension.
783     *
784     * @param location The type path.
785     * @param onLambda The lambda for this variable.
786     * @param pos The position from the associated tree node.
787     */
788    public static TypeAnnotationPosition
789        classExtends(final List<TypePathEntry> location,
790                     final JCLambda onLambda,
791                     final int pos) {
792        return classExtends(location, onLambda, 65535, pos);
793    }
794
795    /**
796     * Create a {@code TypeAnnotationPosition} for a class extension.
797     *
798     * @param location The type path.
799     * @param type_index The index of the interface.
800     */
801    public static TypeAnnotationPosition
802        classExtends(final List<TypePathEntry> location,
803                     final int type_index) {
804        return classExtends(location, null, type_index, -1);
805    }
806
807    /**
808     * Create a {@code TypeAnnotationPosition} for a class extension.
809     *
810     * @param type_index The index of the interface.
811     * @param pos The position from the associated tree node.
812     */
813    public static TypeAnnotationPosition classExtends(final int type_index,
814                                                      final int pos) {
815        return classExtends(emptyPath, null, type_index, pos);
816    }
817
818    /**
819     * Create a {@code TypeAnnotationPosition} for a class extension.
820     *
821     * @param pos The position from the associated tree node.
822     */
823    public static TypeAnnotationPosition classExtends(final int pos) {
824        return classExtends(65535, pos);
825    }
826
827    /**
828     * Create a {@code TypeAnnotationPosition} for an instanceof.
829     *
830     * @param location The type path.
831     * @param onLambda The lambda for this variable.
832     * @param pos The position from the associated tree node.
833     */
834    public static TypeAnnotationPosition
835        instanceOf(final List<TypePathEntry> location,
836                   final JCLambda onLambda,
837                   final int pos) {
838        return new TypeAnnotationPosition(TargetType.INSTANCEOF, pos,
839                                          Integer.MIN_VALUE, onLambda,
840                                          Integer.MIN_VALUE, Integer.MIN_VALUE,
841                                          location);
842    }
843    /**
844     * Create a {@code TypeAnnotationPosition} for an instanceof.
845     *
846     * @param location The type path.
847     */
848    public static TypeAnnotationPosition
849        instanceOf(final List<TypePathEntry> location) {
850        return instanceOf(location, null, -1);
851    }
852
853    /**
854     * Create a {@code TypeAnnotationPosition} for a type cast.
855     *
856     * @param location The type path.
857     * @param onLambda The lambda for this variable.
858     * @param type_index The index into an intersection type.
859     * @param pos The position from the associated tree node.
860     */
861    public static TypeAnnotationPosition
862        typeCast(final List<TypePathEntry> location,
863                 final JCLambda onLambda,
864                 final int type_index,
865                 final int pos) {
866        return new TypeAnnotationPosition(TargetType.CAST, pos,
867                                          Integer.MIN_VALUE, onLambda,
868                                          type_index, Integer.MIN_VALUE,
869                                          location);
870    }
871
872    /**
873     * Create a {@code TypeAnnotationPosition} for a type cast.
874     *
875     * @param location The type path.
876     * @param type_index The index into an intersection type.
877     */
878    public static TypeAnnotationPosition
879        typeCast(final List<TypePathEntry> location,
880                 final int type_index) {
881        return typeCast(location, null, type_index, -1);
882    }
883
884    /**
885     * Create a {@code TypeAnnotationPosition} for a method
886     * invocation type argument.
887     *
888     * @param location The type path.
889     * @param onLambda The lambda for this variable.
890     * @param type_index The index of the type argument.
891     * @param pos The position from the associated tree node.
892     */
893    public static TypeAnnotationPosition
894        methodInvocationTypeArg(final List<TypePathEntry> location,
895                                final JCLambda onLambda,
896                                final int type_index,
897                                final int pos) {
898        return new TypeAnnotationPosition(TargetType.METHOD_INVOCATION_TYPE_ARGUMENT,
899                                          pos, Integer.MIN_VALUE, onLambda,
900                                          type_index, Integer.MIN_VALUE,
901                                          location);
902    }
903
904    /**
905     * Create a {@code TypeAnnotationPosition} for a method
906     * invocation type argument.
907     *
908     * @param location The type path.
909     * @param type_index The index of the type argument.
910     */
911    public static TypeAnnotationPosition
912        methodInvocationTypeArg(final List<TypePathEntry> location,
913                                final int type_index) {
914        return methodInvocationTypeArg(location, null, type_index, -1);
915    }
916
917    /**
918     * Create a {@code TypeAnnotationPosition} for a constructor
919     * invocation type argument.
920     *
921     * @param location The type path.
922     * @param onLambda The lambda for this variable.
923     * @param type_index The index of the type argument.
924     * @param pos The position from the associated tree node.
925     */
926    public static TypeAnnotationPosition
927        constructorInvocationTypeArg(final List<TypePathEntry> location,
928                                     final JCLambda onLambda,
929                                     final int type_index,
930                                     final int pos) {
931        return new TypeAnnotationPosition(TargetType.CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT,
932                                          pos, Integer.MIN_VALUE, onLambda,
933                                          type_index, Integer.MIN_VALUE,
934                                          location);
935    }
936
937    /**
938     * Create a {@code TypeAnnotationPosition} for a constructor
939     * invocation type argument.
940     *
941     * @param location The type path.
942     * @param type_index The index of the type argument.
943     */
944    public static TypeAnnotationPosition
945        constructorInvocationTypeArg(final List<TypePathEntry> location,
946                                     final int type_index) {
947        return constructorInvocationTypeArg(location, null, type_index, -1);
948    }
949
950    /**
951     * Create a {@code TypeAnnotationPosition} for a type parameter.
952     *
953     * @param location The type path.
954     * @param onLambda The lambda for this variable.
955     * @param parameter_index The index of the type parameter.
956     * @param pos The position from the associated tree node.
957     */
958    public static TypeAnnotationPosition
959        typeParameter(final List<TypePathEntry> location,
960                      final JCLambda onLambda,
961                      final int parameter_index,
962                      final int pos) {
963        return new TypeAnnotationPosition(TargetType.CLASS_TYPE_PARAMETER, pos,
964                                          parameter_index, onLambda,
965                                          Integer.MIN_VALUE, Integer.MIN_VALUE,
966                                          location);
967    }
968
969    /**
970     * Create a {@code TypeAnnotationPosition} for a type parameter.
971     *
972     * @param location The type path.
973     * @param parameter_index The index of the type parameter.
974     */
975    public static TypeAnnotationPosition
976        typeParameter(final List<TypePathEntry> location,
977                      final int parameter_index) {
978        return typeParameter(location, null, parameter_index, -1);
979    }
980
981    /**
982     * Create a {@code TypeAnnotationPosition} for a method type parameter.
983     *
984     * @param location The type path.
985     * @param onLambda The lambda for this variable.
986     * @param parameter_index The index of the type parameter.
987     * @param pos The position from the associated tree node.
988     */
989    public static TypeAnnotationPosition
990        methodTypeParameter(final List<TypePathEntry> location,
991                            final JCLambda onLambda,
992                            final int parameter_index,
993                            final int pos) {
994        return new TypeAnnotationPosition(TargetType.METHOD_TYPE_PARAMETER,
995                                          pos, parameter_index, onLambda,
996                                          Integer.MIN_VALUE, Integer.MIN_VALUE,
997                                          location);
998    }
999
1000    /**
1001     * Create a {@code TypeAnnotationPosition} for a method type parameter.
1002     *
1003     * @param location The type path.
1004     * @param parameter_index The index of the type parameter.
1005     */
1006    public static TypeAnnotationPosition
1007        methodTypeParameter(final List<TypePathEntry> location,
1008                            final int parameter_index) {
1009        return methodTypeParameter(location, null, parameter_index, -1);
1010    }
1011
1012    /**
1013     * Create a {@code TypeAnnotationPosition} for a throws clause.
1014     *
1015     * @param location The type path.
1016     * @param onLambda The lambda for this variable.
1017     * @param type_index The index of the exception.
1018     * @param pos The position from the associated tree node.
1019     */
1020    public static TypeAnnotationPosition
1021        methodThrows(final List<TypePathEntry> location,
1022                     final JCLambda onLambda,
1023                     final int type_index,
1024                     final int pos) {
1025        return new TypeAnnotationPosition(TargetType.THROWS, pos,
1026                                          Integer.MIN_VALUE, onLambda,
1027                                          type_index, Integer.MIN_VALUE,
1028                                          location);
1029    }
1030
1031    /**
1032     * Create a {@code TypeAnnotationPosition} for a throws clause.
1033     *
1034     * @param location The type path.
1035     * @param type_index The index of the exception.
1036     */
1037    public static TypeAnnotationPosition
1038        methodThrows(final List<TypePathEntry> location,
1039                     final int type_index) {
1040        return methodThrows(location, null, type_index, -1);
1041    }
1042
1043    /**
1044     * Create a {@code TypeAnnotationPosition} for a method reference
1045     * type argument.
1046     *
1047     * @param location The type path.
1048     * @param onLambda The lambda for this variable.
1049     * @param type_index The index of the type argument.
1050     * @param pos The position from the associated tree node.
1051     */
1052    public static TypeAnnotationPosition
1053        methodRefTypeArg(final List<TypePathEntry> location,
1054                         final JCLambda onLambda,
1055                         final int type_index,
1056                         final int pos) {
1057        return new TypeAnnotationPosition(TargetType.METHOD_REFERENCE_TYPE_ARGUMENT,
1058                                          pos, Integer.MIN_VALUE, onLambda,
1059                                          type_index, Integer.MIN_VALUE,
1060                                          location);
1061    }
1062
1063    /**
1064     * Create a {@code TypeAnnotationPosition} for a method reference
1065     * type argument.
1066     *
1067     * @param location The type path.
1068     * @param type_index The index of the type argument.
1069     */
1070    public static TypeAnnotationPosition
1071        methodRefTypeArg(final List<TypePathEntry> location,
1072                         final int type_index) {
1073        return methodRefTypeArg(location, null, type_index, -1);
1074    }
1075
1076    /**
1077     * Create a {@code TypeAnnotationPosition} for a constructor reference
1078     * type argument.
1079     *
1080     * @param location The type path.
1081     * @param onLambda The lambda for this variable.
1082     * @param type_index The index of the type argument.
1083     * @param pos The position from the associated tree node.
1084     */
1085    public static TypeAnnotationPosition
1086        constructorRefTypeArg(final List<TypePathEntry> location,
1087                              final JCLambda onLambda,
1088                              final int type_index,
1089                              final int pos) {
1090        return new TypeAnnotationPosition(TargetType.CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT,
1091                                          pos, Integer.MIN_VALUE, onLambda,
1092                                          type_index, Integer.MIN_VALUE,
1093                                          location);
1094    }
1095
1096    /**
1097     * Create a {@code TypeAnnotationPosition} for a constructor reference
1098     * type argument.
1099     *
1100     * @param location The type path.
1101     * @param type_index The index of the type argument.
1102     */
1103    public static TypeAnnotationPosition
1104        constructorRefTypeArg(final List<TypePathEntry> location,
1105                              final int type_index) {
1106        return constructorRefTypeArg(location, null, type_index, -1);
1107    }
1108
1109    /**
1110     * Create a {@code TypeAnnotationPosition} for a type parameter bound.
1111     *
1112     * @param location The type path.
1113     * @param onLambda The lambda for this variable.
1114     * @param parameter_index The index of the type parameter.
1115     * @param bound_index The index of the type parameter bound.
1116     * @param pos The position from the associated tree node.
1117     */
1118    public static TypeAnnotationPosition
1119        typeParameterBound(final List<TypePathEntry> location,
1120                           final JCLambda onLambda,
1121                           final int parameter_index,
1122                           final int bound_index,
1123                           final int pos) {
1124        return new TypeAnnotationPosition(TargetType.CLASS_TYPE_PARAMETER_BOUND,
1125                                          pos, parameter_index, onLambda,
1126                                          Integer.MIN_VALUE, bound_index,
1127                                          location);
1128    }
1129
1130    /**
1131     * Create a {@code TypeAnnotationPosition} for a type parameter bound.
1132     *
1133     * @param location The type path.
1134     * @param parameter_index The index of the type parameter.
1135     * @param bound_index The index of the type parameter bound.
1136     */
1137    public static TypeAnnotationPosition
1138        typeParameterBound(final List<TypePathEntry> location,
1139                           final int parameter_index,
1140                           final int bound_index) {
1141        return typeParameterBound(location, null, parameter_index,
1142                                  bound_index, -1);
1143    }
1144
1145    /**
1146     * Create a {@code TypeAnnotationPosition} for a method type
1147     * parameter bound.
1148     *
1149     * @param location The type path.
1150     * @param onLambda The lambda for this variable.
1151     * @param parameter_index The index of the type parameter.
1152     * @param bound_index The index of the type parameter bound.
1153     * @param pos The position from the associated tree node.
1154     */
1155    public static TypeAnnotationPosition
1156        methodTypeParameterBound(final List<TypePathEntry> location,
1157                                 final JCLambda onLambda,
1158                                 final int parameter_index,
1159                                 final int bound_index,
1160                                 final int pos) {
1161        return new TypeAnnotationPosition(TargetType.METHOD_TYPE_PARAMETER_BOUND,
1162                                          pos, parameter_index, onLambda,
1163                                          Integer.MIN_VALUE, bound_index,
1164                                          location);
1165    }
1166
1167    /**
1168     * Create a {@code TypeAnnotationPosition} for a method type
1169     * parameter bound.
1170     *
1171     * @param location The type path.
1172     * @param parameter_index The index of the type parameter.
1173     * @param bound_index The index of the type parameter bound.
1174     */
1175    public static TypeAnnotationPosition
1176        methodTypeParameterBound(final List<TypePathEntry> location,
1177                                 final int parameter_index,
1178                                 final int bound_index) {
1179        return methodTypeParameterBound(location, null, parameter_index,
1180                                        bound_index, -1);
1181    }
1182
1183    // Consider this deprecated on arrival.  We eventually want to get
1184    // rid of this value altogether.  Do not use it for anything new.
1185    public static final TypeAnnotationPosition unknown =
1186        new TypeAnnotationPosition(TargetType.UNKNOWN, -1,
1187                                   Integer.MIN_VALUE, null,
1188                                   Integer.MIN_VALUE, Integer.MIN_VALUE,
1189                                   emptyPath);
1190}
1191