2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.apicheck;
19 public class FieldInfo {
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;
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) {
39 mIsTransient = isTransient;
40 mIsVolatile = isVolatile;
44 mDeprecated = deprecated;
46 mExistsInBoth = false;
47 mSourcePosition = source;
51 public boolean isInBoth() {
54 public SourcePositionInfo position() {
55 return mSourcePosition;
58 public String name() {
62 public String qualifiedName() {
63 String parentQName = (mClass != null)
64 ? (mClass.qualifiedName() + ".")
66 return parentQName + name();
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))) {
78 // Null values are considered equal
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.
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);
96 } catch (NumberFormatException e) {
100 return mValue.equals(other.mValue);
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");
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);
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);
127 if (mIsStatic != fInfo.mIsStatic) {
128 Errors.error(Errors.CHANGED_STATIC, fInfo.position(),
129 "Field " + fInfo.qualifiedName() + " has changed 'static' qualifier");
133 if (mIsFinal != fInfo.mIsFinal) {
134 Errors.error(Errors.CHANGED_FINAL, fInfo.position(),
135 "Field " + fInfo.qualifiedName() + " has changed 'final' qualifier");
139 if (mIsTransient != fInfo.mIsTransient) {
140 Errors.error(Errors.CHANGED_TRANSIENT, fInfo.position(),
141 "Field " + fInfo.qualifiedName() + " has changed 'transient' qualifier");
145 if (mIsVolatile != fInfo.mIsVolatile) {
146 Errors.error(Errors.CHANGED_VOLATILE, fInfo.position(),
147 "Field " + fInfo.qualifiedName() + " has changed 'volatile' qualifier");
151 if (!mDeprecated.equals(fInfo.mDeprecated)) {
152 Errors.error(Errors.CHANGED_DEPRECATED, fInfo.position(),
153 "Field " + fInfo.qualifiedName() + " has changed deprecation state");