325N/A/*
325N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
325N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
325N/A *
325N/A * This code is free software; you can redistribute it and/or modify it
325N/A * under the terms of the GNU General Public License version 2 only, as
325N/A * published by the Free Software Foundation. Oracle designates this
325N/A * particular file as subject to the "Classpath" exception as provided
325N/A * by Oracle in the LICENSE file that accompanied this code.
325N/A *
325N/A * This code is distributed in the hope that it will be useful, but WITHOUT
325N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
325N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
325N/A * version 2 for more details (a copy is included in the LICENSE file that
325N/A * accompanied this code).
325N/A *
325N/A * You should have received a copy of the GNU General Public License version
325N/A * 2 along with this work; if not, write to the Free Software Foundation,
325N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
325N/A *
325N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
325N/A * or visit www.oracle.com if you need additional information or have any
325N/A * questions.
325N/A */
325N/A
325N/Apackage com.sun.codemodel.internal;
325N/A
325N/Aimport java.lang.annotation.Annotation;
325N/Aimport java.util.ArrayList;
325N/Aimport java.util.Collection;
325N/Aimport java.util.Collections;
325N/Aimport java.util.Iterator;
325N/Aimport java.util.LinkedHashMap;
325N/Aimport java.util.List;
325N/Aimport java.util.Map;
325N/Aimport java.util.Set;
325N/Aimport java.util.TreeMap;
325N/Aimport java.util.TreeSet;
325N/A
325N/A/**
325N/A * A generated Java class/interface/enum/....
325N/A *
325N/A * <p>
325N/A * This class models a declaration, and since a declaration can be always
325N/A * used as a reference, it inherits {@link JClass}.
325N/A *
325N/A * <h2>Where to go from here?</h2>
325N/A * <p>
325N/A * You'd want to generate fields and methods on a class.
325N/A * See {@link #method(int, JType, String)} and {@link #field(int, JType, String)}.
325N/A */
325N/Apublic class JDefinedClass
325N/A extends JClass
325N/A implements JDeclaration, JClassContainer, JGenerifiable, JAnnotatable, JDocCommentable {
325N/A
325N/A /** Name of this class. Null if anonymous. */
325N/A private String name = null;
325N/A
325N/A /** Modifiers for the class declaration */
325N/A private JMods mods;
325N/A
325N/A /** Name of the super class of this class. */
325N/A private JClass superClass;
325N/A
325N/A /** List of interfaces that this class implements */
325N/A private final Set<JClass> interfaces = new TreeSet<JClass>();
325N/A
325N/A /** Fields keyed by their names. */
325N/A /*package*/ final Map<String,JFieldVar> fields = new LinkedHashMap<String,JFieldVar>();
325N/A
325N/A /** Static initializer, if this class has one */
325N/A private JBlock init = null;
325N/A
325N/A /** class javadoc */
325N/A private JDocComment jdoc = null;
325N/A
325N/A /** Set of constructors for this class, if any */
325N/A private final List<JMethod> constructors = new ArrayList<JMethod>();
325N/A
325N/A /** Set of methods that are members of this class */
325N/A private final List<JMethod> methods = new ArrayList<JMethod>();
325N/A
325N/A /**
325N/A * Nested classes as a map from name to JDefinedClass.
325N/A * The name is all capitalized in a case sensitive file system
325N/A * ({@link JCodeModel#isCaseSensitiveFileSystem}) to avoid conflicts.
325N/A *
325N/A * Lazily created to save footprint.
325N/A *
325N/A * @see #getClasses()
325N/A */
325N/A private Map<String,JDefinedClass> classes;
325N/A
325N/A
325N/A /**
325N/A * Flag that controls whether this class should be really generated or not.
325N/A *
325N/A * Sometimes it is useful to generate code that refers to class X,
325N/A * without actually generating the code of X.
325N/A * This flag is used to surpress X.java file in the output.
325N/A */
325N/A private boolean hideFile = false;
325N/A
325N/A /**
325N/A * Client-app spcific metadata associated with this user-created class.
325N/A */
325N/A public Object metadata;
325N/A
325N/A /**
325N/A * String that will be put directly inside the generated code.
325N/A * Can be null.
325N/A */
325N/A private String directBlock;
325N/A
325N/A /**
325N/A * If this is a package-member class, this is {@link JPackage}.
325N/A * If this is a nested class, this is {@link JDefinedClass}.
325N/A * If this is an anonymous class, this constructor shouldn't be used.
325N/A */
325N/A private JClassContainer outer = null;
325N/A
325N/A
325N/A /** Default value is class or interface
325N/A * or annotationTypeDeclaration
325N/A * or enum
325N/A *
325N/A */
325N/A private final ClassType classType;
325N/A
325N/A /** List containing the enum value declarations
325N/A *
325N/A */
325N/A// private List enumValues = new ArrayList();
325N/A
325N/A /**
325N/A * Set of enum constants that are keyed by names.
325N/A * In Java, enum constant order is actually significant,
325N/A * because of order ID they get. So let's preserve the order.
325N/A */
325N/A private final Map<String,JEnumConstant> enumConstantsByName = new LinkedHashMap<String,JEnumConstant>();
325N/A
325N/A /**
325N/A * Annotations on this variable. Lazily created.
325N/A */
325N/A private List<JAnnotationUse> annotations = null;
325N/A
325N/A
325N/A /**
325N/A * Helper class to implement {@link JGenerifiable}.
325N/A */
325N/A private final JGenerifiableImpl generifiable = new JGenerifiableImpl() {
325N/A protected JCodeModel owner() {
325N/A return JDefinedClass.this.owner();
325N/A }
325N/A };
325N/A
325N/A JDefinedClass(JClassContainer parent, int mods, String name, ClassType classTypeval) {
325N/A this(mods, name, parent, parent.owner(), classTypeval);
325N/A }
325N/A
325N/A /**
325N/A * Constructor for creating anonymous inner class.
325N/A */
325N/A JDefinedClass(
325N/A JCodeModel owner,
325N/A int mods,
325N/A String name) {
325N/A this(mods, name, null, owner);
325N/A }
325N/A
325N/A private JDefinedClass(
325N/A int mods,
325N/A String name,
325N/A JClassContainer parent,
325N/A JCodeModel owner) {
325N/A this (mods,name,parent,owner,ClassType.CLASS);
325N/A }
325N/A
325N/A /**
325N/A * JClass constructor
325N/A *
325N/A * @param mods
325N/A * Modifiers for this class declaration
325N/A *
325N/A * @param name
325N/A * Name of this class
325N/A */
325N/A private JDefinedClass(
325N/A int mods,
325N/A String name,
325N/A JClassContainer parent,
325N/A JCodeModel owner,
325N/A ClassType classTypeVal) {
325N/A super(owner);
325N/A
325N/A if(name!=null) {
325N/A if (name.trim().length() == 0)
325N/A throw new IllegalArgumentException("JClass name empty");
325N/A
325N/A if (!Character.isJavaIdentifierStart(name.charAt(0))) {
325N/A String msg =
325N/A "JClass name "
325N/A + name
325N/A + " contains illegal character"
325N/A + " for beginning of identifier: "
325N/A + name.charAt(0);
325N/A throw new IllegalArgumentException(msg);
325N/A }
325N/A for (int i = 1; i < name.length(); i++) {
325N/A if (!Character.isJavaIdentifierPart(name.charAt(i))) {
325N/A String msg =
325N/A "JClass name "
325N/A + name
325N/A + " contains illegal character "
325N/A + name.charAt(i);
325N/A throw new IllegalArgumentException(msg);
325N/A }
325N/A }
325N/A }
325N/A
325N/A this.classType = classTypeVal;
325N/A if (isInterface())
325N/A this.mods = JMods.forInterface(mods);
325N/A else
325N/A this.mods = JMods.forClass(mods);
325N/A
325N/A this.name = name;
325N/A
325N/A this.outer = parent;
325N/A }
325N/A
325N/A /**
325N/A * Returns true if this is an anonymous class.
325N/A */
325N/A public final boolean isAnonymous() {
325N/A return name == null;
325N/A }
325N/A
325N/A /**
325N/A * This class extends the specifed class.
325N/A *
325N/A * @param superClass
325N/A * Superclass for this class
325N/A *
325N/A * @return This class
325N/A */
325N/A public JDefinedClass _extends(JClass superClass) {
325N/A if (this.classType==ClassType.INTERFACE)
325N/A if(superClass.isInterface()){
325N/A return this._implements(superClass);
325N/A } else throw new IllegalArgumentException("unable to set the super class for an interface");
325N/A if (superClass == null)
325N/A throw new NullPointerException();
325N/A
325N/A for( JClass o=superClass.outer(); o!=null; o=o.outer() ){
325N/A if(this==o){
325N/A throw new IllegalArgumentException("Illegal class inheritance loop." +
325N/A " Outer class " + this.name + " may not subclass from inner class: " + o.name());
325N/A }
325N/A }
325N/A
325N/A this.superClass = superClass;
325N/A return this;
325N/A }
325N/A
325N/A public JDefinedClass _extends(Class<?> superClass) {
325N/A return _extends(owner().ref(superClass));
325N/A }
325N/A
325N/A /**
325N/A * Returns the class extended by this class.
325N/A */
325N/A public JClass _extends() {
325N/A if(superClass==null)
325N/A superClass = owner().ref(Object.class);
325N/A return superClass;
325N/A }
325N/A
325N/A /**
325N/A * This class implements the specifed interface.
325N/A *
325N/A * @param iface
325N/A * Interface that this class implements
325N/A *
325N/A * @return This class
325N/A */
325N/A public JDefinedClass _implements(JClass iface) {
325N/A interfaces.add(iface);
325N/A return this;
325N/A }
325N/A
325N/A public JDefinedClass _implements(Class<?> iface) {
325N/A return _implements(owner().ref(iface));
325N/A }
325N/A
325N/A /**
325N/A * Returns an iterator that walks the nested classes defined in this
325N/A * class.
325N/A */
325N/A public Iterator<JClass> _implements() {
325N/A return interfaces.iterator();
325N/A }
325N/A
325N/A /**
325N/A * JClass name accessor.
325N/A *
325N/A * <p>
325N/A * For example, for <code>java.util.List</code>, this method
325N/A * returns <code>"List"</code>"
325N/A *
325N/A * @return Name of this class
325N/A */
325N/A public String name() {
325N/A return name;
325N/A }
325N/A
325N/A /**
325N/A * If the named enum already exists, the reference to it is returned.
325N/A * Otherwise this method generates a new enum reference with the given
325N/A * name and returns it.
325N/A *
325N/A * @param name
325N/A * The name of the constant.
325N/A * @return
325N/A * The generated type-safe enum constant.
325N/A */
325N/A public JEnumConstant enumConstant(String name){
325N/A JEnumConstant ec = enumConstantsByName.get(name);
325N/A if (null == ec) {
325N/A ec = new JEnumConstant(this, name);
325N/A enumConstantsByName.put(name, ec);
325N/A }
325N/A return ec;
325N/A }
325N/A
325N/A /**
325N/A * Gets the fully qualified name of this class.
325N/A */
325N/A public String fullName() {
325N/A if (outer instanceof JDefinedClass)
325N/A return ((JDefinedClass) outer).fullName() + '.' + name();
325N/A
325N/A JPackage p = _package();
325N/A if (p.isUnnamed())
325N/A return name();
325N/A else
325N/A return p.name() + '.' + name();
325N/A }
325N/A
325N/A @Override
325N/A public String binaryName() {
325N/A if (outer instanceof JDefinedClass)
325N/A return ((JDefinedClass) outer).binaryName() + '$' + name();
325N/A else
325N/A return fullName();
325N/A }
325N/A
325N/A public boolean isInterface() {
325N/A return this.classType==ClassType.INTERFACE;
325N/A }
325N/A
325N/A public boolean isAbstract() {
325N/A return mods.isAbstract();
325N/A }
325N/A
325N/A /**
325N/A * Adds a field to the list of field members of this JDefinedClass.
325N/A *
325N/A * @param mods
325N/A * Modifiers for this field
325N/A *
325N/A * @param type
325N/A * JType of this field
325N/A *
325N/A * @param name
325N/A * Name of this field
325N/A *
325N/A * @return Newly generated field
325N/A */
325N/A public JFieldVar field(int mods, JType type, String name) {
325N/A return field(mods, type, name, null);
325N/A }
325N/A
325N/A public JFieldVar field(int mods, Class<?> type, String name) {
325N/A return field(mods, owner()._ref(type), name);
325N/A }
325N/A
325N/A /**
325N/A * Adds a field to the list of field members of this JDefinedClass.
325N/A *
325N/A * @param mods
325N/A * Modifiers for this field.
325N/A * @param type
325N/A * JType of this field.
325N/A * @param name
325N/A * Name of this field.
325N/A * @param init
325N/A * Initial value of this field.
325N/A *
325N/A * @return Newly generated field
325N/A */
325N/A public JFieldVar field(
325N/A int mods,
325N/A JType type,
325N/A String name,
325N/A JExpression init) {
325N/A JFieldVar f = new JFieldVar(this,JMods.forField(mods), type, name, init);
325N/A
325N/A if (fields.containsKey(name)) {
325N/A throw new IllegalArgumentException("trying to create the same field twice: "+name);
325N/A }
325N/A
325N/A fields.put(name, f);
325N/A return f;
325N/A }
325N/A
325N/A /** This method indicates if the interface
325N/A * is an annotationTypeDeclaration
325N/A *
325N/A */
325N/A public boolean isAnnotationTypeDeclaration() {
325N/A return this.classType==ClassType.ANNOTATION_TYPE_DECL;
325N/A
325N/A
325N/A }
325N/A
325N/A /**
325N/A * Add an annotationType Declaration to this package
325N/A * @param name
325N/A * Name of the annotation Type declaration to be added to this package
325N/A * @return
325N/A * newly created Annotation Type Declaration
325N/A * @exception JClassAlreadyExistsException
325N/A * When the specified class/interface was already created.
325N/A
325N/A */
325N/A public JDefinedClass _annotationTypeDeclaration(String name) throws JClassAlreadyExistsException {
325N/A return _class (JMod.PUBLIC,name,ClassType.ANNOTATION_TYPE_DECL);
325N/A }
325N/A
325N/A /**
325N/A * Add a public enum to this package
325N/A * @param name
325N/A * Name of the enum to be added to this package
325N/A * @return
325N/A * newly created Enum
325N/A * @exception JClassAlreadyExistsException
325N/A * When the specified class/interface was already created.
325N/A
325N/A */
325N/A public JDefinedClass _enum (String name) throws JClassAlreadyExistsException {
325N/A return _class (JMod.PUBLIC,name,ClassType.ENUM);
325N/A }
325N/A
325N/A /**
325N/A * Add a public enum to this package
325N/A * @param name
325N/A * Name of the enum to be added to this package
325N/A * @param mods
325N/A * Modifiers for this enum declaration
325N/A * @return
325N/A * newly created Enum
325N/A * @exception JClassAlreadyExistsException
325N/A * When the specified class/interface was already created.
325N/A
325N/A */
325N/A public JDefinedClass _enum (int mods,String name) throws JClassAlreadyExistsException {
325N/A return _class (mods,name,ClassType.ENUM);
325N/A }
325N/A
325N/A
325N/A
325N/A
325N/A
325N/A public ClassType getClassType(){
325N/A return this.classType;
325N/A }
325N/A
325N/A public JFieldVar field(
325N/A int mods,
325N/A Class<?> type,
325N/A String name,
325N/A JExpression init) {
325N/A return field(mods, owner()._ref(type), name, init);
325N/A }
325N/A
325N/A /**
325N/A * Returns all the fields declred in this class.
325N/A * The returned {@link Map} is a read-only live view.
325N/A *
325N/A * @return always non-null.
325N/A */
325N/A public Map<String,JFieldVar> fields() {
325N/A return Collections.unmodifiableMap(fields);
325N/A }
325N/A
325N/A /**
325N/A * Removes a {@link JFieldVar} from this class.
325N/A *
325N/A * @throws IllegalArgumentException
325N/A * if the given field is not a field on this class.
325N/A */
325N/A public void removeField(JFieldVar field) {
325N/A if(fields.remove(field.name())!=field)
325N/A throw new IllegalArgumentException();
325N/A }
325N/A
325N/A /**
325N/A * Creates, if necessary, and returns the static initializer
325N/A * for this class.
325N/A *
325N/A * @return JBlock containing initialization statements for this class
325N/A */
325N/A public JBlock init() {
325N/A if (init == null)
325N/A init = new JBlock();
325N/A return init;
325N/A }
325N/A
325N/A /**
325N/A * Adds a constructor to this class.
325N/A *
325N/A * @param mods
325N/A * Modifiers for this constructor
325N/A */
325N/A public JMethod constructor(int mods) {
325N/A JMethod c = new JMethod(mods, this);
325N/A constructors.add(c);
325N/A return c;
325N/A }
325N/A
325N/A /**
325N/A * Returns an iterator that walks the constructors defined in this class.
325N/A */
325N/A public Iterator<JMethod> constructors() {
325N/A return constructors.iterator();
325N/A }
325N/A
325N/A /**
325N/A * Looks for a method that has the specified method signature
325N/A * and return it.
325N/A *
325N/A * @return
325N/A * null if not found.
325N/A */
325N/A public JMethod getConstructor(JType[] argTypes) {
325N/A for (JMethod m : constructors) {
325N/A if (m.hasSignature(argTypes))
325N/A return m;
325N/A }
325N/A return null;
325N/A }
325N/A
325N/A /**
325N/A * Add a method to the list of method members of this JDefinedClass instance.
325N/A *
325N/A * @param mods
325N/A * Modifiers for this method
325N/A *
325N/A * @param type
325N/A * Return type for this method
325N/A *
325N/A * @param name
325N/A * Name of the method
325N/A *
325N/A * @return Newly generated JMethod
325N/A */
325N/A public JMethod method(int mods, JType type, String name) {
325N/A // XXX problems caught in M constructor
325N/A JMethod m = new JMethod(this, mods, type, name);
325N/A methods.add(m);
325N/A return m;
325N/A }
325N/A
325N/A public JMethod method(int mods, Class<?> type, String name) {
325N/A return method(mods, owner()._ref(type), name);
325N/A }
325N/A
325N/A /**
325N/A * Returns the set of methods defined in this class.
325N/A */
325N/A public Collection<JMethod> methods() {
325N/A return methods;
325N/A }
325N/A
325N/A /**
325N/A * Looks for a method that has the specified method signature
325N/A * and return it.
325N/A *
325N/A * @return
325N/A * null if not found.
325N/A */
325N/A public JMethod getMethod(String name, JType[] argTypes) {
325N/A for (JMethod m : methods) {
325N/A if (!m.name().equals(name))
325N/A continue;
325N/A
325N/A if (m.hasSignature(argTypes))
325N/A return m;
325N/A }
325N/A return null;
325N/A }
325N/A
325N/A public boolean isClass() {
325N/A return true;
325N/A }
325N/A public boolean isPackage() {
325N/A return false;
325N/A }
325N/A public JPackage getPackage() { return parentContainer().getPackage(); }
325N/A
325N/A /**
325N/A * Add a new nested class to this class.
325N/A *
325N/A * @param mods
325N/A * Modifiers for this class declaration
325N/A *
325N/A * @param name
325N/A * Name of class to be added to this package
325N/A *
325N/A * @return Newly generated class
325N/A */
325N/A public JDefinedClass _class(int mods, String name)
325N/A throws JClassAlreadyExistsException {
325N/A return _class(mods, name, ClassType.CLASS);
325N/A }
325N/A
325N/A /**
325N/A * {@inheritDoc}
325N/A *
325N/A * @deprecated
325N/A */
325N/A public JDefinedClass _class(int mods, String name, boolean isInterface) throws JClassAlreadyExistsException {
325N/A return _class(mods,name,isInterface?ClassType.INTERFACE:ClassType.CLASS);
325N/A }
325N/A
325N/A public JDefinedClass _class(int mods, String name, ClassType classTypeVal)
325N/A throws JClassAlreadyExistsException {
325N/A
325N/A String NAME;
325N/A if (JCodeModel.isCaseSensitiveFileSystem)
325N/A NAME = name.toUpperCase();
325N/A else
325N/A NAME = name;
325N/A
325N/A if (getClasses().containsKey(NAME))
325N/A throw new JClassAlreadyExistsException(getClasses().get(NAME));
325N/A else {
325N/A // XXX problems caught in the NC constructor
325N/A JDefinedClass c = new JDefinedClass(this, mods, name, classTypeVal);
325N/A getClasses().put(NAME,c);
325N/A return c;
325N/A }
325N/A }
325N/A
325N/A /**
325N/A * Add a new public nested class to this class.
325N/A */
325N/A public JDefinedClass _class(String name)
325N/A throws JClassAlreadyExistsException {
325N/A return _class(JMod.PUBLIC, name);
325N/A }
325N/A
325N/A /**
325N/A * Add an interface to this package.
325N/A *
325N/A * @param mods
325N/A * Modifiers for this interface declaration
325N/A *
325N/A * @param name
325N/A * Name of interface to be added to this package
325N/A *
325N/A * @return Newly generated interface
325N/A */
325N/A public JDefinedClass _interface(int mods, String name)
325N/A throws JClassAlreadyExistsException {
325N/A return _class(mods, name, ClassType.INTERFACE);
325N/A }
325N/A
325N/A /**
325N/A * Adds a public interface to this package.
325N/A */
325N/A public JDefinedClass _interface(String name)
325N/A throws JClassAlreadyExistsException {
325N/A return _interface(JMod.PUBLIC, name);
325N/A }
325N/A
325N/A /**
325N/A * Creates, if necessary, and returns the class javadoc for this
325N/A * JDefinedClass
325N/A *
325N/A * @return JDocComment containing javadocs for this class
325N/A */
325N/A public JDocComment javadoc() {
325N/A if (jdoc == null)
325N/A jdoc = new JDocComment(owner());
325N/A return jdoc;
325N/A }
325N/A
325N/A /**
325N/A * Mark this file as hidden, so that this file won't be
325N/A * generated.
325N/A *
325N/A * <p>
325N/A * This feature could be used to generate code that refers
325N/A * to class X, without actually generating X.java.
325N/A */
325N/A public void hide() {
325N/A hideFile = true;
325N/A }
325N/A
325N/A public boolean isHidden() {
325N/A return hideFile;
325N/A }
325N/A
325N/A /**
325N/A * Returns an iterator that walks the nested classes defined in this
325N/A * class.
325N/A */
325N/A public final Iterator<JDefinedClass> classes() {
325N/A if(classes==null)
325N/A return Collections.<JDefinedClass>emptyList().iterator();
325N/A else
325N/A return classes.values().iterator();
325N/A }
325N/A
325N/A private Map<String,JDefinedClass> getClasses() {
325N/A if(classes==null)
325N/A classes = new TreeMap<String,JDefinedClass>();
325N/A return classes;
325N/A }
325N/A
325N/A
325N/A /**
325N/A * Returns all the nested classes defined in this class.
325N/A */
325N/A public final JClass[] listClasses() {
325N/A if(classes==null)
325N/A return new JClass[0];
325N/A else
325N/A return classes.values().toArray(new JClass[classes.values().size()]);
325N/A }
325N/A
325N/A @Override
325N/A public JClass outer() {
325N/A if (outer.isClass())
325N/A return (JClass) outer;
325N/A else
325N/A return null;
325N/A }
325N/A
325N/A public void declare(JFormatter f) {
325N/A if (jdoc != null)
325N/A f.nl().g(jdoc);
325N/A
325N/A if (annotations != null){
325N/A for (JAnnotationUse annotation : annotations)
325N/A f.g(annotation).nl();
325N/A }
325N/A
325N/A f.g(mods).p(classType.declarationToken).id(name).d(generifiable);
325N/A
325N/A if (superClass != null && superClass != owner().ref(Object.class))
325N/A f.nl().i().p("extends").g(superClass).nl().o();
325N/A
325N/A if (!interfaces.isEmpty()) {
325N/A if (superClass == null)
325N/A f.nl();
325N/A f.i().p(classType==ClassType.INTERFACE ? "extends" : "implements");
325N/A f.g(interfaces);
325N/A f.nl().o();
325N/A }
325N/A declareBody(f);
325N/A }
325N/A
325N/A /**
325N/A * prints the body of a class.
325N/A */
325N/A protected void declareBody(JFormatter f) {
325N/A f.p('{').nl().nl().i();
325N/A boolean first = true;
325N/A
325N/A if (!enumConstantsByName.isEmpty()) {
325N/A for (JEnumConstant c : enumConstantsByName.values()) {
325N/A if (!first) f.p(',').nl();
325N/A f.d(c);
325N/A first = false;
325N/A }
325N/A f.p(';').nl();
325N/A }
325N/A
325N/A for( JFieldVar field : fields.values() )
325N/A f.d(field);
325N/A if (init != null)
325N/A f.nl().p("static").s(init);
325N/A for (JMethod m : constructors) {
325N/A f.nl().d(m);
325N/A }
325N/A for (JMethod m : methods) {
325N/A f.nl().d(m);
325N/A }
325N/A if(classes!=null)
325N/A for (JDefinedClass dc : classes.values())
325N/A f.nl().d(dc);
325N/A
325N/A
325N/A if (directBlock != null)
325N/A f.p(directBlock);
325N/A f.nl().o().p('}').nl();
325N/A }
325N/A
325N/A /**
325N/A * Places the given string directly inside the generated class.
325N/A *
325N/A * This method can be used to add methods/fields that are not
325N/A * generated by CodeModel.
325N/A * This method should be used only as the last resort.
325N/A */
325N/A public void direct(String string) {
325N/A if (directBlock == null)
325N/A directBlock = string;
325N/A else
325N/A directBlock += string;
325N/A }
325N/A
325N/A public final JPackage _package() {
325N/A JClassContainer p = outer;
325N/A while (!(p instanceof JPackage))
325N/A p = p.parentContainer();
325N/A return (JPackage) p;
325N/A }
325N/A
325N/A public final JClassContainer parentContainer() {
325N/A return outer;
325N/A }
325N/A
325N/A public JTypeVar generify(String name) {
325N/A return generifiable.generify(name);
325N/A }
325N/A public JTypeVar generify(String name, Class<?> bound) {
325N/A return generifiable.generify(name, bound);
325N/A }
325N/A public JTypeVar generify(String name, JClass bound) {
325N/A return generifiable.generify(name, bound);
325N/A }
325N/A @Override
325N/A public JTypeVar[] typeParams() {
325N/A return generifiable.typeParams();
325N/A }
325N/A
325N/A protected JClass substituteParams(
325N/A JTypeVar[] variables,
325N/A List<JClass> bindings) {
325N/A return this;
325N/A }
325N/A
325N/A /** Adding ability to annotate a class
325N/A * @param clazz
325N/A * The annotation class to annotate the class with
325N/A */
325N/A public JAnnotationUse annotate(Class <? extends Annotation> clazz){
325N/A return annotate(owner().ref(clazz));
325N/A }
325N/A
325N/A /** Adding ability to annotate a class
325N/A * @param clazz
325N/A * The annotation class to annotate the class with
325N/A */
325N/A public JAnnotationUse annotate(JClass clazz){
325N/A if(annotations==null)
325N/A annotations = new ArrayList<JAnnotationUse>();
325N/A JAnnotationUse a = new JAnnotationUse(clazz);
325N/A annotations.add(a);
325N/A return a;
325N/A }
325N/A
325N/A public <W extends JAnnotationWriter> W annotate2(Class<W> clazz) {
325N/A return TypedAnnotationWriter.create(clazz,this);
325N/A }
325N/A
325N/A /**
325N/A * {@link JAnnotatable#annotations()}
325N/A */
325N/A public Collection<JAnnotationUse> annotations() {
325N/A if (annotations == null)
325N/A annotations = new ArrayList<JAnnotationUse>();
325N/A return Collections.unmodifiableCollection(annotations);
325N/A }
325N/A
325N/A /**
325N/A * @return
325N/A * the current modifiers of this class.
325N/A * Always return non-null valid object.
325N/A */
325N/A public JMods mods() {
325N/A return mods;
325N/A }
325N/A}