KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > codegen > JavaField


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: JavaField.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.codegen;
25
26 import org.enhydra.xml.xmlc.XMLCError;
27
28 /**
29  * Class to store a class field.
30  * Objects of this class are immutable.
31  */

32 public final class JavaField extends JavaVariable {
33     /**
34      * Initializer code, or null if not used.
35      */

36     private JavaCode fInitializerCode;
37
38     /**
39      * Simple initializer, or null if not used.
40      */

41     private String JavaDoc fInitializer;
42     
43     /**
44      * Adjust modifiers based on other modifiers.
45      */

46     private static int adjustModifiers(int modifiers) {
47         // Only public fields are include in interfaces.
48
if ((modifiers & (JavaModifiers.PROTECTED|JavaModifiers.PRIVATE)) != 0) {
49             modifiers |= JavaModifiers.OMIT_INTERFACE;
50         }
51         // Only static fields are included in interfaces.
52
if ((modifiers & JavaModifiers.STATIC) == 0) {
53             modifiers |= JavaModifiers.OMIT_INTERFACE;
54         }
55         // Fields included in interfaces are not included in the implementation.
56
if ((modifiers & JavaModifiers.OMIT_INTERFACE) == 0) {
57             modifiers |= JavaModifiers.OMIT_IMPLEMENTATION;
58         }
59         return modifiers;
60     }
61
62     /**
63      * Convert the initializer to the internal format.
64      */

65     private void buildInitializer(Object JavaDoc initializer) {
66         if (initializer == null) {
67             // do nothing..
68
} else if (initializer instanceof String JavaDoc) {
69             fInitializer = (String JavaDoc)initializer;
70         } else if (initializer instanceof JavaCode) {
71             fInitializerCode = (JavaCode)initializer;
72         } else {
73             throw new XMLCError("Invalid type for field initializer: " + initializer.getClass());
74         }
75     }
76
77     /**
78      * Constructor with doc array.
79      * @param name The field name.
80      * @param type The field type.
81      * @param modifiers The modifier bit set.
82      * @param doc The field documentation, where each row is a line of
83      * the document. If null, no documention is created.
84      * @param initializer The code to initialize the variable, or
85      * null if none. Should not include `=' or closing `;'.
86      * This can be a String (for scaler initialization) or JavaCode object
87      * (for array initialization).
88      */

89     public JavaField(String JavaDoc name,
90                      String JavaDoc type,
91                      int modifiers,
92                      String JavaDoc[] doc,
93                      Object JavaDoc initializer) {
94         super(name, type, adjustModifiers(modifiers), doc);
95         buildInitializer(initializer);
96     }
97
98     /**
99      * Constructor with doc array.
100      * @param name The field name.
101      * @param type The field type.
102      * @param modifiers The modifier bit set.
103      * @param doc The field documentation. If null, no documention is created.
104      * @param initializer The code to initialize the variable, or
105      * null if none. Should not include `=' or closing `;'.
106      * This can be a String (for scaler initialization) or JavaCode object
107      * (for array initialization).
108      */

109     public JavaField(String JavaDoc name,
110                      String JavaDoc type,
111                      int modifiers,
112                      String JavaDoc doc,
113                      Object JavaDoc initializer) {
114         super(name, type, adjustModifiers(modifiers), doc);
115         buildInitializer(initializer);
116     }
117     /**
118      * Print declaration and initialization of field.
119      */

120     public void print(IndentWriter out) {
121         String JavaDoc[] doc = getDoc();
122         if (doc != null) {
123             out.println("/**");
124             for (int idx = 0; idx < doc.length; idx++) {
125                 out.print(" * ");
126                 out.println(doc[idx]);
127             }
128             out.println(" */");
129         }
130         printDefinition(out);
131         if (fInitializer != null) {
132             // Simple initializer.
133
out.print(" = ");
134             out.print(fInitializer);
135             out.println(';');
136         } else if (fInitializerCode != null) {
137             // Array initializer
138
out.println(" = {");
139             out.enter();
140             fInitializerCode.print(out);
141             out.leave();
142             out.println("};");
143         } else {
144             out.println(";");
145         }
146         out.println();
147     }
148 }
149
Popular Tags