1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/sun/rmi/rmic/iiop/Generator.java Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,436 @@ 1.4 +/* 1.5 + * Portions Copyright 1998-2007 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 +/* 1.30 + * Licensed Materials - Property of IBM 1.31 + * RMI-IIOP v1.0 1.32 + * Copyright IBM Corp. 1998 1999 All Rights Reserved 1.33 + * 1.34 + */ 1.35 + 1.36 + 1.37 +package sun.rmi.rmic.iiop; 1.38 + 1.39 +import java.io.File; 1.40 +import java.io.FileOutputStream; 1.41 +import java.io.OutputStreamWriter; 1.42 +import java.io.IOException; 1.43 +import sun.tools.java.Identifier; 1.44 +import sun.tools.java.ClassPath; 1.45 +import sun.tools.java.ClassFile; 1.46 +import sun.tools.java.ClassNotFound; 1.47 +import sun.tools.java.ClassDefinition; 1.48 +import sun.tools.java.ClassDeclaration; 1.49 +import sun.rmi.rmic.IndentingWriter; 1.50 +import sun.rmi.rmic.Main; 1.51 +import sun.rmi.rmic.iiop.Util; 1.52 +import java.util.HashSet; 1.53 + 1.54 +/** 1.55 + * Generator provides a small framework from which IIOP-specific 1.56 + * generators can inherit. Common logic is implemented here which uses 1.57 + * both abstract methods as well as concrete methods which subclasses may 1.58 + * want to override. The following methods must be present in any subclass: 1.59 + * <pre> 1.60 + * Default constructor 1.61 + * CompoundType getTopType(BatchEnvironment env, ClassDefinition cdef); 1.62 + * int parseArgs(String argv[], int currentIndex); 1.63 + * boolean requireNewInstance(); 1.64 + * OutputType[] getOutputTypesFor(CompoundType topType, 1.65 + * HashSet alreadyChecked); 1.66 + * String getFileNameExtensionFor(OutputType outputType); 1.67 + * void writeOutputFor ( OutputType outputType, 1.68 + * HashSet alreadyChecked, 1.69 + * IndentingWriter writer) throws IOException; 1.70 + * </pre> 1.71 + * @author Bryan Atsatt 1.72 + */ 1.73 +public abstract class Generator implements sun.rmi.rmic.Generator, 1.74 + sun.rmi.rmic.iiop.Constants { 1.75 + 1.76 + protected boolean alwaysGenerate = false; 1.77 + protected BatchEnvironment env = null; 1.78 + protected ContextStack contextStack = null; 1.79 + private boolean trace = false; 1.80 + protected boolean idl = false; 1.81 + 1.82 + /** 1.83 + * Examine and consume command line arguments. 1.84 + * @param argv The command line arguments. Ignore null 1.85 + * and unknown arguments. Set each consumed argument to null. 1.86 + * @param error Report any errors using the main.error() methods. 1.87 + * @return true if no errors, false otherwise. 1.88 + */ 1.89 + public boolean parseArgs(String argv[], Main main) { 1.90 + for (int i = 0; i < argv.length; i++) { 1.91 + if (argv[i] != null) { 1.92 + if (argv[i].equalsIgnoreCase("-always") || 1.93 + argv[i].equalsIgnoreCase("-alwaysGenerate")) { 1.94 + alwaysGenerate = true; 1.95 + argv[i] = null; 1.96 + } else if (argv[i].equalsIgnoreCase("-xtrace")) { 1.97 + trace = true; 1.98 + argv[i] = null; 1.99 + } 1.100 + } 1.101 + } 1.102 + return true; 1.103 + } 1.104 + 1.105 + /** 1.106 + * Return true if non-conforming types should be parsed. 1.107 + * @param stack The context stack. 1.108 + */ 1.109 + protected abstract boolean parseNonConforming(ContextStack stack); 1.110 + 1.111 + /** 1.112 + * Create and return a top-level type. 1.113 + * @param cdef The top-level class definition. 1.114 + * @param stack The context stack. 1.115 + * @return The compound type or null if is non-conforming. 1.116 + */ 1.117 + protected abstract CompoundType getTopType(ClassDefinition cdef, ContextStack stack); 1.118 + 1.119 + /** 1.120 + * Return an array containing all the file names and types that need to be 1.121 + * generated for the given top-level type. The file names must NOT have an 1.122 + * extension (e.g. ".java"). 1.123 + * @param topType The type returned by getTopType(). 1.124 + * @param alreadyChecked A set of Types which have already been checked. 1.125 + * Intended to be passed to Type.collectMatching(filter,alreadyChecked). 1.126 + */ 1.127 + protected abstract OutputType[] getOutputTypesFor(CompoundType topType, 1.128 + HashSet alreadyChecked); 1.129 + 1.130 + /** 1.131 + * Return the file name extension for the given file name (e.g. ".java"). 1.132 + * All files generated with the ".java" extension will be compiled. To 1.133 + * change this behavior for ".java" files, override the compileJavaSourceFile 1.134 + * method to return false. 1.135 + * @param outputType One of the items returned by getOutputTypesFor(...) 1.136 + */ 1.137 + protected abstract String getFileNameExtensionFor(OutputType outputType); 1.138 + 1.139 + /** 1.140 + * Write the output for the given OutputFileName into the output stream. 1.141 + * @param name One of the items returned by getOutputTypesFor(...) 1.142 + * @param alreadyChecked A set of Types which have already been checked. 1.143 + * Intended to be passed to Type.collectMatching(filter,alreadyChecked). 1.144 + * @param writer The output stream. 1.145 + */ 1.146 + protected abstract void writeOutputFor(OutputType outputType, 1.147 + HashSet alreadyChecked, 1.148 + IndentingWriter writer) throws IOException; 1.149 + 1.150 + /** 1.151 + * Return true if a new instance should be created for each 1.152 + * class on the command line. Subclasses which return true 1.153 + * should override newInstance() to return an appropriately 1.154 + * constructed instance. 1.155 + */ 1.156 + protected abstract boolean requireNewInstance(); 1.157 + 1.158 + /** 1.159 + * Return true if the specified file needs generation. 1.160 + */ 1.161 + public boolean requiresGeneration (File target, Type theType) { 1.162 + 1.163 + boolean result = alwaysGenerate; 1.164 + 1.165 + if (!result) { 1.166 + 1.167 + // Get a ClassFile instance for base source or class 1.168 + // file. We use ClassFile so that if the base is in 1.169 + // a zip file, we can still get at it's mod time... 1.170 + 1.171 + ClassFile baseFile; 1.172 + ClassPath path = env.getClassPath(); 1.173 + String className = theType.getQualifiedName().replace('.',File.separatorChar); 1.174 + 1.175 + // First try the source file... 1.176 + 1.177 + baseFile = path.getFile(className + ".source"); 1.178 + 1.179 + if (baseFile == null) { 1.180 + 1.181 + // Then try class file... 1.182 + 1.183 + baseFile = path.getFile(className + ".class"); 1.184 + } 1.185 + 1.186 + // Do we have a baseFile? 1.187 + 1.188 + if (baseFile != null) { 1.189 + 1.190 + // Yes, grab baseFile's mod time... 1.191 + 1.192 + long baseFileMod = baseFile.lastModified(); 1.193 + 1.194 + // Get a File instance for the target. If it is a source 1.195 + // file, create a class file instead since the source file 1.196 + // will frequently be deleted... 1.197 + 1.198 + String targetName = IDLNames.replace(target.getName(),".java",".class"); 1.199 + String parentPath = target.getParent(); 1.200 + File targetFile = new File(parentPath,targetName); 1.201 + 1.202 + // Does the target file exist? 1.203 + 1.204 + if (targetFile.exists()) { 1.205 + 1.206 + // Yes, so grab it's mod time... 1.207 + 1.208 + long targetFileMod = targetFile.lastModified(); 1.209 + 1.210 + // Set result... 1.211 + 1.212 + result = targetFileMod < baseFileMod; 1.213 + 1.214 + } else { 1.215 + 1.216 + // No, so we must generate... 1.217 + 1.218 + result = true; 1.219 + } 1.220 + } else { 1.221 + 1.222 + // No, so we must generate... 1.223 + 1.224 + result = true; 1.225 + } 1.226 + } 1.227 + 1.228 + return result; 1.229 + } 1.230 + 1.231 + /** 1.232 + * Create and return a new instance of self. Subclasses 1.233 + * which need to do something other than default construction 1.234 + * must override this method. 1.235 + */ 1.236 + protected Generator newInstance() { 1.237 + Generator result = null; 1.238 + try { 1.239 + result = (Generator) getClass().newInstance(); 1.240 + } 1.241 + catch (Exception e){} // Should ALWAYS work! 1.242 + 1.243 + return result; 1.244 + } 1.245 + 1.246 + /** 1.247 + * Default constructor for subclasses to use. 1.248 + */ 1.249 + protected Generator() { 1.250 + } 1.251 + 1.252 + /** 1.253 + * Generate output. Any source files created which need compilation should 1.254 + * be added to the compiler environment using the addGeneratedFile(File) 1.255 + * method. 1.256 + * 1.257 + * @param env The compiler environment 1.258 + * @param cdef The definition for the implementation class or interface from 1.259 + * which to generate output 1.260 + * @param destDir The directory for the root of the package hierarchy 1.261 + * for generated files. May be null. 1.262 + */ 1.263 + public void generate(sun.rmi.rmic.BatchEnvironment env, ClassDefinition cdef, File destDir) { 1.264 + 1.265 + this.env = (BatchEnvironment) env; 1.266 + contextStack = new ContextStack(this.env); 1.267 + contextStack.setTrace(trace); 1.268 + 1.269 + // Make sure the environment knows whether or not to parse 1.270 + // non-conforming types. This will clear out any previously 1.271 + // parsed types if necessary... 1.272 + 1.273 + this.env.setParseNonConforming(parseNonConforming(contextStack)); 1.274 + 1.275 + // Get our top level type... 1.276 + 1.277 + CompoundType topType = getTopType(cdef,contextStack); 1.278 + if (topType != null) { 1.279 + 1.280 + Generator generator = this; 1.281 + 1.282 + // Do we need to make a new instance? 1.283 + 1.284 + if (requireNewInstance()) { 1.285 + 1.286 + // Yes, so make one. 'this' instance is the one instantiated by Main 1.287 + // and which knows any needed command line args... 1.288 + 1.289 + generator = newInstance(); 1.290 + } 1.291 + 1.292 + // Now generate all output files... 1.293 + 1.294 + generator.generateOutputFiles(topType, this.env, destDir); 1.295 + } 1.296 + } 1.297 + 1.298 + /** 1.299 + * Create and return a new instance of self. Subclasses 1.300 + * which need to do something other than default construction 1.301 + * must override this method. 1.302 + */ 1.303 + protected void generateOutputFiles (CompoundType topType, 1.304 + BatchEnvironment env, 1.305 + File destDir) { 1.306 + 1.307 + // Grab the 'alreadyChecked' HashSet from the environment... 1.308 + 1.309 + HashSet alreadyChecked = env.alreadyChecked; 1.310 + 1.311 + // Ask subclass for a list of output types... 1.312 + 1.313 + OutputType[] types = getOutputTypesFor(topType,alreadyChecked); 1.314 + 1.315 + // Process each file... 1.316 + 1.317 + for (int i = 0; i < types.length; i++) { 1.318 + OutputType current = types[i]; 1.319 + String className = current.getName(); 1.320 + File file = getFileFor(current,destDir); 1.321 + boolean sourceFile = false; 1.322 + 1.323 + // Do we need to generate this file? 1.324 + 1.325 + if (requiresGeneration(file,current.getType())) { 1.326 + 1.327 + // Yes. If java source file, add to environment so will be compiled... 1.328 + 1.329 + if (file.getName().endsWith(".java")) { 1.330 + sourceFile = compileJavaSourceFile(current); 1.331 + 1.332 + // Are we supposeded to compile this one? 1.333 + 1.334 + if (sourceFile) { 1.335 + env.addGeneratedFile(file); 1.336 + } 1.337 + } 1.338 + 1.339 + // Now create an output stream and ask subclass to fill it up... 1.340 + 1.341 + try { 1.342 + IndentingWriter out = new IndentingWriter( 1.343 + new OutputStreamWriter(new FileOutputStream(file)),INDENT_STEP,TAB_SIZE); 1.344 + 1.345 + long startTime = 0; 1.346 + if (env.verbose()) { 1.347 + startTime = System.currentTimeMillis(); 1.348 + } 1.349 + 1.350 + writeOutputFor(types[i],alreadyChecked,out); 1.351 + out.close(); 1.352 + 1.353 + if (env.verbose()) { 1.354 + long duration = System.currentTimeMillis() - startTime; 1.355 + env.output(Main.getText("rmic.generated", file.getPath(), Long.toString(duration))); 1.356 + } 1.357 + if (sourceFile) { 1.358 + env.parseFile(new ClassFile(file)); 1.359 + } 1.360 + } catch (IOException e) { 1.361 + env.error(0, "cant.write", file.toString()); 1.362 + return; 1.363 + } 1.364 + } else { 1.365 + 1.366 + // No, say so if we need to... 1.367 + 1.368 + if (env.verbose()) { 1.369 + env.output(Main.getText("rmic.previously.generated", file.getPath())); 1.370 + } 1.371 + } 1.372 + } 1.373 + } 1.374 + 1.375 + /** 1.376 + * Return the File object that should be used as the output file 1.377 + * for the given OutputType. 1.378 + * @param outputType The type to create a file for. 1.379 + * @param destinationDir The directory to use as the root of the 1.380 + * package heirarchy. May be null, in which case the current 1.381 + * classpath is searched to find the directory in which to create 1.382 + * the output file. If that search fails (most likely because the 1.383 + * package directory lives in a zip or jar file rather than the 1.384 + * file system), the current user directory is used. 1.385 + */ 1.386 + protected File getFileFor(OutputType outputType, File destinationDir) { 1.387 + // Calling this method does some crucial initialization 1.388 + // in a subclass implementation. Don't skip it. 1.389 + Identifier id = getOutputId(outputType); 1.390 + File packageDir = null; 1.391 + if(idl){ 1.392 + packageDir = Util.getOutputDirectoryForIDL(id,destinationDir,env); 1.393 + } else { 1.394 + packageDir = Util.getOutputDirectoryForStub(id,destinationDir,env); 1.395 + } 1.396 + String classFileName = outputType.getName() + getFileNameExtensionFor(outputType); 1.397 + return new File(packageDir, classFileName); 1.398 + } 1.399 + 1.400 + /** 1.401 + * Return an identifier to use for output. 1.402 + * @param outputType the type for which output is to be generated. 1.403 + * @return the new identifier. This implementation returns the input parameter. 1.404 + */ 1.405 + protected Identifier getOutputId (OutputType outputType) { 1.406 + return outputType.getType().getIdentifier(); 1.407 + } 1.408 + 1.409 + /** 1.410 + * Return true if the given file should be compiled. 1.411 + * @param outputType One of the items returned by getOutputTypesFor(...) for 1.412 + * which getFileNameExtensionFor(OutputType) returned ".java". 1.413 + */ 1.414 + protected boolean compileJavaSourceFile (OutputType outputType) { 1.415 + return true; 1.416 + } 1.417 + 1.418 + //_____________________________________________________________________ 1.419 + // OutputType is a simple wrapper for a name and a Type 1.420 + //_____________________________________________________________________ 1.421 + 1.422 + public class OutputType { 1.423 + private String name; 1.424 + private Type type; 1.425 + 1.426 + public OutputType (String name, Type type) { 1.427 + this.name = name; 1.428 + this.type = type; 1.429 + } 1.430 + 1.431 + public String getName() { 1.432 + return name; 1.433 + } 1.434 + 1.435 + public Type getType() { 1.436 + return type; 1.437 + } 1.438 + } 1.439 +}