• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pmd / pmd / 225

28 Oct 2025 07:44AM UTC coverage: 78.72% (+0.04%) from 78.679%
225

push

github

adangel
[java] Fix #6146: ClassCastException in TypeTestUtil (#6156)

18307 of 24103 branches covered (75.95%)

Branch coverage included in aggregate %.

37 of 39 new or added lines in 11 files covered. (94.87%)

6 existing lines in 1 file now uncovered.

39859 of 49787 relevant lines covered (80.06%)

0.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.49
/pmd-java/src/main/java/net/sourceforge/pmd/lang/java/types/TypeOps.java
1
/*
2
 * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3
 */
4

5
package net.sourceforge.pmd.lang.java.types;
6

7
import static net.sourceforge.pmd.lang.java.types.Substitution.EMPTY;
8
import static net.sourceforge.pmd.lang.java.types.Substitution.mapping;
9
import static net.sourceforge.pmd.lang.java.types.TypeConversion.capture;
10

11
import java.lang.reflect.Modifier;
12
import java.util.ArrayList;
13
import java.util.Arrays;
14
import java.util.Collection;
15
import java.util.Collections;
16
import java.util.HashSet;
17
import java.util.LinkedHashSet;
18
import java.util.List;
19
import java.util.Map;
20
import java.util.Map.Entry;
21
import java.util.Objects;
22
import java.util.Set;
23
import java.util.function.BiFunction;
24
import java.util.function.Function;
25
import java.util.function.Predicate;
26
import java.util.stream.Collectors;
27

28
import org.checkerframework.checker.nullness.qual.NonNull;
29
import org.checkerframework.checker.nullness.qual.Nullable;
30

31
import net.sourceforge.pmd.lang.java.symbols.JClassSymbol;
32
import net.sourceforge.pmd.lang.java.symbols.JConstructorSymbol;
33
import net.sourceforge.pmd.lang.java.symbols.JExecutableSymbol;
34
import net.sourceforge.pmd.lang.java.symbols.JMethodSymbol;
35
import net.sourceforge.pmd.lang.java.symbols.JTypeDeclSymbol;
36
import net.sourceforge.pmd.lang.java.symbols.table.coreimpl.CoreResolvers;
37
import net.sourceforge.pmd.lang.java.symbols.table.coreimpl.NameResolver;
38
import net.sourceforge.pmd.lang.java.symbols.table.internal.JavaResolvers;
39
import net.sourceforge.pmd.lang.java.types.JVariableSig.FieldSig;
40
import net.sourceforge.pmd.lang.java.types.internal.infer.InferenceVar;
41
import net.sourceforge.pmd.lang.java.types.internal.infer.InferenceVar.BoundKind;
42
import net.sourceforge.pmd.lang.java.types.internal.infer.OverloadSet;
43
import net.sourceforge.pmd.util.AssertionUtil;
44
import net.sourceforge.pmd.util.CollectionUtil;
45
import net.sourceforge.pmd.util.IteratorUtil;
46

47
/**
48
 * Common operations on types.
49
 */
50
@SuppressWarnings("PMD.CompareObjectsWithEquals")
51
public final class TypeOps {
52

53
    private TypeOps() {
54
        // utility class
55
    }
56

57

58
    // <editor-fold  defaultstate="collapsed" desc="Type equality">
59

60
    /**
61
     * Return true if t and s are the same method type. This compares
62
     * their declaring type, and then their signature.
63
     *
64
     * @see #haveSameSignature(JMethodSig, JMethodSig)
65
     */
66
    public static boolean isSameType(JMethodSig t, JMethodSig s) {
67
        return t.getDeclaringType().equals(s.getDeclaringType()) && haveSameSignature(t, s);
1✔
68
    }
69

70
    /*
71
     * Note that type mirror implementations use this method as their
72
     * Object#equals, which means it can't be used here unless it's on
73
     * the smaller parts of a type.
74
     */
75

76

77
    /**
78
     * Return true if t and s are the same type, ignoring any type annotations
79
     * appearing within them. This is the implementation of the equals method
80
     * of {@link JTypeMirror}.
81
     */
82
    public static boolean isSameType(JTypeMirror t, JTypeMirror s) {
83
        return isSameType(t, s, true, false);
1✔
84
    }
85

86
    /**
87
     * Return true if t and s are the same type, considering any type annotations
88
     * appearing within them.
89
     */
90
    public static boolean isSameTypeWithSameAnnotations(JTypeMirror t, JTypeMirror s) {
91
        return isSameType(t, s, true, true);
1✔
92
    }
93

94
    /**
95
     * Return true if t and s are the same type. This may perform side effects
96
     * on inference variables. Annotations are ignored.
97
     *
98
     * @apiNote Internal API
99
     */
100
    static boolean isSameTypeInInference(JTypeMirror t, JTypeMirror s) {
101
        return isSameType(t, s, false, false);
1✔
102
    }
103

104
    /**
105
     * Returns true if t and s are the same type. If 'inInference' is
106
     * true, then encountering inference variables produces side effects
107
     * on them, adding bounds.
108
     */
109
    private static boolean isSameType(JTypeMirror t, JTypeMirror s, boolean pure, boolean considerAnnotations) {
110
        if (t == s) {
1✔
111
            // also returns true if both t and s are null
112
            return true;
1✔
113
        }
114

115
        if (t == null || s == null) {
1!
116
            return false;
×
117
        }
118

119
        if (pure) {
1✔
120
            if (considerAnnotations) {
1✔
121
                if (t instanceof CaptureMatcher || s instanceof CaptureMatcher) {
1!
122
                    return t.equals(s); // skip check for type annotations
1✔
123
                }
124
                return t.getTypeAnnotations().equals(s.getTypeAnnotations())
1!
125
                    && t.acceptVisitor(SameTypeVisitor.PURE_WITH_ANNOTATIONS, s);
1✔
126
            } else {
127
                return t.acceptVisitor(SameTypeVisitor.PURE, s);
1✔
128
            }
129
        }
130

131
        // reorder
132
        if (t instanceof InferenceVar) {
1✔
133
            return t.acceptVisitor(SameTypeVisitor.INFERENCE, s);
1✔
134
        } else {
135
            return s.acceptVisitor(SameTypeVisitor.INFERENCE, t);
1✔
136
        }
137
    }
138

139
    public static boolean areSameTypes(List<JTypeMirror> ts, List<JTypeMirror> ss) {
140
        return areSameTypes(ts, ss, EMPTY, true, false);
×
141
    }
142

143
    public static boolean areSameTypesInInference(List<JTypeMirror> ts, List<JTypeMirror> ss) {
144
        return areSameTypes(ts, ss, EMPTY, false, false);
1✔
145
    }
146

147
    private static boolean areSameTypes(List<JTypeMirror> ts, List<JTypeMirror> ss, boolean pure, boolean considerAnnotations) {
148
        return areSameTypes(ts, ss, EMPTY, pure, considerAnnotations);
1✔
149
    }
150

151
    private static boolean areSameTypes(List<JTypeMirror> ts, List<JTypeMirror> ss, Substitution subst) {
152
        return areSameTypes(ts, ss, subst, true, false);
1✔
153
    }
154

155
    private static boolean areSameTypes(List<JTypeMirror> ts, List<JTypeMirror> ss, Substitution subst, boolean pure, boolean considerAnnotations) {
156
        if (ts.size() != ss.size()) {
1✔
157
            return false;
1✔
158
        }
159
        for (int i = 0; i < ts.size(); i++) {
1✔
160
            if (!isSameType(ts.get(i), ss.get(i).subst(subst), pure, considerAnnotations)) {
1✔
161
                return false;
1✔
162
            }
163
        }
164
        return true;
1✔
165
    }
166

167

168
    // note that this does not take type annotations into account
169
    private static final class SameTypeVisitor implements JTypeVisitor<Boolean, JTypeMirror> {
170

171
        static final SameTypeVisitor INFERENCE = new SameTypeVisitor(false, false);
1✔
172
        static final SameTypeVisitor PURE = new SameTypeVisitor(true, false);
1✔
173
        static final SameTypeVisitor PURE_WITH_ANNOTATIONS = new SameTypeVisitor(true, true);
1✔
174

175
        private final boolean pure;
176
        private final boolean considerAnnotations;
177

178
        private SameTypeVisitor(boolean pure, boolean considerAnnotations) {
1✔
179
            this.pure = pure;
1✔
180
            this.considerAnnotations = considerAnnotations;
1✔
181
        }
1✔
182

183
        @Override
184
        public Boolean visit(JTypeMirror t, JTypeMirror s) {
NEW
185
            throw AssertionUtil.shouldNotReachHere("other overload should be chosen");
×
186
        }
187

188
        @Override
189
        public Boolean visitSentinel(JTypeMirror t, JTypeMirror s) {
190
            return t == s;
1!
191
        }
192

193
        @Override
194
        public Boolean visitPrimitive(JPrimitiveType t, JTypeMirror s) {
195
            return s.isPrimitive(t.getKind());
1✔
196
        }
197

198
        @Override
199
        public Boolean visitClass(JClassType t, JTypeMirror s) {
200
            if (s instanceof JClassType) {
1✔
201
                JClassType s2 = (JClassType) s;
1✔
202
                return t.getSymbol().equals(s2.getSymbol()) // maybe compare the type system as well.
1✔
203
                    && t.hasErasedSuperTypes() == s2.hasErasedSuperTypes()
1✔
204
                    && isSameType(t.getEnclosingType(), s2.getEnclosingType(), pure, considerAnnotations)
1✔
205
                    && areSameTypes(t.getTypeArgs(), s2.getTypeArgs(), pure, considerAnnotations);
1✔
206
            }
207
            return false;
1✔
208
        }
209

210
        @Override
211
        public Boolean visitTypeVar(JTypeVar t, JTypeMirror s) {
212
            return t.equals(s);
1✔
213
        }
214

215
        @Override
216
        public Boolean visitWildcard(JWildcardType t, JTypeMirror s) {
217
            if (!(s instanceof JWildcardType)) {
1✔
218
                return false;
1✔
219
            }
220
            JWildcardType s2 = (JWildcardType) s;
1✔
221
            return s2.isUpperBound() == t.isUpperBound() && isSameType(t.getBound(), s2.getBound(), pure, considerAnnotations);
1✔
222
        }
223

224
        @Override
225
        public Boolean visitInferenceVar(InferenceVar t, JTypeMirror s) {
226
            if (pure) {
1✔
227
                return t == s || t.getBounds(BoundKind.EQ).contains(s);
1!
228
            }
229

230
            if (s instanceof JPrimitiveType) {
1!
231
                return false;
×
232
            }
233

234
            if (s instanceof JWildcardType) {
1✔
235
                JWildcardType s2 = (JWildcardType) s;
1✔
236
                if (s2.isUpperBound()) {
1✔
237
                    t.addBound(BoundKind.UPPER, s2.asUpperBound());
1✔
238
                } else {
239
                    t.addBound(BoundKind.LOWER, s2.asLowerBound());
1✔
240
                }
241
                return true;
1✔
242
            }
243

244
            // add an equality bound
245
            t.addBound(BoundKind.EQ, s);
1✔
246
            return true;
1✔
247
        }
248

249
        @Override
250
        public Boolean visitIntersection(JIntersectionType t, JTypeMirror s) {
251
            if (!(s instanceof JIntersectionType)) {
1✔
252
                return false;
1✔
253
            }
254

255
            JIntersectionType s2 = (JIntersectionType) s;
1✔
256

257
            // order is irrelevant
258

259
            if (s2.getComponents().size() != t.getComponents().size()) {
1!
260
                return false;
×
261
            }
262

263
            if (!isSameType(t.getPrimaryBound(), s2.getPrimaryBound(), pure, considerAnnotations)) {
1!
264
                return false;
×
265
            }
266

267
            List<JTypeMirror> sComps = ((JIntersectionType) s).getComponents();
1✔
268
            for (JTypeMirror ti : t.getComponents()) {
1✔
269
                boolean found = false;
1✔
270
                for (JTypeMirror si : sComps) {
1!
271
                    // todo won't this behaves weirdly during inference? test it
272
                    if (isSameType(ti, si, pure, considerAnnotations)) {
1✔
273
                        found = true;
1✔
274
                        break;
1✔
275
                    }
276
                }
1✔
277
                if (!found) {
1!
278
                    return false;
×
279
                }
280
            }
1✔
281
            return true;
1✔
282
        }
283

284
        @Override
285
        public Boolean visitArray(JArrayType t, JTypeMirror s) {
286
            return s instanceof JArrayType
1✔
287
                && isSameType(t.getComponentType(), ((JArrayType) s).getComponentType(), pure, considerAnnotations);
1✔
288
        }
289
    }
290

291
    // </editor-fold>
292

293
    // <editor-fold  defaultstate="collapsed" desc="Supertype enumeration">
294

295

296
    /**
297
     * Returns the set of all supertypes of the given type.
298
     *
299
     * @see JTypeMirror#getSuperTypeSet()
300
     */
301
    public static Set<JTypeMirror> getSuperTypeSet(@NonNull JTypeMirror t) {
302
        Set<JTypeMirror> result = new LinkedHashSet<>();
1✔
303
        t.acceptVisitor(SuperTypesVisitor.INSTANCE, result);
1✔
304
        assert !result.isEmpty() : "Empty supertype set for " + t;
1!
305
        return result;
1✔
306
    }
307

308
    private static final class SuperTypesVisitor implements JTypeVisitor<Void, Set<JTypeMirror>> {
309

310
        static final SuperTypesVisitor INSTANCE = new SuperTypesVisitor();
1✔
311

312
        @Override
313
        public Void visit(JTypeMirror t, Set<JTypeMirror> result) {
314
            throw new IllegalStateException("Should not be called");
×
315
        }
316

317
        @Override
318
        public Void visitTypeVar(JTypeVar t, Set<JTypeMirror> result) {
319
            if (result.add(t)) {
1!
320
                // prevent infinite loop
321
                t.getUpperBound().acceptVisitor(this, result);
1✔
322
            }
323
            return null;
1✔
324
        }
325

326
        @Override
327
        public Void visitNullType(JTypeMirror t, Set<JTypeMirror> result) {
328
            // too many types
329
            throw new UnsupportedOperationException("The null type has all reference types as supertype");
1✔
330
        }
331

332
        @Override
333
        public Void visitSentinel(JTypeMirror t, Set<JTypeMirror> result) {
334
            result.add(t);
×
335
            return null;
×
336
        }
337

338
        @Override
339
        public Void visitInferenceVar(InferenceVar t, Set<JTypeMirror> result) {
340
            result.add(t);
×
341
            return null;
×
342
        }
343

344
        @Override
345
        public Void visitWildcard(JWildcardType t, Set<JTypeMirror> result) {
346
            t.asUpperBound().acceptVisitor(this, result);
1✔
347
            // wildcards should be captured and so we should not end up here
348
            return null;
1✔
349
        }
350

351
        @Override
352
        public Void visitClass(JClassType t, Set<JTypeMirror> result) {
353
            result.add(t);
1✔
354

355

356
            // prefer digging up the superclass first
357
            JClassType sup = t.getSuperClass();
1✔
358
            if (sup != null) {
1✔
359
                sup.acceptVisitor(this, result);
1✔
360
            }
361
            for (JClassType i : t.getSuperInterfaces()) {
1✔
362
                visitClass(i, result);
1✔
363
            }
1✔
364
            if (t.isInterface() && t.getSuperInterfaces().isEmpty()) {
1✔
365
                result.add(t.getTypeSystem().OBJECT);
1✔
366
            }
367
            return null;
1✔
368
        }
369

370
        @Override
371
        public Void visitIntersection(JIntersectionType t, Set<JTypeMirror> result) {
372
            for (JTypeMirror it : t.getComponents()) {
1✔
373
                it.acceptVisitor(this, result);
1✔
374
            }
1✔
375
            return null;
1✔
376
        }
377

378
        @Override
379
        public Void visitArray(JArrayType t, Set<JTypeMirror> result) {
380
            result.add(t);
1✔
381

382
            TypeSystem ts = t.getTypeSystem();
1✔
383

384
            for (JTypeMirror componentSuper : t.getComponentType().getSuperTypeSet()) {
1✔
385
                result.add(ts.arrayType(componentSuper));
1✔
386
            }
1✔
387
            result.add(ts.CLONEABLE);
1✔
388
            result.add(ts.SERIALIZABLE);
1✔
389
            result.add(ts.OBJECT);
1✔
390

391
            return null;
1✔
392
        }
393

394
        @Override
395
        public Void visitPrimitive(JPrimitiveType t, Set<JTypeMirror> result) {
396
            result.addAll(t.getSuperTypeSet()); // special implementation in JPrimitiveType
×
397
            return null;
×
398
        }
399
    }
400

401
    // </editor-fold>
402

403
    // <editor-fold  defaultstate="collapsed" desc="Subtyping">
404

405

406
    public static Convertibility isConvertible(@NonNull JTypeMirror t, @NonNull JTypeMirror s) {
407
        return SubtypeVisitor.INFERENCE.isConvertible(t, s, true);
1✔
408
    }
409

410
    @Deprecated // unused
411
    public static Convertibility isConvertible(@NonNull JTypeMirror t, @NonNull JTypeMirror s, boolean capture) {
412
        return SubtypeVisitor.PURE.isConvertible(t, s, capture);
×
413
    }
414

415
    public static Convertibility isConvertibleNoCapture(@NonNull JTypeMirror t, @NonNull JTypeMirror s) {
416
        return SubtypeVisitor.PURE.isConvertible(t, s, false);
1✔
417
    }
418

419
    // does not perform side effects on inference vars
420
    public static Convertibility isConvertiblePure(JTypeMirror t, JTypeMirror s) {
421
        return SubtypeVisitor.PURE.isConvertible(t, s);
1✔
422
    }
423

424
    public static boolean allArgsAreUnboundedWildcards(List<JTypeMirror> sargs) {
425
        for (JTypeMirror sarg : sargs) {
1✔
426
            if (!(sarg instanceof JWildcardType) || !((JWildcardType) sarg).isUnbounded()) {
1✔
427
                return false;
1✔
428
            }
429
        }
1✔
430
        return true;
1✔
431
    }
432

433
    /**
434
     * A result for a convertibility check. This is a tiny generalization of
435
     * a subtyping check.
436
     *
437
     * <p>Primitive types are implicitly convertible to each other by
438
     * widening primitive conversion. For reference types, subtyping
439
     * implies convertibility (the conversion is technically called
440
     * "widening reference conversion"). You can check those cases using:
441
     *
442
     * {@link #bySubtyping() t.isConvertibleTo(s).bySubtyping()}
443
     *
444
     * <p>Unchecked conversion may go backwards from subtyping. For example,
445
     * {@code List<String>} is a subtype of the raw type {@code List}, and
446
     * as such is convertible to it by reference widening. But {@code List}
447
     * may be "coerced" to {@code List<String>} with an unchecked warning:
448
     *
449
     * {@link #withUncheckedWarning() t.isConvertibleTo(s).withUncheckedWarning()}
450
     *
451
     * <p>If the parameterized type only has wildcard type arguments,
452
     * then the conversion produces no warning.
453
     *
454
     * {@link #UNCHECKED_NO_WARNING t.isConvertibleTo(s) == UNCHECKED_NO_WARNING}
455
     *
456
     * <p>Two types may be unconvertible:
457
     *
458
     * {@link #never() t.isConvertibleTo(s).never()}
459
     *
460
     * <p>the negation of which being
461
     *
462
     * {@link #somehow() t.isConvertibleTo(s).somehow()}
463
     *
464
     * <p>Note that this does not check for boxing or unboxing conversions,
465
     * nor for narrowing conversions, which may happen through casts.
466
     */
467
    public enum Convertibility {
1✔
468
        /** T is never implicitly convertible to S. */
469
        NEVER,
1✔
470

471
        /**
472
         * T is not a subtype of S, but every time T is used in a context
473
         * where an S is expected, unchecked conversion converts the T to
474
         * an S with a mandated warning. For example the raw type {@code Class}
475
         * is convertible to {@code Class<String>} with an unchecked warning.
476
         */
477
        UNCHECKED_WARNING,
1✔
478

479
        /**
480
         * {@code T <: |S|} and {@code T </: S}, but S is
481
         * parameterized with only unbounded wildcards. This is a special
482
         * case of unchecked conversion that produces no warning. We keep
483
         * it distinct from subtyping to help some algorithms that require
484
         * subtyping to be a partial order.
485
         *
486
         * <p>For example, {@code List<String>} is a subtype of the raw
487
         * {@code Collection}, not a subtype of {@code Collection<?>},
488
         * but it is still convertible without warning.
489
         */
490
        UNCHECKED_NO_WARNING,
1✔
491

492
        /**
493
         * T is a subtype of S ({@code T <: S}). In particular, any type
494
         * is a subtype of itself ({@code T <: T}).
495
         *
496
         * <p>For example, {@code int} can be widened to {@code long},
497
         * so we consider {@code int <: long}.
498
         */
499
        SUBTYPING;
1✔
500

501
        // public:
502

503
        /** Returns true if this is {@link #NEVER}. */
504
        public boolean never() {
505
            return this == NEVER;
1✔
506
        }
507

508
        /**
509
         * Returns true if this is anything but {@link #NEVER}.
510
         */
511
        public boolean somehow() {
512
            return this != NEVER;
1✔
513
        }
514

515
        /**
516
         * True if this is {@link #SUBTYPING}.
517
         */
518
        public boolean bySubtyping() {
519
            return this == SUBTYPING;
1✔
520
        }
521

522
        /**
523
         * True if this is {@link #UNCHECKED_WARNING}.
524
         */
525
        public boolean withUncheckedWarning() {
526
            return this == UNCHECKED_WARNING;
1✔
527
        }
528

529
        /** True if this is {@link #SUBTYPING} or {@link #UNCHECKED_NO_WARNING}. */
530
        public boolean withoutWarnings() {
531
            return this == SUBTYPING || this == UNCHECKED_NO_WARNING;
1✔
532
        }
533

534
        // package:
535

536

537
        /** Preserves an unchecked warning. */
538
        Convertibility and(Convertibility b) {
539
            return min(this, b);
1✔
540
        }
541

542
        static Convertibility min(Convertibility c1, Convertibility c2) {
543
            return c1.ordinal() < c2.ordinal() ? c1 : c2;
1!
544
        }
545

546
        static Convertibility subtypeIf(boolean b) {
547
            return b ? SUBTYPING : NEVER;
1✔
548
        }
549

550
    }
551

552
    private static JTypeMirror wildUpperBound(JTypeMirror type) {
553
        if (type instanceof JWildcardType) {
1✔
554
            JWildcardType wild = (JWildcardType) type;
1✔
555
            if (wild.isUpperBound()) {
1✔
556
                return wildUpperBound(wild.asUpperBound());
1✔
557
            } else if (wild.asLowerBound() instanceof JTypeVar) {
1!
558
                return ((JTypeVar) wild.asLowerBound()).getUpperBound();
×
559
            }
560
        } else if (type instanceof JTypeVar && ((JTypeVar) type).isCaptured()) {
1✔
561
            // note: tvar.getUpperBound() != tvar.getCapturedOrigin().asUpperBound()
562
            return wildUpperBound(((JTypeVar) type).getUpperBound());
1✔
563
        }
564
        return type;
1✔
565
    }
566

567
    private static JTypeMirror wildLowerBound(JTypeMirror type) {
568
        if (type instanceof JWildcardType) {
1✔
569
            return wildLowerBound(((JWildcardType) type).asLowerBound());
1✔
570
        }
571
        return type;
1✔
572
    }
573

574
    private static JTypeMirror lowerBoundRec(JTypeMirror type) {
575
        if (type instanceof JWildcardType) {
1!
576
            return lowerBoundRec(((JWildcardType) type).asLowerBound());
×
577
        } else if (type instanceof JTypeVar && ((JTypeVar) type).isCaptured()) {
1✔
578
            return lowerBoundRec(((JTypeVar) type).getLowerBound());
1✔
579
        }
580
        return type;
1✔
581
    }
582

583
    private static boolean isTypeRange(JTypeMirror s) {
584
        return s instanceof JWildcardType || isCvar(s);
1!
585
    }
586

587
    private static boolean isCvar(JTypeMirror s) {
588
        return s instanceof JTypeVar && ((JTypeVar) s).isCaptured();
1✔
589
    }
590

591

592
    private static final class SubtypeVisitor implements JTypeVisitor<Convertibility, JTypeMirror> {
593

594
        static final SubtypeVisitor INFERENCE = new SubtypeVisitor(false);
1✔
595
        static final SubtypeVisitor PURE = new SubtypeVisitor(true);
1✔
596
        private final boolean pure;
597

598
        private SubtypeVisitor(boolean pure) {
1✔
599
            this.pure = pure;
1✔
600
        }
1✔
601

602

603
        Convertibility isConvertible(@NonNull JTypeMirror t, @NonNull JTypeMirror s) {
604
            return isConvertible(t, s, false);
1✔
605
        }
606

607
        /**
608
         * Returns whether if {@code T <: S}, ie T is a subtype of S.
609
         *
610
         * <p>Note that {@link TypeSystem#ERROR} and {@link TypeSystem#UNKNOWN}
611
         * are considered subtypes of anything.
612
         *
613
         * @param t A type T
614
         * @param s A type S
615
         */
616
        Convertibility isConvertible(@NonNull JTypeMirror t, @NonNull JTypeMirror s, boolean capture) {
617
            // This is commented out as it makes JTypeMirror#isSubtypeOf partial,
618
            // which is not nice for the API... But this assert caught a bug and
619
            // should probably be enabled.
620
            // assert !(t instanceof JWildcardType || s instanceof JWildcardType) : "Wildcards do not support subtyping";
621

622
            if (t == s) {
1✔
623
                Objects.requireNonNull(t);
1✔
624
                return Convertibility.SUBTYPING;
1✔
625
            } else if (s.isTop()) {
1✔
626
                return Convertibility.subtypeIf(!t.isPrimitive());
1✔
627
            } else if (s.isVoid() || t.isVoid()) { // t != s
1✔
628
                return Convertibility.NEVER;
1✔
629
            } else if (s instanceof InferenceVar) {
1✔
630
                if (!pure) {
1!
631
                    // it's possible to add a bound to UNKNOWN or ERROR
632
                    ((InferenceVar) s).addBound(BoundKind.LOWER, t);
1✔
633
                }
634
                return Convertibility.SUBTYPING;
1✔
635
            } else if (isTypeRange(s)) {
1✔
636
                // If s is a type range L..U,
637
                // then showing t <: s is the same thing as t <: L
638
                JTypeMirror lower = lowerBoundRec(s);
1✔
639
                if (!lower.isBottom()) {
1✔
640
                    return isConvertible(t, lower);
1✔
641
                }
642
                // otherwise fallthrough
643
            } else if (hasUnresolvedSymbol(t)) {
1✔
644
                // This also considers types with an unresolved symbol
645
                // subtypes of (nearly) anything. This allows them to
646
                // pass bound checks on type variables.
647
                if (Objects.equals(t.getSymbol(), s.getSymbol())) {
1✔
648
                    return typeArgsAreContained((JClassType) t, (JClassType) s);
1✔
649
                } else {
650
                    return Convertibility.subtypeIf(s instanceof JClassType); // excludes array or so
1✔
651
                }
652
            } else if (s instanceof JIntersectionType) { // TODO test intersection with tvars & arrays
1✔
653
                // If S is an intersection, then T must conform to *all* bounds of S
654
                // Symmetrically, if T is an intersection, T <: S requires only that
655
                // at least one bound of T is a subtype of S.
656
                return subtypesAll(t, asList(s));
1✔
657
            }
658

659
            if (capture) {
1✔
660
                t = capture(t);
1✔
661
            }
662
            return t.acceptVisitor(this, s);
1✔
663
        }
664

665
        Convertibility subtypesAll(JTypeMirror t, Iterable<? extends JTypeMirror> supers) {
666
            Convertibility result = Convertibility.SUBTYPING;
1✔
667
            for (JTypeMirror ui : supers) {
1✔
668
                Convertibility sub = isConvertible(t, ui);
1✔
669
                if (sub == Convertibility.NEVER) {
1✔
670
                    return Convertibility.NEVER;
1✔
671
                }
672
                result = result.and(sub);
1✔
673
            }
1✔
674
            return result;
1✔
675
        }
676

677
        Convertibility anySubTypesAny(Iterable<? extends JTypeMirror> us, Iterable<? extends JTypeMirror> vs) {
678
            for (JTypeMirror ui : us) {
1✔
679
                for (JTypeMirror vi : vs) {
1✔
680
                    Convertibility sub = isConvertible(ui, vi);
1✔
681
                    if (sub != Convertibility.NEVER) {
1✔
682
                        return sub.and(Convertibility.SUBTYPING); // never return identity here
1✔
683
                    }
684
                }
1✔
685
            }
1✔
686
            return Convertibility.NEVER;
1✔
687
        }
688

689
        /**
690
         * Generalises containment to check if for each i, {@code Ti <= Si}.
691
         */
692
        Convertibility typeArgsAreContained(JClassType t, JClassType s) {
693
            List<JTypeMirror> targs = t.getTypeArgs();
1✔
694
            List<JTypeMirror> sargs = s.getTypeArgs();
1✔
695

696
            if (targs.isEmpty()) {
1✔
697
                if (sargs.isEmpty()) {
1✔
698
                    // Some "erased" non-generic types may appear as the supertypes
699
                    // of raw types, and they're different from the regular flavor
700
                    // as their own supertypes are erased, yet they're not considered
701
                    // raw. To fix the subtyping relation, we say that `C <: (erased) C`
702
                    // but `(erased) C` converts to `C` by unchecked conversion, without
703
                    // warning.
704
                    boolean tRaw = t.hasErasedSuperTypes();
1✔
705
                    boolean sRaw = s.hasErasedSuperTypes();
1✔
706
                    if (tRaw && !sRaw) {
1✔
707
                        return Convertibility.UNCHECKED_NO_WARNING;
1✔
708
                    } else {
709
                        return Convertibility.SUBTYPING;
1✔
710
                    }
711
                }
712
                // for some C, S = C<...> and T = C, ie T is raw
713
                // T is convertible to S, by unchecked conversion.
714
                // If S = D<?, .., ?>, then the conversion produces
715
                // no unchecked warning.
716
                return allArgsAreUnboundedWildcards(sargs) ? Convertibility.UNCHECKED_NO_WARNING
1✔
717
                                                           : Convertibility.UNCHECKED_WARNING;
1✔
718
            } else if (sargs.isEmpty()) {
1✔
719
                // C<T1...TN> <: |C|
720
                // JLS 4.10.2
721
                // unchecked conversion converts a raw type to a generic type
722
                // subtyping converts a generic type to its raw type
723
                return Convertibility.SUBTYPING;
1✔
724
            }
725

726
            if (targs.size() != sargs.size()) {
1✔
727
                // types are not well-formed
728
                return Convertibility.NEVER;
1✔
729
            }
730

731
            Convertibility result = Convertibility.SUBTYPING;
1✔
732
            for (int i = 0; i < targs.size(); i++) {
1✔
733
                Convertibility sub = typeArgContains(sargs.get(i), targs.get(i));
1✔
734
                if (sub == Convertibility.NEVER) {
1✔
735
                    return Convertibility.NEVER;
1✔
736
                }
737
                result = result.and(sub);
1✔
738
            }
739

740
            return result;
1✔
741
        }
742

743
        /**
744
         * Returns true if {@code T <= S}, ie "S contains T".
745
         *
746
         * <p>S contains T if:
747
         *
748
         * <p>{@code L(S) <: L(T) && U(T) <: U(S)}
749
         *
750
         * <p>This only makes sense for type arguments, it's a component of
751
         * subtype checks for parameterized types:
752
         *
753
         * <p>{@code C<S> <: C<T> if S <= T}
754
         *
755
         * <p>Defined in JLS§4.5.1 (Type Arguments of Parameterized Types)
756
         */
757
        Convertibility typeArgContains(JTypeMirror s, JTypeMirror t) {
758
            // the contains relation can be understood intuitively if we
759
            // represent types as ranges on a line:
760

761
            // ⊥ ---------L(S)---L(T)------U(T)-----U(S)---> Object
762
            // range of S   [-------------------------]
763
            // range of T          [---------]
764

765
            // here S contains T because its range is greater
766

767
            // since a wildcard is either "super" or "extends", in reality
768
            // either L(S) = ⊥, or U(S) = Object.
769

770
            // meaning when S != T, we only have two scenarios where T <= S:
771

772
            //      ⊥ -------U(T)-----U(S)------> Object   (L(T) = L(S) = ⊥)
773
            //      ⊥ -------L(S)-----L(T)------> Object   (U(T) = U(S) = Object)
774

775
            if (isSameType(s, t, pure, false)) {
1✔
776
                // S <= S
777
                return Convertibility.SUBTYPING;
1✔
778
            }
779

780
            if (s instanceof JWildcardType) {
1✔
781
                JWildcardType sw = (JWildcardType) s;
1✔
782

783
                // capt(? extends T) <= ? extends T
784
                // capt(? super T) <= ? super T
785
                if (t instanceof JTypeVar && ((JTypeVar) t).isCaptureOf(sw)) {
1✔
786
                    return Convertibility.SUBTYPING;
1✔
787
                }
788

789
                if (sw.isUpperBound()) {
1✔
790
                    // Test U(T) <: U(S),  we already know L(S) <: L(T), because L(S) is bottom
791
                    return this.isConvertible(wildUpperBound(t), sw.asUpperBound());
1✔
792
                } else {
793
                    // Test L(S) <: L(T), we already know U(T) <: U(S), because U(S) is top
794
                    return this.isConvertible(sw.asLowerBound(), wildLowerBound(t));
1✔
795
                }
796
            }
797

798
            return Convertibility.NEVER;
1✔
799
        }
800

801
        @Override
802
        public Convertibility visit(JTypeMirror t, JTypeMirror s) {
803
            throw new IllegalStateException("Should not be called");
×
804
        }
805

806
        @Override
807
        public Convertibility visitTypeVar(JTypeVar t, JTypeMirror s) {
808
            if (s instanceof JTypeVar && t.getSymbol() != null && Objects.equals(t.getSymbol(), s.getSymbol())) {
1✔
809
                return Convertibility.SUBTYPING;
1✔
810
            } else if (s instanceof SentinelType) {
1!
811
                return Convertibility.SUBTYPING;
×
812
            }
813

814
            if (isTypeRange(s)) {
1✔
815
                return isConvertible(t, lowerBoundRec(s));
1✔
816
            }
817
            return isConvertible(t.getUpperBound(), s);
1✔
818
        }
819

820
        @Override
821
        public Convertibility visitNullType(JTypeMirror t, JTypeMirror s) {
822
            return Convertibility.subtypeIf(!s.isPrimitive());
1✔
823
        }
824

825
        @Override
826
        public Convertibility visitSentinel(JTypeMirror t, JTypeMirror s) {
827
            // t may be (*unknown*), (*error*) or void
828
            // we know t != s
829
            if (t.isVoid()) {
1!
830
                return Convertibility.NEVER;
×
831
            }
832
            // unknown and error are subtypes of everything.
833
            // however we want them to add constrains on unknown
834
            if (!pure && !(s instanceof SentinelType)) {
1!
835
                s.acceptVisitor(this, t);
1✔
836
            }
837
            return Convertibility.SUBTYPING;
1✔
838
        }
839

840
        @Override
841
        public Convertibility visitInferenceVar(InferenceVar t, JTypeMirror s) {
842
            if (s == t.getTypeSystem().NULL_TYPE || s instanceof JPrimitiveType) {
1!
843
                return Convertibility.NEVER;
×
844
            }
845
            if (!pure) {
1!
846
                // here we add a constraint on the variable
847
                t.addBound(BoundKind.UPPER, s);
1✔
848
            }
849
            return Convertibility.SUBTYPING;
1✔
850
        }
851

852
        @Override
853
        public Convertibility visitWildcard(JWildcardType t, JTypeMirror s) {
854
            // wildcards should be captured and so we should not end up here
855
            return Convertibility.NEVER;
1✔
856
        }
857

858
        @Override
859
        public Convertibility visitClass(JClassType t, JTypeMirror s) {
860
            if (isSpecialUnresolved(s)) {
1✔
861
                if (!pure) {
1!
862
                    for (JTypeMirror arg : t.getTypeArgs()) {
1✔
863
                        typeArgContains(arg, s);
1✔
864
                    }
1✔
865
                }
866
                return Convertibility.SUBTYPING;
1✔
867
            }
868
            if (!(s instanceof JClassType)) {
1✔
869
                // note, that this ignores wildcard types,
870
                // because they're only compared through
871
                // type argument containment.
872
                return Convertibility.NEVER;
1✔
873
            }
874

875
            JClassType cs = (JClassType) s;
1✔
876

877
            JClassType superDecl = t.getAsSuper(cs.getSymbol());
1✔
878

879
            if (superDecl == null) {
1✔
880
                return Convertibility.NEVER;
1✔
881
            } else if (cs.isRaw()) {
1✔
882
                // a raw type C is a supertype for all the family of parameterized type generated by C<F1, .., Fn>
883
                return Convertibility.SUBTYPING;
1✔
884
            } else {
885
                return typeArgsAreContained(superDecl, cs);
1✔
886
            }
887
        }
888

889
        @Override
890
        public Convertibility visitIntersection(JIntersectionType t, JTypeMirror s) {
891
            // A & B <: A
892
            // A & B <: B
893

894
            // But for a class C, `C <: A & B` if `C <: A` and `C <: B`
895

896
            // So we can't just say, "any component of t must subtype s",
897
            // because if s is itself an intersection we have a problem:
898
            // Eg let T = S = A & B
899
            // T <: S -> A & B <: S
900
            //        -> A <: S OR B <: S
901
            //        -> A <: A & B OR B <: A & B
902
            //        -> A <: A AND A <: B OR B <: A AND B <: B
903
            //        -> true   AND false  OR false  AND true
904
            //        -> false
905

906
            // what we mean is, if S is an intersection, then
907
            // "any component of T subtypes any component of S"
908

909
            return anySubTypesAny(t.getComponents(), asList(s));
1✔
910
        }
911

912
        @Override
913
        public Convertibility visitArray(JArrayType t, JTypeMirror s) {
914
            TypeSystem ts = t.getTypeSystem();
1✔
915
            if (s == ts.OBJECT || s.equals(ts.CLONEABLE) || s.equals(ts.SERIALIZABLE)) {
1!
916
                return Convertibility.SUBTYPING;
1✔
917
            }
918
            if (isSpecialUnresolved(s)) {
1✔
919
                if (!pure) {
1!
920
                    t.getElementType().acceptVisitor(this, s);
1✔
921
                }
922
                return Convertibility.SUBTYPING;
1✔
923
            }
924

925
            if (!(s instanceof JArrayType)) {
1✔
926
                // not comparable to any other type
927
                return Convertibility.NEVER;
1✔
928
            }
929

930
            JArrayType cs = (JArrayType) s;
1✔
931

932
            if (t.getComponentType().isPrimitive() || cs.getComponentType().isPrimitive()) {
1✔
933
                // arrays of primitive types have no sub-/ supertype
934
                return Convertibility.subtypeIf(cs.getComponentType() == t.getComponentType());
1✔
935
            } else {
936
                return isConvertible(t.getComponentType(), cs.getComponentType());
1✔
937
            }
938
        }
939

940
        @Override
941
        public Convertibility visitPrimitive(JPrimitiveType t, JTypeMirror s) {
942
            if (s instanceof JPrimitiveType) {
1✔
943
                return t.superTypes.contains(s) ? Convertibility.SUBTYPING
1✔
944
                                                : Convertibility.NEVER;
1✔
945
            }
946
            return Convertibility.NEVER;
1✔
947
        }
948
    }
949

950
    public static boolean isStrictSubtype(@NonNull JTypeMirror t, @NonNull JTypeMirror s) {
951
        return !t.equals(s) && t.isSubtypeOf(s);
1!
952
    }
953

954
    // </editor-fold>
955

956
    // <editor-fold  defaultstate="collapsed" desc="Substitution">
957

958
    /**
959
     * Replace the type variables occurring in the given type to their
960
     * image by the given function. Substitutions are not applied
961
     * recursively.
962
     *
963
     * @param type  Type to substitute
964
     * @param subst Substitution function, eg a {@link Substitution}
965
     */
966
    public static JTypeMirror subst(@Nullable JTypeMirror type, Function<? super SubstVar, ? extends @NonNull JTypeMirror> subst) {
967
        if (type == null || Substitution.isEmptySubst(subst)) {
1!
968
            return type;
1✔
969
        }
970
        return type.subst(subst);
1✔
971
    }
972

973

974
    /** Substitute on a list of types. */
975
    public static List<JTypeMirror> subst(List<? extends JTypeMirror> ts, Function<? super SubstVar, ? extends @NonNull JTypeMirror> subst) {
976
        if (Substitution.isEmptySubst(subst)) {
1✔
977
            return CollectionUtil.makeUnmodifiableAndNonNull(ts);
1✔
978
        }
979
        return mapPreservingSelf(ts, t -> t.subst(subst));
1✔
980
    }
981

982
    public static List<JClassType> substClasses(List<JClassType> ts, Function<? super SubstVar, ? extends @NonNull JTypeMirror> subst) {
983
        if (Substitution.isEmptySubst(subst)) {
1✔
984
            return ts;
1✔
985
        }
986
        return mapPreservingSelf(ts, t -> t.subst(subst));
1✔
987
    }
988

989
    public static List<JTypeVar> substInBoundsOnly(List<JTypeVar> ts, Function<? super SubstVar, ? extends @NonNull JTypeMirror> subst) {
990
        if (Substitution.isEmptySubst(subst)) {
1✔
991
            return ts;
1✔
992
        }
993
        return mapPreservingSelf(ts, t -> t.substInBounds(subst));
1✔
994
    }
995

996
    // relies on the fact the original list is unmodifiable or won't be
997
    // modified
998
    @SuppressWarnings("unchecked")
999
    private static @NonNull <T> List<T> mapPreservingSelf(List<? extends T> ts, Function<? super T, ? extends @NonNull T> subst) {
1000
        // Profiling shows, only 10% of calls to this method need to
1001
        // create a new list. Substitution in general is a hot spot
1002
        // of the framework, so optimizing this out is nice
1003
        List<T> list = null;
1✔
1004
        for (int i = 0, size = ts.size(); i < size; i++) {
1✔
1005
            T it = ts.get(i);
1✔
1006
            T substed = subst.apply(it);
1✔
1007
            if (substed != it) {
1✔
1008
                if (list == null) {
1✔
1009
                    list = Arrays.asList((T[]) ts.toArray()); // NOPMD ClassCastExceptionWithToArray
1✔
1010
                }
1011
                list.set(i, substed);
1✔
1012
            }
1013
        }
1014

1015
        // subst relies on the fact that the original list is returned
1016
        // to avoid new type creation. Thus one cannot use
1017
        // Collections::unmodifiableList here
1018
        return list != null ? list : (List<T>) ts;
1✔
1019
    }
1020

1021
    // </editor-fold>
1022

1023
    // <editor-fold  defaultstate="collapsed" desc="Projection">
1024

1025

1026
    /**
1027
     * Returns the upwards projection of the given type, with respect
1028
     * to the set of capture variables that are found in it. This is
1029
     * some supertype of T which does not mention those capture variables.
1030
     * This is used for local variable type inference.
1031
     *
1032
     * https://docs.oracle.com/javase/specs/jls/se11/html/jls-4.html#jls-4.10.5
1033
     */
1034
    public static JTypeMirror projectUpwards(JTypeMirror t) {
1035
        return t.acceptVisitor(UPWARDS_PROJECTOR, new RecursionStop());
1✔
1036
    }
1037

1038
    private static final JTypeMirror NO_DOWN_PROJECTION = null;
1✔
1039
    private static final ProjectionVisitor UPWARDS_PROJECTOR = new ProjectionVisitor(true) {
1✔
1040

1041
        @Override
1042
        public JTypeMirror visitTypeVar(JTypeVar t, RecursionStop recursionStop) {
1043
            if (t.isCaptured()) {
1✔
1044
                return t.getUpperBound().acceptVisitor(UPWARDS_PROJECTOR, recursionStop);
1✔
1045
            }
1046
            return t;
1✔
1047
        }
1048

1049

1050
        @Override
1051
        public JTypeMirror visitWildcard(JWildcardType t, RecursionStop recursionStop) {
1052
            JTypeMirror u = t.getBound().acceptVisitor(UPWARDS_PROJECTOR, recursionStop);
1✔
1053
            TypeSystem ts = t.getTypeSystem();
1✔
1054
            if (u == t.getBound()) {
1✔
1055
                return t;
1✔
1056
            }
1057

1058
            if (t.isUpperBound()) {
1!
1059
                return ts.wildcard(true, u);
×
1060
            } else {
1061
                JTypeMirror down = t.getBound().acceptVisitor(DOWNWARDS_PROJECTOR, recursionStop);
1✔
1062
                return down == NO_DOWN_PROJECTION ? ts.UNBOUNDED_WILD : ts.wildcard(false, down);
1!
1063
            }
1064
        }
1065

1066

1067
        @Override
1068
        public JTypeMirror visitNullType(JTypeMirror t, RecursionStop recursionStop) {
1069
            return t;
×
1070
        }
1071

1072
    };
1073

1074

1075
    private static final ProjectionVisitor DOWNWARDS_PROJECTOR = new ProjectionVisitor(false) {
1✔
1076

1077
        @Override
1078
        public JTypeMirror visitWildcard(JWildcardType t, RecursionStop recursionStop) {
1079
            JTypeMirror u = t.getBound().acceptVisitor(UPWARDS_PROJECTOR, recursionStop);
×
1080
            if (u == t.getBound()) {
×
1081
                return t;
×
1082
            }
1083
            TypeSystem ts = t.getTypeSystem();
×
1084

1085
            if (t.isUpperBound()) {
×
1086
                JTypeMirror down = t.getBound().acceptVisitor(DOWNWARDS_PROJECTOR, recursionStop);
×
1087
                return down == NO_DOWN_PROJECTION ? NO_DOWN_PROJECTION
×
1088
                                                  : ts.wildcard(true, down);
×
1089
            } else {
1090
                return ts.wildcard(false, u);
×
1091
            }
1092
        }
1093

1094

1095
        @Override
1096
        public JTypeMirror visitTypeVar(JTypeVar t, RecursionStop recursionStop) {
1097
            if (t.isCaptured()) {
1!
1098
                return t.getLowerBound().acceptVisitor(DOWNWARDS_PROJECTOR, recursionStop);
1✔
1099
            }
1100
            return t;
×
1101
        }
1102

1103
        @Override
1104
        public JTypeMirror visitNullType(JTypeMirror t, RecursionStop recursionStop) {
1105
            return NO_DOWN_PROJECTION;
1✔
1106
        }
1107
    };
1108

1109
    static final class RecursionStop {
1✔
1110

1111
        private Set<JTypeVar> set;
1112

1113
        boolean isAbsent(JTypeVar tvar) {
1114
            if (set == null) {
1✔
1115
                set = new LinkedHashSet<>(1);
1✔
1116
            }
1117
            return set.add(tvar);
1✔
1118
        }
1119

1120
        <T extends JTypeMirror> JTypeMirror recurseIfNotDone(T t, BiFunction<T, RecursionStop, JTypeMirror> body) {
1121
            if (t instanceof JTypeVar) {
1✔
1122
                JTypeVar var = (JTypeVar) t;
1✔
1123
                if (isAbsent(var)) {
1✔
1124
                    return body.apply(t, this);
1✔
1125
                } else {
1126
                    return t;
1✔
1127
                }
1128
            } else {
1129
                return body.apply(t, this);
1✔
1130
            }
1131
        }
1132
    }
1133

1134
    /**
1135
     * Restricted type variables are:
1136
     * - Inference vars
1137
     * - Capture vars
1138
     *
1139
     * See
1140
     *
1141
     * https://docs.oracle.com/javase/specs/jls/se11/html/jls-4.html#jls-4.10.5
1142
     *
1143
     *
1144
     * <p>Here we use {@link #NO_DOWN_PROJECTION} as a sentinel
1145
     * (downwards projection is a partial function). If a type does not mention
1146
     * restricted type variables, then the visitor should return the original
1147
     * type (same reference). This allows testing predicates like
1148
     * <blockquote>
1149
     * "If Ai does not mention any restricted type variable, then Ai' = Ai."
1150
     * </blockquote>
1151
     */
1152
    private abstract static class ProjectionVisitor implements JTypeVisitor<JTypeMirror, RecursionStop> {
1153

1154
        private final boolean upwards;
1155

1156
        private ProjectionVisitor(boolean upwards) {
1✔
1157
            this.upwards = upwards;
1✔
1158
        }
1✔
1159

1160

1161
        @Override
1162
        public abstract JTypeMirror visitNullType(JTypeMirror t, RecursionStop recursionStop);
1163

1164

1165
        @Override
1166
        public abstract JTypeMirror visitWildcard(JWildcardType t, RecursionStop recursionStop);
1167

1168

1169
        @Override
1170
        public abstract JTypeMirror visitTypeVar(JTypeVar t, RecursionStop recursionStop);
1171

1172

1173
        @Override
1174
        public JTypeMirror visit(JTypeMirror t, RecursionStop recursionStop) {
1175
            return t;
1✔
1176
        }
1177

1178
        @Override
1179
        public JTypeMirror visitClass(JClassType t, RecursionStop recursionStop) {
1180
            if (t.isParameterizedType()) {
1✔
1181
                TypeSystem ts = t.getTypeSystem();
1✔
1182

1183
                List<JTypeMirror> targs = t.getTypeArgs();
1✔
1184
                List<JTypeMirror> newTargs = new ArrayList<>(targs.size());
1✔
1185
                List<JTypeVar> formals = t.getFormalTypeParams();
1✔
1186
                boolean change = false;
1✔
1187

1188
                for (int i = 0; i < targs.size(); i++) {
1✔
1189
                    JTypeMirror ai = targs.get(i);
1✔
1190
                    JTypeMirror u = recursionStop.recurseIfNotDone(ai, (s, stop) -> s.acceptVisitor(this, stop));
1✔
1191
                    if (u == ai) {
1✔
1192
                        if (isCvar(ai)) { // cvar hit recursion stop
1✔
1193
                            u = ts.UNBOUNDED_WILD;
1✔
1194
                            change = true;
1✔
1195
                        }
1196
                        // no change, or handled by the visitWildcard
1197
                        newTargs.add(u);
1✔
1198
                        continue;
1✔
1199
                    } else if (!upwards) {
1!
1200
                        // If Ai is a type that mentions a restricted type variable, then Ai' is undefined.
1201
                        return NO_DOWN_PROJECTION;
×
1202
                    } else if (u instanceof JWildcardType) {
1✔
1203
                        // The rest of this function, below, treats u as the bound of a wildcard,
1204
                        // but if u is already a wildcard (and therefore ai was a wildcard), we
1205
                        // are already done.
1206
                        newTargs.add(u);
1✔
1207
                        change = true;
1✔
1208
                        continue;
1✔
1209
                    }
1210

1211
                    change = true;
1✔
1212

1213
                    /*
1214
                        If Ai is a type that mentions a restricted type variable...
1215
                     */
1216
                    JTypeMirror bi = formals.get(i).getUpperBound();
1✔
1217

1218
                    if (u != ts.OBJECT && (mentionsAny(bi, formals) || !bi.isSubtypeOf(u))) {
1!
1219
                        newTargs.add(ts.wildcard(true, u));
1✔
1220
                    } else {
1221
                        JTypeMirror down = ai.acceptVisitor(DOWNWARDS_PROJECTOR, recursionStop);
1✔
1222
                        if (down == NO_DOWN_PROJECTION) {
1!
1223
                            newTargs.add(ts.UNBOUNDED_WILD);
1✔
1224
                        } else {
1225
                            newTargs.add(ts.wildcard(false, down));
×
1226
                        }
1227
                    }
1228
                }
1229

1230
                return change ? t.withTypeArguments(newTargs) : t;
1✔
1231
            } else {
1232
                return t;
1✔
1233
            }
1234
        }
1235

1236
        @Override
1237
        public JTypeMirror visitIntersection(JIntersectionType t, RecursionStop recursionStop) {
1238
            List<JTypeMirror> comps = new ArrayList<>(t.getComponents());
1✔
1239
            boolean change = false;
1✔
1240
            for (int i = 0; i < comps.size(); i++) {
1✔
1241
                JTypeMirror ci = comps.get(i);
1✔
1242
                JTypeMirror proj = ci.acceptVisitor(this, recursionStop);
1✔
1243
                if (proj == NO_DOWN_PROJECTION) {
1!
1244
                    return NO_DOWN_PROJECTION;
×
1245
                } else {
1246
                    comps.set(i, proj);
1✔
1247
                    if (ci != proj) {
1!
1248
                        change = true;
×
1249
                    }
1250
                }
1251
            }
1252
            return change ? t.getTypeSystem().glb(comps) : t;
1!
1253
        }
1254

1255
        @Override
1256
        public JTypeMirror visitArray(JArrayType t, RecursionStop recursionStop) {
1257
            JTypeMirror comp2 = t.getComponentType().acceptVisitor(this, recursionStop);
1✔
1258
            return comp2 == NO_DOWN_PROJECTION
1!
1259
                   ? NO_DOWN_PROJECTION
×
1260
                   : comp2 == t.getComponentType()
1!
1261
                     ? t : t.getTypeSystem().arrayType(comp2);
1✔
1262
        }
1263

1264
        @Override
1265
        public JTypeMirror visitSentinel(JTypeMirror t, RecursionStop recursionStop) {
1266
            return t;
1✔
1267
        }
1268
    }
1269

1270
    // </editor-fold>
1271

1272
    // <editor-fold  defaultstate="collapsed" desc="Overriding">
1273

1274
    /**
1275
     * Returns true if m1 is return-type substitutable with m2. The notion of return-type-substitutability
1276
     * supports covariant returns, that is, the specialization of the return type to a subtype.
1277
     *
1278
     * https://docs.oracle.com/javase/specs/jls/se9/html/jls-8.html#jls-8.4.5
1279
     */
1280
    public static boolean isReturnTypeSubstitutable(JMethodSig m1, JMethodSig m2) {
1281

1282
        JTypeMirror r1 = m1.getReturnType();
1✔
1283
        JTypeMirror r2 = m2.getReturnType();
1✔
1284

1285
        if (r1 == r1.getTypeSystem().NO_TYPE) {
1✔
1286
            return r1 == r2;
1!
1287
        }
1288

1289
        if (r1.isPrimitive()) {
1✔
1290
            return r1 == r2;
1!
1291
        }
1292

1293
        JMethodSig m1Prime = adaptForTypeParameters(m1, m2);
1✔
1294
        return m1Prime != null && isConvertible(m1Prime.getReturnType(), r2) != Convertibility.NEVER
1!
1295
                || !haveSameSignature(m1, m2) && isSameType(r1, r2.getErasure());
1!
1296
    }
1297

1298
    /**
1299
     * Adapt m1 to the type parameters of m2. Returns null if that's not possible.
1300
     *
1301
     * https://docs.oracle.com/javase/specs/jls/se9/html/jls-8.html#jls-8.4.4
1302
     *
1303
     * <p>Note that the type parameters of m1 are not replaced, only
1304
     * their occurrences in the rest of the signature.
1305
     */
1306
    static @Nullable JMethodSig adaptForTypeParameters(JMethodSig m1, JMethodSig m2) {
1307
        if (haveSameTypeParams(m1, m2)) {
1!
1308
            return m1.subst(mapping(m1.getTypeParameters(), m2.getTypeParameters()));
1✔
1309
        }
1310

1311
        return null;
×
1312
    }
1313

1314
    public static boolean haveSameTypeParams(JMethodSig m1, JMethodSig m2) {
1315
        List<JTypeVar> tp1 = m1.getTypeParameters();
1✔
1316
        List<JTypeVar> tp2 = m2.getTypeParameters();
1✔
1317
        if (tp1.size() != tp2.size()) {
1✔
1318
            return false;
1✔
1319
        }
1320

1321
        if (tp1.isEmpty()) {
1✔
1322
            return true;
1✔
1323
        }
1324

1325
        Substitution mapping = mapping(tp2, tp1);
1✔
1326
        for (int i = 0; i < tp1.size(); i++) {
1✔
1327
            JTypeVar p1 = tp1.get(i);
1✔
1328
            JTypeVar p2 = tp2.get(i);
1✔
1329

1330
            if (!isSameType(p1.getUpperBound(), subst(p2.getUpperBound(), mapping))) {
1✔
1331
                return false;
1✔
1332
            }
1333
        }
1334

1335
        return true;
1✔
1336
    }
1337

1338
    /**
1339
     * Two method signatures m1 and m2 are override-equivalent iff either
1340
     * m1 is a subsignature of m2 or m2 is a subsignature of m1. This does
1341
     * not look at the origin of the methods (their declaring class).
1342
     *
1343
     * <p>This is a prerequisite for one method to override the other,
1344
     * but not the only condition. See {@link #overrides(JMethodSig, JMethodSig, JTypeMirror)}.
1345
     *
1346
     * See <a href="https://docs.oracle.com/javase/specs/jls/se9/html/jls-8.html#jls-8.4.2">JLS§8</a>
1347
     */
1348
    public static boolean areOverrideEquivalent(JMethodSig m1, JMethodSig m2) {
1349
        // This method is a very hot spot as it is used to prune shadowed/overridden/hidden
1350
        // methods from overload candidates before overload resolution.
1351
        // Any optimization makes a big impact.
1352
        if (m1.getArity() != m2.getArity()) {
1✔
1353
            return false; // easy case
1✔
1354
        } else if (m1 == m2) {
1!
1355
            return true;
×
1356
        } else if (!m1.getName().equals(m2.getName())) {
1!
1357
            // note: most call sites statically know this is true
1358
            // profile to figure out whether this matters
1359
            return false;
×
1360
        }
1361

1362
        List<JTypeMirror> formals1 = m1.getFormalParameters();
1✔
1363
        List<JTypeMirror> formals2 = m2.getFormalParameters();
1✔
1364
        for (int i = 0; i < formals1.size(); i++) {
1✔
1365
            JTypeMirror fi1 = formals1.get(i);
1✔
1366
            JTypeMirror fi2 = formals2.get(i);
1✔
1367

1368
            if (!isSameType(fi1.getErasure(), fi2.getErasure())) {
1✔
1369
                return false;
1✔
1370
            }
1371
        }
1372

1373
        // a non-generic method may override a generic one
1374
        return !m1.isGeneric() || !m2.isGeneric()
1!
1375
            // if both are generic, they must have the same type params
1376
            || haveSameTypeParams(m1, m2);
1✔
1377
    }
1378

1379
    /**
1380
     * The signature of a method m1 is a subsignature of the signature of a method m2 if either:
1381
     * - m2 has the same signature as m1, or
1382
     * - the signature of m1 is the same as the erasure (§4.6) of the signature of m2.
1383
     */
1384
    public static boolean isSubSignature(JMethodSig m1, JMethodSig m2) {
1385
        // prune easy cases
1386
        if (m1.getArity() != m2.getArity() || !m1.getName().equals(m2.getName())) {
1✔
1387
            return false;
1✔
1388
        }
1389
        boolean m1Gen = m1.isGeneric();
1✔
1390
        boolean m2Gen = m2.isGeneric();
1✔
1391
        if (m1Gen ^ m2Gen) {
1✔
1392
            if (m1Gen) {
1!
1393
                return false; // this test is asymmetric
×
1394
            } else {
1395
                m2 = m2.getErasure();
1✔
1396
            }
1397
        }
1398
        return haveSameSignature(m1, m2);
1✔
1399
    }
1400

1401
    /**
1402
     * Two methods or constructors, M and N, have the same signature if
1403
     * they have the same name, the same type parameters (if any) (§8.4.4),
1404
     * and, after adapting the formal parameter types of N to the the type
1405
     * parameters of M, the same formal parameter types.
1406
     *
1407
     * Thrown exceptions are not part of the signature of a method.
1408
     */
1409
    private static boolean haveSameSignature(JMethodSig m1, JMethodSig m2) {
1410
        return m1.getName().equals(m2.getName())
1✔
1411
                && m1.getArity() == m2.getArity()
1✔
1412
                && haveSameTypeParams(m1, m2)
1✔
1413
                && areSameTypes(m1.getFormalParameters(),
1✔
1414
                            m2.getFormalParameters(),
1✔
1415
                            Substitution.mapping(m2.getTypeParameters(), m1.getTypeParameters()));
1✔
1416
    }
1417

1418
    /**
1419
     * Returns true if m1 overrides m2, when both are view as members of
1420
     * class origin. m1 and m2 may be declared in supertypes of origin,
1421
     * possibly unrelated (default methods), which is why we need that
1422
     * third parameter. By convention a method overrides itself.
1423
     *
1424
     * <p>This method ignores the static modifier. If both methods are
1425
     * static, then this method tests for <i>hiding</i>. Otherwise, this
1426
     * method properly tests for overriding. Note that it is an error for
1427
     * a static method to override an instance method, or the reverse.
1428
     */
1429
    public static boolean overrides(JMethodSig m1, JMethodSig m2, JTypeMirror origin) {
1430

1431
        if (m1.isConstructor() || m2.isConstructor()) {
1!
1432
            return m1.equals(m2); // "by convention a method overrides itself"
1✔
1433
        }
1434

1435
        JTypeMirror m1Owner = m1.getDeclaringType();
1✔
1436
        JClassType m2Owner = (JClassType) m2.getDeclaringType();
1✔
1437

1438
        if (isOverridableIn(m2, m1Owner.getSymbol())) {
1✔
1439
            JClassType m2AsM1Supertype = (JClassType) m1Owner.getAsSuper(m2Owner.getSymbol());
1✔
1440
            if (m2AsM1Supertype != null) {
1✔
1441
                JMethodSig m2Prime = m2AsM1Supertype.getDeclaredMethod(m2.getSymbol());
1✔
1442
                assert m2Prime != null;
1!
1443
                if (isSubSignature(m1, m2Prime)) {
1✔
1444
                    return true;
1✔
1445
                }
1446
            }
1447
        }
1448

1449
        // todo that is very weird
1450
        if (m1.isAbstract()
1✔
1451
            || !m2.isAbstract() && !m2.getSymbol().isDefaultMethod()
1!
1452
            || !isOverridableIn(m2, origin.getSymbol())
1!
1453
            || !(m1Owner instanceof JClassType)) {
1454
            return false;
1✔
1455
        }
1456

1457
        JTypeMirror m1AsSuper = origin.getAsSuper(((JClassType) m1Owner).getSymbol());
1✔
1458
        JTypeMirror m2AsSuper = origin.getAsSuper(m2Owner.getSymbol());
1✔
1459
        if (m1AsSuper instanceof JClassType && m2AsSuper instanceof JClassType) {
1!
1460
            m1 = ((JClassType) m1AsSuper).getDeclaredMethod(m1.getSymbol());
1✔
1461
            m2 = ((JClassType) m2AsSuper).getDeclaredMethod(m2.getSymbol());
1✔
1462
            assert m1 != null && m2 != null;
1!
1463
            return isSubSignature(m1, m2);
1✔
1464
        }
1465
        return false;
1✔
1466
    }
1467

1468
    /**
1469
     * Returns true if both methods override the same method of a common supertype.
1470
     */
1471
    public static boolean overrideSameMethod(JMethodSig m1, JMethodSig m2) {
1472
        if (Objects.equals(m1.getSymbol(), m2.getSymbol())) {
1✔
1473
            return true;
1✔
1474
        }
1475
        if (!haveSameSignature(m1, m2)) {
1✔
1476
            return false;
1✔
1477
        }
1478
        JTypeMirror declaringType1 = m1.getDeclaringType();
1✔
1479
        JTypeMirror declaringType2 = m2.getDeclaringType();
1✔
1480
        return declaringType2.getSuperTypeSet().stream().anyMatch(st ->
1✔
1481
            TypeTestUtil.isA(st, declaringType1) && st.streamDeclaredMethods(
1✔
1482
                method -> method.nameEquals(m1.getName())
1✔
1483
                    && method.getFormalParameters().equals(m1.getSymbol().getFormalParameters())
1✔
1484
            ).findAny().isPresent()
1✔
1485
        );
1486
    }
1487

1488
    private static boolean isOverridableIn(JMethodSig m, JTypeDeclSymbol origin) {
1489
        return isOverridableIn(m.getSymbol(), origin);
1✔
1490
    }
1491

1492
    /**
1493
     * Returns true if the given method can be overridden in the origin
1494
     * class. This only checks access modifiers and not eg whether the
1495
     * method is final or static. Regardless of whether the method is
1496
     * final it is overridden - whether this is a compile error or not
1497
     * is another matter.
1498
     *
1499
     * <p>Like {@link #overrides(JMethodSig, JMethodSig, JTypeMirror)},
1500
     * this does not check the static modifier, and tests for hiding
1501
     * if the method is static.
1502
     *
1503
     * @param m      Method to test
1504
     * @param origin Site of the potential override
1505
     */
1506
    public static boolean isOverridableIn(JExecutableSymbol m, JTypeDeclSymbol origin) {
1507
        if (m instanceof JConstructorSymbol) {
1!
1508
            return false;
×
1509
        }
1510

1511
        final int accessFlags = Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE;
1✔
1512

1513
        // JLS 8.4.6.1
1514
        switch (m.getModifiers() & accessFlags) {
1✔
1515
        case Modifier.PUBLIC:
1516
            return true;
1✔
1517
        case Modifier.PROTECTED:
1518
            return !origin.isInterface();
1✔
1519
        case 0:
1520
            // package private
1521
            return
1✔
1522
                m.getPackageName().equals(origin.getPackageName())
1✔
1523
                    && !origin.isInterface();
1!
1524
        default:
1525
            // private
1526
            return false;
1✔
1527
        }
1528
    }
1529

1530
    // </editor-fold>
1531

1532
    // <editor-fold  defaultstate="collapsed" desc="SAM types">
1533

1534
    /*
1535
     * Function types of SAM (single-abstract-method) types.
1536
     *
1537
     * See https://docs.oracle.com/javase/specs/jls/se11/html/jls-9.html#jls-9.9
1538
     */
1539

1540

1541
    /**
1542
     * Returns the non-wildcard parameterization of the given functional
1543
     * interface type. Returns null if such a parameterization does not
1544
     * exist.
1545
     *
1546
     * <p>This is used to remove wildcards from the type of a functional
1547
     * interface.
1548
     *
1549
     * https://docs.oracle.com/javase/specs/jls/se9/html/jls-9.html#jls-9.9
1550
     *
1551
     * @param type A parameterized functional interface type
1552
     */
1553
    public static @Nullable JClassType nonWildcardParameterization(@NonNull JClassType type) {
1554
        TypeSystem ts = type.getTypeSystem();
1✔
1555

1556
        List<JTypeMirror> targs = type.getTypeArgs();
1✔
1557
        if (targs.stream().noneMatch(it -> it instanceof JWildcardType)) {
1✔
1558
            return type;
1✔
1559
        }
1560

1561
        List<JTypeVar> tparams = type.getFormalTypeParams();
1✔
1562
        List<JTypeMirror> newArgs = new ArrayList<>();
1✔
1563

1564
        for (int i = 0; i < tparams.size(); i++) {
1✔
1565
            JTypeMirror ai = targs.get(i);
1✔
1566
            if (ai instanceof JWildcardType) {
1✔
1567
                JTypeVar pi = tparams.get(i);
1✔
1568
                JTypeMirror bi = pi.getUpperBound();
1✔
1569
                if (mentionsAny(bi, new HashSet<>(tparams))) {
1!
1570
                    return null;
×
1571
                }
1572

1573
                JWildcardType ai2 = (JWildcardType) ai;
1✔
1574

1575
                if (ai2.isUnbounded()) {
1✔
1576
                    newArgs.add(bi);
1✔
1577
                } else if (ai2.isUpperBound()) {
1✔
1578
                    newArgs.add(ts.glb(Arrays.asList(ai2.asUpperBound(), bi)));
1✔
1579
                } else { // lower bound
1580
                    newArgs.add(ai2.asLowerBound());
1✔
1581
                }
1582

1583
            } else {
1✔
1584
                newArgs.add(ai);
1✔
1585
            }
1586

1587
        }
1588

1589
        return type.withTypeArguments(newArgs);
1✔
1590
    }
1591

1592
    /**
1593
     * Finds the method of the given type that can be overridden as a lambda
1594
     * expression. That is more complicated than "the unique abstract method",
1595
     * it's actually a function type which can override all abstract methods
1596
     * of the SAM at once.
1597
     *
1598
     * https://docs.oracle.com/javase/specs/jls/se9/html/jls-9.html#jls-9.9
1599
     *
1600
     * <p>If the parameter is not mappable to a class type with {@link #asClassType(JTypeMirror)},
1601
     * or if the functional method does not exist, returns null.
1602
     */
1603
    public static @Nullable JMethodSig findFunctionalInterfaceMethod(@Nullable JTypeMirror type) {
1604
        JClassType candidateSam = asClassType(type);
1✔
1605
        if (candidateSam == null) {
1✔
1606
            return null;
1✔
1607
        }
1608

1609
        if (candidateSam.isParameterizedType()) {
1✔
1610
            return findFunctionTypeImpl(nonWildcardParameterization(candidateSam));
1✔
1611
        } else if (candidateSam.isRaw()) {
1✔
1612
            //  The function type of the raw type of a generic functional
1613
            //  interface I<...> is the erasure of the function type of the generic functional interface I<...>.
1614
            JMethodSig fun = findFunctionTypeImpl(candidateSam.getGenericTypeDeclaration());
1✔
1615
            return fun == null ? null : fun.getErasure();
1!
1616
        } else {
1617
            return findFunctionTypeImpl(candidateSam);
1✔
1618
        }
1619
    }
1620

1621
    /**
1622
     * Returns t if it is a class or interface type. If it is an intersection type,
1623
     * returns the induced class or interface type. Returns null otherwise, including
1624
     * if the parameter is null.
1625
     */
1626
    public static @Nullable JClassType asClassType(@Nullable JTypeMirror t) {
1627
        if (t instanceof JClassType) {
1✔
1628
            return (JClassType) t;
1✔
1629
        } else if (t instanceof JIntersectionType) {
1✔
1630
            return ((JIntersectionType) t).getInducedClassType();
1✔
1631
        }
1632
        return null;
1✔
1633
    }
1634

1635
    private static @Nullable JMethodSig findFunctionTypeImpl(@Nullable JClassType candidateSam) {
1636

1637
        if (candidateSam == null || !candidateSam.isInterface() || candidateSam.getSymbol().isAnnotation()) {
1!
1638
            return null;
1✔
1639
        }
1640

1641
        Map<String, List<JMethodSig>> relevantMethods = candidateSam.streamMethods(it -> !Modifier.isStatic(it.getModifiers()))
1✔
1642
                                                                    .filter(TypeOps::isNotDeclaredInClassObject)
1✔
1643
                                                                    .collect(Collectors.groupingBy(JMethodSig::getName, OverloadSet.collectMostSpecific(candidateSam)));
1✔
1644

1645

1646
        List<JMethodSig> candidates = new ArrayList<>();
1✔
1647
        for (Entry<String, List<JMethodSig>> entry : relevantMethods.entrySet()) {
1✔
1648
            for (JMethodSig sig : entry.getValue()) {
1✔
1649
                if (sig.isAbstract()) {
1✔
1650
                    candidates.add(sig);
1✔
1651
                }
1652
            }
1✔
1653
        }
1✔
1654

1655
        if (candidates.isEmpty()) {
1✔
1656
            return null;
1✔
1657
        } else if (candidates.size() == 1) {
1✔
1658
            return candidates.get(0);
1✔
1659
        }
1660

1661
        JMethodSig currentBest = null;
1✔
1662

1663
        nextCandidate:
1664
        for (int i = 0; i < candidates.size(); i++) {
1✔
1665
            JMethodSig cand = candidates.get(i);
1✔
1666

1667
            for (JMethodSig other : candidates) {
1!
1668
                if (!isSubSignature(cand, other)
1✔
1669
                    || !isReturnTypeSubstitutable(cand, other)) {
1!
1670
                    continue nextCandidate;
×
1671
                }
1672
            }
1✔
1673

1674
            if (currentBest == null) {
×
1675
                currentBest = cand;
×
1676
            } else if (cand.getReturnType().isSubtypeOf(currentBest.getReturnType())) {
×
1677
                // select the most specific return type
1678
                currentBest = cand;
×
1679
            }
1680
        }
1681

1682
        return currentBest;
1✔
1683
    }
1684

1685
    private static boolean isNotDeclaredInClassObject(JMethodSig it) {
1686
        TypeSystem ts = it.getDeclaringType().getTypeSystem();
1✔
1687
        return ts.OBJECT.streamDeclaredMethods(om -> Modifier.isPublic(om.getModifiers())
1✔
1688
            && om.nameEquals(it.getName()))
1✔
1689
                        .noneMatch(om -> haveSameSignature(it, om));
1✔
1690
    }
1691

1692
    // </editor-fold>
1693

1694
    // <editor-fold  defaultstate="collapsed" desc="As super">
1695

1696
    /**
1697
     * @see JTypeMirror#getAsSuper(JClassSymbol)
1698
     */
1699
    public static @Nullable JTypeMirror asSuper(@NonNull JTypeMirror t, @NonNull JClassSymbol s) {
1700

1701
        if (!t.isPrimitive() && s.equals(t.getTypeSystem().OBJECT.getSymbol())) {
1!
1702
            // interface types need to have OBJECT somewhere up their hierarchy
1703
            return t.getTypeSystem().OBJECT;
1✔
1704
        }
1705

1706
        return t.acceptVisitor(AsSuperVisitor.INSTANCE, s);
1✔
1707
    }
1708

1709
    /**
1710
     * Return the base type of t or any of its outer types that starts
1711
     * with the given type.  If none exists, return null.
1712
     */
1713
    public static @Nullable JClassType asOuterSuper(JTypeMirror t, JClassSymbol sym) {
1714
        if (t instanceof JClassType) {
×
1715
            JClassType ct = (JClassType) t;
×
1716
            do {
1717
                JClassType sup = ct.getAsSuper(sym);
×
1718
                if (sup != null) {
×
1719
                    return sup;
×
1720
                }
1721
                ct = ct.getEnclosingType();
×
1722
            } while (ct != null);
×
1723
        } else if (t instanceof JTypeVar || t instanceof JArrayType) {
×
1724
            return (JClassType) t.getAsSuper(sym);
×
1725
        }
1726
        return null;
×
1727
    }
1728

1729
    /**
1730
     * Return the first enclosing type of the container type
1731
     * that has the given symbol in its supertypes. Return null
1732
     * if this is not found.
1733
     */
1734
    public static @Nullable JClassType getReceiverType(@NonNull JClassType containerType, JClassSymbol sym) {
1735
        JClassType ct = containerType;
1✔
1736
        do {
1737
            JClassType sup = ct.getAsSuper(sym);
1✔
1738
            if (sup != null) {
1✔
1739
                return ct;
1✔
1740
            }
1741
            ct = ct.getEnclosingType();
1✔
1742
        } while (ct != null);
1!
1743
        return null;
×
1744
    }
1745

1746
    private static final class AsSuperVisitor implements JTypeVisitor<@Nullable JTypeMirror, JClassSymbol> {
1747

1748
        static final AsSuperVisitor INSTANCE = new AsSuperVisitor();
1✔
1749

1750
        /** Parameter is the erasure of the target. */
1751

1752
        @Override
1753
        public JTypeMirror visit(JTypeMirror t, JClassSymbol target) {
1754
            return null;
1✔
1755
        }
1756

1757
        @Override
1758
        public JTypeMirror visitClass(JClassType t, JClassSymbol target) {
1759
            if (target.equals(t.getSymbol())) {
1✔
1760
                return t;
1✔
1761
            }
1762

1763
            // prefer digging up the superclass first
1764
            JClassType sup = t.getSuperClass();
1✔
1765
            JClassType res = sup == null ? null : (JClassType) sup.acceptVisitor(this, target);
1✔
1766
            if (res != null) {
1✔
1767
                return res;
1✔
1768
            } else {
1769
                // then look in interfaces if possible
1770
                if (target.isInterface() || target.isUnresolved()) {
1✔
1771
                    return firstResult(target, t.getSuperInterfaces());
1✔
1772
                }
1773
            }
1774

1775
            return null;
1✔
1776
        }
1777

1778
        @Override
1779
        public JTypeMirror visitIntersection(JIntersectionType t, JClassSymbol target) {
1780
            return firstResult(target, t.getComponents());
×
1781
        }
1782

1783
        public @Nullable JTypeMirror firstResult(JClassSymbol target, Iterable<? extends JTypeMirror> components) {
1784
            for (JTypeMirror ci : components) {
1✔
1785
                @Nullable JTypeMirror sup = ci.acceptVisitor(this, target);
1✔
1786
                if (sup != null) {
1✔
1787
                    return sup;
1✔
1788
                }
1789
            }
1✔
1790
            return null;
1✔
1791
        }
1792

1793
        @Override
1794
        public JTypeMirror visitTypeVar(JTypeVar t, JClassSymbol target) {
1795
            // caution, infinite recursion
1796
            return t.getUpperBound().acceptVisitor(this, target);
1✔
1797
        }
1798

1799
        @Override
1800
        public JTypeMirror visitArray(JArrayType t, JClassSymbol target) {
1801
            // Cloneable, Serializable, Object
1802
            JTypeMirror decl = t.getTypeSystem().declaration(target);
×
1803
            return t.isSubtypeOf(decl) ? decl : null;
×
1804
        }
1805
    }
1806

1807
    // </editor-fold>
1808

1809
    // <editor-fold  defaultstate="collapsed" desc="LUB/GLB">
1810

1811
    /**
1812
     * Returns a subset S of the parameter, whose components have no
1813
     * strict supertype in S.
1814
     *
1815
     * <pre>{@code
1816
     * S = { V | V in set, and for all W ≠ V in set, it is not the case that W <: V }
1817
     * }</pre>
1818
     */
1819
    public static Set<JTypeMirror> mostSpecific(Collection<? extends JTypeMirror> set) {
1820
        Set<JTypeMirror> result = new LinkedHashSet<>(set.size());
1✔
1821

1822
        // Notice that this loop needs a well-behaved subtyping relation,
1823
        // i.e. antisymmetric: A <: B && A != B implies not(B <: A)
1824
        // This is not the case if we include unchecked conversion in there,
1825
        // or special provisions for unresolved types.
1826
        vLoop:
1827
        for (JTypeMirror v : set) {
1✔
1828
            for (JTypeMirror w : set) {
1✔
1829
                if (!w.equals(v) && !hasUnresolvedSymbolOrArray(w)) {
1✔
1830
                    Convertibility isConvertible = isConvertibleNoCapture(w, v);
1✔
1831
                    if (isConvertible.bySubtyping()
1✔
1832
                        // This last case covers unchecked conversion. It is made antisymmetric by the
1833
                        // test for a symbol. eg |G| <~> G<?> so it would fail.
1834
                        // However, |G| ~> S if |G| <: |S|, so we should consider |G| more specific than S.
1835
                        || isConvertible.withoutWarnings() && !Objects.equals(w.getSymbol(), v.getSymbol())) {
1✔
1836
                        continue vLoop;
1✔
1837
                    }
1838
                }
1839
            }
1✔
1840
            result.add(v);
1✔
1841
        }
1✔
1842
        return result;
1✔
1843
    }
1844

1845
    // </editor-fold>
1846

1847
    /**
1848
     * Returns the components of t if it is an intersection type,
1849
     * otherwise returns t.
1850
     */
1851
    public static List<JTypeMirror> asList(JTypeMirror t) {
1852
        if (t instanceof JIntersectionType) {
1✔
1853
            return ((JIntersectionType) t).getComponents();
1✔
1854
        } else {
1855
            return Collections.singletonList(t);
1✔
1856
        }
1857
    }
1858

1859
    /** Returns a list with the erasures of the given types, may be unmodifiable. */
1860
    public static List<JTypeMirror> erase(Collection<? extends JTypeMirror> ts) {
1861
        return CollectionUtil.map(ts, JTypeMirror::getErasure);
1✔
1862
    }
1863

1864
    // <editor-fold  defaultstate="collapsed" desc="Mentions">
1865

1866

1867
    public static boolean mentions(@NonNull JTypeVisitable type, @NonNull InferenceVar parent) {
1868
        return type.acceptVisitor(MentionsVisitor.INSTANCE, Collections.singleton(parent));
1✔
1869
    }
1870

1871
    public static boolean mentionsAny(JTypeVisitable t, Collection<? extends SubstVar> vars) {
1872
        return !vars.isEmpty() && t.acceptVisitor(MentionsVisitor.INSTANCE, vars);
1✔
1873
    }
1874

1875

1876
    private static final class MentionsVisitor implements JTypeVisitor<Boolean, Collection<? extends JTypeMirror>> {
1877

1878
        static final MentionsVisitor INSTANCE = new MentionsVisitor();
1✔
1879

1880
        @Override
1881
        public Boolean visit(JTypeMirror t, Collection<? extends JTypeMirror> targets) {
1882
            return false;
1✔
1883
        }
1884

1885
        @Override
1886
        public Boolean visitTypeVar(JTypeVar t, Collection<? extends JTypeMirror> targets) {
1887
            return targets.contains(t);
1✔
1888
        }
1889

1890
        @Override
1891
        public Boolean visitInferenceVar(InferenceVar t, Collection<? extends JTypeMirror> targets) {
1892
            return targets.contains(t);
1✔
1893
        }
1894

1895
        @Override
1896
        public Boolean visitWildcard(JWildcardType t, Collection<? extends JTypeMirror> targets) {
1897
            return t.getBound().acceptVisitor(this, targets);
1✔
1898
        }
1899

1900
        @Override
1901
        public Boolean visitMethodType(JMethodSig t, Collection<? extends JTypeMirror> targets) {
1902
            if (t.getReturnType().acceptVisitor(this, targets)) {
1✔
1903
                return true;
1✔
1904
            }
1905
            for (JTypeMirror fi : t.getFormalParameters()) {
1✔
1906
                if (fi.acceptVisitor(this, targets)) {
1✔
1907
                    return true;
1✔
1908
                }
1909
            }
1✔
1910
            for (JTypeMirror ti : t.getThrownExceptions()) {
1✔
1911
                if (ti.acceptVisitor(this, targets)) {
1!
1912
                    return true;
×
1913
                }
1914
            }
1✔
1915
            return false;
1✔
1916
        }
1917

1918
        @Override
1919
        public Boolean visitClass(JClassType t, Collection<? extends JTypeMirror> targets) {
1920
            JClassType encl = t.getEnclosingType();
1✔
1921
            if (encl != null && encl.acceptVisitor(this, targets)) {
1✔
1922
                return true;
1✔
1923
            }
1924

1925
            for (JTypeMirror typeArg : t.getTypeArgs()) {
1✔
1926
                if (typeArg.acceptVisitor(this, targets)) {
1✔
1927
                    return true;
1✔
1928
                }
1929
            }
1✔
1930

1931
            return false;
1✔
1932
        }
1933

1934
        @Override
1935
        public Boolean visitIntersection(JIntersectionType t, Collection<? extends JTypeMirror> targets) {
1936
            for (JTypeMirror comp : t.getComponents()) {
1✔
1937
                if (comp.acceptVisitor(this, targets)) {
1!
1938
                    return true;
×
1939
                }
1940
            }
1✔
1941
            return false;
1✔
1942
        }
1943

1944
        @Override
1945
        public Boolean visitArray(JArrayType t, Collection<? extends JTypeMirror> targets) {
1946
            return t.getComponentType().acceptVisitor(this, targets);
1✔
1947
        }
1948
    }
1949

1950
    // </editor-fold>
1951

1952
    // <editor-fold  defaultstate="collapsed" desc="Accessibility utils">
1953

1954

1955
    public static Predicate<JMethodSymbol> accessibleMethodFilter(String name, @NonNull JClassSymbol symbol) {
1956
        return it -> it.nameEquals(name) && isAccessible(it, symbol);
1✔
1957
    }
1958

1959
    public static Iterable<JMethodSig> lazyFilterAccessible(List<JMethodSig> visible, @NonNull JClassSymbol accessSite) {
1960
        return () -> IteratorUtil.filter(visible.iterator(), it -> isAccessible(it.getSymbol(), accessSite));
1✔
1961
    }
1962

1963
    public static List<JMethodSig> filterAccessible(List<JMethodSig> visible, @NonNull JClassSymbol accessSite) {
1964
        return CollectionUtil.mapNotNull(visible, m -> isAccessible(m.getSymbol(), accessSite) ? m : null);
1!
1965
    }
1966

1967
    /**
1968
     * Methods and fields of a type variable come from its upper bound, which must be captured.
1969
     * Capturing a type var does NOT capture its upper bound, so we must treat this
1970
     * case here.
1971
     */
1972
    public static JTypeMirror getMemberSource(JTypeMirror t) {
1973
        if (t instanceof JTypeVar) {
1✔
1974
            JTypeVar tv = (JTypeVar) t;
1✔
1975
            return capture(tv.getUpperBound());
1✔
1976
        }
1977
        return capture(t);
1✔
1978
    }
1979

1980
    public static List<JMethodSig> getMethodsOf(JTypeMirror type, String name, boolean staticOnly, @NonNull JClassSymbol enclosing) {
1981
        if (staticOnly && type.isInterface()) {
1✔
1982
            // static methods, start on interface
1983
            // static interface methods are not inherited
1984
            return type.streamDeclaredMethods(staticMethodFilter(name, true, enclosing)).collect(Collectors.toList());
1✔
1985
        } else if (staticOnly) {
1✔
1986
            // static methods, doesn't start on interface
1987
            // -> ignore non-static, ignore any that are interfaces
1988
            return type.streamMethods(staticMethodFilter(name, false, enclosing)).collect(OverloadSet.collectMostSpecific(type));
1✔
1989
        } else {
1990
            return type.streamMethods(methodFilter(name, enclosing))
1✔
1991
                       .collect(OverloadSet.collectMostSpecific(type));
1✔
1992
        }
1993
    }
1994

1995
    private static @NonNull Predicate<JMethodSymbol> methodFilter(String name, @NonNull JClassSymbol enclosing) {
1996
        return it -> isAccessibleWithName(name, enclosing, it);
1✔
1997
    }
1998

1999
    private static @NonNull Predicate<JMethodSymbol> staticMethodFilter(String name, boolean acceptItfs, @NonNull JClassSymbol enclosing) {
2000
        return it -> Modifier.isStatic(it.getModifiers())
1✔
2001
            && (acceptItfs || !it.getEnclosingClass().isInterface())
1✔
2002
            && isAccessibleWithName(name, enclosing, it);
1✔
2003
    }
2004

2005
    private static boolean isAccessibleWithName(String name, @NonNull JClassSymbol enclosing, JMethodSymbol m) {
2006
        return m.nameEquals(name) && isAccessible(m, enclosing);
1✔
2007
    }
2008

2009

2010
    private static boolean isAccessible(JExecutableSymbol method, JClassSymbol ctx) {
2011
        Objects.requireNonNull(ctx, "Cannot check a null symbol");
1✔
2012

2013
        int mods = method.getModifiers();
1✔
2014
        if (Modifier.isPublic(mods)) {
1✔
2015
            return true;
1✔
2016
        }
2017

2018
        JClassSymbol owner = method.getEnclosingClass();
1✔
2019

2020
        if (Modifier.isPrivate(mods)) {
1✔
2021
            return ctx.getNestRoot().equals(owner.getNestRoot());
1✔
2022
        }
2023

2024
        return ctx.getPackageName().equals(owner.getPackageName())
1✔
2025
            // we can exclude interfaces because their members are all public
2026
            || Modifier.isProtected(mods) && isSubClassOfNoInterface(ctx, owner);
1✔
2027
    }
2028

2029
    private static boolean isSubClassOfNoInterface(JClassSymbol sub, JClassSymbol symbol) {
2030
        if (symbol.equals(sub)) {
1✔
2031
            return true;
1✔
2032
        }
2033

2034
        JClassSymbol superclass = sub.getSuperclass();
1✔
2035
        return superclass != null && isSubClassOfNoInterface(superclass, symbol);
1✔
2036
    }
2037

2038
    public static NameResolver<FieldSig> getMemberFieldResolver(JTypeMirror c, @NonNull String accessPackageName, @Nullable JClassSymbol access, String name) {
2039
        if (c instanceof JClassType) {
1✔
2040
            // fast path
2041
            return JavaResolvers.getMemberFieldResolver((JClassType) c, accessPackageName, access, name);
1✔
2042
        }
2043

2044
        return c.acceptVisitor(GetFieldVisitor.INSTANCE, new FieldSearchParams(accessPackageName, access, name));
1✔
2045
    }
2046

2047
    private static final class FieldSearchParams {
2048

2049
        private final @NonNull String accessPackageName;
2050
        private final @Nullable JClassSymbol access;
2051
        private final String name;
2052

2053
        FieldSearchParams(@NonNull String accessPackageName, @Nullable JClassSymbol access, String name) {
1✔
2054
            this.accessPackageName = accessPackageName;
1✔
2055
            this.access = access;
1✔
2056
            this.name = name;
1✔
2057
        }
1✔
2058
    }
2059

2060
    private static final class GetFieldVisitor implements JTypeVisitor<NameResolver<FieldSig>, FieldSearchParams> {
2061

2062
        static final GetFieldVisitor INSTANCE = new GetFieldVisitor();
1✔
2063

2064
        @Override
2065
        public NameResolver<FieldSig> visit(JTypeMirror t, FieldSearchParams fieldSearchParams) {
2066
            return CoreResolvers.emptyResolver();
1✔
2067
        }
2068

2069
        @Override
2070
        public NameResolver<FieldSig> visitClass(JClassType t, FieldSearchParams fieldSearchParams) {
2071
            return JavaResolvers.getMemberFieldResolver(t, fieldSearchParams.accessPackageName, fieldSearchParams.access, fieldSearchParams.name);
×
2072
        }
2073

2074
        @Override
2075
        public NameResolver<FieldSig> visitTypeVar(JTypeVar t, FieldSearchParams fieldSearchParams) {
2076
            return t.getUpperBound().acceptVisitor(this, fieldSearchParams);
×
2077
        }
2078

2079
        @Override
2080
        public NameResolver<FieldSig> visitIntersection(JIntersectionType t, FieldSearchParams fieldSearchParams) {
2081
            return NameResolver.composite(
×
2082
                CollectionUtil.map(t.getComponents(), c -> c.acceptVisitor(this, fieldSearchParams))
×
2083
            );
2084
        }
2085

2086
        @Override
2087
        public NameResolver<FieldSig> visitArray(JArrayType t, FieldSearchParams fieldSearchParams) {
2088
            if ("length".equals(fieldSearchParams.name)) {
1!
2089
                return CoreResolvers.singleton("length", t.getTypeSystem().sigOf(t, t.getSymbol().getDeclaredField("length")));
1✔
2090
            }
2091
            return CoreResolvers.emptyResolver();
×
2092
        }
2093
    }
2094

2095
    // </editor-fold>
2096

2097
    // <editor-fold  defaultstate="collapsed" desc="Miscellaneous">
2098

2099
    /**
2100
     * Returns true if both types have a common supertype that is not Object.
2101
     * Primitive types are only related to themselves.
2102
     *
2103
     * @param t Non-null type
2104
     * @param s Non-null type
2105
     *
2106
     * @throws NullPointerException if a parameter is null
2107
     */
2108
    public static boolean areRelated(@NonNull JTypeMirror t, JTypeMirror s) {
2109
        if (t.isPrimitive() || s.isPrimitive()) {
1!
2110
            return s.equals(t);
×
2111
        }
2112
        if (t.equals(s)) {
1✔
2113
            return true;
1✔
2114
        }
2115
        // maybe they have a common supertype
2116
        Set<JTypeMirror> tSupertypes = new HashSet<>(t.getSuperTypeSet());
1✔
2117
        tSupertypes.retainAll(s.getSuperTypeSet());
1✔
2118
        return !tSupertypes.equals(Collections.singleton(t.getTypeSystem().OBJECT));
1✔
2119
    }
2120

2121
    /**
2122
     * Returns true if the type is {@link TypeSystem#UNKNOWN},
2123
     * {@link TypeSystem#ERROR}, or a class type with unresolved
2124
     * symbol.
2125
     *
2126
     * @param t Non-null type
2127
     *
2128
     * @throws NullPointerException if the parameter is null
2129
     */
2130
    public static boolean isUnresolved(@NonNull JTypeMirror t) {
2131
        return isSpecialUnresolved(t) || hasUnresolvedSymbol(t);
1✔
2132
    }
2133

2134
    /**
2135
     * Returns true if the type is {@link TypeSystem#UNKNOWN},
2136
     * or {@link TypeSystem#ERROR}, or a class type with unresolved
2137
     * symbol, or an array of such types.
2138
     *
2139
     * @param t Non-null type
2140
     *
2141
     * @throws NullPointerException if the parameter is null
2142
     */
2143
    public static boolean isUnresolvedOrArray(@NonNull JTypeMirror t) {
2144
        return isSpecialUnresolvedOrArray(t) || hasUnresolvedSymbolOrArray(t);
1!
2145
    }
2146

2147
    /**
2148
     * Returns true if the type is {@link TypeSystem#UNKNOWN},
2149
     * or {@link TypeSystem#ERROR}.
2150
     *
2151
     * @param t Non-null type
2152
     *
2153
     * @throws NullPointerException if the parameter is null
2154
     */
2155
    public static boolean isSpecialUnresolved(@NonNull JTypeMirror t) {
2156
        TypeSystem ts = t.getTypeSystem();
1✔
2157
        return t == ts.UNKNOWN || t == ts.ERROR;
1✔
2158
    }
2159

2160
    /**
2161
     * Returns true if the type is {@link TypeSystem#UNKNOWN},
2162
     * or {@link TypeSystem#ERROR}, or an array of such types.
2163
     *
2164
     * @param t Non-null type
2165
     *
2166
     * @throws NullPointerException if the parameter is null
2167
     */
2168
    public static boolean isSpecialUnresolvedOrArray(@Nullable JTypeMirror t) {
2169
        return t == null
1!
2170
            || isSpecialUnresolved(t)
1✔
2171
            || t instanceof JArrayType && isSpecialUnresolved(((JArrayType) t).getElementType());
1✔
2172
    }
2173

2174
    /**
2175
     * Return true if the argument is a {@link JClassType} with
2176
     * {@linkplain JClassSymbol#isUnresolved() an unresolved symbol}.
2177
     */
2178
    public static boolean hasUnresolvedSymbol(@Nullable JTypeMirror t) {
2179
        return t instanceof JClassType && t.getSymbol().isUnresolved();
1✔
2180
    }
2181

2182
    /**
2183
     * Return true if the argument is a {@link JClassType} with
2184
     * {@linkplain JClassSymbol#isUnresolved() an unresolved symbol},
2185
     * or an array whose element type has an unresolved symbol.
2186
     */
2187
    public static boolean hasUnresolvedSymbolOrArray(@Nullable JTypeMirror t) {
2188
        if (!(t instanceof JClassType)) {
1✔
2189
            return t instanceof JArrayType && hasUnresolvedSymbol(((JArrayType) t).getElementType());
1✔
2190
        }
2191
        return hasUnresolvedSymbol(t);
1✔
2192
    }
2193

2194
    public static boolean isUnresolvedOrNull(@Nullable JTypeMirror t) {
2195
        return t == null || isUnresolved(t);
1!
2196
    }
2197

2198
    public static @Nullable JTypeMirror getArrayComponent(@Nullable JTypeMirror t) {
2199
        return t instanceof JArrayType ? ((JArrayType) t).getComponentType() : null;
1!
2200
    }
2201

2202

2203
    /**
2204
     * Return true if the method is context dependent. That
2205
     * means its return type is influenced by the surrounding
2206
     * context during type inference. Generic constructors
2207
     * are always context dependent.
2208
     *
2209
     * @deprecated Since 7.11.0. Use {@link #isContextDependent(JExecutableSymbol)} instead which is more flexible.
2210
     */
2211
    @Deprecated
2212
    public static boolean isContextDependent(JMethodSig sig) {
2213
        return isContextDependent(sig.getSymbol());
×
2214
    }
2215

2216
    /**
2217
     * Return true if the method is context dependent. That
2218
     * means its return type is influenced by the surrounding
2219
     * context during type inference. Generic constructors
2220
     * are always context dependent.
2221
     */
2222
    public static boolean isContextDependent(JExecutableSymbol symbol) {
2223
        if (symbol.isGeneric() || symbol.getEnclosingClass().isGeneric()) {
1!
2224
            if (symbol instanceof JMethodSymbol) {
1!
2225
                JTypeMirror returnType = ((JMethodSymbol) symbol).getReturnType(EMPTY);
1✔
2226
                return mentionsAny(returnType, symbol.getTypeParameters())
1!
2227
                    || mentionsAny(returnType, symbol.getEnclosingClass().getTypeParameters());
1✔
2228
            }
2229
            // generic ctors are context dependent
2230
            return true;
×
2231
        }
2232
        return false;
1✔
2233
    }
2234
    // </editor-fold>
2235
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc