summaryrefslogtreecommitdiffstats
path: root/tools/apicheck/src/com/android/apicheck/FieldInfo.java
blob: d80d9f6a679a003ff91caaa5b154ecd43e9dcf01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.apicheck;

public class FieldInfo {
  
    private String mName;
    private String mType;
    private boolean mIsTransient;
    private boolean mIsVolatile;
    private String mValue;
    private boolean mIsStatic;
    private boolean mIsFinal;
    private String mDeprecated;
    private String mScope;
    private boolean mExistsInBoth;
    private SourcePositionInfo mSourcePosition;
    private ClassInfo mClass;
    
    public FieldInfo (String name, String type, boolean isTransient, boolean isVolatile,
                       String value, boolean isStatic, boolean isFinal, String deprecated,
                       String scope, SourcePositionInfo source, ClassInfo parent) {
        mName = name;
        mType = type;
        mIsTransient = isTransient;
        mIsVolatile = isVolatile;
        mValue = value;
        mIsStatic = isStatic;
        mIsFinal = isFinal;
        mDeprecated = deprecated;
        mScope = scope;
        mExistsInBoth = false;
        mSourcePosition = source;
        mClass = parent;
    }
    
    public boolean isInBoth() {
        return mExistsInBoth;
    }
    public SourcePositionInfo position() {
        return mSourcePosition;
    }
    
    public String name() {
        return mName;
    }
    
    public String qualifiedName() {
        String parentQName = (mClass != null)
                ? (mClass.qualifiedName() + ".")
                : "";
        return parentQName + name();
    }
    
    // Check the declared value with a typed comparison, not a string comparison,
    // to accommodate toolchains with different fp -> string conversions.
    public boolean valueEquals(FieldInfo other) {
        // Type mismatch means nonequal, as does a null/non-null mismatch
        if (!mType.equals(other.mType)
                || ((mValue == null) != (other.mValue == null))) {
            return false;
        }

        // Null values are considered equal
        if (mValue == null) {
            return true;
        }

        // Floating point gets an implementation-type comparison; all others just use the string
        // If float/double parse fails, fall back to string comparison -- it means that it's a
        // canonical droiddoc-generated constant expression that represents a NaN.
        try {
            if (mType.equals("float")) {
                float val = Float.parseFloat(mValue);
                float otherVal = Float.parseFloat(other.mValue);
                return (val == otherVal);
            } else if (mType.equals("double")) {
                double val = Double.parseDouble(mValue);
                double otherVal = Double.parseDouble(other.mValue);
                return (val == otherVal);
            }
        } catch (NumberFormatException e) {
            // fall through
        }
        
        return mValue.equals(other.mValue);
    }

    public boolean isConsistent(FieldInfo fInfo) {
      fInfo.mExistsInBoth = true;
      mExistsInBoth = true;
      boolean consistent = true;
      if (!mType.equals(fInfo.mType)) {
          Errors.error(Errors.CHANGED_TYPE, fInfo.position(),
                  "Field " + fInfo.qualifiedName() + " has changed type");
          consistent = false;
      }

      if (!this.valueEquals(fInfo)) {
          Errors.error(Errors.CHANGED_VALUE, fInfo.position(),
                  "Field " + fInfo.qualifiedName() + " has changed value from "
                  + mValue + " to " + fInfo.mValue);
          consistent = false;
      }
      
      if (!mScope.equals(fInfo.mScope)) {
          Errors.error(Errors.CHANGED_SCOPE, fInfo.position(),
                  "Method " + fInfo.qualifiedName() + " changed scope from "
                  + mScope + " to " + fInfo.mScope);
          consistent = false;
      }
      
      if (mIsStatic != fInfo.mIsStatic) {
          Errors.error(Errors.CHANGED_STATIC, fInfo.position(),
                  "Field " + fInfo.qualifiedName() + " has changed 'static' qualifier");
          consistent = false;
      }
      
      if (mIsFinal != fInfo.mIsFinal) {
          Errors.error(Errors.CHANGED_FINAL, fInfo.position(),
                  "Field " + fInfo.qualifiedName() + " has changed 'final' qualifier");
          consistent = false;
      }
      
      if (mIsTransient != fInfo.mIsTransient) {
          Errors.error(Errors.CHANGED_TRANSIENT, fInfo.position(),
                  "Field " + fInfo.qualifiedName() + " has changed 'transient' qualifier");
          consistent = false;
      }
      
      if (mIsVolatile != fInfo.mIsVolatile) {
          Errors.error(Errors.CHANGED_VOLATILE, fInfo.position(),
                  "Field " + fInfo.qualifiedName() + " has changed 'volatile' qualifier");
          consistent = false;
      }
      
      if (!mDeprecated.equals(fInfo.mDeprecated)) {
          Errors.error(Errors.CHANGED_DEPRECATED, fInfo.position(),
                  "Field " + fInfo.qualifiedName() + " has changed deprecation state");
          consistent = false;
      }
      
      return consistent;
    }

}