1 /*
   2  * Copyright (c) 2003, 2013, 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 
  26 package com.sun.tools.javac.code;
  27 
  28 import java.util.Iterator;
  29 
  30 import com.sun.tools.javac.tree.JCTree.JCLambda;
  31 import 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
  41 public 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                     "Invalid TypePathEntryKind: " + tag);
  72             this.tag = tag;
  73             this.arg = 0;
  74         }
  75 
  76         public TypePathEntry(TypePathEntryKind tag, int arg) {
  77             Assert.check(tag == TypePathEntryKind.TYPE_ARGUMENT,
  78                     "Invalid TypePathEntryKind: " + tag);
  79             this.tag = tag;
  80             this.arg = arg;
  81         }
  82 
  83         public static TypePathEntry fromBinary(int tag, int arg) {
  84             Assert.check(arg == 0 || tag == TypePathEntryKind.TYPE_ARGUMENT.tag,
  85                     "Invalid TypePathEntry tag/arg: " + tag + "/" + arg);
  86             switch (tag) {
  87             case 0:
  88                 return ARRAY;
  89             case 1:
  90                 return INNER_TYPE;
  91             case 2:
  92                 return WILDCARD;
  93             case 3:
  94                 return new TypePathEntry(TypePathEntryKind.TYPE_ARGUMENT, arg);
  95             default:
  96                 Assert.error("Invalid TypePathEntryKind tag: " + tag);
  97                 return null;
  98             }
  99         }
 100 
 101         @Override
 102         public String toString() {
 103             return tag.toString() +
 104                     (tag == TypePathEntryKind.TYPE_ARGUMENT ? ("(" + arg + ")") : "");
 105         }
 106 
 107         @Override
 108         public boolean equals(Object other) {
 109             if (! (other instanceof TypePathEntry)) {
 110                 return false;
 111             }
 112             TypePathEntry tpe = (TypePathEntry) other;
 113             return this.tag == tpe.tag && this.arg == tpe.arg;
 114         }
 115 
 116         @Override
 117         public int hashCode() {
 118             return this.tag.hashCode() * 17 + this.arg;
 119         }
 120     }
 121 
 122     public static final List<TypePathEntry> emptyPath = List.nil();
 123 
 124     // NOTE: All of these will be converted to final fields eventually.
 125 
 126     public final TargetType type;
 127 
 128     // For generic/array types.
 129 
 130     // This field is in the process of being made final.  Do not
 131     // introduce new mutations.
 132     public List<TypePathEntry> location;
 133 
 134     // Tree position.
 135     public final int pos;
 136 
 137     // For type casts, type tests, new, locals (as start_pc),
 138     // and method and constructor reference type arguments.
 139     public boolean isValidOffset = false;
 140     public int offset = -1;
 141 
 142     // For locals. arrays same length
 143     public int[] lvarOffset = null;
 144     public int[] lvarLength = null;
 145     public int[] lvarIndex = null;
 146 
 147     // For type parameter bound
 148     public final int bound_index;
 149 
 150     // For type parameter and method parameter
 151     public final int parameter_index;
 152 
 153     // For class extends, implements, and throws clauses
 154     public final int type_index;
 155 
 156     // For exception parameters, index into exception table.  In
 157     // com.sun.tools.javac.jvm.Gen.genCatch, we first use this to hold
 158     // the catch type index.  Then in
 159     // com.sun.tools.javac.jvm.Code.fillExceptionParameterPositions we
 160     // use that value to determine the exception table index.
 161     private int exception_index = Integer.MIN_VALUE;
 162 
 163     // If this type annotation is within a lambda expression,
 164     // store a pointer to the lambda expression tree in order
 165     // to allow a later translation to the right method.
 166     public final JCLambda onLambda;
 167 
 168     // NOTE: This constructor will eventually go away, and be replaced
 169     // by static builder methods.
 170 
 171     @Override
 172     public String toString() {
 173         StringBuilder sb = new StringBuilder();
 174         sb.append('[');
 175         sb.append(type);
 176 
 177         switch (type) {
 178         // instanceof
 179         case INSTANCEOF:
 180         // new expression
 181         case NEW:
 182         // constructor/method reference receiver
 183         case CONSTRUCTOR_REFERENCE:
 184         case METHOD_REFERENCE:
 185             sb.append(", offset = ");
 186             sb.append(offset);
 187             break;
 188         // local variable
 189         case LOCAL_VARIABLE:
 190         // resource variable
 191         case RESOURCE_VARIABLE:
 192             if (lvarOffset == null) {
 193                 sb.append(", lvarOffset is null!");
 194                 break;
 195             }
 196             sb.append(", {");
 197             for (int i = 0; i < lvarOffset.length; ++i) {
 198                 if (i != 0) sb.append("; ");
 199                 sb.append("start_pc = ");
 200                 sb.append(lvarOffset[i]);
 201                 sb.append(", length = ");
 202                 sb.append(lvarLength[i]);
 203                 sb.append(", index = ");
 204                 sb.append(lvarIndex[i]);
 205             }
 206             sb.append("}");
 207             break;
 208         // method receiver
 209         case METHOD_RECEIVER:
 210             // Do nothing
 211             break;
 212         // type parameter
 213         case CLASS_TYPE_PARAMETER:
 214         case METHOD_TYPE_PARAMETER:
 215             sb.append(", param_index = ");
 216             sb.append(parameter_index);
 217             break;
 218         // type parameter bound
 219         case CLASS_TYPE_PARAMETER_BOUND:
 220         case METHOD_TYPE_PARAMETER_BOUND:
 221             sb.append(", param_index = ");
 222             sb.append(parameter_index);
 223             sb.append(", bound_index = ");
 224             sb.append(bound_index);
 225             break;
 226         // class extends or implements clause
 227         case CLASS_EXTENDS:
 228             sb.append(", type_index = ");
 229             sb.append(type_index);
 230             break;
 231         // throws
 232         case THROWS:
 233             sb.append(", type_index = ");
 234             sb.append(type_index);
 235             break;
 236         // exception parameter
 237         case EXCEPTION_PARAMETER:
 238             sb.append(", exception_index = ");
 239             sb.append(exception_index);
 240             break;
 241         // method parameter
 242         case METHOD_FORMAL_PARAMETER:
 243             sb.append(", param_index = ");
 244             sb.append(parameter_index);
 245             break;
 246         // type cast
 247         case CAST:
 248         // method/constructor/reference type argument
 249         case CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT:
 250         case METHOD_INVOCATION_TYPE_ARGUMENT:
 251         case CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT:
 252         case METHOD_REFERENCE_TYPE_ARGUMENT:
 253             sb.append(", offset = ");
 254             sb.append(offset);
 255             sb.append(", type_index = ");
 256             sb.append(type_index);
 257             break;
 258         // We don't need to worry about these
 259         case METHOD_RETURN:
 260         case FIELD:
 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(), "Could not decode type path: " + list);
 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
 465      * parameter parameter.
 466      *
 467      * @param location The type path.
 468      * @param onLambda The lambda for this parameter.
 469      * @param index The index of the parameter.
 470      * @param pos The position from the associated tree node.
 471      */
 472     public static TypeAnnotationPosition
 473         methodParameter(final List<TypePathEntry> location,
 474                         final JCLambda onLambda,
 475                         final int parameter_index,
 476                         final int pos) {
 477         return new TypeAnnotationPosition(TargetType.METHOD_FORMAL_PARAMETER,
 478                                           pos, parameter_index, onLambda,
 479                                           Integer.MIN_VALUE, Integer.MIN_VALUE,
 480                                           location);
 481     }
 482 
 483     /**
 484      * Create a {@code TypeAnnotationPosition} for a method formal parameter.
 485      *
 486      * @param onLambda The lambda for this parameter.
 487      * @param index The index of the parameter.
 488      * @param pos The position from the associated tree node.
 489      */
 490     public static TypeAnnotationPosition
 491         methodParameter(final JCLambda onLambda,
 492                         final int parameter_index,
 493                         final int pos) {
 494         return methodParameter(emptyPath, onLambda,
 495                                parameter_index, pos);
 496     }
 497 
 498     /**
 499      * Create a {@code TypeAnnotationPosition} for a method formal parameter.
 500      *
 501      * @param index The index of the parameter.
 502      * @param pos The position from the associated tree node.
 503      */
 504     public static TypeAnnotationPosition
 505         methodParameter(final int parameter_index,
 506                         final int pos) {
 507         return methodParameter(null, parameter_index, pos);
 508     }
 509 
 510     /**
 511      * Create a {@code TypeAnnotationPosition} for a method formal parameter.
 512      *
 513      * @param location The type path.
 514      * @param index The index of the parameter.
 515      */
 516     public static TypeAnnotationPosition
 517         methodParameter(final List<TypePathEntry> location,
 518                         final int parameter_index) {
 519         return methodParameter(location, null, parameter_index, -1);
 520     }
 521 
 522     /**
 523      * Create a {@code TypeAnnotationPosition} for a method reference.
 524      *
 525      * @param location The type path.
 526      * @param onLambda The lambda for this method reference.
 527      * @param pos The position from the associated tree node.
 528      */
 529     public static TypeAnnotationPosition
 530         methodRef(final List<TypePathEntry> location,
 531                   final JCLambda onLambda,
 532                   final int pos) {
 533         return new TypeAnnotationPosition(TargetType.METHOD_REFERENCE, pos,
 534                                           Integer.MIN_VALUE, onLambda,
 535                                           Integer.MIN_VALUE, Integer.MIN_VALUE,
 536                                           location);
 537     }
 538 
 539     /**
 540      * Create a {@code TypeAnnotationPosition} for a method reference.
 541      *
 542      * @param location The type path.
 543      * @param onLambda The lambda for this method reference.
 544      * @param pos The position from the associated tree node.
 545      */
 546     public static TypeAnnotationPosition
 547         methodRef(final List<TypePathEntry> location) {
 548         return methodRef(location, null, -1);
 549     }
 550 
 551     /**
 552      * Create a {@code TypeAnnotationPosition} for a constructor reference.
 553      *
 554      * @param location The type path.
 555      * @param onLambda The lambda for this constructor reference.
 556      * @param pos The position from the associated tree node.
 557      */
 558     public static TypeAnnotationPosition
 559         constructorRef(final List<TypePathEntry> location,
 560                        final JCLambda onLambda,
 561                        final int pos) {
 562         return new TypeAnnotationPosition(TargetType.CONSTRUCTOR_REFERENCE, pos,
 563                                           Integer.MIN_VALUE, onLambda,
 564                                           Integer.MIN_VALUE, Integer.MIN_VALUE,
 565                                           location);
 566     }
 567 
 568     /**
 569      * Create a {@code TypeAnnotationPosition} for a constructor reference.
 570      *
 571      * @param location The type path.
 572      * @param onLambda The lambda for this constructor reference.
 573      * @param pos The position from the associated tree node.
 574      */
 575     public static TypeAnnotationPosition
 576         constructorRef(final List<TypePathEntry> location) {
 577         return constructorRef(location, null, -1);
 578     }
 579 
 580     /**
 581      * Create a {@code TypeAnnotationPosition} for a field.
 582      *
 583      * @param location The type path.
 584      * @param onLambda The lambda for this variable.
 585      * @param pos The position from the associated tree node.
 586      */
 587     public static TypeAnnotationPosition
 588         field(final List<TypePathEntry> location,
 589               final JCLambda onLambda,
 590               final int pos) {
 591         return new TypeAnnotationPosition(TargetType.FIELD, pos,
 592                                           Integer.MIN_VALUE, onLambda,
 593                                           Integer.MIN_VALUE, Integer.MIN_VALUE,
 594                                           location);
 595     }
 596 
 597     /**
 598      * Create a {@code TypeAnnotationPosition} for a field.
 599      *
 600      * @param location The type path.
 601      */
 602     public static TypeAnnotationPosition
 603         field(final List<TypePathEntry> location) {
 604         return field(location, null, -1);
 605     }
 606 
 607     /**
 608      * Create a {@code TypeAnnotationPosition} for a field.
 609      *
 610      * @param pos The position from the associated tree node.
 611      */
 612     public static TypeAnnotationPosition field(final int pos) {
 613         return field(emptyPath, null, pos);
 614     }
 615 
 616     /**
 617      * Create a {@code TypeAnnotationPosition} for a local variable.
 618      *
 619      * @param location The type path.
 620      * @param onLambda The lambda for this variable.
 621      * @param pos The position from the associated tree node.
 622      */
 623     public static TypeAnnotationPosition
 624         localVariable(final List<TypePathEntry> location,
 625                       final JCLambda onLambda,
 626                       final int pos) {
 627         return new TypeAnnotationPosition(TargetType.LOCAL_VARIABLE, pos,
 628                                           Integer.MIN_VALUE, onLambda,
 629                                           Integer.MIN_VALUE, Integer.MIN_VALUE,
 630                                           location);
 631     }
 632 
 633     /**
 634      * Create a {@code TypeAnnotationPosition} for a local variable.
 635      *
 636      * @param onLambda The lambda for this variable.
 637      * @param pos The position from the associated tree node.
 638      */
 639     public static TypeAnnotationPosition
 640         localVariable(final JCLambda onLambda,
 641                       final int pos) {
 642         return localVariable(emptyPath, onLambda, pos);
 643     }
 644 
 645     /**
 646      * Create a {@code TypeAnnotationPosition} for a local variable.
 647      *
 648      * @param location The type path.
 649      */
 650     public static TypeAnnotationPosition
 651         localVariable(final List<TypePathEntry> location) {
 652         return localVariable(location, null, -1);
 653     }
 654 
 655     /**
 656      * Create a {@code TypeAnnotationPosition} for an exception parameter.
 657      *
 658      * @param location The type path.
 659      * @param onLambda The lambda for this parameter.
 660      * @param pos The position from the associated tree node.
 661      */
 662     public static TypeAnnotationPosition
 663         exceptionParameter(final List<TypePathEntry> location,
 664                            final JCLambda onLambda,
 665                            final int type_index,
 666                            final int pos) {
 667         return new TypeAnnotationPosition(TargetType.EXCEPTION_PARAMETER, pos,
 668                                           Integer.MIN_VALUE, onLambda,
 669                                           type_index, Integer.MIN_VALUE,
 670                                           location);
 671     }
 672 
 673     /**
 674      * Create a {@code TypeAnnotationPosition} for an exception parameter.
 675      *
 676      * @param onLambda The lambda for this parameter.
 677      * @param pos The position from the associated tree node.
 678      */
 679     public static TypeAnnotationPosition
 680         exceptionParameter(final JCLambda onLambda,
 681                            final int pos) {
 682         return exceptionParameter(emptyPath, onLambda, Integer.MIN_VALUE, pos);
 683     }
 684 
 685     /**
 686      * Create a {@code TypeAnnotationPosition} for an exception parameter.
 687      *
 688      * @param location The type path.
 689      */
 690     public static TypeAnnotationPosition
 691         exceptionParameter(final List<TypePathEntry> location) {
 692         return exceptionParameter(location, null, Integer.MIN_VALUE, -1);
 693     }
 694 
 695 
 696     /**
 697      * Create a {@code TypeAnnotationPosition} for a resource variable.
 698      *
 699      * @param location The type path.
 700      * @param onLambda The lambda for this variable.
 701      * @param pos The position from the associated tree node.
 702      */
 703     public static TypeAnnotationPosition
 704         resourceVariable(final List<TypePathEntry> location,
 705                          final JCLambda onLambda,
 706                          final int pos) {
 707         return new TypeAnnotationPosition(TargetType.RESOURCE_VARIABLE, pos,
 708                                           Integer.MIN_VALUE, onLambda,
 709                                           Integer.MIN_VALUE, Integer.MIN_VALUE,
 710                                           location);
 711     }
 712 
 713     /**
 714      * Create a {@code TypeAnnotationPosition} for a resource variable.
 715      *
 716      * @param onLambda The lambda for this variable.
 717      * @param pos The position from the associated tree node.
 718      */
 719     public static TypeAnnotationPosition
 720         resourceVariable(final JCLambda onLambda,
 721                          final int pos) {
 722         return resourceVariable(emptyPath, onLambda, pos);
 723     }
 724 
 725     /**
 726      * Create a {@code TypeAnnotationPosition} for a resource variable.
 727      *
 728      * @param location The type path.
 729      * @param onLambda The lambda for this variable.
 730      * @param pos The position from the associated tree node.
 731      */
 732     public static TypeAnnotationPosition
 733         resourceVariable(final List<TypePathEntry> location) {
 734         return resourceVariable(location, null, -1);
 735     }
 736 
 737     /**
 738      * Create a {@code TypeAnnotationPosition} for a new.
 739      *
 740      * @param location The type path.
 741      * @param onLambda The lambda for this variable.
 742      * @param pos The position from the associated tree node.
 743      */
 744     public static TypeAnnotationPosition
 745         newObj(final List<TypePathEntry> location,
 746                final JCLambda onLambda,
 747                final int pos) {
 748         return new TypeAnnotationPosition(TargetType.NEW, pos,
 749                                           Integer.MIN_VALUE, onLambda,
 750                                           Integer.MIN_VALUE, Integer.MIN_VALUE,
 751                                           location);
 752     }
 753 
 754     /**
 755      * Create a {@code TypeAnnotationPosition} for a new.
 756      *
 757      * @param location The type path.
 758      * @param onLambda The lambda for this variable.
 759      * @param pos The position from the associated tree node.
 760      */
 761     public static TypeAnnotationPosition newObj(final int pos) {
 762         return newObj(emptyPath, null, pos);
 763     }
 764 
 765     /**
 766      * Create a {@code TypeAnnotationPosition} for a new.
 767      *
 768      * @param location The type path.
 769      * @param onLambda The lambda for this variable.
 770      * @param pos The position from the associated tree node.
 771      */
 772     public static TypeAnnotationPosition
 773         newObj(final List<TypePathEntry> location) {
 774         return newObj(location, null, -1);
 775     }
 776 
 777     /**
 778      * Create a {@code TypeAnnotationPosition} for a class extension.
 779      *
 780      * @param location The type path.
 781      * @param onLambda The lambda for this variable.
 782      * @param type_index The index of the interface.
 783      * @param pos The position from the associated tree node.
 784      */
 785     public static TypeAnnotationPosition
 786         classExtends(final List<TypePathEntry> location,
 787                      final JCLambda onLambda,
 788                      final int type_index,
 789                      final int pos) {
 790         return new TypeAnnotationPosition(TargetType.CLASS_EXTENDS, pos,
 791                                           Integer.MIN_VALUE, onLambda,
 792                                           type_index, Integer.MIN_VALUE,
 793                                           location);
 794     }
 795 
 796     /**
 797      * Create a {@code TypeAnnotationPosition} for a class extension.
 798      *
 799      * @param location The type path.
 800      * @param onLambda The lambda for this variable.
 801      * @param type_index The index of the interface.
 802      * @param pos The position from the associated tree node.
 803      */
 804     public static TypeAnnotationPosition
 805         classExtends(final List<TypePathEntry> location,
 806                      final JCLambda onLambda,
 807                      final int pos) {
 808         return classExtends(location, onLambda, -1, pos);
 809     }
 810 
 811     /**
 812      * Create a {@code TypeAnnotationPosition} for a class extension.
 813      *
 814      * @param location The type path.
 815      * @param type_index The index of the interface.
 816      */
 817     public static TypeAnnotationPosition
 818         classExtends(final List<TypePathEntry> location,
 819                      final int type_index) {
 820         return classExtends(location, null, type_index, -1);
 821     }
 822 
 823     /**
 824      * Create a {@code TypeAnnotationPosition} for a class extension.
 825      *
 826      * @param type_index The index of the interface.
 827      * @param pos The position from the associated tree node.
 828      */
 829     public static TypeAnnotationPosition classExtends(final int type_index,
 830                                                       final int pos) {
 831         return classExtends(emptyPath, null, type_index, pos);
 832     }
 833 
 834     /**
 835      * Create a {@code TypeAnnotationPosition} for a class extension.
 836      *
 837      * @param pos The position from the associated tree node.
 838      */
 839     public static TypeAnnotationPosition classExtends(final int pos) {
 840         return classExtends(-1, pos);
 841     }
 842 
 843     /**
 844      * Create a {@code TypeAnnotationPosition} for an instanceof.
 845      *
 846      * @param location The type path.
 847      * @param onLambda The lambda for this variable.
 848      * @param pos The position from the associated tree node.
 849      */
 850     public static TypeAnnotationPosition
 851         instanceOf(final List<TypePathEntry> location,
 852                    final JCLambda onLambda,
 853                    final int pos) {
 854         return new TypeAnnotationPosition(TargetType.INSTANCEOF, pos,
 855                                           Integer.MIN_VALUE, onLambda,
 856                                           Integer.MIN_VALUE, Integer.MIN_VALUE,
 857                                           location);
 858     }
 859     /**
 860      * Create a {@code TypeAnnotationPosition} for an instanceof.
 861      *
 862      * @param location The type path.
 863      * @param onLambda The lambda for this variable.
 864      * @param pos The position from the associated tree node.
 865      */
 866     public static TypeAnnotationPosition
 867         instanceOf(final List<TypePathEntry> location) {
 868         return instanceOf(location, null, -1);
 869     }
 870 
 871     /**
 872      * Create a {@code TypeAnnotationPosition} for a type cast.
 873      *
 874      * @param location The type path.
 875      * @param onLambda The lambda for this variable.
 876      * @param type_index The index into an intersection type.
 877      * @param pos The position from the associated tree node.
 878      */
 879     public static TypeAnnotationPosition
 880         typeCast(final List<TypePathEntry> location,
 881                  final JCLambda onLambda,
 882                  final int type_index,
 883                  final int pos) {
 884         return new TypeAnnotationPosition(TargetType.CAST, pos,
 885                                           Integer.MIN_VALUE, onLambda,
 886                                           type_index, Integer.MIN_VALUE,
 887                                           location);
 888     }
 889 
 890     /**
 891      * Create a {@code TypeAnnotationPosition} for a type cast.
 892      *
 893      * @param location The type path.
 894      * @param onLambda The lambda for this variable.
 895      * @param type_index The index into an intersection type.
 896      * @param pos The position from the associated tree node.
 897      */
 898     public static TypeAnnotationPosition
 899         typeCast(final List<TypePathEntry> location,
 900                  final int type_index) {
 901         return typeCast(location, null, type_index, -1);
 902     }
 903 
 904     /**
 905      * Create a {@code TypeAnnotationPosition} for a method
 906      * invocation type argument.
 907      *
 908      * @param location The type path.
 909      * @param onLambda The lambda for this variable.
 910      * @param type_index The index of the type argument.
 911      * @param pos The position from the associated tree node.
 912      */
 913     public static TypeAnnotationPosition
 914         methodInvocationTypeArg(final List<TypePathEntry> location,
 915                                 final JCLambda onLambda,
 916                                 final int type_index,
 917                                 final int pos) {
 918         return new TypeAnnotationPosition(TargetType.METHOD_INVOCATION_TYPE_ARGUMENT,
 919                                           pos, Integer.MIN_VALUE, onLambda,
 920                                           type_index, Integer.MIN_VALUE,
 921                                           location);
 922     }
 923 
 924     /**
 925      * Create a {@code TypeAnnotationPosition} for a method
 926      * invocation type argument.
 927      *
 928      * @param location The type path.
 929      * @param type_index The index of the type argument.
 930      */
 931     public static TypeAnnotationPosition
 932         methodInvocationTypeArg(final List<TypePathEntry> location,
 933                                 final int type_index) {
 934         return methodInvocationTypeArg(location, null, type_index, -1);
 935     }
 936 
 937     /**
 938      * Create a {@code TypeAnnotationPosition} for a constructor
 939      * invocation type argument.
 940      *
 941      * @param location The type path.
 942      * @param onLambda The lambda for this variable.
 943      * @param type_index The index of the type argument.
 944      * @param pos The position from the associated tree node.
 945      */
 946     public static TypeAnnotationPosition
 947         constructorInvocationTypeArg(final List<TypePathEntry> location,
 948                                      final JCLambda onLambda,
 949                                      final int type_index,
 950                                      final int pos) {
 951         return new TypeAnnotationPosition(TargetType.CONSTRUCTOR_INVOCATION_TYPE_ARGUMENT,
 952                                           pos, Integer.MIN_VALUE, onLambda,
 953                                           type_index, Integer.MIN_VALUE,
 954                                           location);
 955     }
 956 
 957     /**
 958      * Create a {@code TypeAnnotationPosition} for a constructor
 959      * invocation type argument.
 960      *
 961      * @param location The type path.
 962      * @param type_index The index of the type argument.
 963      */
 964     public static TypeAnnotationPosition
 965         constructorInvocationTypeArg(final List<TypePathEntry> location,
 966                                      final int type_index) {
 967         return constructorInvocationTypeArg(location, null, type_index, -1);
 968     }
 969 
 970     /**
 971      * Create a {@code TypeAnnotationPosition} for a type parameter.
 972      *
 973      * @param location The type path.
 974      * @param onLambda The lambda for this variable.
 975      * @param parameter_index The index of the type parameter.
 976      * @param pos The position from the associated tree node.
 977      */
 978     public static TypeAnnotationPosition
 979         typeParameter(final List<TypePathEntry> location,
 980                       final JCLambda onLambda,
 981                       final int parameter_index,
 982                       final int pos) {
 983         return new TypeAnnotationPosition(TargetType.CLASS_TYPE_PARAMETER, pos,
 984                                           parameter_index, onLambda,
 985                                           Integer.MIN_VALUE, Integer.MIN_VALUE,
 986                                           location);
 987     }
 988 
 989     /**
 990      * Create a {@code TypeAnnotationPosition} for a type parameter.
 991      *
 992      * @param location The type path.
 993      * @param onLambda The lambda for this variable.
 994      * @param parameter_index The index of the type parameter.
 995      * @param pos The position from the associated tree node.
 996      */
 997     public static TypeAnnotationPosition
 998         typeParameter(final List<TypePathEntry> location,
 999                       final int parameter_index) {
1000         return typeParameter(location, null, parameter_index, -1);
1001     }
1002 
1003     /**
1004      * Create a {@code TypeAnnotationPosition} for a method type parameter.
1005      *
1006      * @param location The type path.
1007      * @param onLambda The lambda for this variable.
1008      * @param parameter_index The index of the type parameter.
1009      * @param pos The position from the associated tree node.
1010      */
1011     public static TypeAnnotationPosition
1012         methodTypeParameter(final List<TypePathEntry> location,
1013                             final JCLambda onLambda,
1014                             final int parameter_index,
1015                             final int pos) {
1016         return new TypeAnnotationPosition(TargetType.METHOD_TYPE_PARAMETER,
1017                                           pos, parameter_index, onLambda,
1018                                           Integer.MIN_VALUE, Integer.MIN_VALUE,
1019                                           location);
1020     }
1021 
1022     /**
1023      * Create a {@code TypeAnnotationPosition} for a method type parameter.
1024      *
1025      * @param location The type path.
1026      * @param parameter_index The index of the type parameter.
1027      */
1028     public static TypeAnnotationPosition
1029         methodTypeParameter(final List<TypePathEntry> location,
1030                             final int parameter_index) {
1031         return methodTypeParameter(location, null, parameter_index, -1);
1032     }
1033 
1034     /**
1035      * Create a {@code TypeAnnotationPosition} for a throws clause.
1036      *
1037      * @param location The type path.
1038      * @param onLambda The lambda for this variable.
1039      * @param type_index The index of the exception.
1040      * @param pos The position from the associated tree node.
1041      */
1042     public static TypeAnnotationPosition
1043         methodThrows(final List<TypePathEntry> location,
1044                      final JCLambda onLambda,
1045                      final int type_index,
1046                      final int pos) {
1047         return new TypeAnnotationPosition(TargetType.THROWS, pos,
1048                                           Integer.MIN_VALUE, onLambda,
1049                                           type_index, Integer.MIN_VALUE,
1050                                           location);
1051     }
1052 
1053     /**
1054      * Create a {@code TypeAnnotationPosition} for a throws clause.
1055      *
1056      * @param location The type path.
1057      * @param type_index The index of the exception.
1058      */
1059     public static TypeAnnotationPosition
1060         methodThrows(final List<TypePathEntry> location,
1061                      final int type_index) {
1062         return methodThrows(location, null, type_index, -1);
1063     }
1064 
1065     /**
1066      * Create a {@code TypeAnnotationPosition} for a method reference
1067      * type argument.
1068      *
1069      * @param location The type path.
1070      * @param onLambda The lambda for this variable.
1071      * @param parameter_index The index of the type argument.
1072      * @param pos The position from the associated tree node.
1073      */
1074     public static TypeAnnotationPosition
1075         methodRefTypeArg(final List<TypePathEntry> location,
1076                          final JCLambda onLambda,
1077                          final int type_index,
1078                          final int pos) {
1079         return new TypeAnnotationPosition(TargetType.METHOD_REFERENCE_TYPE_ARGUMENT,
1080                                           pos, Integer.MIN_VALUE, onLambda,
1081                                           type_index, Integer.MIN_VALUE,
1082                                           location);
1083     }
1084 
1085     /**
1086      * Create a {@code TypeAnnotationPosition} for a method reference
1087      * type argument.
1088      *
1089      * @param location The type path.
1090      * @param onLambda The lambda for this variable.
1091      * @param parameter_index The index of the type argument.
1092      * @param pos The position from the associated tree node.
1093      */
1094     public static TypeAnnotationPosition
1095         methodRefTypeArg(final List<TypePathEntry> location,
1096                          final int type_index) {
1097         return methodRefTypeArg(location, null, type_index, -1);
1098     }
1099 
1100     /**
1101      * Create a {@code TypeAnnotationPosition} for a constructor reference
1102      * type argument.
1103      *
1104      * @param location The type path.
1105      * @param onLambda The lambda for this variable.
1106      * @param parameter_index The index of the type argument.
1107      * @param pos The position from the associated tree node.
1108      */
1109     public static TypeAnnotationPosition
1110         constructorRefTypeArg(final List<TypePathEntry> location,
1111                               final JCLambda onLambda,
1112                               final int type_index,
1113                               final int pos) {
1114         return new TypeAnnotationPosition(TargetType.CONSTRUCTOR_REFERENCE_TYPE_ARGUMENT,
1115                                           pos, Integer.MIN_VALUE, onLambda,
1116                                           type_index, Integer.MIN_VALUE,
1117                                           location);
1118     }
1119 
1120     /**
1121      * Create a {@code TypeAnnotationPosition} for a constructor reference
1122      * type argument.
1123      *
1124      * @param location The type path.
1125      * @param parameter_index The index of the type argument.
1126      */
1127     public static TypeAnnotationPosition
1128         constructorRefTypeArg(final List<TypePathEntry> location,
1129                               final int type_index) {
1130         return constructorRefTypeArg(location, null, type_index, -1);
1131     }
1132 
1133     /**
1134      * Create a {@code TypeAnnotationPosition} for a type parameter bound.
1135      *
1136      * @param location The type path.
1137      * @param onLambda The lambda for this variable.
1138      * @param parameter_index The index of the type parameter.
1139      * @param bound_index The index of the type parameter bound.
1140      * @param pos The position from the associated tree node.
1141      */
1142     public static TypeAnnotationPosition
1143         typeParameterBound(final List<TypePathEntry> location,
1144                            final JCLambda onLambda,
1145                            final int parameter_index,
1146                            final int bound_index,
1147                            final int pos) {
1148         return new TypeAnnotationPosition(TargetType.CLASS_TYPE_PARAMETER_BOUND,
1149                                           pos, parameter_index, onLambda,
1150                                           Integer.MIN_VALUE, bound_index,
1151                                           location);
1152     }
1153 
1154     /**
1155      * Create a {@code TypeAnnotationPosition} for a type parameter bound.
1156      *
1157      * @param location The type path.
1158      * @param parameter_index The index of the type parameter.
1159      * @param bound_index The index of the type parameter bound.
1160      */
1161     public static TypeAnnotationPosition
1162         typeParameterBound(final List<TypePathEntry> location,
1163                            final int parameter_index,
1164                            final int bound_index) {
1165         return typeParameterBound(location, null, parameter_index,
1166                                   bound_index, -1);
1167     }
1168 
1169     /**
1170      * Create a {@code TypeAnnotationPosition} for a method type
1171      * parameter bound.
1172      *
1173      * @param location The type path.
1174      * @param onLambda The lambda for this variable.
1175      * @param parameter_index The index of the type parameter.
1176      * @param bound_index The index of the type parameter bound.
1177      * @param pos The position from the associated tree node.
1178      */
1179     public static TypeAnnotationPosition
1180         methodTypeParameterBound(final List<TypePathEntry> location,
1181                                  final JCLambda onLambda,
1182                                  final int parameter_index,
1183                                  final int bound_index,
1184                                  final int pos) {
1185         return new TypeAnnotationPosition(TargetType.METHOD_TYPE_PARAMETER_BOUND,
1186                                           pos, parameter_index, onLambda,
1187                                           Integer.MIN_VALUE, bound_index,
1188                                           location);
1189     }
1190 
1191     /**
1192      * Create a {@code TypeAnnotationPosition} for a method type
1193      * parameter bound.
1194      *
1195      * @param location The type path.
1196      * @param parameter_index The index of the type parameter.
1197      * @param bound_index The index of the type parameter bound.
1198      */
1199     public static TypeAnnotationPosition
1200         methodTypeParameterBound(final List<TypePathEntry> location,
1201                                  final int parameter_index,
1202                                  final int bound_index) {
1203         return methodTypeParameterBound(location, null, parameter_index,
1204                                         bound_index, -1);
1205     }
1206 }