OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libjava/] [classpath/] [tools/] [gnu/] [classpath/] [tools/] [gjdoc/] [FieldDocImpl.java] - Blame information for rev 779

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 779 jeremybenn
/* gnu.classpath.tools.gjdoc.FieldDocImpl
2
   Copyright (C) 2001 Free Software Foundation, Inc.
3
 
4
This file is part of GNU Classpath.
5
 
6
GNU Classpath is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2, or (at your option)
9
any later version.
10
 
11
GNU Classpath is distributed in the hope that it will be useful, but
12
WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with GNU Classpath; see the file COPYING.  If not, write to the
18
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19
02111-1307 USA.
20
 
21
Linking this library statically or dynamically with other modules is
22
making a combined work based on this library.  Thus, the terms and
23
conditions of the GNU General Public License cover the whole
24
combination.
25
 
26
As a special exception, the copyright holders of this library give you
27
permission to link this library with independent modules to produce an
28
executable, regardless of the license terms of these independent
29
modules, and to copy and distribute the resulting executable under
30
terms of your choice, provided that you also meet, for each linked
31
independent module, the terms and conditions of the license of that
32
module.  An independent module is a module which is not derived from
33
or based on this library.  If you modify this library, you may extend
34
this exception to your version of the library, but you are not
35
obligated to do so.  If you do not wish to do so, delete this
36
exception statement from your version. */
37
 
38
package gnu.classpath.tools.gjdoc;
39
 
40
import java.util.*;
41
import com.sun.javadoc.*;
42
import java.lang.reflect.Modifier;
43
 
44
import gnu.classpath.tools.gjdoc.expr.Evaluator;
45
import gnu.classpath.tools.gjdoc.expr.CircularExpressionException;
46
import gnu.classpath.tools.gjdoc.expr.IllegalExpressionException;
47
 
48
public class FieldDocImpl
49
   extends MemberDocImpl
50
   implements FieldDoc, Cloneable
51
{
52
 
53
   private boolean isTransient;
54
   private boolean isVolatile;
55
   private String valueLiteral;
56
   private Object constantValue;
57
   private boolean constantValueEvaluated;
58
 
59
   private FieldDocImpl(ClassDoc containingClass,
60
                        PackageDoc containingPackage,
61
                        SourcePosition position) {
62
 
63
      super(containingClass,
64
            containingPackage,
65
            position);
66
   }
67
 
68
   private static FieldDocImpl createFieldDoc(FieldDocImpl prototype,
69
                                              String fieldDef,
70
                                              String fieldValueLiteral)
71
   {
72
      if (null != fieldValueLiteral && fieldValueLiteral.length() == 0) {
73
         fieldValueLiteral = null;
74
      }
75
 
76
      try {
77
         FieldDocImpl fieldDoc=(FieldDocImpl)prototype.clone();
78
         String dimSuffix="";
79
         while (fieldDef.trim().endsWith("[")
80
                || fieldDef.trim().endsWith("]")) {
81
            fieldDef=fieldDef.trim();
82
            dimSuffix=fieldDef.charAt(fieldDef.length()-1)+dimSuffix;
83
            fieldDef=fieldDef.substring(0,fieldDef.length()-1);
84
         }
85
 
86
         fieldDoc.setTypeName(fieldDoc.getTypeName()+dimSuffix);
87
         fieldDoc.setName(fieldDef.trim());
88
         fieldDoc.setValueLiteral(fieldValueLiteral);
89
         return fieldDoc;
90
      }
91
      catch (CloneNotSupportedException e) {
92
         // should not happen
93
         e.printStackTrace();
94
         return null;
95
      }
96
   }
97
 
98
   public static Collection createFromSource(ClassDoc containingClass,
99
                                             PackageDoc containingPackage,
100
                                             char[] source, int startIndex, int endIndex) {
101
 
102
      List rcList=new ArrayList();
103
 
104
      FieldDocImpl fd=new FieldDocImpl(containingClass,
105
                                       containingPackage,
106
                                       DocImpl.getPosition(containingClass, source, startIndex));
107
 
108
      int ndx=fd.parseModifiers(source, startIndex, endIndex);
109
 
110
      if (containingClass.isInterface()) {
111
         fd.accessLevel = ACCESS_PUBLIC;
112
      }
113
 
114
      final int STATE_FIELDNAME   = 1;
115
      final int STATE_FIELDVALUE  = 2;
116
      final int STATE_QUOTE       = 3;
117
      final int STATE_QUOTEBS     = 4;
118
      final int STATE_SQUOTE      = 5;
119
      final int STATE_SQUOTEBS    = 6;
120
      final int STATE_COMMENT     = 7;
121
      final int STATE_LINECOMMENT = 8;
122
 
123
      int lastFieldDefStart = ndx;
124
      int state = STATE_FIELDNAME;
125
      int prevState = state;
126
 
127
      int bracketCount = 0;
128
 
129
      StringBuffer fieldNameBuf = new StringBuffer();
130
      StringBuffer fieldValueLiteralBuf = new StringBuffer();
131
 
132
      for (int i=ndx; i<endIndex; ++i) {
133
 
134
         char c = source[i];
135
         char nextChar = '\0';
136
         if (i + 1 < endIndex) {
137
            nextChar = source[i + 1];
138
         }
139
         switch (state) {
140
         case STATE_FIELDNAME:
141
            if ('/' == c && '/' == nextChar) {
142
               prevState = state;
143
               state = STATE_LINECOMMENT;
144
            }
145
            else if ('/' == c && '*' == nextChar) {
146
               prevState = state;
147
               state = STATE_COMMENT;
148
            }
149
            else if (',' == c || ';' == c) {
150
               rcList.add(createFieldDoc(fd, fieldNameBuf.toString(), null));
151
               fieldNameBuf.setLength(0);
152
            }
153
            else if ('=' == c) {
154
               state = STATE_FIELDVALUE;
155
            }
156
            else if (!(' ' == c || '\n' == c || '\r' == c || '\t' == c)) {
157
               fieldNameBuf.append(c);
158
            }
159
            break;
160
 
161
         case STATE_FIELDVALUE:
162
            if ('/' == c && '/' == nextChar) {
163
               prevState = state;
164
               state = STATE_LINECOMMENT;
165
            }
166
            else if ('/' == c && '*' == nextChar) {
167
               prevState = state;
168
               state = STATE_COMMENT;
169
            }
170
            else if ('\"' == c) {
171
               prevState = state;
172
               state = STATE_QUOTE;
173
               fieldValueLiteralBuf.append(c);
174
            }
175
            else if ('\'' == c) {
176
               prevState = state;
177
               state = STATE_SQUOTE;
178
               fieldValueLiteralBuf.append(c);
179
            }
180
            else if ('{' == c || '(' == c) {
181
               ++ bracketCount;
182
               fieldValueLiteralBuf.append(c);
183
            }
184
            else if ('}' == c || ')' == c) {
185
               -- bracketCount;
186
               fieldValueLiteralBuf.append(c);
187
            }
188
            else if (0 == bracketCount && (',' == c || ';' == c)) {
189
               rcList.add(createFieldDoc(fd, fieldNameBuf.toString(),
190
                                         fieldValueLiteralBuf.toString()));
191
               fieldNameBuf.setLength(0);
192
               fieldValueLiteralBuf.setLength(0);
193
               state = STATE_FIELDNAME;
194
            }
195
            else {
196
               fieldValueLiteralBuf.append(c);
197
            }
198
            break;
199
 
200
         case STATE_QUOTE:
201
            fieldValueLiteralBuf.append(c);
202
            if ('\\' == c) {
203
               state = STATE_QUOTEBS;
204
            }
205
            else if ('\"' == c) {
206
               state = prevState;
207
            }
208
            break;
209
 
210
         case STATE_SQUOTE:
211
            fieldValueLiteralBuf.append(c);
212
            if ('\\' == c) {
213
               state = STATE_SQUOTEBS;
214
            }
215
            else if ('\'' == c) {
216
               state = prevState;
217
            }
218
            break;
219
 
220
         case STATE_QUOTEBS:
221
            fieldValueLiteralBuf.append(c);
222
            state = STATE_QUOTE;
223
            break;
224
 
225
         case STATE_SQUOTEBS:
226
            fieldValueLiteralBuf.append(c);
227
            state = STATE_SQUOTE;
228
            break;
229
 
230
         case STATE_LINECOMMENT:
231
            if ('\n' == c) {
232
               state = prevState;
233
            }
234
            break;
235
 
236
         case STATE_COMMENT:
237
            if ('*' == c && '/' == nextChar) {
238
               ++ i;
239
               state = prevState;
240
            }
241
            break;
242
         }
243
      }
244
 
245
      if (fieldNameBuf.length() > 0) {
246
         rcList.add(createFieldDoc(fd, fieldNameBuf.toString(),
247
                                   fieldValueLiteralBuf.toString()));
248
      }
249
 
250
      return rcList;
251
   }
252
 
253
   public boolean isField() {
254
      return true;
255
   }
256
 
257
   public boolean isTransient() { return isTransient; }
258
 
259
   public boolean isVolatile() { return isVolatile; }
260
 
261
   public SerialFieldTag[] serialFieldTags() { return new SerialFieldTag[0]; }
262
 
263
   public int modifierSpecifier() {
264
      return super.modifierSpecifier()
265
         | (isVolatile()?Modifier.VOLATILE:0)
266
         | (isTransient()?Modifier.TRANSIENT:0)
267
         ;
268
   }
269
 
270
   protected boolean processModifier(String word) {
271
      if (super.processModifier(word)) {
272
         return true;
273
      }
274
      else if (word.equals("transient")) {
275
         isTransient=true;
276
         return true;
277
      }
278
      else if (word.equals("volatile")) {
279
         isVolatile=true;
280
         return true;
281
      }
282
      else {
283
         return false;
284
      }
285
   }
286
 
287
   void resolve() {
288
      resolveTags();
289
   }
290
 
291
   public boolean hasSerialTag() {
292
      return true; //tagMap.get("serial")!=null;
293
   }
294
 
295
   public String toString() { return name(); }
296
 
297
   public Object constantValue() {
298
      return constantValue(new HashSet());
299
   }
300
 
301
   public Object constantValue(Set visitedFields) {
302
      if (!isStatic()
303
          || !isFinal()
304
          || (!type().isPrimitive() && !"java.lang.String".equals(type().qualifiedTypeName()))
305
          || type.dimension().length()>0
306
          || null == valueLiteral) {
307
 
308
         return null;
309
 
310
      }
311
      else {
312
         if (!constantValueEvaluated) {
313
 
314
            visitedFields.add(this);
315
 
316
            String expression = "(" + type().typeName() + ")(" + valueLiteral + ")";
317
            try {
318
               this.constantValue = Evaluator.evaluate(expression,
319
                                                       visitedFields,
320
                                                       (ClassDocImpl)containingClass());
321
            }
322
            catch (CircularExpressionException e) {
323
               // FIXME: This should use the error reporter
324
               System.err.println("WARNING: Cannot resolve expression for field " + containingClass.qualifiedTypeName() + "." + name() + ": " + e.getMessage());
325
            }
326
            catch (IllegalExpressionException ignore) {
327
            }
328
            constantValueEvaluated = true;
329
         }
330
         return this.constantValue;
331
      }
332
   }
333
 
334
   private static void appendCharString(StringBuffer result, char c, boolean inSingleCuotes)
335
   {
336
      switch (c) {
337
      case '\b': result.append("\\b"); break;
338
      case '\t': result.append("\\t"); break;
339
      case '\n': result.append("\\n"); break;
340
      case '\f': result.append("\\f"); break;
341
      case '\r': result.append("\\r"); break;
342
      case '\"': result.append("\\\""); break;
343
      case '\'': result.append(inSingleCuotes ? "\\'" : "'"); break;
344
      default:
345
         if (c >= 32 && c <= 127) {
346
            result.append(c);
347
         }
348
         else {
349
            result.append("\\u");
350
            String hexValue = Integer.toString((int)c, 16);
351
            int zeroCount = 4 - hexValue.length();
352
            for (int i=0; i<zeroCount; ++i) {
353
               result.append('0');
354
            }
355
            result.append(hexValue);
356
         }
357
      }
358
   }
359
 
360
   public String constantValueExpression() {
361
      Object value = constantValue();
362
 
363
      if (null == value) {
364
         return "null";
365
      }
366
      else if (value instanceof String) {
367
         StringBuffer result = new StringBuffer("\"");
368
         char[] chars = ((String)value).toCharArray();
369
         for (int i=0; i<chars.length; ++i) {
370
            appendCharString(result, chars[i], false);
371
         }
372
         result.append("\"");
373
         return result.toString();
374
      }
375
      else if (value instanceof Float) {
376
         return value.toString() + "f";
377
      }
378
      else if (value instanceof Long) {
379
         return value.toString() + "L";
380
      }
381
      else if (value instanceof Character) {
382
         StringBuffer result = new StringBuffer("'");
383
         appendCharString(result, ((Character)value).charValue(), false);
384
         result.append("'");
385
         return result.toString();
386
      }
387
      else /* if (value instanceof Double
388
               || value instanceof Integer
389
               || value instanceof Short
390
               || value instanceof Byte) */ {
391
         return value.toString();
392
      }
393
   }
394
 
395
   void setValueLiteral(String valueLiteral)
396
   {
397
      this.valueLiteral = valueLiteral;
398
   }
399
 
400
   public boolean isStatic()
401
   {
402
      return super.isStatic() || containingClass().isInterface();
403
   }
404
 
405
   public boolean isFinal()
406
   {
407
      return super.isFinal() || containingClass().isInterface();
408
   }
409
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.