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 import org.clearsilver.HDF;
18 import org.clearsilver.CS;
20 import java.util.Comparator;
22 public class FieldInfo extends MemberInfo
24 public static final Comparator<FieldInfo> comparator = new Comparator<FieldInfo>() {
25 public int compare(FieldInfo a, FieldInfo b) {
26 return a.name().compareTo(b.name());
30 public FieldInfo(String name, ClassInfo containingClass, ClassInfo realContainingClass,
31 boolean isPublic, boolean isProtected,
32 boolean isPackagePrivate, boolean isPrivate,
33 boolean isFinal, boolean isStatic, boolean isTransient, boolean isVolatile,
34 boolean isSynthetic, TypeInfo type, String rawCommentText,
36 SourcePositionInfo position,
37 AnnotationInstanceInfo[] annotations)
39 super(rawCommentText, name, null, containingClass, realContainingClass,
40 isPublic, isProtected, isPackagePrivate, isPrivate,
41 isFinal, isStatic, isSynthetic, chooseKind(isFinal, isStatic), position,
43 mIsTransient = isTransient;
44 mIsVolatile = isVolatile;
46 mConstantValue = constantValue;
49 public FieldInfo cloneForClass(ClassInfo newContainingClass) {
50 return new FieldInfo(name(), newContainingClass, realContainingClass(),
51 isPublic(), isProtected(), isPackagePrivate(),
52 isPrivate(), isFinal(), isStatic(), isTransient(), isVolatile(),
53 isSynthetic(), mType, getRawCommentText(), mConstantValue, position(),
57 static String chooseKind(boolean isFinal, boolean isStatic)
59 if (isStatic && isFinal) {
66 public TypeInfo type()
71 public boolean isConstant()
73 return isStatic() && isFinal();
76 public TagInfo[] firstSentenceTags()
78 return comment().briefTags();
81 public TagInfo[] inlineTags()
83 return comment().tags();
86 public Object constantValue()
88 return mConstantValue;
91 public String constantLiteralValue()
93 return constantLiteralValue(mConstantValue);
96 public boolean isDeprecated() {
97 boolean deprecated = false;
98 if (!mDeprecatedKnown) {
99 boolean commentDeprecated = (comment().deprecatedTags().length > 0);
100 boolean annotationDeprecated = false;
101 for (AnnotationInstanceInfo annotation : annotations()) {
102 if (annotation.type().qualifiedName().equals("java.lang.Deprecated")) {
103 annotationDeprecated = true;
108 if (commentDeprecated != annotationDeprecated) {
109 Errors.error(Errors.DEPRECATION_MISMATCH, position(),
110 "Field " + mContainingClass.qualifiedName() + "." + name()
111 + ": @Deprecated annotation and @deprecated comment do not match");
114 mIsDeprecated = commentDeprecated | annotationDeprecated;
115 mDeprecatedKnown = true;
117 return mIsDeprecated;
120 public static String constantLiteralValue(Object val)
124 if (val instanceof Boolean
125 || val instanceof Byte
126 || val instanceof Short
127 || val instanceof Integer)
129 str = val.toString();
131 //catch all special values
132 else if (val instanceof Double){
133 Double dbl = (Double) val;
134 if (dbl.toString().equals("Infinity")){
136 } else if (dbl.toString().equals("-Infinity")) {
137 str = "(-1.0 / 0.0)";
138 } else if (dbl.isNaN()) {
141 str = dbl.toString();
144 else if (val instanceof Long) {
145 str = val.toString() + "L";
147 else if (val instanceof Float) {
148 Float fl = (Float) val;
149 if (fl.toString().equals("Infinity")) {
150 str = "(1.0f / 0.0f)";
151 } else if (fl.toString().equals("-Infinity")) {
152 str = "(-1.0f / 0.0f)";
153 } else if (fl.isNaN()) {
154 str = "(0.0f / 0.0f)";
156 str = val.toString() + "f";
159 else if (val instanceof Character) {
160 str = String.format("\'\\u%04x\'", val);
162 else if (val instanceof String) {
163 str = "\"" + javaEscapeString((String)val) + "\"";
166 str = "<<<<" +val.toString() + ">>>>";
175 public static String javaEscapeString(String str) {
177 final int N = str.length();
178 for (int i=0; i<N; i++) {
179 char c = str.charAt(i);
183 else if (c == '\t') {
186 else if (c == '\b') {
189 else if (c == '\r') {
192 else if (c == '\n') {
195 else if (c == '\f') {
198 else if (c == '\'') {
201 else if (c == '\"') {
204 else if (c >= ' ' && c <= '~') {
208 result += String.format("\\u%04x", new Integer((int)c));
215 public void makeHDF(HDF data, String base)
217 data.setValue(base + ".kind", kind());
218 type().makeHDF(data, base + ".type");
219 data.setValue(base + ".name", name());
220 data.setValue(base + ".href", htmlPage());
221 data.setValue(base + ".anchor", anchor());
222 TagInfo.makeHDF(data, base + ".shortDescr", firstSentenceTags());
223 TagInfo.makeHDF(data, base + ".descr", inlineTags());
224 TagInfo.makeHDF(data, base + ".deprecated", comment().deprecatedTags());
225 TagInfo.makeHDF(data, base + ".seeAlso", comment().seeTags());
226 data.setValue(base + ".since", getSince());
227 data.setValue(base + ".final", isFinal() ? "final" : "");
228 data.setValue(base + ".static", isStatic() ? "static" : "");
230 data.setValue(base + ".scope", "public");
232 else if (isProtected()) {
233 data.setValue(base + ".scope", "protected");
235 else if (isPackagePrivate()) {
236 data.setValue(base + ".scope", "");
238 else if (isPrivate()) {
239 data.setValue(base + ".scope", "private");
241 Object val = mConstantValue;
247 if (val instanceof Boolean) {
248 str = ((Boolean)val).toString();
250 else if (val instanceof Byte) {
251 dec = String.format("%d", val);
252 hex = String.format("0x%02x", val);
254 else if (val instanceof Character) {
255 dec = String.format("\'%c\'", val);
256 hex = String.format("0x%04x", val);
258 else if (val instanceof Double) {
259 str = ((Double)val).toString();
261 else if (val instanceof Float) {
262 str = ((Float)val).toString();
264 else if (val instanceof Integer) {
265 dec = String.format("%d", val);
266 hex = String.format("0x%08x", val);
268 else if (val instanceof Long) {
269 dec = String.format("%d", val);
270 hex = String.format("0x%016x", val);
272 else if (val instanceof Short) {
273 dec = String.format("%d", val);
274 hex = String.format("0x%04x", val);
276 else if (val instanceof String) {
277 str = "\"" + ((String)val) + "\"";
283 if (dec != null && hex != null) {
284 data.setValue(base + ".constantValue.dec", DroidDoc.escape(dec));
285 data.setValue(base + ".constantValue.hex", DroidDoc.escape(hex));
288 data.setValue(base + ".constantValue.str", DroidDoc.escape(str));
289 data.setValue(base + ".constantValue.isString", "1");
295 public boolean isExecutable()
300 public boolean isTransient()
305 public boolean isVolatile()
310 boolean mIsTransient;
312 boolean mDeprecatedKnown;
313 boolean mIsDeprecated;
315 Object mConstantValue;