📄 jclass.java
字号:
/**
* Redistribution and use of this software and associated documentation
* ("Software"), with or without modification, are permitted provided
* that the following conditions are met:
*
* 1. Redistributions of source code must retain copyright
* statements and notices. Redistributions must also contain a
* copy of this document.
*
* 2. 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.
*
* 3. The name "Exolab" must not be used to endorse or promote
* products derived from this Software without prior written
* permission of Intalio, Inc. For written permission,
* please contact info@exolab.org.
*
* 4. Products derived from this Software may not be called "Exolab"
* nor may "Exolab" appear in their names without prior written
* permission of Intalio, Inc. Exolab is a registered
* trademark of Intalio, Inc.
*
* 5. Due credit should be given to the Exolab Project
* (http://www.exolab.org/).
*
* THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESSED 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
* INTALIO, INC. OR ITS 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.
*
* Copyright 1999-2000 (C) Intalio, Inc. All Rights Reserved.
*
* $Id: JClass.java,v 1.1 2001/08/07 18:58:43 joncrlsn Exp $
*/
package org.exolab.javasource;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.io.PrintWriter;
import java.util.Vector;
/**
* A representation of the Java Source code for a Java Class. This is
* a useful utility when creating in memory source code
* @author <a href="mailto:kvisco@intalio.com">Keith Visco</a>
* @version $Revision: 1.1 $ $Date: 2001/08/07 18:58:43 $
**/
public class JClass extends JType {
public static final JClass String = new JClass("String");
public static final JClass BigDecimal = new JClass("java.math.BigDecimal");
public static final JClass Boolean = new JClass("Boolean");
public static final JClass Byte = new JClass("Byte");
public static final JClass Short = new JClass("Short");
public static final JClass Integer = new JClass("Integer");
public static final JClass Long = new JClass("Long");
public static final JClass Float = new JClass("Float");
public static final JClass Double = new JClass("Double");
public static final JClass Object = new JClass("Object");
/**
* The Id for Source control systems
* I needed to separate this line to prevent CVS from
* expanding it here! ;-)
**/
private static final String DEFAULT_HEADER
= "$"+"Id";
/**
* The version for JavaDoc
* I needed to separate this line to prevent CVS from
* expanding it here! ;-)
**/
private static final String version = "$"+"Revision$ $"+"Date$";
private JComment header = null;
/**
* List of imported classes and packages
**/
private Vector imports = null;
private String superClass = null;
/**
* The list of member variables of this JClass
**/
private JNamedMap members = null;
/**
* The list of constructors for this JClass
**/
private Vector constructors = null;
/**
* The set of interfaces implemented by this JClass
**/
private Vector interfaces = null;
private JDocComment jdc = null;
/**
* The list of methods of this JClass
**/
private Vector methods = null;
/**
* The JModifiers for this JClass, which allows us to
* change the resulting qualifiers
**/
private JModifiers modifiers = null;
/**
* The package to which this JClass belongs
**/
private String packageName = null;
private Vector packageClasses = null;
/**
* Creates a new Class with the given name
* @param name the name of the Class to create
* @exception IllegalArgumentException when the given name
* is not a valid Class name
**/
public JClass(String name)
throws IllegalArgumentException
{
super(name);
this.packageName = getPackageFromClassName(name);
imports = new Vector();
interfaces = new Vector();
jdc = new JDocComment();
constructors = new Vector();
members = new JNamedMap();
methods = new Vector();
modifiers = new JModifiers();
packageClasses = new Vector();
//-- initialize default Java doc
jdc.addDescriptor(JDocDescriptor.createVersionDesc(version));
} //-- JClass
public void addImport(String className) {
if (className == null) return;
if (className.length() == 0) return;
//-- getPackageName
String pkgName = getPackageFromClassName(className);
if (pkgName != null) {
if (pkgName.equals(this.packageName)) return;
if (pkgName.equals("java.lang")) return;
//-- for readabilty keep import list sorted, and make sure
//-- we do not include more than one of the same import
for (int i = 0; i < imports.size(); i++) {
String imp = (String)imports.elementAt(i);
if (imp.equals(className)) return;
if (imp.compareTo(className) > 0) {
imports.insertElementAt(className, i);
return;
}
}
imports.addElement(className);
}
} //-- addImport
public void addInterface(String interfaceName) {
if (!interfaces.contains(interfaceName))
interfaces.addElement(interfaceName);
} //-- addInterface
/**
* Adds the given Constructor to this classes list of constructors.
* The constructor must have been created with this JClass'
* createConstructor.
* @exception IllegalArgumentException
**/
public void addConstructor(JConstructor constructor)
throws IllegalArgumentException
{
if (constructor == null)
throw new IllegalArgumentException("Constructors cannot be null");
if (constructor.getDeclaringClass() == this) {
/** check signatures (add later) **/
constructors.addElement(constructor);
}
else {
String err = "The given JConstructor was not created ";
err += "by this JClass";
throw new IllegalArgumentException(err);
}
}
public void addMember(JMember jMember)
throws IllegalArgumentException
{
if (jMember == null) {
throw new IllegalArgumentException("Class members cannot be null");
}
if (members.get(jMember.getName()) != null) {
String err = "duplicate name found: " + jMember.getName();
throw new IllegalArgumentException(err);
}
members.put(jMember.getName(), jMember);
// if member is of a type not imported by this class
// then add import
JType type = jMember.getType();
while (type.isArray()) type = type.getComponentType();
if (!type.isPrimitive()) {
addImport( ((JClass)type).getName());
}
} //-- addMember
public void addMethod(JMethod jMethod)
throws IllegalArgumentException
{
if (jMethod == null) {
throw new IllegalArgumentException("Class methods cannot be null");
}
//-- check method name and signatures *add later*
//-- keep method list sorted for esthetics when printing
//-- START SORT :-)
boolean added = false;
short modifierVal = 0;
JModifiers modifiers = jMethod.getModifiers();
if (modifiers.isAbstract()) {
this.modifiers.setAbstract(true);
}
for (int i = 0; i < methods.size(); i++) {
JMethod tmp = (JMethod) methods.elementAt(i);
//-- first compare modifiers
if (tmp.getModifiers().isPrivate()) {
if (!modifiers.isPrivate()) {
methods.insertElementAt(jMethod, i);
added = true;
break;
}
}
//-- compare names
if (jMethod.getName().compareTo(tmp.getName()) < 0) {
methods.insertElementAt(jMethod, i);
added = true;
break;
}
}
//-- END SORT
if (!added) methods.addElement(jMethod);
//-- check parameter packages to make sure we have them
//-- in our import list
String[] pkgNames = jMethod.getParameterClassNames();
for (int i = 0; i < pkgNames.length; i++) {
addImport(pkgNames[i]);
}
//-- check return type to make sure it's included in the
//-- import list
JType jType = jMethod.getReturnType();
if (jType != null) {
while (jType.isArray())
jType = jType.getComponentType();
if (!jType.isPrimitive())
addImport( ((JClass)jType).getName());
}
//-- check exceptions
JClass[] exceptions = jMethod.getExceptions();
for (int i = 0; i < exceptions.length; i++) {
addImport(exceptions[i].getName());
}
} //-- addMethod
public void addMethods(JMethod[] jMethods)
throws IllegalArgumentException
{
for (int i = 0; i < jMethods.length; i++)
addMethod(jMethods[i]);
} //-- addMethods
/**
* Adds a JClass which should be printed in the same
* source file as this JClass
**
public void addPackageClass(JClass jClass) {
if (jClass != null) packageClasses.addElement(jClass);
} //-- addPackageClass
*/
public JConstructor createConstructor() {
return new JConstructor(this);
} //-- createConstructor
public JConstructor getConstructor(int index) {
return (JConstructor)constructors.elementAt(index);
} //-- getConstructor
public JConstructor[] getConstructors() {
int size = constructors.size();
JConstructor[] jcArray = new JConstructor[size];
for (int i = 0; i < constructors.size(); i++) {
jcArray[i] = (JConstructor)constructors.elementAt(i);
}
return jcArray;
} //-- getConstructors
/**
* Returns the name of the file that this JClass would be
* printed as, given a call to #print.
*
* @param destDir the destination directory. This may be null.
* @return the name of the file that this JClass would be
* printed as, given a call to #print.
**/
public String getFilename(String destDir) {
String filename = getLocalName() + ".java";
if ((packageName != null) && (packageName.length() > 0)) {
String path = packageName.replace('.',File.separatorChar);
File pathFile;
if (destDir==null)
pathFile=new File(path);
else
pathFile=new File(destDir,path);
if (!pathFile.exists()) {
pathFile.mkdirs();
}
filename = pathFile.toString()+File.separator+filename;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -