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

import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.resolution.typeinference.BoundSet;
import com.github.javaparser.symbolsolver.resolution.typeinference.ConstraintFormula;
import com.github.javaparser.symbolsolver.resolution.typeinference.bounds.SameAsBound;

import java.util.List;

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

/**
 * A type S is the same as a type T (§4.3.4), or a type argument S is the same as type argument T
 *
 * @author Federico Tomassetti
 */
public class TypeSameAsType extends ConstraintFormula {
    private ResolvedType S;
    private ResolvedType T;

    public TypeSameAsType(ResolvedType s, ResolvedType t) {
        S = s;
        T = t;
    }

    @Override
    public ReductionResult reduce(BoundSet currentBoundSet) {
        // A constraint formula of the form ‹S = T›, where S and T are types, is reduced as follows:

        if (!S.isWildcard() && !T.isWildcard()) {

            // - If S and T are proper types, the constraint reduces to true if S is the same as T (§4.3.4), and false
            //   otherwise.

            if (isProperType(S) && isProperType(T)) {
                if (S.equals(T)) {
                    return ReductionResult.trueResult();
                } else {
                    return ReductionResult.falseResult();
                }
            }

            // - Otherwise, if S or T is the null type, the constraint reduces to false.

            if (S.isNull() || T.isNull()) {
                return ReductionResult.falseResult();
            }

            // - Otherwise, if S is an inference variable, α, and T is not a primitive type, the constraint reduces to the
            //   bound α = T.

            if (isInferenceVariable(S) && !T.isPrimitive()) {
                return ReductionResult.oneBound(new SameAsBound(S, T));
            }

            // - Otherwise, if T is an inference variable, α, and S is not a primitive type, the constraint reduces to the
            //   bound S = α.

            if (isInferenceVariable(T) && !S.isPrimitive()) {
                return ReductionResult.oneBound(new SameAsBound(S, T));
            }

            // - Otherwise, if S and T are class or interface types with the same erasure, where S has
            //   type arguments B1, ..., Bn and T has type arguments A1, ..., An, the constraint reduces to the following
            //   new constraints: for all i (1 ≤ i ≤ n), ‹Bi = Ai›.

            if (S.isReferenceType() && T.isReferenceType()
                    && S.asReferenceType().toRawType().equals(T.asReferenceType().toRawType())) {
                ReductionResult res = ReductionResult.empty();
                List<ResolvedType> Bs = S.asReferenceType().typeParametersValues();
                List<ResolvedType> As = T.asReferenceType().typeParametersValues();
                for (int i = 0; i < Bs.size(); i++) {
                    res = res.withConstraint(new TypeSameAsType(Bs.get(i), As.get(i)));
                }
                return res;
            }

            // - Otherwise, if S and T are array types, S'[] and T'[], the constraint reduces to ‹S' = T'›.

            if (S.isArray() && T.isArray()) {
                return ReductionResult.oneConstraint(new TypeSameAsType(
                        S.asArrayType().getComponentType(),
                        T.asArrayType().getComponentType()));
            }

            // - Otherwise, the constraint reduces to false.

            return ReductionResult.falseResult();
        }

        // Note that we do not address intersection types above, because it is impossible for reduction to encounter an
        // intersection type that is not a proper type.

        // A constraint formula of the form ‹S = T›, where S and T are type arguments (§4.5.1), is reduced as follows:
        //
        // - If S and T are types, the constraint is reduced as described above.
        //
        // - If S has the form ? and T has the form ?, the constraint reduces to true.
        //
        // - If S has the form ? and T has the form ? extends T', the constraint reduces to ‹Object = T'›.
        //
        // - If S has the form ? extends S' and T has the form ?, the constraint reduces to ‹S' = Object›.
        //
        // - If S has the form ? extends S' and T has the form ? extends T', the constraint reduces to ‹S' = T'›.
        //
        // - If S has the form ? super S' and T has the form ? super T', the constraint reduces to ‹S' = T'›.
        //
        // - Otherwise, the constraint reduces to false.


        throw new UnsupportedOperationException();
    }

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

        TypeSameAsType that = (TypeSameAsType) 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 "TypeSameAsType{" +
                "S=" + S +
                ", T=" + T +
                '}';
    }
}