01: /*
02: * Copyright 2006 Ethan Nicholas. All rights reserved.
03: * Use is subject to license terms.
04: */
05: package jaxx.compiler;
06:
07: /** Represents a field in a Java source file being generated for output. <code>JavaFields</code> are created
08: * and added to a {@link JavaFile}, which can then output Java source code.
09: */
10: public class JavaField {
11: private int modifiers;
12: private String type;
13: private String name;
14: private String initializer;
15:
16: /** Constructs a new <code>JavaField</code>. The <code>modifiers</code> parameter is a bit mask of the
17: * constants from {@link java.lang.reflect.Modifier}, and the <code>type</code> of the field should be
18: * represented as it would appear in Java source code.
19: *
20: *@param modifiers the modifier keywords that should appear as part of the field's declaration
21: *@param type the type of the field as it would appear in Java source code
22: *@param name the field's name
23: */
24: public JavaField(int modifiers, String type, String name) {
25: this (modifiers, type, name, null);
26: }
27:
28: /** Constructs a new <code>JavaField</code>. The <code>modifiers</code> parameter is a bit mask of the
29: * constants from <code>java.lang.reflect.Modifier</code>, and the <code>type</code> of the field should be
30: * represented as it would appear in Java source code. The <code>initializer</code> is the initial
31: * value of the field as it would appear in Java source code, or <code>null</code> to leave it at the
32: * default value.
33: *
34: *@param modifiers the modifier keywords that should appear as part of the field's declaration
35: *@param type the type of the field as it would appear in Java source code
36: *@param name the field's name
37: *@param initializer the initial value of the field, as it would appear in Java source code
38: */
39: public JavaField(int modifiers, String type, String name,
40: String initializer) {
41: this .modifiers = modifiers;
42: this .type = type;
43: this .name = name;
44: this .initializer = initializer;
45: }
46:
47: /** Returns a bit mask describing the modifier keywords which should appear as part of this field's
48: * declaration. See <code>java.lang.reflect.Modifier</code> for more information on decoding this
49: * field.
50: *
51: *@return the modifier bit mask
52: */
53: public int getModifiers() {
54: return modifiers;
55: }
56:
57: /** Returns the field's name.
58: *
59: *@return the field's name
60: */
61: public String getName() {
62: return name;
63: }
64:
65: /** Returns the field's type, as it would be represented in Java source code.
66: *
67: *@return the field's type
68: */
69: public String getType() {
70: return type;
71: }
72:
73: /** Returns the Java source code for this field.
74: *
75: *@return the Java source code for this field
76: */
77: public String toString() {
78: StringBuffer result = new StringBuffer();
79: result.append(JavaFile.getModifiersText(modifiers));
80: result.append(type);
81: result.append(' ');
82: result.append(name);
83: if (initializer != null) {
84: result.append(" = ");
85: result.append(initializer);
86: }
87: result.append(';');
88: return result.toString();
89: }
90: }
|