
org.enhydra.xml.xmlc.codegen.JavaField Maven / Gradle / Ivy
The newest version!
/*
* Enhydra Java Application Server Project
*
* The contents of this file are subject to the Enhydra Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License on
* the Enhydra web site ( http://www.enhydra.org/ ).
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific terms governing rights and limitations
* under the License.
*
* The Initial Developer of the Enhydra Application Server is Lutris
* Technologies, Inc. The Enhydra Application Server and portions created
* by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
* All Rights Reserved.
*
* Contributor(s):
*
* $Id: JavaField.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
*/
package org.enhydra.xml.xmlc.codegen;
import org.enhydra.xml.xmlc.XMLCError;
/**
* Class to store a class field.
* Objects of this class are immutable.
*/
public final class JavaField extends JavaVariable {
/**
* Initializer code, or null if not used.
*/
private JavaCode fInitializerCode;
/**
* Simple initializer, or null if not used.
*/
private String fInitializer;
/**
* Adjust modifiers based on other modifiers.
*/
private static int adjustModifiers(int modifiers) {
// Only public fields are include in interfaces.
if ((modifiers & (JavaModifiers.PROTECTED|JavaModifiers.PRIVATE)) != 0) {
modifiers |= JavaModifiers.OMIT_INTERFACE;
}
// Only static fields are included in interfaces.
if ((modifiers & JavaModifiers.STATIC) == 0) {
modifiers |= JavaModifiers.OMIT_INTERFACE;
}
// Fields included in interfaces are not included in the implementation.
if ((modifiers & JavaModifiers.OMIT_INTERFACE) == 0) {
modifiers |= JavaModifiers.OMIT_IMPLEMENTATION;
}
return modifiers;
}
/**
* Convert the initializer to the internal format.
*/
private void buildInitializer(Object initializer) {
if (initializer == null) {
// do nothing..
} else if (initializer instanceof String) {
fInitializer = (String)initializer;
} else if (initializer instanceof JavaCode) {
fInitializerCode = (JavaCode)initializer;
} else {
throw new XMLCError("Invalid type for field initializer: " + initializer.getClass());
}
}
/**
* Constructor with doc array.
* @param name The field name.
* @param type The field type.
* @param modifiers The modifier bit set.
* @param doc The field documentation, where each row is a line of
* the document. If null, no documention is created.
* @param initializer The code to initialize the variable, or
* null if none. Should not include `=' or closing `;'.
* This can be a String (for scaler initialization) or JavaCode object
* (for array initialization).
*/
public JavaField(String name,
String type,
int modifiers,
String[] doc,
Object initializer) {
super(name, type, adjustModifiers(modifiers), doc);
buildInitializer(initializer);
}
/**
* Constructor with doc array.
* @param name The field name.
* @param type The field type.
* @param modifiers The modifier bit set.
* @param doc The field documentation. If null, no documention is created.
* @param initializer The code to initialize the variable, or
* null if none. Should not include `=' or closing `;'.
* This can be a String (for scaler initialization) or JavaCode object
* (for array initialization).
*/
public JavaField(String name,
String type,
int modifiers,
String doc,
Object initializer) {
super(name, type, adjustModifiers(modifiers), doc);
buildInitializer(initializer);
}
/**
* Print declaration and initialization of field.
*/
public void print(IndentWriter out) {
String[] doc = getDoc();
if (doc != null) {
out.println("/**");
for (int idx = 0; idx < doc.length; idx++) {
out.print(" * ");
out.println(doc[idx]);
}
out.println(" */");
}
printDefinition(out);
if (fInitializer != null) {
// Simple initializer.
out.print(" = ");
out.print(fInitializer);
out.println(';');
} else if (fInitializerCode != null) {
// Array initializer
out.println(" = {");
out.enter();
fInitializerCode.print(out);
out.leave();
out.println("};");
} else {
out.println(";");
}
out.println();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy