OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / build / tools / apicheck / src / com / android / apicheck / FieldInfo.java
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.android.apicheck;
18
19 public class FieldInfo {
20   
21     private String mName;
22     private String mType;
23     private boolean mIsTransient;
24     private boolean mIsVolatile;
25     private String mValue;
26     private boolean mIsStatic;
27     private boolean mIsFinal;
28     private String mDeprecated;
29     private String mScope;
30     private boolean mExistsInBoth;
31     private SourcePositionInfo mSourcePosition;
32     private ClassInfo mClass;
33     
34     public FieldInfo (String name, String type, boolean isTransient, boolean isVolatile,
35                        String value, boolean isStatic, boolean isFinal, String deprecated,
36                        String scope, SourcePositionInfo source, ClassInfo parent) {
37         mName = name;
38         mType = type;
39         mIsTransient = isTransient;
40         mIsVolatile = isVolatile;
41         mValue = value;
42         mIsStatic = isStatic;
43         mIsFinal = isFinal;
44         mDeprecated = deprecated;
45         mScope = scope;
46         mExistsInBoth = false;
47         mSourcePosition = source;
48         mClass = parent;
49     }
50     
51     public boolean isInBoth() {
52         return mExistsInBoth;
53     }
54     public SourcePositionInfo position() {
55         return mSourcePosition;
56     }
57     
58     public String name() {
59         return mName;
60     }
61     
62     public String qualifiedName() {
63         String parentQName = (mClass != null)
64                 ? (mClass.qualifiedName() + ".")
65                 : "";
66         return parentQName + name();
67     }
68     
69     // Check the declared value with a typed comparison, not a string comparison,
70     // to accommodate toolchains with different fp -> string conversions.
71     public boolean valueEquals(FieldInfo other) {
72         // Type mismatch means nonequal, as does a null/non-null mismatch
73         if (!mType.equals(other.mType)
74                 || ((mValue == null) != (other.mValue == null))) {
75             return false;
76         }
77
78         // Null values are considered equal
79         if (mValue == null) {
80             return true;
81         }
82
83         // Floating point gets an implementation-type comparison; all others just use the string
84         // If float/double parse fails, fall back to string comparison -- it means that it's a
85         // canonical droiddoc-generated constant expression that represents a NaN.
86         try {
87             if (mType.equals("float")) {
88                 float val = Float.parseFloat(mValue);
89                 float otherVal = Float.parseFloat(other.mValue);
90                 return (val == otherVal);
91             } else if (mType.equals("double")) {
92                 double val = Double.parseDouble(mValue);
93                 double otherVal = Double.parseDouble(other.mValue);
94                 return (val == otherVal);
95             }
96         } catch (NumberFormatException e) {
97             // fall through
98         }
99         
100         return mValue.equals(other.mValue);
101     }
102
103     public boolean isConsistent(FieldInfo fInfo) {
104       fInfo.mExistsInBoth = true;
105       mExistsInBoth = true;
106       boolean consistent = true;
107       if (!mType.equals(fInfo.mType)) {
108           Errors.error(Errors.CHANGED_TYPE, fInfo.position(),
109                   "Field " + fInfo.qualifiedName() + " has changed type");
110           consistent = false;
111       }
112
113       if (!this.valueEquals(fInfo)) {
114           Errors.error(Errors.CHANGED_VALUE, fInfo.position(),
115                   "Field " + fInfo.qualifiedName() + " has changed value from "
116                   + mValue + " to " + fInfo.mValue);
117           consistent = false;
118       }
119       
120       if (!mScope.equals(fInfo.mScope)) {
121           Errors.error(Errors.CHANGED_SCOPE, fInfo.position(),
122                   "Method " + fInfo.qualifiedName() + " changed scope from "
123                   + mScope + " to " + fInfo.mScope);
124           consistent = false;
125       }
126       
127       if (mIsStatic != fInfo.mIsStatic) {
128           Errors.error(Errors.CHANGED_STATIC, fInfo.position(),
129                   "Field " + fInfo.qualifiedName() + " has changed 'static' qualifier");
130           consistent = false;
131       }
132       
133       if (mIsFinal != fInfo.mIsFinal) {
134           Errors.error(Errors.CHANGED_FINAL, fInfo.position(),
135                   "Field " + fInfo.qualifiedName() + " has changed 'final' qualifier");
136           consistent = false;
137       }
138       
139       if (mIsTransient != fInfo.mIsTransient) {
140           Errors.error(Errors.CHANGED_TRANSIENT, fInfo.position(),
141                   "Field " + fInfo.qualifiedName() + " has changed 'transient' qualifier");
142           consistent = false;
143       }
144       
145       if (mIsVolatile != fInfo.mIsVolatile) {
146           Errors.error(Errors.CHANGED_VOLATILE, fInfo.position(),
147                   "Field " + fInfo.qualifiedName() + " has changed 'volatile' qualifier");
148           consistent = false;
149       }
150       
151       if (!mDeprecated.equals(fInfo.mDeprecated)) {
152           Errors.error(Errors.CHANGED_DEPRECATED, fInfo.position(),
153                   "Field " + fInfo.qualifiedName() + " has changed deprecation state");
154           consistent = false;
155       }
156       
157       return consistent;
158     }
159
160 }