aboutsummaryrefslogtreecommitdiffstats
path: root/javaparser-symbol-solver-core/src/main/java/com/github/javaparser/symbolsolver/resolution/typeinference/constraintformulas/TypeCompatibleWithType.java
blob: 1c03064a81588bcd77e6b81290c9014d5c583b6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package com.github.javaparser.symbolsolver.resolution.typeinference.constraintformulas;

import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.model.resolution.TypeSolver;
import com.github.javaparser.symbolsolver.model.typesystem.ReferenceTypeImpl;
import com.github.javaparser.symbolsolver.resolution.typeinference.BoundSet;
import com.github.javaparser.symbolsolver.resolution.typeinference.ConstraintFormula;
import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver;

import static com.github.javaparser.symbolsolver.resolution.typeinference.TypeHelper.isCompatibleInALooseInvocationContext;
import static com.github.javaparser.symbolsolver.resolution.typeinference.TypeHelper.isProperType;

/**
 * A type S is compatible in a loose invocation context with type T
 *
 * @author Federico Tomassetti
 */
public class TypeCompatibleWithType extends ConstraintFormula {
    private ResolvedType s;
    private ResolvedType t;
    private TypeSolver typeSolver;

    public TypeCompatibleWithType(TypeSolver typeSolver, ResolvedType s, ResolvedType t) {
        this.typeSolver = typeSolver;
        this.s = s;
        this.t = t;
    }

    @Override
    public ReductionResult reduce(BoundSet currentBoundSet) {
        // A constraint formula of the form ‹S → T› is reduced as follows:
        //
        // 1. If S and T are proper types, the constraint reduces to true if S is compatible in a loose invocation context with T (§5.3), and false otherwise.

        if (isProperType(s) && isProperType(t)) {
            if (isCompatibleInALooseInvocationContext(s, t)) {
                return ReductionResult.trueResult();
            } else {
                return ReductionResult.falseResult();
            }
        }

        // 2. Otherwise, if S is a primitive type, let S' be the result of applying boxing conversion (§5.1.7) to S. Then the constraint reduces to ‹S' → T›.

        if (s.isPrimitive()) {
            ReflectionTypeSolver typeSolver = new ReflectionTypeSolver();
            ResolvedType sFirst = new ReferenceTypeImpl(typeSolver.solveType(s.asPrimitive().getBoxTypeQName()), typeSolver);
            return ReductionResult.oneConstraint(new TypeCompatibleWithType(typeSolver, sFirst, t));
        }

        // 3. Otherwise, if T is a primitive type, let T' be the result of applying boxing conversion (§5.1.7) to T. Then the constraint reduces to ‹S = T'›.

        if (t.isPrimitive()) {
            ReflectionTypeSolver typeSolver = new ReflectionTypeSolver();
            ResolvedType tFirst = new ReferenceTypeImpl(typeSolver.solveType(t.asPrimitive().getBoxTypeQName()), typeSolver);
            return ReductionResult.oneConstraint(new TypeSameAsType(s, tFirst));
        }

        // The fourth and fifth cases are implicit uses of unchecked conversion (§5.1.9). These, along with any use of
        // unchecked conversion in the first case, may result in compile-time unchecked warnings, and may influence a
        // method's invocation type (§15.12.2.6).

        // 4. Otherwise, if T is a parameterized type of the form G<T1, ..., Tn>, and there exists no type of the
        //    form G<...> that is a supertype of S, but the raw type G is a supertype of S, then the constraint reduces
        //    to true.

        if (t.isReferenceType() && !t.asReferenceType().getTypeDeclaration().getTypeParameters().isEmpty()) {
            // FIXME I really cannot understand what the specification means...

            // there exists a type of the form G<...> that is a supertype of S?
            boolean condition1 = t.isAssignableBy(s);

            // the raw type G is a supertype of S
            ResolvedType G = t.asReferenceType().toRawType();
            boolean condition2 = G.isAssignableBy(s);

            if (!condition1 && condition2) {
                return ReductionResult.trueResult();
            }

            //throw new UnsupportedOperationException();
        }

        // 5. Otherwise, if T is an array type of the form G<T1, ..., Tn>[]k, and there exists no type of the form
        //    G<...>[]k that is a supertype of S, but the raw type G[]k is a supertype of S, then the constraint
        //    reduces to true. (The notation []k indicates an array type of k dimensions.)

        if (t.isArray()) {
            throw new UnsupportedOperationException();
        }

        // 6. Otherwise, the constraint reduces to ‹S <: T›

        return ReductionResult.empty().withConstraint(new TypeSubtypeOfType(typeSolver, s, t));
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        TypeCompatibleWithType that = (TypeCompatibleWithType) o;

        if (!s.equals(that.s)) return false;
        return t.equals(that.t);
    }

    @Override
    public int hashCode() {
        int result = s.hashCode();
        result = 31 * result + t.hashCode();
        return result;
    }

    @Override
    public String toString() {
        return "TypeCompatibleWithType{" +
                "s=" + s +
                ", t=" + t +
                '}';
    }
}