📄 javaclass.java
字号:
/*
* Copyright (c) 2003, Alexander Greif
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the Flow4J-Eclipse project nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.orthanc.flow4j.model.codegen.javasrc.structure;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import net.orthanc.flow4j.model.codegen.structure.ISrcStructure;
/**
* @author agreif
*
* TODO
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class JavaClass implements ISrcStructure, IJavadocHolder {
private JavadocBlock javadoc;
private String className;
private String superClassName;
private List interfaces = new ArrayList();
private List modifiers = new ArrayList();
private List fields = new ArrayList();
private CodeBlock staticInitializer; // will be initialized lazily
private List methods = new ArrayList();
private List innerClasses; // will be initialized lazily
private List constructors; // will be initialized lazily
/**
* Creates a JavaClass instance
*
*/
public JavaClass() {
super();
}
/**
* Creates a new java class instance with the given class name.
* @param className the name of the class
*/
public JavaClass(String className) {
this();
setClassName(className);
}
/**
* TODO
* @param className
*/
public void setClassName(String className) {
this.className = className;
}
/**
* Returns the class name
* @return the className
*/
public String getClassName() {
return className;
}
/**
* Sets the name of the super class
* @param string the name of the super class
*/
public void setSuperClassName(String superClassName) {
this.superClassName = superClassName;
}
/**
* Adds a modifier to the method.
* @param modifier the modifier
*/
public void addModifier(String modifier) {
modifiers.add(modifier);
}
/**
* Adds a method to the class
* @param method the method to be added
*/
public void addMethod(Method method) {
methods.add(method);
}
/**
* Adds a field to the class
* @param field the field to be added
*/
public void addField(Field field) {
fields.add(field);
}
/**
* Adds an inner class.
* if the inner class container is null then a new
* container will be created
* @param innerClass the inner class to add
*/
public void addInnerClass(JavaClass innerClass) {
if (innerClasses == null)
innerClasses = new ArrayList();
innerClasses.add(innerClass);
}
/**
* Adds a constructor.
* if the constructors container is null then a new
* container will be created
* @param constructor the constructor to add
*/
private void addConstructor(Constructor constructor) {
if (constructors == null)
constructors = new ArrayList();
constructors.add(constructor);
}
/**
* Returns a new Constructor instance, that is added to the
* constructors collection of this class.
* @return a new Constructor instance
*/
public Constructor getNewConstructor() {
Constructor c = new Constructor(className);
addConstructor(c);
return c;
}
/**
* returns true if the class already contains the field with the given name
* @param fieldName field name
* @return true if the class already contains the field with the given name
*/
public boolean hasField(String fieldName) {
for (Iterator iter = fields.iterator(); iter.hasNext();) {
Field field = (Field)iter.next();
if (field.getName().equals(fieldName))
return true;
}
return false;
}
/**
* Adds an interface to the class
* @param interfaceName the name of the interface
*/
public void addInterface(String interfaceName) {
interfaces.add(interfaceName);
}
/**
*
* Serializes the classes source code to the given Writer
* @param writer the writer where the contents should be serialized to
*/
public void serialize(Writer writer) throws IOException {
// javadoc
if (javadoc != null)
javadoc.serialize(writer);
// class
for (Iterator iter = modifiers.iterator(); iter.hasNext();)
writer.write((String)iter.next() + " ");
writer.write("class " + className + " ");
if (superClassName != null && superClassName.trim().length() != 0)
writer.write("extends " + superClassName + " ");
if (interfaces.size() != 0) {
writer.write("implements ");
for (Iterator iter = interfaces.iterator(); iter.hasNext();)
writer.write((String)iter.next() + " ");
}
writer.write("{");
// fields
for (Iterator iter = fields.iterator(); iter.hasNext();)
((Field)iter.next()).serialize(writer);
// static initializer
if (staticInitializer != null)
staticInitializer.serialize(writer);
// constructors
if (constructors != null) {
for (Iterator iter = constructors.iterator(); iter.hasNext();) {
Constructor constructor = (Constructor) iter.next();
constructor.serialize(writer);
}
}
// methods
for (Iterator iter = methods.iterator(); iter.hasNext();)
((Method)iter.next()).serialize(writer);
// inner classes
if (innerClasses != null) {
for (Iterator iter = innerClasses.iterator(); iter.hasNext();) {
JavaClass javaClass = (JavaClass) iter.next();
javaClass.serialize(writer);
}
}
writer.write("}");
}
/**
* Returns the static initializer field. If it is null the a new instance is created and then
* returned
* @return the static initializer field
*/
public CodeBlock getStaticInitializer() {
if (staticInitializer == null)
staticInitializer = new CodeBlock();
return staticInitializer;
}
/**
* Sets the class's javadoc.
* @see net.orthanc.flow4j.model.codegen.javasrc.structure.IJavadocHolder#addJavadoc(net.orthanc.flow4j.model.codegen.javasrc.structure.JavadocBlock)
*/
public void setJavadoc(JavadocBlock javadoc) {
this.javadoc = javadoc;
}
/** Returns the class's javadoc
* @see net.orthanc.flow4j.model.codegen.javasrc.structure.IJavadocHolder#getJavadoc()
*/
public JavadocBlock getJavadoc() {
return javadoc;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -