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