1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/codemodel/internal/JDefinedClass.java Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,866 @@ 1.4 +/* 1.5 + * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Sun designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Sun in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.26 + * have any questions. 1.27 + */ 1.28 + 1.29 +package com.sun.codemodel.internal; 1.30 + 1.31 +import java.lang.annotation.Annotation; 1.32 +import java.util.ArrayList; 1.33 +import java.util.Collection; 1.34 +import java.util.Collections; 1.35 +import java.util.Iterator; 1.36 +import java.util.LinkedHashMap; 1.37 +import java.util.List; 1.38 +import java.util.Map; 1.39 +import java.util.Set; 1.40 +import java.util.TreeMap; 1.41 +import java.util.TreeSet; 1.42 + 1.43 +/** 1.44 + * A generated Java class/interface/enum/.... 1.45 + * 1.46 + * <p> 1.47 + * This class models a declaration, and since a declaration can be always 1.48 + * used as a reference, it inherits {@link JClass}. 1.49 + * 1.50 + * <h2>Where to go from here?</h2> 1.51 + * <p> 1.52 + * You'd want to generate fields and methods on a class. 1.53 + * See {@link #method(int, JType, String)} and {@link #field(int, JType, String)}. 1.54 + */ 1.55 +public class JDefinedClass 1.56 + extends JClass 1.57 + implements JDeclaration, JClassContainer, JGenerifiable, JAnnotatable { 1.58 + 1.59 + /** Name of this class. Null if anonymous. */ 1.60 + private String name = null; 1.61 + 1.62 + 1.63 + /** Modifiers for the class declaration */ 1.64 + private JMods mods; 1.65 + 1.66 + /** Name of the super class of this class. */ 1.67 + private JClass superClass; 1.68 + 1.69 + /** List of interfaces that this class implements */ 1.70 + private final Set<JClass> interfaces = new TreeSet<JClass>(); 1.71 + 1.72 + /** Fields keyed by their names. */ 1.73 + /*package*/ final Map<String,JFieldVar> fields = new LinkedHashMap<String,JFieldVar>(); 1.74 + 1.75 + /** Static initializer, if this class has one */ 1.76 + private JBlock init = null; 1.77 + 1.78 + /** class javadoc */ 1.79 + private JDocComment jdoc = null; 1.80 + 1.81 + /** Set of constructors for this class, if any */ 1.82 + private final List<JMethod> constructors = new ArrayList<JMethod>(); 1.83 + 1.84 + /** Set of methods that are members of this class */ 1.85 + private final List<JMethod> methods = new ArrayList<JMethod>(); 1.86 + 1.87 + /** 1.88 + * Nested classes as a map from name to JDefinedClass. 1.89 + * The name is all capitalized in a case sensitive file system 1.90 + * ({@link JCodeModel#isCaseSensitiveFileSystem}) to avoid conflicts. 1.91 + * 1.92 + * Lazily created to save footprint. 1.93 + * 1.94 + * @see #getClasses() 1.95 + */ 1.96 + private Map<String,JDefinedClass> classes; 1.97 + 1.98 + 1.99 + /** 1.100 + * Flag that controls whether this class should be really generated or not. 1.101 + * 1.102 + * Sometimes it is useful to generate code that refers to class X, 1.103 + * without actually generating the code of X. 1.104 + * This flag is used to surpress X.java file in the output. 1.105 + */ 1.106 + private boolean hideFile = false; 1.107 + 1.108 + /** 1.109 + * Client-app spcific metadata associated with this user-created class. 1.110 + */ 1.111 + public Object metadata; 1.112 + 1.113 + /** 1.114 + * String that will be put directly inside the generated code. 1.115 + * Can be null. 1.116 + */ 1.117 + private String directBlock; 1.118 + 1.119 + /** 1.120 + * If this is a package-member class, this is {@link JPackage}. 1.121 + * If this is a nested class, this is {@link JDefinedClass}. 1.122 + * If this is an anonymous class, this constructor shouldn't be used. 1.123 + */ 1.124 + private JClassContainer outer = null; 1.125 + 1.126 + 1.127 + /** Default value is class or interface 1.128 + * or annotationTypeDeclaration 1.129 + * or enum 1.130 + * 1.131 + */ 1.132 + private final ClassType classType; 1.133 + 1.134 + /** List containing the enum value declarations 1.135 + * 1.136 + */ 1.137 +// private List enumValues = new ArrayList(); 1.138 + 1.139 + /** 1.140 + * Set of enum constants that are keyed by names. 1.141 + * In Java, enum constant order is actually significant, 1.142 + * because of order ID they get. So let's preserve the order. 1.143 + */ 1.144 + private final Map<String,JEnumConstant> enumConstantsByName = new LinkedHashMap<String,JEnumConstant>(); 1.145 + 1.146 + /** 1.147 + * Annotations on this variable. Lazily created. 1.148 + */ 1.149 + private List<JAnnotationUse> annotations = null; 1.150 + 1.151 + 1.152 + /** 1.153 + * Helper class to implement {@link JGenerifiable}. 1.154 + */ 1.155 + private final JGenerifiableImpl generifiable = new JGenerifiableImpl() { 1.156 + protected JCodeModel owner() { 1.157 + return JDefinedClass.this.owner(); 1.158 + } 1.159 + }; 1.160 + 1.161 + JDefinedClass(JClassContainer parent, int mods, String name, ClassType classTypeval) { 1.162 + this(mods, name, parent, parent.owner(), classTypeval); 1.163 + } 1.164 + 1.165 + /** 1.166 + * Constructor for creating anonymous inner class. 1.167 + */ 1.168 + JDefinedClass( 1.169 + JCodeModel owner, 1.170 + int mods, 1.171 + String name) { 1.172 + this(mods, name, null, owner); 1.173 + } 1.174 + 1.175 + private JDefinedClass( 1.176 + int mods, 1.177 + String name, 1.178 + JClassContainer parent, 1.179 + JCodeModel owner) { 1.180 + this (mods,name,parent,owner,ClassType.CLASS); 1.181 + } 1.182 + 1.183 + /** 1.184 + * JClass constructor 1.185 + * 1.186 + * @param mods 1.187 + * Modifiers for this class declaration 1.188 + * 1.189 + * @param name 1.190 + * Name of this class 1.191 + */ 1.192 + private JDefinedClass( 1.193 + int mods, 1.194 + String name, 1.195 + JClassContainer parent, 1.196 + JCodeModel owner, 1.197 + ClassType classTypeVal) { 1.198 + super(owner); 1.199 + 1.200 + if(name!=null) { 1.201 + if (name.trim().length() == 0) 1.202 + throw new IllegalArgumentException("JClass name empty"); 1.203 + 1.204 + if (!Character.isJavaIdentifierStart(name.charAt(0))) { 1.205 + String msg = 1.206 + "JClass name " 1.207 + + name 1.208 + + " contains illegal character" 1.209 + + " for beginning of identifier: " 1.210 + + name.charAt(0); 1.211 + throw new IllegalArgumentException(msg); 1.212 + } 1.213 + for (int i = 1; i < name.length(); i++) { 1.214 + if (!Character.isJavaIdentifierPart(name.charAt(i))) { 1.215 + String msg = 1.216 + "JClass name " 1.217 + + name 1.218 + + " contains illegal character " 1.219 + + name.charAt(i); 1.220 + throw new IllegalArgumentException(msg); 1.221 + } 1.222 + } 1.223 + } 1.224 + 1.225 + this.classType = classTypeVal; 1.226 + if (isInterface()) 1.227 + this.mods = JMods.forInterface(mods); 1.228 + else 1.229 + this.mods = JMods.forClass(mods); 1.230 + 1.231 + this.name = name; 1.232 + 1.233 + this.outer = parent; 1.234 + } 1.235 + 1.236 + /** 1.237 + * Returns true if this is an anonymous class. 1.238 + */ 1.239 + public final boolean isAnonymous() { 1.240 + return name == null; 1.241 + } 1.242 + 1.243 + /** 1.244 + * This class extends the specifed class. 1.245 + * 1.246 + * @param superClass 1.247 + * Superclass for this class 1.248 + * 1.249 + * @return This class 1.250 + */ 1.251 + public JDefinedClass _extends(JClass superClass) { 1.252 + if (this.classType==ClassType.INTERFACE) 1.253 + throw new IllegalArgumentException("unable to set the super class for an interface"); 1.254 + if (superClass == null) 1.255 + throw new NullPointerException(); 1.256 + 1.257 + this.superClass = superClass; 1.258 + return this; 1.259 + } 1.260 + 1.261 + public JDefinedClass _extends(Class superClass) { 1.262 + return _extends(owner().ref(superClass)); 1.263 + } 1.264 + 1.265 + /** 1.266 + * Returns the class extended by this class. 1.267 + */ 1.268 + public JClass _extends() { 1.269 + if(superClass==null) 1.270 + superClass = owner().ref(Object.class); 1.271 + return superClass; 1.272 + } 1.273 + 1.274 + /** 1.275 + * This class implements the specifed interface. 1.276 + * 1.277 + * @param iface 1.278 + * Interface that this class implements 1.279 + * 1.280 + * @return This class 1.281 + */ 1.282 + public JDefinedClass _implements(JClass iface) { 1.283 + interfaces.add(iface); 1.284 + return this; 1.285 + } 1.286 + 1.287 + public JDefinedClass _implements(Class iface) { 1.288 + return _implements(owner().ref(iface)); 1.289 + } 1.290 + 1.291 + /** 1.292 + * Returns an iterator that walks the nested classes defined in this 1.293 + * class. 1.294 + */ 1.295 + public Iterator<JClass> _implements() { 1.296 + return interfaces.iterator(); 1.297 + } 1.298 + 1.299 + /** 1.300 + * JClass name accessor. 1.301 + * 1.302 + * <p> 1.303 + * For example, for <code>java.util.List</code>, this method 1.304 + * returns <code>"List"</code>" 1.305 + * 1.306 + * @return Name of this class 1.307 + */ 1.308 + public String name() { 1.309 + return name; 1.310 + } 1.311 + 1.312 + /** 1.313 + * This method generates reference to the JEnumConstant in 1.314 + * the class 1.315 + * 1.316 + * @param name 1.317 + * The name of the constant. 1.318 + * @return 1.319 + * The generated type-safe enum constant. 1.320 + */ 1.321 + public JEnumConstant enumConstant(String name){ 1.322 + JEnumConstant ec = new JEnumConstant(this, name); 1.323 + enumConstantsByName.put(name, ec); 1.324 + return ec; 1.325 + } 1.326 + 1.327 + /** 1.328 + * Gets the fully qualified name of this class. 1.329 + */ 1.330 + public String fullName() { 1.331 + if (outer instanceof JDefinedClass) 1.332 + return ((JDefinedClass) outer).fullName() + '.' + name(); 1.333 + 1.334 + JPackage p = _package(); 1.335 + if (p.isUnnamed()) 1.336 + return name(); 1.337 + else 1.338 + return p.name() + '.' + name(); 1.339 + } 1.340 + 1.341 + public String binaryName() { 1.342 + if (outer instanceof JDefinedClass) 1.343 + return ((JDefinedClass) outer).binaryName() + '$' + name(); 1.344 + else 1.345 + return fullName(); 1.346 + } 1.347 + 1.348 + public boolean isInterface() { 1.349 + return this.classType==ClassType.INTERFACE; 1.350 + } 1.351 + 1.352 + public boolean isAbstract() { 1.353 + return mods.isAbstract(); 1.354 + } 1.355 + 1.356 + /** 1.357 + * Adds a field to the list of field members of this JDefinedClass. 1.358 + * 1.359 + * @param mods 1.360 + * Modifiers for this field 1.361 + * 1.362 + * @param type 1.363 + * JType of this field 1.364 + * 1.365 + * @param name 1.366 + * Name of this field 1.367 + * 1.368 + * @return Newly generated field 1.369 + */ 1.370 + public JFieldVar field(int mods, JType type, String name) { 1.371 + return field(mods, type, name, null); 1.372 + } 1.373 + 1.374 + public JFieldVar field(int mods, Class type, String name) { 1.375 + return field(mods, owner()._ref(type), name); 1.376 + } 1.377 + 1.378 + /** 1.379 + * Adds a field to the list of field members of this JDefinedClass. 1.380 + * 1.381 + * @param mods 1.382 + * Modifiers for this field. 1.383 + * @param type 1.384 + * JType of this field. 1.385 + * @param name 1.386 + * Name of this field. 1.387 + * @param init 1.388 + * Initial value of this field. 1.389 + * 1.390 + * @return Newly generated field 1.391 + */ 1.392 + public JFieldVar field( 1.393 + int mods, 1.394 + JType type, 1.395 + String name, 1.396 + JExpression init) { 1.397 + JFieldVar f = new JFieldVar(this,JMods.forField(mods), type, name, init); 1.398 + 1.399 + if(fields.put(name, f)!=null) 1.400 + throw new IllegalArgumentException("trying to create the same field twice: "+name); 1.401 + 1.402 + return f; 1.403 + } 1.404 + 1.405 + /** This method indicates if the interface 1.406 + * is an annotationTypeDeclaration 1.407 + * 1.408 + */ 1.409 + public boolean isAnnotationTypeDeclaration() { 1.410 + return this.classType==ClassType.ANNOTATION_TYPE_DECL; 1.411 + 1.412 + 1.413 + } 1.414 + 1.415 + /** 1.416 + * Add an annotationType Declaration to this package 1.417 + * @param name 1.418 + * Name of the annotation Type declaration to be added to this package 1.419 + * @return 1.420 + * newly created Annotation Type Declaration 1.421 + * @exception JClassAlreadyExistsException 1.422 + * When the specified class/interface was already created. 1.423 + 1.424 + */ 1.425 + public JDefinedClass _annotationTypeDeclaration(String name) throws JClassAlreadyExistsException { 1.426 + return _class (JMod.PUBLIC,name,ClassType.ANNOTATION_TYPE_DECL); 1.427 + } 1.428 + 1.429 + /** 1.430 + * Add a public enum to this package 1.431 + * @param name 1.432 + * Name of the enum to be added to this package 1.433 + * @return 1.434 + * newly created Enum 1.435 + * @exception JClassAlreadyExistsException 1.436 + * When the specified class/interface was already created. 1.437 + 1.438 + */ 1.439 + public JDefinedClass _enum (String name) throws JClassAlreadyExistsException { 1.440 + return _class (JMod.PUBLIC,name,ClassType.ENUM); 1.441 + } 1.442 + 1.443 + /** 1.444 + * Add a public enum to this package 1.445 + * @param name 1.446 + * Name of the enum to be added to this package 1.447 + * @param mods 1.448 + * Modifiers for this enum declaration 1.449 + * @return 1.450 + * newly created Enum 1.451 + * @exception JClassAlreadyExistsException 1.452 + * When the specified class/interface was already created. 1.453 + 1.454 + */ 1.455 + public JDefinedClass _enum (int mods,String name) throws JClassAlreadyExistsException { 1.456 + return _class (mods,name,ClassType.ENUM); 1.457 + } 1.458 + 1.459 + 1.460 + 1.461 + 1.462 + 1.463 + public ClassType getClassType(){ 1.464 + return this.classType; 1.465 + } 1.466 + 1.467 + public JFieldVar field( 1.468 + int mods, 1.469 + Class type, 1.470 + String name, 1.471 + JExpression init) { 1.472 + return field(mods, owner()._ref(type), name, init); 1.473 + } 1.474 + 1.475 + /** 1.476 + * Returns all the fields declred in this class. 1.477 + * The returned {@link Map} is a read-only live view. 1.478 + * 1.479 + * @return always non-null. 1.480 + */ 1.481 + public Map<String,JFieldVar> fields() { 1.482 + return Collections.unmodifiableMap(fields); 1.483 + } 1.484 + 1.485 + /** 1.486 + * Removes a {@link JFieldVar} from this class. 1.487 + * 1.488 + * @throws IllegalArgumentException 1.489 + * if the given field is not a field on this class. 1.490 + */ 1.491 + public void removeField(JFieldVar field) { 1.492 + if(fields.remove(field.name())!=field) 1.493 + throw new IllegalArgumentException(); 1.494 + } 1.495 + 1.496 + /** 1.497 + * Creates, if necessary, and returns the static initializer 1.498 + * for this class. 1.499 + * 1.500 + * @return JBlock containing initialization statements for this class 1.501 + */ 1.502 + public JBlock init() { 1.503 + if (init == null) 1.504 + init = new JBlock(); 1.505 + return init; 1.506 + } 1.507 + 1.508 + /** 1.509 + * Adds a constructor to this class. 1.510 + * 1.511 + * @param mods 1.512 + * Modifiers for this constructor 1.513 + */ 1.514 + public JMethod constructor(int mods) { 1.515 + JMethod c = new JMethod(mods, this); 1.516 + constructors.add(c); 1.517 + return c; 1.518 + } 1.519 + 1.520 + /** 1.521 + * Returns an iterator that walks the constructors defined in this class. 1.522 + */ 1.523 + public Iterator constructors() { 1.524 + return constructors.iterator(); 1.525 + } 1.526 + 1.527 + /** 1.528 + * Looks for a method that has the specified method signature 1.529 + * and return it. 1.530 + * 1.531 + * @return 1.532 + * null if not found. 1.533 + */ 1.534 + public JMethod getConstructor(JType[] argTypes) { 1.535 + for (JMethod m : constructors) { 1.536 + if (m.hasSignature(argTypes)) 1.537 + return m; 1.538 + } 1.539 + return null; 1.540 + } 1.541 + 1.542 + /** 1.543 + * Add a method to the list of method members of this JDefinedClass instance. 1.544 + * 1.545 + * @param mods 1.546 + * Modifiers for this method 1.547 + * 1.548 + * @param type 1.549 + * Return type for this method 1.550 + * 1.551 + * @param name 1.552 + * Name of the method 1.553 + * 1.554 + * @return Newly generated JMethod 1.555 + */ 1.556 + public JMethod method(int mods, JType type, String name) { 1.557 + // XXX problems caught in M constructor 1.558 + JMethod m = new JMethod(this, mods, type, name); 1.559 + methods.add(m); 1.560 + return m; 1.561 + } 1.562 + 1.563 + public JMethod method(int mods, Class type, String name) { 1.564 + return method(mods, owner()._ref(type), name); 1.565 + } 1.566 + 1.567 + /** 1.568 + * Returns the set of methods defined in this class. 1.569 + */ 1.570 + public Collection<JMethod> methods() { 1.571 + return methods; 1.572 + } 1.573 + 1.574 + /** 1.575 + * Looks for a method that has the specified method signature 1.576 + * and return it. 1.577 + * 1.578 + * @return 1.579 + * null if not found. 1.580 + */ 1.581 + public JMethod getMethod(String name, JType[] argTypes) { 1.582 + outer : 1.583 + for (JMethod m : methods) { 1.584 + if (!m.name().equals(name)) 1.585 + continue; 1.586 + 1.587 + if (m.hasSignature(argTypes)) 1.588 + return m; 1.589 + } 1.590 + return null; 1.591 + } 1.592 + 1.593 + public boolean isClass() { 1.594 + return true; 1.595 + } 1.596 + public boolean isPackage() { 1.597 + return false; 1.598 + } 1.599 + public JPackage getPackage() { return parentContainer().getPackage(); } 1.600 + 1.601 + /** 1.602 + * Add a new nested class to this class. 1.603 + * 1.604 + * @param mods 1.605 + * Modifiers for this class declaration 1.606 + * 1.607 + * @param name 1.608 + * Name of class to be added to this package 1.609 + * 1.610 + * @return Newly generated class 1.611 + */ 1.612 + public JDefinedClass _class(int mods, String name) 1.613 + throws JClassAlreadyExistsException { 1.614 + return _class(mods, name, ClassType.CLASS); 1.615 + } 1.616 + 1.617 + /** 1.618 + * {@inheritDoc} 1.619 + * 1.620 + * @deprecated 1.621 + */ 1.622 + public JDefinedClass _class(int mods, String name, boolean isInterface) throws JClassAlreadyExistsException { 1.623 + return _class(mods,name,isInterface?ClassType.INTERFACE:ClassType.CLASS); 1.624 + } 1.625 + 1.626 + public JDefinedClass _class(int mods, String name, ClassType classTypeVal) 1.627 + throws JClassAlreadyExistsException { 1.628 + 1.629 + String NAME; 1.630 + if (JCodeModel.isCaseSensitiveFileSystem) 1.631 + NAME = name.toUpperCase(); 1.632 + else 1.633 + NAME = name; 1.634 + 1.635 + if (getClasses().containsKey(NAME)) 1.636 + throw new JClassAlreadyExistsException(getClasses().get(NAME)); 1.637 + else { 1.638 + // XXX problems caught in the NC constructor 1.639 + JDefinedClass c = new JDefinedClass(this, mods, name, classTypeVal); 1.640 + getClasses().put(NAME,c); 1.641 + return c; 1.642 + } 1.643 + } 1.644 + 1.645 + /** 1.646 + * Add a new public nested class to this class. 1.647 + */ 1.648 + public JDefinedClass _class(String name) 1.649 + throws JClassAlreadyExistsException { 1.650 + return _class(JMod.PUBLIC, name); 1.651 + } 1.652 + 1.653 + /** 1.654 + * Add an interface to this package. 1.655 + * 1.656 + * @param mods 1.657 + * Modifiers for this interface declaration 1.658 + * 1.659 + * @param name 1.660 + * Name of interface to be added to this package 1.661 + * 1.662 + * @return Newly generated interface 1.663 + */ 1.664 + public JDefinedClass _interface(int mods, String name) 1.665 + throws JClassAlreadyExistsException { 1.666 + return _class(mods, name, ClassType.INTERFACE); 1.667 + } 1.668 + 1.669 + /** 1.670 + * Adds a public interface to this package. 1.671 + */ 1.672 + public JDefinedClass _interface(String name) 1.673 + throws JClassAlreadyExistsException { 1.674 + return _interface(JMod.PUBLIC, name); 1.675 + } 1.676 + 1.677 + /** 1.678 + * Creates, if necessary, and returns the class javadoc for this 1.679 + * JDefinedClass 1.680 + * 1.681 + * @return JDocComment containing javadocs for this class 1.682 + */ 1.683 + public JDocComment javadoc() { 1.684 + if (jdoc == null) 1.685 + jdoc = new JDocComment(owner()); 1.686 + return jdoc; 1.687 + } 1.688 + 1.689 + /** 1.690 + * Mark this file as hidden, so that this file won't be 1.691 + * generated. 1.692 + * 1.693 + * <p> 1.694 + * This feature could be used to generate code that refers 1.695 + * to class X, without actually generating X.java. 1.696 + */ 1.697 + public void hide() { 1.698 + hideFile = true; 1.699 + } 1.700 + 1.701 + public boolean isHidden() { 1.702 + return hideFile; 1.703 + } 1.704 + 1.705 + /** 1.706 + * Returns an iterator that walks the nested classes defined in this 1.707 + * class. 1.708 + */ 1.709 + public final Iterator<JDefinedClass> classes() { 1.710 + if(classes==null) 1.711 + return Collections.<JDefinedClass>emptyList().iterator(); 1.712 + else 1.713 + return classes.values().iterator(); 1.714 + } 1.715 + 1.716 + private Map<String,JDefinedClass> getClasses() { 1.717 + if(classes==null) 1.718 + classes = new TreeMap<String,JDefinedClass>(); 1.719 + return classes; 1.720 + } 1.721 + 1.722 + 1.723 + /** 1.724 + * Returns all the nested classes defined in this class. 1.725 + */ 1.726 + public final JClass[] listClasses() { 1.727 + if(classes==null) 1.728 + return new JClass[0]; 1.729 + else 1.730 + return classes.values().toArray(new JClass[classes.values().size()]); 1.731 + } 1.732 + 1.733 + @Override 1.734 + public JClass outer() { 1.735 + if (outer.isClass()) 1.736 + return (JClass) outer; 1.737 + else 1.738 + return null; 1.739 + } 1.740 + 1.741 + public void declare(JFormatter f) { 1.742 + if (jdoc != null) 1.743 + f.nl().g(jdoc); 1.744 + 1.745 + if (annotations != null){ 1.746 + for( int i=0; i<annotations.size(); i++ ) 1.747 + f.g(annotations.get(i)).nl(); 1.748 + } 1.749 + 1.750 + f.g(mods).p(classType.declarationToken).id(name).d(generifiable); 1.751 + 1.752 + if (superClass != null && superClass != owner().ref(Object.class)) 1.753 + f.nl().i().p("extends").g(superClass).nl().o(); 1.754 + 1.755 + if (!interfaces.isEmpty()) { 1.756 + if (superClass == null) 1.757 + f.nl(); 1.758 + f.i().p(classType==ClassType.INTERFACE ? "extends" : "implements"); 1.759 + f.g(interfaces); 1.760 + f.nl().o(); 1.761 + } 1.762 + declareBody(f); 1.763 + } 1.764 + 1.765 + /** 1.766 + * prints the body of a class. 1.767 + */ 1.768 + protected void declareBody(JFormatter f) { 1.769 + f.p('{').nl().nl().i(); 1.770 + boolean first = true; 1.771 + 1.772 + if (!enumConstantsByName.isEmpty()) { 1.773 + for (JEnumConstant c : enumConstantsByName.values()) { 1.774 + if (!first) f.p(',').nl(); 1.775 + f.d(c); 1.776 + first = false; 1.777 + } 1.778 + f.p(';').nl(); 1.779 + } 1.780 + 1.781 + for( JFieldVar field : fields.values() ) 1.782 + f.d(field); 1.783 + if (init != null) 1.784 + f.nl().p("static").s(init); 1.785 + for (JMethod m : constructors) { 1.786 + f.nl().d(m); 1.787 + } 1.788 + for (JMethod m : methods) { 1.789 + f.nl().d(m); 1.790 + } 1.791 + if(classes!=null) 1.792 + for (JDefinedClass dc : classes.values()) 1.793 + f.nl().d(dc); 1.794 + 1.795 + 1.796 + if (directBlock != null) 1.797 + f.p(directBlock); 1.798 + f.nl().o().p('}').nl(); 1.799 + } 1.800 + 1.801 + /** 1.802 + * Places the given string directly inside the generated class. 1.803 + * 1.804 + * This method can be used to add methods/fields that are not 1.805 + * generated by CodeModel. 1.806 + * This method should be used only as the last resort. 1.807 + */ 1.808 + public void direct(String string) { 1.809 + if (directBlock == null) 1.810 + directBlock = string; 1.811 + else 1.812 + directBlock += string; 1.813 + } 1.814 + 1.815 + public final JPackage _package() { 1.816 + JClassContainer p = outer; 1.817 + while (!(p instanceof JPackage)) 1.818 + p = p.parentContainer(); 1.819 + return (JPackage) p; 1.820 + } 1.821 + 1.822 + public final JClassContainer parentContainer() { 1.823 + return outer; 1.824 + } 1.825 + 1.826 + public JTypeVar generify(String name) { 1.827 + return generifiable.generify(name); 1.828 + } 1.829 + public JTypeVar generify(String name, Class bound) { 1.830 + return generifiable.generify(name, bound); 1.831 + } 1.832 + public JTypeVar generify(String name, JClass bound) { 1.833 + return generifiable.generify(name, bound); 1.834 + } 1.835 + @Override 1.836 + public JTypeVar[] typeParams() { 1.837 + return generifiable.typeParams(); 1.838 + } 1.839 + 1.840 + protected JClass substituteParams( 1.841 + JTypeVar[] variables, 1.842 + List<JClass> bindings) { 1.843 + return this; 1.844 + } 1.845 + 1.846 + /** Adding ability to annotate a class 1.847 + * @param clazz 1.848 + * The annotation class to annotate the class with 1.849 + */ 1.850 + public JAnnotationUse annotate(Class <? extends Annotation> clazz){ 1.851 + return annotate(owner().ref(clazz)); 1.852 + } 1.853 + 1.854 + /** Adding ability to annotate a class 1.855 + * @param clazz 1.856 + * The annotation class to annotate the class with 1.857 + */ 1.858 + public JAnnotationUse annotate(JClass clazz){ 1.859 + if(annotations==null) 1.860 + annotations = new ArrayList<JAnnotationUse>(); 1.861 + JAnnotationUse a = new JAnnotationUse(clazz); 1.862 + annotations.add(a); 1.863 + return a; 1.864 + } 1.865 + 1.866 + public <W extends JAnnotationWriter> W annotate2(Class<W> clazz) { 1.867 + return TypedAnnotationWriter.create(clazz,this); 1.868 + } 1.869 +}