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

pmd / pmd / 380

29 Jan 2026 03:55PM UTC coverage: 78.964%. Remained the same
380

push

github

adangel
[doc] ADR 3: Clarify javadoc tags (#6392)

18537 of 24358 branches covered (76.1%)

Branch coverage included in aggregate %.

40391 of 50268 relevant lines covered (80.35%)

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
     * @internalApi None of this is published API, and compatibility can be broken anytime! Use this only at your own risk.
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) {
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
    /**
411
     * @deprecated Since 7.2.0. Use {@link #isConvertible(JTypeMirror, JTypeMirror)} or {@link #isConvertibleNoCapture(JTypeMirror, JTypeMirror)} instead.
412
     */
413
    @Deprecated // unused
414
    public static Convertibility isConvertible(@NonNull JTypeMirror t, @NonNull JTypeMirror s, boolean capture) {
415
        return SubtypeVisitor.PURE.isConvertible(t, s, capture);
×
416
    }
417

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

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

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

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

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

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

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

504
        // public:
505

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

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

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

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

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

537
        // package:
538

539

540
        /** Preserves an unchecked warning. */
541
        Convertibility and(Convertibility b) {
542
            return min(this, b);
1✔
543
        }
544

545
        static Convertibility min(Convertibility c1, Convertibility c2) {
546
            return c1.ordinal() < c2.ordinal() ? c1 : c2;
1!
547
        }
548

549
        static Convertibility subtypeIf(boolean b) {
550
            return b ? SUBTYPING : NEVER;
1✔
551
        }
552

553
    }
554

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

570
    private static JTypeMirror wildLowerBound(JTypeMirror type) {
571
        if (type instanceof JWildcardType) {
1✔
572
            return wildLowerBound(((JWildcardType) type).asLowerBound());
1✔
573
        }
574
        return type;
1✔
575
    }
576

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

586
    private static boolean isTypeRange(JTypeMirror s) {
587
        return s instanceof JWildcardType || isCvar(s);
1!
588
    }
589

590
    private static boolean isCvar(JTypeMirror s) {
591
        return s instanceof JTypeVar && ((JTypeVar) s).isCaptured();
1✔
592
    }
593

594

595
    private static final class SubtypeVisitor implements JTypeVisitor<Convertibility, JTypeMirror> {
596

597
        static final SubtypeVisitor INFERENCE = new SubtypeVisitor(false);
1✔
598
        static final SubtypeVisitor PURE = new SubtypeVisitor(true);
1✔
599
        private final boolean pure;
600

601
        private SubtypeVisitor(boolean pure) {
1✔
602
            this.pure = pure;
1✔
603
        }
1✔
604

605

606
        Convertibility isConvertible(@NonNull JTypeMirror t, @NonNull JTypeMirror s) {
607
            return isConvertible(t, s, false);
1✔
608
        }
609

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

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

662
            if (capture) {
1✔
663
                t = capture(t);
1✔
664
            }
665
            return t.acceptVisitor(this, s);
1✔
666
        }
667

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

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

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

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

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

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

743
            return result;
1✔
744
        }
745

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

764
            // ⊥ ---------L(S)---L(T)------U(T)-----U(S)---> Object
765
            // range of S   [-------------------------]
766
            // range of T          [---------]
767

768
            // here S contains T because its range is greater
769

770
            // since a wildcard is either "super" or "extends", in reality
771
            // either L(S) = ⊥, or U(S) = Object.
772

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

775
            //      ⊥ -------U(T)-----U(S)------> Object   (L(T) = L(S) = ⊥)
776
            //      ⊥ -------L(S)-----L(T)------> Object   (U(T) = U(S) = Object)
777

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

783
            if (s instanceof JWildcardType) {
1✔
784
                JWildcardType sw = (JWildcardType) s;
1✔
785

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

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

801
            return Convertibility.NEVER;
1✔
802
        }
803

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

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

817
            if (isTypeRange(s)) {
1✔
818
                return isConvertible(t, lowerBoundRec(s));
1✔
819
            }
820
            return isConvertible(t.getUpperBound(), s);
1✔
821
        }
822

823
        @Override
824
        public Convertibility visitNullType(JTypeMirror t, JTypeMirror s) {
825
            return Convertibility.subtypeIf(!s.isPrimitive());
1✔
826
        }
827

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

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

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

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

878
            JClassType cs = (JClassType) s;
1✔
879

880
            JClassType superDecl = t.getAsSuper(cs.getSymbol());
1✔
881

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

892
        @Override
893
        public Convertibility visitIntersection(JIntersectionType t, JTypeMirror s) {
894
            // A & B <: A
895
            // A & B <: B
896

897
            // But for a class C, `C <: A & B` if `C <: A` and `C <: B`
898

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

909
            // what we mean is, if S is an intersection, then
910
            // "any component of T subtypes any component of S"
911

912
            return anySubTypesAny(t.getComponents(), asList(s));
1✔
913
        }
914

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

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

933
            JArrayType cs = (JArrayType) s;
1✔
934

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

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

953
    public static boolean isStrictSubtype(@NonNull JTypeMirror t, @NonNull JTypeMirror s) {
954
        return !t.equals(s) && t.isSubtypeOf(s);
1!
955
    }
956

957
    // </editor-fold>
958

959
    // <editor-fold  defaultstate="collapsed" desc="Substitution">
960

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

976

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

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

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

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

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

1024
    // </editor-fold>
1025

1026
    // <editor-fold  defaultstate="collapsed" desc="Projection">
1027

1028

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

1041
    private static final JTypeMirror NO_DOWN_PROJECTION = null;
1✔
1042
    private static final ProjectionVisitor UPWARDS_PROJECTOR = new ProjectionVisitor(true) {
1✔
1043

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

1052

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

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

1069

1070
        @Override
1071
        public JTypeMirror visitNullType(JTypeMirror t, RecursionStop recursionStop) {
1072
            return t;
×
1073
        }
1074

1075
    };
1076

1077

1078
    private static final ProjectionVisitor DOWNWARDS_PROJECTOR = new ProjectionVisitor(false) {
1✔
1079

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

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

1097

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

1106
        @Override
1107
        public JTypeMirror visitNullType(JTypeMirror t, RecursionStop recursionStop) {
1108
            return NO_DOWN_PROJECTION;
1✔
1109
        }
1110
    };
1111

1112
    static final class RecursionStop {
1✔
1113

1114
        private Set<JTypeVar> set;
1115

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

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

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

1157
        private final boolean upwards;
1158

1159
        private ProjectionVisitor(boolean upwards) {
1✔
1160
            this.upwards = upwards;
1✔
1161
        }
1✔
1162

1163

1164
        @Override
1165
        public abstract JTypeMirror visitNullType(JTypeMirror t, RecursionStop recursionStop);
1166

1167

1168
        @Override
1169
        public abstract JTypeMirror visitWildcard(JWildcardType t, RecursionStop recursionStop);
1170

1171

1172
        @Override
1173
        public abstract JTypeMirror visitTypeVar(JTypeVar t, RecursionStop recursionStop);
1174

1175

1176
        @Override
1177
        public JTypeMirror visit(JTypeMirror t, RecursionStop recursionStop) {
1178
            return t;
1✔
1179
        }
1180

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

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

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

1214
                    change = true;
1✔
1215

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

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

1233
                return change ? t.withTypeArguments(newTargs) : t;
1✔
1234
            } else {
1235
                return t;
1✔
1236
            }
1237
        }
1238

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

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

1267
        @Override
1268
        public JTypeMirror visitSentinel(JTypeMirror t, RecursionStop recursionStop) {
1269
            return t;
1✔
1270
        }
1271
    }
1272

1273
    // </editor-fold>
1274

1275
    // <editor-fold  defaultstate="collapsed" desc="Overriding">
1276

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

1285
        JTypeMirror r1 = m1.getReturnType();
1✔
1286
        JTypeMirror r2 = m2.getReturnType();
1✔
1287

1288
        if (r1 == r1.getTypeSystem().NO_TYPE) {
1✔
1289
            return r1 == r2;
1!
1290
        }
1291

1292
        if (r1.isPrimitive()) {
1✔
1293
            return r1 == r2;
1!
1294
        }
1295

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

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

1314
        return null;
×
1315
    }
1316

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

1324
        if (tp1.isEmpty()) {
1✔
1325
            return true;
1✔
1326
        }
1327

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

1333
            if (!isSameType(p1.getUpperBound(), subst(p2.getUpperBound(), mapping))) {
1✔
1334
                return false;
1✔
1335
            }
1336
        }
1337

1338
        return true;
1✔
1339
    }
1340

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

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

1371
            if (!isSameType(fi1.getErasure(), fi2.getErasure())) {
1✔
1372
                return false;
1✔
1373
            }
1374
        }
1375

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

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

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

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

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

1438
        JTypeMirror m1Owner = m1.getDeclaringType();
1✔
1439
        JClassType m2Owner = (JClassType) m2.getDeclaringType();
1✔
1440

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

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

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

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

1491
    private static boolean isOverridableIn(JMethodSig m, JTypeDeclSymbol origin) {
1492
        return isOverridableIn(m.getSymbol(), origin);
1✔
1493
    }
1494

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

1514
        final int accessFlags = Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE;
1✔
1515

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

1533
    // </editor-fold>
1534

1535
    // <editor-fold  defaultstate="collapsed" desc="SAM types">
1536

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

1543

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

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

1564
        List<JTypeVar> tparams = type.getFormalTypeParams();
1✔
1565
        List<JTypeMirror> newArgs = new ArrayList<>();
1✔
1566

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

1576
                JWildcardType ai2 = (JWildcardType) ai;
1✔
1577

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

1586
            } else {
1✔
1587
                newArgs.add(ai);
1✔
1588
            }
1589

1590
        }
1591

1592
        return type.withTypeArguments(newArgs);
1✔
1593
    }
1594

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

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

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

1638
    private static @Nullable JMethodSig findFunctionTypeImpl(@Nullable JClassType candidateSam) {
1639

1640
        if (candidateSam == null || !candidateSam.isInterface() || candidateSam.getSymbol().isAnnotation()) {
1!
1641
            return null;
1✔
1642
        }
1643

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

1648

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

1658
        if (candidates.isEmpty()) {
1✔
1659
            return null;
1✔
1660
        } else if (candidates.size() == 1) {
1✔
1661
            return candidates.get(0);
1✔
1662
        }
1663

1664
        JMethodSig currentBest = null;
1✔
1665

1666
        nextCandidate:
1667
        for (int i = 0; i < candidates.size(); i++) {
1✔
1668
            JMethodSig cand = candidates.get(i);
1✔
1669

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

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

1685
        return currentBest;
1✔
1686
    }
1687

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

1695
    // </editor-fold>
1696

1697
    // <editor-fold  defaultstate="collapsed" desc="As super">
1698

1699
    /**
1700
     * @see JTypeMirror#getAsSuper(JClassSymbol)
1701
     */
1702
    public static @Nullable JTypeMirror asSuper(@NonNull JTypeMirror t, @NonNull JClassSymbol s) {
1703

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

1709
        return t.acceptVisitor(AsSuperVisitor.INSTANCE, s);
1✔
1710
    }
1711

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

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

1749
    private static final class AsSuperVisitor implements JTypeVisitor<@Nullable JTypeMirror, JClassSymbol> {
1750

1751
        static final AsSuperVisitor INSTANCE = new AsSuperVisitor();
1✔
1752

1753
        /** Parameter is the erasure of the target. */
1754

1755
        @Override
1756
        public JTypeMirror visit(JTypeMirror t, JClassSymbol target) {
1757
            return null;
1✔
1758
        }
1759

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

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

1778
            return null;
1✔
1779
        }
1780

1781
        @Override
1782
        public JTypeMirror visitIntersection(JIntersectionType t, JClassSymbol target) {
1783
            return firstResult(target, t.getComponents());
×
1784
        }
1785

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

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

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

1810
    // </editor-fold>
1811

1812
    // <editor-fold  defaultstate="collapsed" desc="LUB/GLB">
1813

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

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

1848
    // </editor-fold>
1849

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

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

1867
    // <editor-fold  defaultstate="collapsed" desc="Mentions">
1868

1869

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

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

1878

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

1881
        static final MentionsVisitor INSTANCE = new MentionsVisitor();
1✔
1882

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

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

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

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

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

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

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

1934
            return false;
1✔
1935
        }
1936

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

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

1953
    // </editor-fold>
1954

1955
    // <editor-fold  defaultstate="collapsed" desc="Accessibility utils">
1956

1957

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

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

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

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

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

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

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

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

2012

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

2016
        int mods = method.getModifiers();
1✔
2017
        if (Modifier.isPublic(mods)) {
1✔
2018
            return true;
1✔
2019
        }
2020

2021
        JClassSymbol owner = method.getEnclosingClass();
1✔
2022

2023
        if (Modifier.isPrivate(mods)) {
1✔
2024
            return ctx.getNestRoot().equals(owner.getNestRoot());
1✔
2025
        }
2026

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

2032
    private static boolean isSubClassOfNoInterface(JClassSymbol sub, JClassSymbol symbol) {
2033
        if (symbol.equals(sub)) {
1✔
2034
            return true;
1✔
2035
        }
2036

2037
        JClassSymbol superclass = sub.getSuperclass();
1✔
2038
        return superclass != null && isSubClassOfNoInterface(superclass, symbol);
1✔
2039
    }
2040

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

2047
        return c.acceptVisitor(GetFieldVisitor.INSTANCE, new FieldSearchParams(accessPackageName, access, name));
1✔
2048
    }
2049

2050
    private static final class FieldSearchParams {
2051

2052
        private final @NonNull String accessPackageName;
2053
        private final @Nullable JClassSymbol access;
2054
        private final String name;
2055

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

2063
    private static final class GetFieldVisitor implements JTypeVisitor<NameResolver<FieldSig>, FieldSearchParams> {
2064

2065
        static final GetFieldVisitor INSTANCE = new GetFieldVisitor();
1✔
2066

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

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

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

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

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

2098
    // </editor-fold>
2099

2100
    // <editor-fold  defaultstate="collapsed" desc="Miscellaneous">
2101

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

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

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

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

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

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

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

2197
    public static boolean isUnresolvedOrNull(@Nullable JTypeMirror t) {
2198
        return t == null || isUnresolved(t);
1!
2199
    }
2200

2201
    public static @Nullable JTypeMirror getArrayComponent(@Nullable JTypeMirror t) {
2202
        return t instanceof JArrayType ? ((JArrayType) t).getComponentType() : null;
1!
2203
    }
2204

2205

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

2219
    /**
2220
     * Return true if the method is context dependent. That
2221
     * means its return type is influenced by the surrounding
2222
     * context during type inference. Generic constructors
2223
     * are always context dependent.
2224
     */
2225
    public static boolean isContextDependent(JExecutableSymbol symbol) {
2226
        if (symbol.isGeneric() || symbol.getEnclosingClass().isGeneric()) {
1!
2227
            if (symbol instanceof JMethodSymbol) {
1!
2228
                JTypeMirror returnType = ((JMethodSymbol) symbol).getReturnType(EMPTY);
1✔
2229
                return mentionsAny(returnType, symbol.getTypeParameters())
1!
2230
                    || mentionsAny(returnType, symbol.getEnclosingClass().getTypeParameters());
1✔
2231
            }
2232
            // generic ctors are context dependent
2233
            return true;
×
2234
        }
2235
        return false;
1✔
2236
    }
2237
    // </editor-fold>
2238
}
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