aboutsummaryrefslogtreecommitdiffhomepage
path: root/third_party/checker_framework_javacutil/java/org/checkerframework/javacutil/TypeAnnotationUtils.java
blob: c7faceb53a76d04f8736d4d5a7ff25bc5142f6a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
package org.checkerframework.javacutil;

import java.lang.reflect.InvocationTargetException;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Map;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.AnnotationValueVisitor;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.ArrayType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;

import com.sun.tools.javac.code.Attribute;
import com.sun.tools.javac.code.Attribute.TypeCompound;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.TargetType;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.TypeAnnotationPosition;
import com.sun.tools.javac.processing.JavacProcessingEnvironment;
import com.sun.tools.javac.tree.JCTree.JCLambda;
import com.sun.tools.javac.util.Context;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.Name;
import com.sun.tools.javac.util.Pair;

/**
 * A collection of helper methods related to type annotation handling.
 *
 * @see AnnotationUtils
 */
public class TypeAnnotationUtils {

    // Class cannot be instantiated.
    private TypeAnnotationUtils() { throw new AssertionError("Class TypeAnnotationUtils cannot be instantiated."); }

    /**
     * Check whether a TypeCompound is contained in a list of TypeCompounds.
     *
     * @param list the input list of TypeCompounds
     * @param tc the TypeCompound to find
     * @return true, iff a TypeCompound equal to tc is contained in list
     */
    public static boolean isTypeCompoundContained(Types types, List<TypeCompound> list, TypeCompound tc) {
        for (Attribute.TypeCompound rawat : list) {
            if (contentEquals(rawat.type.tsym.name, tc.type.tsym.name) &&
                    // TODO: in previous line, it would be nicer to use reference equality:
                    //   rawat.type == tc.type &&
                    // or at least "isSameType":
                    //   types.isSameType(rawat.type, tc.type) &&
                    // but each fails in some cases.
                    rawat.values.equals(tc.values) &&
                    isSameTAPositionExceptTreePos(rawat.position, tc.position)) {
                return true;
            }
        }
        return false;
    }

    private static boolean contentEquals(Name n1, Name n2) {
        // Views of underlying bytes, not copies as with Name#contentEquals
        ByteBuffer b1 = ByteBuffer.wrap(n1.getByteArray(), n1.getByteOffset(), n1.getByteLength());
        ByteBuffer b2 = ByteBuffer.wrap(n2.getByteArray(), n2.getByteOffset(), n2.getByteLength());

        return b1.equals(b2);
    }

    /**
     * Compare two TypeAnnotationPositions for equality.
     *
     * @param p1 the first position
     * @param p2 the second position
     * @return true, iff the two positions are equal
     */
    public static boolean isSameTAPosition(TypeAnnotationPosition p1,
            TypeAnnotationPosition p2) {
        return isSameTAPositionExceptTreePos(p1, p2) && p1.pos == p2.pos;
    }

    public static boolean isSameTAPositionExceptTreePos(TypeAnnotationPosition p1,
                                           TypeAnnotationPosition p2) {
        return p1.isValidOffset == p2.isValidOffset &&
               p1.bound_index == p2.bound_index &&
               p1.exception_index == p2.exception_index &&
               p1.location.equals(p2.location) &&
               Arrays.equals(p1.lvarIndex, p2.lvarIndex) &&
               Arrays.equals(p1.lvarLength, p2.lvarLength) &&
               Arrays.equals(p1.lvarOffset, p2.lvarOffset) &&
               p1.offset == p2.offset &&
               p1.onLambda == p2.onLambda &&
               p1.parameter_index == p2.parameter_index &&
               p1.type == p2.type &&
               p1.type_index == p2.type_index;
    }

    /**
     * Returns a newly created Attribute.Compound corresponding to an
     * argument AnnotationMirror.
     *
     * @param am  an AnnotationMirror, which may be part of an AST or an internally
     *            created subclass
     * @return  a new Attribute.Compound corresponding to the AnnotationMirror
     */
    public static Attribute.Compound createCompoundFromAnnotationMirror(ProcessingEnvironment env,
            AnnotationMirror am) {
        // Create a new Attribute to match the AnnotationMirror.
        List<Pair<Symbol.MethodSymbol, Attribute>> values = List.nil();
        for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry :
                 am.getElementValues().entrySet()) {
            Attribute attribute = attributeFromAnnotationValue(env, entry.getKey(), entry.getValue());
            values = values.append(new Pair<>((Symbol.MethodSymbol)entry.getKey(),
                                              attribute));
        }
        return new Attribute.Compound((Type.ClassType)am.getAnnotationType(), values);
    }

    /**
     * Returns a newly created Attribute.TypeCompound corresponding to an
     * argument AnnotationMirror.
     *
     * @param am  an AnnotationMirror, which may be part of an AST or an internally
     *            created subclass
     * @param tapos  the type annotation position to use
     * @return  a new Attribute.TypeCompound corresponding to the AnnotationMirror
     */
    public static Attribute.TypeCompound createTypeCompoundFromAnnotationMirror(ProcessingEnvironment env,
            AnnotationMirror am, TypeAnnotationPosition tapos) {
        // Create a new Attribute to match the AnnotationMirror.
        List<Pair<Symbol.MethodSymbol, Attribute>> values = List.nil();
        for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry :
                 am.getElementValues().entrySet()) {
            Attribute attribute = attributeFromAnnotationValue(env, entry.getKey(), entry.getValue());
            values = values.append(new Pair<>((Symbol.MethodSymbol)entry.getKey(),
                                              attribute));
        }
        return new Attribute.TypeCompound((Type.ClassType)am.getAnnotationType(), values, tapos);
    }

    /**
     * Returns a newly created Attribute corresponding to an argument
     * AnnotationValue.
     *
     * @param meth the ExecutableElement that is assigned the value, needed for empty arrays
     * @param av  an AnnotationValue, which may be part of an AST or an internally
     *            created subclass
     * @return  a new Attribute corresponding to the AnnotationValue
     */
    public static Attribute attributeFromAnnotationValue(ProcessingEnvironment env, ExecutableElement meth, AnnotationValue av) {
        return av.accept(new AttributeCreator(env, meth), null);
    }

    private static class AttributeCreator implements AnnotationValueVisitor<Attribute, Void> {
        private final ProcessingEnvironment processingEnv;
        private final Types modelTypes;
        private final Elements elements;
        private final com.sun.tools.javac.code.Types javacTypes;

        private final ExecutableElement meth;

        public AttributeCreator(ProcessingEnvironment env, ExecutableElement meth) {
            this.processingEnv = env;
            Context context = ((JavacProcessingEnvironment)env).getContext();
            this.elements = env.getElementUtils();
            this.modelTypes = env.getTypeUtils();
            this.javacTypes = com.sun.tools.javac.code.Types.instance(context);

            this.meth = meth;
        }

        @Override
        public Attribute visit(AnnotationValue av, Void p) {
            return av.accept(this, p);
        }

        @Override
        public Attribute visit(AnnotationValue av) {
            return visit(av, null);
        }

        @Override
        public Attribute visitBoolean(boolean b, Void p) {
            TypeMirror booleanType = modelTypes.getPrimitiveType(TypeKind.BOOLEAN);
            return new Attribute.Constant((Type) booleanType, b ? 1 : 0);
        }

        @Override
        public Attribute visitByte(byte b, Void p) {
            TypeMirror byteType = modelTypes.getPrimitiveType(TypeKind.BYTE);
            return new Attribute.Constant((Type)byteType, b);
        }

        @Override
        public Attribute visitChar(char c, Void p) {
            TypeMirror charType = modelTypes.getPrimitiveType(TypeKind.CHAR);
            return new Attribute.Constant((Type)charType, c);
        }

        @Override
        public Attribute visitDouble(double d, Void p) {
            TypeMirror doubleType = modelTypes.getPrimitiveType(TypeKind.DOUBLE);
            return new Attribute.Constant((Type)doubleType, d);
        }

        @Override
        public Attribute visitFloat(float f, Void p) {
            TypeMirror floatType = modelTypes.getPrimitiveType(TypeKind.FLOAT);
            return new Attribute.Constant((Type)floatType, f);
        }

        @Override
        public Attribute visitInt(int i, Void p) {
            TypeMirror intType = modelTypes.getPrimitiveType(TypeKind.INT);
            return new Attribute.Constant((Type)intType, i);
        }

        @Override
        public Attribute visitLong(long i, Void p) {
            TypeMirror longType = modelTypes.getPrimitiveType(TypeKind.LONG);
            return new Attribute.Constant((Type)longType, i);
        }

        @Override
        public Attribute visitShort(short s, Void p) {
            TypeMirror shortType = modelTypes.getPrimitiveType(TypeKind.SHORT);
            return new Attribute.Constant((Type)shortType, s);
        }

        @Override
        public Attribute visitString(String s, Void p) {
            TypeMirror stringType = elements.getTypeElement("java.lang.String").asType();
            return new Attribute.Constant((Type)stringType, s);
        }

        @Override
        public Attribute visitType(TypeMirror t, Void p) {
            if (t instanceof Type) {
                return new Attribute.Class(javacTypes, (Type)t);
            }

            assert false : "Unexpected type of TypeMirror: " + t.getClass();
            return null;
        }

        @Override
        public Attribute visitEnumConstant(VariableElement c, Void p) {
            if (c instanceof Symbol.VarSymbol) {
                Symbol.VarSymbol sym = (Symbol.VarSymbol) c;
                if (sym.getKind() == ElementKind.ENUM_CONSTANT) {
                    return new Attribute.Enum(sym.type, sym);
                }
            }

            assert false : "Unexpected type of VariableElement: " + c.getClass();
            return null;
        }

        @Override
        public Attribute visitAnnotation(AnnotationMirror a, Void p) {
            return createCompoundFromAnnotationMirror(processingEnv, a);
        }

        @Override
        public Attribute visitArray(java.util.List<? extends AnnotationValue> vals, Void p) {
            if (!vals.isEmpty()) {
                List<Attribute> valAttrs = List.nil();
                for (AnnotationValue av : vals) {
                    valAttrs = valAttrs.append(av.accept(this, p));
                }
                ArrayType arrayType = modelTypes.getArrayType(valAttrs.get(0).type);
                return new Attribute.Array((Type)arrayType, valAttrs);
            } else {
                return new Attribute.Array((Type) meth.getReturnType(), List.<Attribute>nil());
            }
        }

        @Override
        public Attribute visitUnknown(AnnotationValue av, Void p) {
            assert false : "Unexpected type of AnnotationValue: " + av.getClass();
            return null;
        }
    }

    /**
     * An interface to abstract a Java 8 and a Java 9 version of how
     * to get a RET reference.
     * These methods must then be implemented using reflection in order to
     * compile in either setting.
     * Note that we cannot use lambda for this as long as we want to
     * support Java 7.
     */
    interface Call8or9<RET> {
        RET call8() throws Throwable;
        RET call9() throws Throwable;
    }

    /**
     * Use the SourceVersion to decide whether to call the Java 8 or Java 9 version.
     * Catch all exceptions and abort if one occurs - the reflection code should
     * never break once fully debugged.
     *
     * @param tc the TAPCall abstraction to encapsulate two methods
     * @return the created TypeAnnotationPosition
     */
    private static <RET> RET call8or9(Call8or9<RET> tc) {
        try {
            boolean hasNine;
            try {
                hasNine = SourceVersion.valueOf("RELEASE_9") != null;
            } catch (IllegalArgumentException iae) {
                hasNine = false;
            }
            if (hasNine) {
                return tc.call9();
            } else {
                boolean hasEight;
                try {
                    hasEight = SourceVersion.valueOf("RELEASE_8") != null;
                } catch (IllegalArgumentException iae) {
                    hasEight = false;
                }
                if (hasEight) {
                    return tc.call8();
                } else {
                    assert false : "Checker Framework needs a Java 8 or 9 javac.";
                    return null;
                }
            }
        } catch (Throwable t) {
            assert false : "Checker Framework internal error: " + t;
            t.printStackTrace();
            return null;
        }
    }

    public static TypeAnnotationPosition unknownTAPosition() {
        return call8or9(
                new Call8or9<TypeAnnotationPosition>() {
                    @Override
                    public TypeAnnotationPosition call8() throws InstantiationException, IllegalAccessException {
                        return TypeAnnotationPosition.class.newInstance();
                    }
                    @Override
                    public TypeAnnotationPosition call9() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException {
                        return (TypeAnnotationPosition) TypeAnnotationPosition.class
                                .getField("unknown")
                                .get(null);
                    }
                }
            );
    }

    public static TypeAnnotationPosition methodReturnTAPosition(final int pos) {
        return call8or9(
                new Call8or9<TypeAnnotationPosition>() {
                    @Override
                    public TypeAnnotationPosition call8() throws InstantiationException, IllegalAccessException, IllegalArgumentException, NoSuchFieldException, SecurityException {
                        TypeAnnotationPosition tapos = TypeAnnotationPosition.class.newInstance();
                        TypeAnnotationPosition.class.getField("type").set(tapos, TargetType.METHOD_RETURN);
                        TypeAnnotationPosition.class.getField("pos").set(tapos, pos);
                        return tapos;
                    }
                    @Override
                    public TypeAnnotationPosition call9() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
                        return (TypeAnnotationPosition) TypeAnnotationPosition.class
                                .getMethod("methodReturn", int.class)
                                .invoke(null, pos);
                    }
                }
            );
    }

    public static TypeAnnotationPosition methodReceiverTAPosition(final int pos) {
        return call8or9(
                new Call8or9<TypeAnnotationPosition>() {
                    @Override
                    public TypeAnnotationPosition call8() throws InstantiationException, IllegalAccessException, IllegalArgumentException, NoSuchFieldException, SecurityException {
                        TypeAnnotationPosition tapos = TypeAnnotationPosition.class.newInstance();
                        TypeAnnotationPosition.class.getField("type").set(tapos, TargetType.METHOD_RECEIVER);
                        TypeAnnotationPosition.class.getField("pos").set(tapos, pos);
                        return tapos;
                    }
                    @Override
                    public TypeAnnotationPosition call9() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
                        return (TypeAnnotationPosition) TypeAnnotationPosition.class
                                .getMethod("methodReceiver", int.class)
                                .invoke(null, pos);
                    }
                }
            );
    }

    public static TypeAnnotationPosition methodParameterTAPosition(final int pidx, final int pos) {
        return call8or9(
                new Call8or9<TypeAnnotationPosition>() {
                    @Override
                    public TypeAnnotationPosition call8() throws InstantiationException, IllegalAccessException, IllegalArgumentException, NoSuchFieldException, SecurityException {
                        TypeAnnotationPosition tapos = TypeAnnotationPosition.class.newInstance();
                        TypeAnnotationPosition.class.getField("type").set(tapos, TargetType.METHOD_FORMAL_PARAMETER);
                        TypeAnnotationPosition.class.getField("parameter_index").set(tapos, pidx);
                        TypeAnnotationPosition.class.getField("pos").set(tapos, pos);
                        return tapos;
                    }
                    @Override
                    public TypeAnnotationPosition call9() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
                        return (TypeAnnotationPosition) TypeAnnotationPosition.class
                                .getMethod("methodParameter", int.class, int.class)
                                .invoke(null, pidx, pos);
                    }
                }
            );
    }

    public static TypeAnnotationPosition methodThrowsTAPosition(final int tidx, final int pos) {
        return call8or9(
                new Call8or9<TypeAnnotationPosition>() {
                    @Override
                    public TypeAnnotationPosition call8() throws InstantiationException, IllegalAccessException, IllegalArgumentException, NoSuchFieldException, SecurityException {
                        TypeAnnotationPosition tapos = TypeAnnotationPosition.class.newInstance();
                        TypeAnnotationPosition.class.getField("type").set(tapos, TargetType.THROWS);
                        TypeAnnotationPosition.class.getField("type_index").set(tapos, tidx);
                        TypeAnnotationPosition.class.getField("pos").set(tapos, pos);
                        return tapos;
                    }
                    @Override
                    public TypeAnnotationPosition call9() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, NoSuchFieldException {
                        return (TypeAnnotationPosition) TypeAnnotationPosition.class
                                .getMethod("methodThrows", List.class, JCLambda.class, int.class, int.class)
                                .invoke(null, TypeAnnotationPosition.class.getField("emptyPath").get(null), null, tidx, pos);
                    }
                }
            );
    }

    public static TypeAnnotationPosition fieldTAPosition(final int pos) {
        return call8or9(
                new Call8or9<TypeAnnotationPosition>() {
                    @Override
                    public TypeAnnotationPosition call8() throws InstantiationException, IllegalAccessException, IllegalArgumentException, NoSuchFieldException, SecurityException {
                        TypeAnnotationPosition tapos = TypeAnnotationPosition.class.newInstance();
                        TypeAnnotationPosition.class.getField("type").set(tapos, TargetType.FIELD);
                        TypeAnnotationPosition.class.getField("pos").set(tapos, pos);
                        return tapos;
                    }
                    @Override
                    public TypeAnnotationPosition call9() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
                        return (TypeAnnotationPosition) TypeAnnotationPosition.class
                                .getMethod("field", int.class)
                                .invoke(null, pos);
                    }
                }
            );
    }

    public static TypeAnnotationPosition classExtendsTAPosition(final int implidx, final int pos) {
        return call8or9(
                new Call8or9<TypeAnnotationPosition>() {
                    @Override
                    public TypeAnnotationPosition call8() throws InstantiationException, IllegalAccessException, IllegalArgumentException, NoSuchFieldException, SecurityException {
                        TypeAnnotationPosition tapos = TypeAnnotationPosition.class.newInstance();
                        TypeAnnotationPosition.class.getField("type").set(tapos, TargetType.CLASS_EXTENDS);
                        TypeAnnotationPosition.class.getField("type_index").set(tapos, implidx);
                        TypeAnnotationPosition.class.getField("pos").set(tapos, pos);
                        return tapos;
                    }
                    @Override
                    public TypeAnnotationPosition call9() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
                        return (TypeAnnotationPosition) TypeAnnotationPosition.class
                                .getMethod("classExtends", int.class, int.class)
                                .invoke(null, implidx, pos);
                    }
                }
            );
    }

    public static TypeAnnotationPosition typeParameterTAPosition(final int tpidx, final int pos) {
        return call8or9(
                new Call8or9<TypeAnnotationPosition>() {
                    @Override
                    public TypeAnnotationPosition call8() throws InstantiationException, IllegalAccessException, IllegalArgumentException, NoSuchFieldException, SecurityException {
                        TypeAnnotationPosition tapos = TypeAnnotationPosition.class.newInstance();
                        TypeAnnotationPosition.class.getField("type").set(tapos, TargetType.CLASS_TYPE_PARAMETER);
                        TypeAnnotationPosition.class.getField("parameter_index").set(tapos, tpidx);
                        TypeAnnotationPosition.class.getField("pos").set(tapos, pos);
                        return tapos;
                    }
                    @Override
                    public TypeAnnotationPosition call9() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, NoSuchFieldException {
                        return (TypeAnnotationPosition) TypeAnnotationPosition.class
                                .getMethod("typeParameter", List.class, JCLambda.class, int.class, int.class)
                                .invoke(null, TypeAnnotationPosition.class.getField("emptyPath").get(null), null, tpidx, pos);
                    }
                }
            );
    }

    public static TypeAnnotationPosition methodTypeParameterTAPosition(final int tpidx, final int pos) {
        return call8or9(
                new Call8or9<TypeAnnotationPosition>() {
                    @Override
                    public TypeAnnotationPosition call8() throws InstantiationException, IllegalAccessException, IllegalArgumentException, NoSuchFieldException, SecurityException {
                        TypeAnnotationPosition tapos = TypeAnnotationPosition.class.newInstance();
                        TypeAnnotationPosition.class.getField("type").set(tapos, TargetType.METHOD_TYPE_PARAMETER);
                        TypeAnnotationPosition.class.getField("parameter_index").set(tapos, tpidx);
                        TypeAnnotationPosition.class.getField("pos").set(tapos, pos);
                        return tapos;
                    }
                    @Override
                    public TypeAnnotationPosition call9() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, NoSuchFieldException {
                        return (TypeAnnotationPosition) TypeAnnotationPosition.class
                                .getMethod("methodTypeParameter", List.class, JCLambda.class, int.class, int.class)
                                .invoke(null, TypeAnnotationPosition.class.getField("emptyPath").get(null), null, tpidx, pos);
                    }
                }
            );
    }

    public static TypeAnnotationPosition typeParameterBoundTAPosition(final int tpidx, final int bndidx, final int pos) {
        return call8or9(
                new Call8or9<TypeAnnotationPosition>() {
                    @Override
                    public TypeAnnotationPosition call8() throws InstantiationException, IllegalAccessException, IllegalArgumentException, NoSuchFieldException, SecurityException {
                        TypeAnnotationPosition tapos = TypeAnnotationPosition.class.newInstance();
                        TypeAnnotationPosition.class.getField("type").set(tapos, TargetType.CLASS_TYPE_PARAMETER_BOUND);
                        TypeAnnotationPosition.class.getField("parameter_index").set(tapos, tpidx);
                        TypeAnnotationPosition.class.getField("bound_index").set(tapos, bndidx);
                        TypeAnnotationPosition.class.getField("pos").set(tapos, pos);
                        return tapos;
                    }
                    @Override
                    public TypeAnnotationPosition call9() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, NoSuchFieldException {
                        return (TypeAnnotationPosition) TypeAnnotationPosition.class
                                .getMethod("typeParameterBound", List.class, JCLambda.class, int.class, int.class, int.class)
                                .invoke(null, TypeAnnotationPosition.class.getField("emptyPath").get(null), null, tpidx, bndidx, pos);
                    }
                }
            );
    }

    public static TypeAnnotationPosition methodTypeParameterBoundTAPosition(final int tpidx, final int bndidx, final int pos) {
        return call8or9(
                new Call8or9<TypeAnnotationPosition>() {
                    @Override
                    public TypeAnnotationPosition call8() throws InstantiationException, IllegalAccessException, IllegalArgumentException, NoSuchFieldException, SecurityException {
                        TypeAnnotationPosition tapos = TypeAnnotationPosition.class.newInstance();
                        TypeAnnotationPosition.class.getField("type").set(tapos, TargetType.METHOD_TYPE_PARAMETER_BOUND);
                        TypeAnnotationPosition.class.getField("parameter_index").set(tapos, tpidx);
                        TypeAnnotationPosition.class.getField("bound_index").set(tapos, bndidx);
                        TypeAnnotationPosition.class.getField("pos").set(tapos, pos);
                        return tapos;
                    }
                    @Override
                    public TypeAnnotationPosition call9() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, NoSuchFieldException {
                        return (TypeAnnotationPosition) TypeAnnotationPosition.class
                                .getMethod("methodTypeParameterBound", List.class, JCLambda.class, int.class, int.class, int.class)
                                .invoke(null, TypeAnnotationPosition.class.getField("emptyPath").get(null), null, tpidx, bndidx, pos);
                    }
                }
            );
    }

    public static TypeAnnotationPosition copyTAPosition(final TypeAnnotationPosition tapos) {
        return call8or9(
                new Call8or9<TypeAnnotationPosition>() {
                    @Override
                    public TypeAnnotationPosition call8() throws InstantiationException, IllegalAccessException, IllegalArgumentException, NoSuchFieldException, SecurityException {
                        return copyTAPosition8(tapos);
                    }
                    @Override
                    public TypeAnnotationPosition call9() throws IllegalArgumentException, IllegalAccessException, NoSuchFieldException, SecurityException, InvocationTargetException, NoSuchMethodException {
                        return (TypeAnnotationPosition) TypeAnnotationPosition.class
                                .getMethod("copy", TypeAnnotationPosition.class)
                                .invoke(null, tapos);
                    }
                }
            );
    }

    private static TypeAnnotationPosition copyTAPosition8(TypeAnnotationPosition tapos) throws InstantiationException, IllegalAccessException, IllegalArgumentException, NoSuchFieldException, SecurityException {
        TypeAnnotationPosition res = TypeAnnotationPosition.class.newInstance();
        res.isValidOffset = tapos.isValidOffset;
        TypeAnnotationPosition.class.getField("bound_index").set(res, tapos.bound_index);
        res.exception_index = tapos.exception_index;
        res.location = List.from(tapos.location);
        if (tapos.lvarIndex != null) {
            res.lvarIndex = Arrays.copyOf(tapos.lvarIndex, tapos.lvarIndex.length);
        }
        if (tapos.lvarLength != null) {
            res.lvarLength = Arrays.copyOf(tapos.lvarLength, tapos.lvarLength.length);
        }
        if (tapos.lvarOffset != null) {
            res.lvarOffset = Arrays.copyOf(tapos.lvarOffset, tapos.lvarOffset.length);
        }
        res.offset = tapos.offset;
        TypeAnnotationPosition.class.getField("onLambda").set(res, tapos.onLambda);
        TypeAnnotationPosition.class.getField("parameter_index").set(res, tapos.parameter_index);
        TypeAnnotationPosition.class.getField("pos").set(res, tapos.pos);
        TypeAnnotationPosition.class.getField("type").set(res, tapos.type);
        TypeAnnotationPosition.class.getField("type_index").set(res, tapos.type_index);
        return res;
    }

    public static Type unannotatedType(final Type in) {
        return call8or9(
                new Call8or9<Type>() {
                    @Override
                    public Type call8() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException {
                        return (Type) Type.class
                                .getMethod("unannotatedType")
                                .invoke(in);
                    }
                    @Override
                    public Type call9() {
                        return in;
                    }
                }
            );
    }

}