src/share/jaxws_classes/com/sun/codemodel/internal/JPackage.java

Thu, 12 Oct 2017 19:44:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 19:44:07 +0800
changeset 760
e530533619ec
parent 0
373ffda63c9a
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.codemodel.internal;
aoqi@0 27
aoqi@0 28 import java.io.BufferedOutputStream;
aoqi@0 29 import java.io.BufferedWriter;
aoqi@0 30 import java.io.File;
aoqi@0 31 import java.io.IOException;
aoqi@0 32 import java.io.OutputStream;
aoqi@0 33 import java.io.PrintWriter;
aoqi@0 34 import java.io.Writer;
aoqi@0 35 import java.lang.annotation.Annotation;
aoqi@0 36 import java.util.ArrayList;
aoqi@0 37 import java.util.HashMap;
aoqi@0 38 import java.util.HashSet;
aoqi@0 39 import java.util.Iterator;
aoqi@0 40 import java.util.List;
aoqi@0 41 import java.util.Map;
aoqi@0 42 import java.util.Set;
aoqi@0 43 import java.util.TreeMap;
aoqi@0 44 import java.util.Collection;
aoqi@0 45 import java.util.Collections;
aoqi@0 46
aoqi@0 47
aoqi@0 48 /**
aoqi@0 49 * A Java package.
aoqi@0 50 */
aoqi@0 51 public final class JPackage implements JDeclaration, JGenerable, JClassContainer, JAnnotatable, Comparable<JPackage>, JDocCommentable {
aoqi@0 52
aoqi@0 53 /**
aoqi@0 54 * Name of the package.
aoqi@0 55 * May be the empty string for the root package.
aoqi@0 56 */
aoqi@0 57 private String name;
aoqi@0 58
aoqi@0 59 private final JCodeModel owner;
aoqi@0 60
aoqi@0 61 /**
aoqi@0 62 * List of classes contained within this package keyed by their name.
aoqi@0 63 */
aoqi@0 64 private final Map<String,JDefinedClass> classes = new TreeMap<String,JDefinedClass>();
aoqi@0 65
aoqi@0 66 /**
aoqi@0 67 * List of resources files inside this package.
aoqi@0 68 */
aoqi@0 69 private final Set<JResourceFile> resources = new HashSet<JResourceFile>();
aoqi@0 70
aoqi@0 71 /**
aoqi@0 72 * All {@link JClass}s in this package keyed the upper case class name.
aoqi@0 73 *
aoqi@0 74 * This field is non-null only on Windows, to detect
aoqi@0 75 * "Foo" and "foo" as a collision.
aoqi@0 76 */
aoqi@0 77 private final Map<String,JDefinedClass> upperCaseClassMap;
aoqi@0 78
aoqi@0 79 /**
aoqi@0 80 * Lazily created list of package annotations.
aoqi@0 81 */
aoqi@0 82 private List<JAnnotationUse> annotations = null;
aoqi@0 83
aoqi@0 84 /**
aoqi@0 85 * package javadoc.
aoqi@0 86 */
aoqi@0 87 private JDocComment jdoc = null;
aoqi@0 88
aoqi@0 89 /**
aoqi@0 90 * JPackage constructor
aoqi@0 91 *
aoqi@0 92 * @param name
aoqi@0 93 * Name of package
aoqi@0 94 *
aoqi@0 95 * @param cw The code writer being used to create this package
aoqi@0 96 *
aoqi@0 97 * @throws IllegalArgumentException
aoqi@0 98 * If each part of the package name is not a valid identifier
aoqi@0 99 */
aoqi@0 100 JPackage(String name, JCodeModel cw) {
aoqi@0 101 this.owner = cw;
aoqi@0 102 if (name.equals(".")) {
aoqi@0 103 String msg = "Package name . is not allowed";
aoqi@0 104 throw new IllegalArgumentException(msg);
aoqi@0 105 }
aoqi@0 106
aoqi@0 107 if(JCodeModel.isCaseSensitiveFileSystem)
aoqi@0 108 upperCaseClassMap = null;
aoqi@0 109 else
aoqi@0 110 upperCaseClassMap = new HashMap<String,JDefinedClass>();
aoqi@0 111
aoqi@0 112 this.name = name;
aoqi@0 113 }
aoqi@0 114
aoqi@0 115
aoqi@0 116 public JClassContainer parentContainer() {
aoqi@0 117 return parent();
aoqi@0 118 }
aoqi@0 119
aoqi@0 120 /**
aoqi@0 121 * Gets the parent package, or null if this class is the root package.
aoqi@0 122 */
aoqi@0 123 public JPackage parent() {
aoqi@0 124 if(name.length()==0) return null;
aoqi@0 125
aoqi@0 126 int idx = name.lastIndexOf('.');
aoqi@0 127 return owner._package(name.substring(0,idx));
aoqi@0 128 }
aoqi@0 129
aoqi@0 130 public boolean isClass() { return false; }
aoqi@0 131 public boolean isPackage() { return true; }
aoqi@0 132 public JPackage getPackage() { return this; }
aoqi@0 133
aoqi@0 134 /**
aoqi@0 135 * Add a class to this package.
aoqi@0 136 *
aoqi@0 137 * @param mods
aoqi@0 138 * Modifiers for this class declaration
aoqi@0 139 *
aoqi@0 140 * @param name
aoqi@0 141 * Name of class to be added to this package
aoqi@0 142 *
aoqi@0 143 * @return Newly generated class
aoqi@0 144 *
aoqi@0 145 * @exception JClassAlreadyExistsException
aoqi@0 146 * When the specified class/interface was already created.
aoqi@0 147 */
aoqi@0 148 public JDefinedClass _class(int mods, String name) throws JClassAlreadyExistsException {
aoqi@0 149 return _class(mods,name,ClassType.CLASS);
aoqi@0 150 }
aoqi@0 151
aoqi@0 152 /**
aoqi@0 153 * {@inheritDoc}
aoqi@0 154 * @deprecated
aoqi@0 155 */
aoqi@0 156 public JDefinedClass _class( int mods, String name, boolean isInterface ) throws JClassAlreadyExistsException {
aoqi@0 157 return _class(mods,name, isInterface?ClassType.INTERFACE:ClassType.CLASS );
aoqi@0 158 }
aoqi@0 159
aoqi@0 160 public JDefinedClass _class( int mods, String name, ClassType classTypeVal ) throws JClassAlreadyExistsException {
aoqi@0 161 if(classes.containsKey(name))
aoqi@0 162 throw new JClassAlreadyExistsException(classes.get(name));
aoqi@0 163 else {
aoqi@0 164 // XXX problems caught in the NC constructor
aoqi@0 165 JDefinedClass c = new JDefinedClass(this, mods, name, classTypeVal);
aoqi@0 166
aoqi@0 167 if( upperCaseClassMap!=null ) {
aoqi@0 168 JDefinedClass dc = upperCaseClassMap.get(name.toUpperCase());
aoqi@0 169 if(dc!=null)
aoqi@0 170 throw new JClassAlreadyExistsException(dc);
aoqi@0 171 upperCaseClassMap.put(name.toUpperCase(),c);
aoqi@0 172 }
aoqi@0 173 classes.put(name,c);
aoqi@0 174 return c;
aoqi@0 175 }
aoqi@0 176 }
aoqi@0 177
aoqi@0 178 /**
aoqi@0 179 * Adds a public class to this package.
aoqi@0 180 */
aoqi@0 181 public JDefinedClass _class(String name) throws JClassAlreadyExistsException {
aoqi@0 182 return _class( JMod.PUBLIC, name );
aoqi@0 183 }
aoqi@0 184
aoqi@0 185 /**
aoqi@0 186 * Gets a reference to the already created {@link JDefinedClass}.
aoqi@0 187 *
aoqi@0 188 * @return null
aoqi@0 189 * If the class is not yet created.
aoqi@0 190 */
aoqi@0 191 public JDefinedClass _getClass(String name) {
aoqi@0 192 if(classes.containsKey(name))
aoqi@0 193 return classes.get(name);
aoqi@0 194 else
aoqi@0 195 return null;
aoqi@0 196 }
aoqi@0 197
aoqi@0 198 /**
aoqi@0 199 * Order is based on the lexicological order of the package name.
aoqi@0 200 */
aoqi@0 201 public int compareTo(JPackage that) {
aoqi@0 202 return this.name.compareTo(that.name);
aoqi@0 203 }
aoqi@0 204
aoqi@0 205 /**
aoqi@0 206 * Add an interface to this package.
aoqi@0 207 *
aoqi@0 208 * @param mods
aoqi@0 209 * Modifiers for this interface declaration
aoqi@0 210 *
aoqi@0 211 * @param name
aoqi@0 212 * Name of interface to be added to this package
aoqi@0 213 *
aoqi@0 214 * @return Newly generated interface
aoqi@0 215 */
aoqi@0 216 public JDefinedClass _interface(int mods, String name) throws JClassAlreadyExistsException {
aoqi@0 217 return _class(mods,name,ClassType.INTERFACE);
aoqi@0 218 }
aoqi@0 219
aoqi@0 220 /**
aoqi@0 221 * Adds a public interface to this package.
aoqi@0 222 */
aoqi@0 223 public JDefinedClass _interface(String name) throws JClassAlreadyExistsException {
aoqi@0 224 return _interface(JMod.PUBLIC, name);
aoqi@0 225 }
aoqi@0 226
aoqi@0 227 /**
aoqi@0 228 * Add an annotationType Declaration to this package
aoqi@0 229 * @param name
aoqi@0 230 * Name of the annotation Type declaration to be added to this package
aoqi@0 231 * @return
aoqi@0 232 * newly created Annotation Type Declaration
aoqi@0 233 * @exception JClassAlreadyExistsException
aoqi@0 234 * When the specified class/interface was already created.
aoqi@0 235
aoqi@0 236 */
aoqi@0 237 public JDefinedClass _annotationTypeDeclaration(String name) throws JClassAlreadyExistsException {
aoqi@0 238 return _class (JMod.PUBLIC,name,ClassType.ANNOTATION_TYPE_DECL);
aoqi@0 239 }
aoqi@0 240
aoqi@0 241 /**
aoqi@0 242 * Add a public enum to this package
aoqi@0 243 * @param name
aoqi@0 244 * Name of the enum to be added to this package
aoqi@0 245 * @return
aoqi@0 246 * newly created Enum
aoqi@0 247 * @exception JClassAlreadyExistsException
aoqi@0 248 * When the specified class/interface was already created.
aoqi@0 249
aoqi@0 250 */
aoqi@0 251 public JDefinedClass _enum (String name) throws JClassAlreadyExistsException {
aoqi@0 252 return _class (JMod.PUBLIC,name,ClassType.ENUM);
aoqi@0 253 }
aoqi@0 254 /**
aoqi@0 255 * Adds a new resource file to this package.
aoqi@0 256 */
aoqi@0 257 public JResourceFile addResourceFile(JResourceFile rsrc) {
aoqi@0 258 resources.add(rsrc);
aoqi@0 259 return rsrc;
aoqi@0 260 }
aoqi@0 261
aoqi@0 262 /**
aoqi@0 263 * Checks if a resource of the given name exists.
aoqi@0 264 */
aoqi@0 265 public boolean hasResourceFile(String name) {
aoqi@0 266 for (JResourceFile r : resources)
aoqi@0 267 if (r.name().equals(name))
aoqi@0 268 return true;
aoqi@0 269 return false;
aoqi@0 270 }
aoqi@0 271
aoqi@0 272 /**
aoqi@0 273 * Iterates all resource files in this package.
aoqi@0 274 */
aoqi@0 275 public Iterator<JResourceFile> propertyFiles() {
aoqi@0 276 return resources.iterator();
aoqi@0 277 }
aoqi@0 278
aoqi@0 279 /**
aoqi@0 280 * Creates, if necessary, and returns the package javadoc for this
aoqi@0 281 * JDefinedClass.
aoqi@0 282 *
aoqi@0 283 * @return JDocComment containing javadocs for this class
aoqi@0 284 */
aoqi@0 285 public JDocComment javadoc() {
aoqi@0 286 if (jdoc == null)
aoqi@0 287 jdoc = new JDocComment(owner());
aoqi@0 288 return jdoc;
aoqi@0 289 }
aoqi@0 290
aoqi@0 291 /**
aoqi@0 292 * Removes a class from this package.
aoqi@0 293 */
aoqi@0 294 public void remove(JClass c) {
aoqi@0 295 if (c._package() != this)
aoqi@0 296 throw new IllegalArgumentException(
aoqi@0 297 "the specified class is not a member of this package," + " or it is a referenced class");
aoqi@0 298
aoqi@0 299 // note that c may not be a member of classes.
aoqi@0 300 // this happens when someone is trying to remove a non generated class
aoqi@0 301 classes.remove(c.name());
aoqi@0 302 if (upperCaseClassMap != null)
aoqi@0 303 upperCaseClassMap.remove(c.name().toUpperCase());
aoqi@0 304 }
aoqi@0 305
aoqi@0 306 /**
aoqi@0 307 * Reference a class within this package.
aoqi@0 308 */
aoqi@0 309 public JClass ref(String name) throws ClassNotFoundException {
aoqi@0 310 if (name.indexOf('.') >= 0)
aoqi@0 311 throw new IllegalArgumentException("JClass name contains '.': " + name);
aoqi@0 312
aoqi@0 313 String n = "";
aoqi@0 314 if (!isUnnamed())
aoqi@0 315 n = this.name + '.';
aoqi@0 316 n += name;
aoqi@0 317
aoqi@0 318 return owner.ref(Class.forName(n));
aoqi@0 319 }
aoqi@0 320
aoqi@0 321 /**
aoqi@0 322 * Gets a reference to a sub package of this package.
aoqi@0 323 */
aoqi@0 324 public JPackage subPackage( String pkg ) {
aoqi@0 325 if(isUnnamed()) return owner()._package(pkg);
aoqi@0 326 else return owner()._package(name+'.'+pkg);
aoqi@0 327 }
aoqi@0 328
aoqi@0 329 /**
aoqi@0 330 * Returns an iterator that walks the top-level classes defined in this
aoqi@0 331 * package.
aoqi@0 332 */
aoqi@0 333 public Iterator<JDefinedClass> classes() {
aoqi@0 334 return classes.values().iterator();
aoqi@0 335 }
aoqi@0 336
aoqi@0 337 /**
aoqi@0 338 * Checks if a given name is already defined as a class/interface
aoqi@0 339 */
aoqi@0 340 public boolean isDefined(String classLocalName) {
aoqi@0 341 Iterator<JDefinedClass> itr = classes();
aoqi@0 342 while (itr.hasNext()) {
aoqi@0 343 if ((itr.next()).name().equals(classLocalName))
aoqi@0 344 return true;
aoqi@0 345 }
aoqi@0 346
aoqi@0 347 return false;
aoqi@0 348 }
aoqi@0 349
aoqi@0 350 /**
aoqi@0 351 * Checks if this package is the root, unnamed package.
aoqi@0 352 */
aoqi@0 353 public final boolean isUnnamed() { return name.length() == 0; }
aoqi@0 354
aoqi@0 355 /**
aoqi@0 356 * Get the name of this package
aoqi@0 357 *
aoqi@0 358 * @return
aoqi@0 359 * The name of this package, or the empty string if this is the
aoqi@0 360 * null package. For example, this method returns strings like
aoqi@0 361 * <code>"java.lang"</code>
aoqi@0 362 */
aoqi@0 363 public String name() {
aoqi@0 364 return name;
aoqi@0 365 }
aoqi@0 366
aoqi@0 367 /**
aoqi@0 368 * Return the code model root object being used to create this package.
aoqi@0 369 */
aoqi@0 370 public final JCodeModel owner() { return owner; }
aoqi@0 371
aoqi@0 372
aoqi@0 373 public JAnnotationUse annotate(JClass clazz) {
aoqi@0 374 if(isUnnamed())
aoqi@0 375 throw new IllegalArgumentException("the root package cannot be annotated");
aoqi@0 376 if(annotations==null)
aoqi@0 377 annotations = new ArrayList<JAnnotationUse>();
aoqi@0 378 JAnnotationUse a = new JAnnotationUse(clazz);
aoqi@0 379 annotations.add(a);
aoqi@0 380 return a;
aoqi@0 381 }
aoqi@0 382
aoqi@0 383 public JAnnotationUse annotate(Class<? extends Annotation> clazz) {
aoqi@0 384 return annotate(owner.ref(clazz));
aoqi@0 385 }
aoqi@0 386
aoqi@0 387 public <W extends JAnnotationWriter> W annotate2(Class<W> clazz) {
aoqi@0 388 return TypedAnnotationWriter.create(clazz,this);
aoqi@0 389 }
aoqi@0 390
aoqi@0 391 public Collection<JAnnotationUse> annotations() {
aoqi@0 392 if (annotations == null)
aoqi@0 393 annotations = new ArrayList<JAnnotationUse>();
aoqi@0 394 return Collections.unmodifiableList(annotations);
aoqi@0 395 }
aoqi@0 396
aoqi@0 397 /**
aoqi@0 398 * Convert the package name to directory path equivalent
aoqi@0 399 */
aoqi@0 400 File toPath(File dir) {
aoqi@0 401 if (name == null) return dir;
aoqi@0 402 return new File(dir, name.replace('.', File.separatorChar));
aoqi@0 403 }
aoqi@0 404
aoqi@0 405 public void declare(JFormatter f ) {
aoqi@0 406 if (name.length() != 0)
aoqi@0 407 f.p("package").p(name).p(';').nl();
aoqi@0 408 }
aoqi@0 409
aoqi@0 410 public void generate(JFormatter f) {
aoqi@0 411 f.p(name);
aoqi@0 412 }
aoqi@0 413
aoqi@0 414
aoqi@0 415 void build( CodeWriter src, CodeWriter res ) throws IOException {
aoqi@0 416
aoqi@0 417 // write classes
aoqi@0 418 for (JDefinedClass c : classes.values()) {
aoqi@0 419 if (c.isHidden())
aoqi@0 420 continue; // don't generate this file
aoqi@0 421
aoqi@0 422 JFormatter f = createJavaSourceFileWriter(src, c.name());
aoqi@0 423 f.write(c);
aoqi@0 424 f.close();
aoqi@0 425 }
aoqi@0 426
aoqi@0 427 // write package annotations
aoqi@0 428 if(annotations!=null || jdoc!=null) {
aoqi@0 429 JFormatter f = createJavaSourceFileWriter(src,"package-info");
aoqi@0 430
aoqi@0 431 if (jdoc != null)
aoqi@0 432 f.g(jdoc);
aoqi@0 433
aoqi@0 434 // TODO: think about importing
aoqi@0 435 if (annotations != null){
aoqi@0 436 for (JAnnotationUse a : annotations)
aoqi@0 437 f.g(a).nl();
aoqi@0 438 }
aoqi@0 439 f.d(this);
aoqi@0 440
aoqi@0 441 f.close();
aoqi@0 442 }
aoqi@0 443
aoqi@0 444 // write resources
aoqi@0 445 for (JResourceFile rsrc : resources) {
aoqi@0 446 CodeWriter cw = rsrc.isResource() ? res : src;
aoqi@0 447 OutputStream os = new BufferedOutputStream(cw.openBinary(this, rsrc.name()));
aoqi@0 448 rsrc.build(os);
aoqi@0 449 os.close();
aoqi@0 450 }
aoqi@0 451 }
aoqi@0 452
aoqi@0 453 /*package*/ int countArtifacts() {
aoqi@0 454 int r = 0;
aoqi@0 455 for (JDefinedClass c : classes.values()) {
aoqi@0 456 if (c.isHidden())
aoqi@0 457 continue; // don't generate this file
aoqi@0 458 r++;
aoqi@0 459 }
aoqi@0 460
aoqi@0 461 if(annotations!=null || jdoc!=null) {
aoqi@0 462 r++;
aoqi@0 463 }
aoqi@0 464
aoqi@0 465 r+= resources.size();
aoqi@0 466
aoqi@0 467 return r;
aoqi@0 468 }
aoqi@0 469
aoqi@0 470 private JFormatter createJavaSourceFileWriter(CodeWriter src, String className) throws IOException {
aoqi@0 471 Writer bw = new BufferedWriter(src.openSource(this,className+".java"));
aoqi@0 472 return new JFormatter(new PrintWriter(bw));
aoqi@0 473 }
aoqi@0 474 }

mercurial