📄 netrexxc.java
字号:
} /** * Executes the task - performs the actual compiler call. * @throws BuildException on error. */ public void execute() throws BuildException { // first off, make sure that we've got a srcdir and destdir if (srcDir == null || destDir == null) { throw new BuildException("srcDir and destDir attributes must be set!"); } // scan source and dest dirs to build up both copy lists and // compile lists // scanDir(srcDir, destDir); DirectoryScanner ds = getDirectoryScanner(srcDir); String[] files = ds.getIncludedFiles(); scanDir(srcDir, destDir, files); // copy the source and support files copyFilesToDestination(); // compile the source files if (compileList.size() > 0) { log("Compiling " + compileList.size() + " source file" + (compileList.size() == 1 ? "" : "s") + " to " + destDir); doNetRexxCompile(); } } /** * Scans the directory looking for source files to be compiled and support * files to be copied. */ private void scanDir(File srcDir, File destDir, String[] files) { for (int i = 0; i < files.length; i++) { File srcFile = new File(srcDir, files[i]); File destFile = new File(destDir, files[i]); String filename = files[i]; // if it's a non source file, copy it if a later date than the // dest // if it's a source file, see if the destination class file // needs to be recreated via compilation if (filename.toLowerCase().endsWith(".nrx")) { File classFile = new File(destDir, filename.substring(0, filename.lastIndexOf('.')) + ".class"); if (!compile || srcFile.lastModified() > classFile.lastModified()) { filecopyList.put(srcFile.getAbsolutePath(), destFile.getAbsolutePath()); compileList.addElement(destFile.getAbsolutePath()); } } else { if (srcFile.lastModified() > destFile.lastModified()) { filecopyList.put(srcFile.getAbsolutePath(), destFile.getAbsolutePath()); } } } } /** Copy eligible files from the srcDir to destDir */ private void copyFilesToDestination() { if (filecopyList.size() > 0) { log("Copying " + filecopyList.size() + " file" + (filecopyList.size() == 1 ? "" : "s") + " to " + destDir.getAbsolutePath()); Enumeration e = filecopyList.keys(); while (e.hasMoreElements()) { String fromFile = (String) e.nextElement(); String toFile = (String) filecopyList.get(fromFile); try { FileUtils.getFileUtils().copyFile(fromFile, toFile); } catch (IOException ioe) { String msg = "Failed to copy " + fromFile + " to " + toFile + " due to " + ioe.getMessage(); throw new BuildException(msg, ioe); } } } } /** Performs a compile using the NetRexx 1.1.x compiler */ private void doNetRexxCompile() throws BuildException { log("Using NetRexx compiler", Project.MSG_VERBOSE); String classpath = getCompileClasspath(); StringBuffer compileOptions = new StringBuffer(); // create an array of strings for input to the compiler: one array // comes from the compile options, the other from the compileList String[] compileOptionsArray = getCompileOptionsAsArray(); String[] fileListArray = new String[compileList.size()]; Enumeration e = compileList.elements(); int j = 0; while (e.hasMoreElements()) { fileListArray[j] = (String) e.nextElement(); j++; } // create a single array of arguments for the compiler String[] compileArgs = new String[compileOptionsArray.length + fileListArray.length]; for (int i = 0; i < compileOptionsArray.length; i++) { compileArgs[i] = compileOptionsArray[i]; } for (int i = 0; i < fileListArray.length; i++) { compileArgs[i + compileOptionsArray.length] = fileListArray[i]; } // print nice output about what we are doing for the log compileOptions.append("Compilation args: "); for (int i = 0; i < compileOptionsArray.length; i++) { compileOptions.append(compileOptionsArray[i]); compileOptions.append(" "); } log(compileOptions.toString(), Project.MSG_VERBOSE); String eol = System.getProperty("line.separator"); StringBuffer niceSourceList = new StringBuffer("Files to be compiled:" + eol); for (int i = 0; i < compileList.size(); i++) { niceSourceList.append(" "); niceSourceList.append(compileList.elementAt(i).toString()); niceSourceList.append(eol); } log(niceSourceList.toString(), Project.MSG_VERBOSE); // need to set java.class.path property and restore it later // since the NetRexx compiler has no option for the classpath String currentClassPath = System.getProperty("java.class.path"); Properties currentProperties = System.getProperties(); currentProperties.put("java.class.path", classpath); try { StringWriter out = new StringWriter(); int rc = COM.ibm.netrexx.process.NetRexxC.main(new Rexx(compileArgs), new PrintWriter(out)); String sdir = srcDir.getAbsolutePath(); String ddir = destDir.getAbsolutePath(); boolean doReplace = !(sdir.equals(ddir)); int dlen = ddir.length(); String l; BufferedReader in = new BufferedReader(new StringReader(out.toString())); log("replacing destdir '" + ddir + "' through sourcedir '" + sdir + "'", Project.MSG_VERBOSE); while ((l = in.readLine()) != null) { int idx; while (doReplace && ((idx = l.indexOf(ddir)) != -1)) { // path is mentioned in the message l = (new StringBuffer(l)).replace(idx, idx + dlen, sdir).toString(); } // verbose level logging for suppressed messages if (suppressMethodArgumentNotUsed && l.indexOf(MSG_METHOD_ARGUMENT_NOT_USED) != -1) { log(l, Project.MSG_VERBOSE); } else if (suppressPrivatePropertyNotUsed && l.indexOf(MSG_PRIVATE_PROPERTY_NOT_USED) != -1) { log(l, Project.MSG_VERBOSE); } else if (suppressVariableNotUsed && l.indexOf(MSG_VARIABLE_NOT_USED) != -1) { log(l, Project.MSG_VERBOSE); } else if (suppressExceptionNotSignalled && l.indexOf(MSG_EXCEPTION_NOT_SIGNALLED) != -1) { log(l, Project.MSG_VERBOSE); } else if (suppressDeprecation && l.indexOf(MSG_DEPRECATION) != -1) { log(l, Project.MSG_VERBOSE); } else if (l.indexOf("Error:") != -1) { // error level logging for compiler errors log(l, Project.MSG_ERR); } else if (l.indexOf("Warning:") != -1) { // warning for all warning messages log(l, Project.MSG_WARN); } else { log(l, Project.MSG_INFO); // info level for the rest. } } if (rc > 1) { throw new BuildException("Compile failed, messages should " + "have been provided."); } } catch (IOException ioe) { throw new BuildException("Unexpected IOException while " + "playing with Strings", ioe); } finally { // need to reset java.class.path property // since the NetRexx compiler has no option for the classpath currentProperties = System.getProperties(); currentProperties.put("java.class.path", currentClassPath); } } /** Builds the compilation classpath. */ private String getCompileClasspath() { StringBuffer classpath = new StringBuffer(); // add dest dir to classpath so that previously compiled and // untouched classes are on classpath classpath.append(destDir.getAbsolutePath()); // add our classpath to the mix if (this.classpath != null) { addExistingToClasspath(classpath, this.classpath); } // add the system classpath // addExistingToClasspath(classpath,System.getProperty("java.class.path")); return classpath.toString(); } /** This */ private String[] getCompileOptionsAsArray() { Vector options = new Vector(); options.addElement(binary ? "-binary" : "-nobinary"); options.addElement(comments ? "-comments" : "-nocomments"); options.addElement(compile ? "-compile" : "-nocompile"); options.addElement(compact ? "-compact" : "-nocompact"); options.addElement(console ? "-console" : "-noconsole"); options.addElement(crossref ? "-crossref" : "-nocrossref"); options.addElement(decimal ? "-decimal" : "-nodecimal"); options.addElement(diag ? "-diag" : "-nodiag"); options.addElement(explicit ? "-explicit" : "-noexplicit"); options.addElement(format ? "-format" : "-noformat"); options.addElement(keep ? "-keep" : "-nokeep"); options.addElement(logo ? "-logo" : "-nologo"); options.addElement(replace ? "-replace" : "-noreplace"); options.addElement(savelog ? "-savelog" : "-nosavelog"); options.addElement(sourcedir ? "-sourcedir" : "-nosourcedir"); options.addElement(strictargs ? "-strictargs" : "-nostrictargs"); options.addElement(strictassign ? "-strictassign" : "-nostrictassign"); options.addElement(strictcase ? "-strictcase" : "-nostrictcase"); options.addElement(strictimport ? "-strictimport" : "-nostrictimport"); options.addElement(strictprops ? "-strictprops" : "-nostrictprops"); options.addElement(strictsignal ? "-strictsignal" : "-nostrictsignal"); options.addElement(symbols ? "-symbols" : "-nosymbols"); options.addElement(time ? "-time" : "-notime"); options.addElement("-" + trace); options.addElement(utf8 ? "-utf8" : "-noutf8"); options.addElement("-" + verbose); String[] results = new String[options.size()]; options.copyInto(results); return results; } /** * Takes a classpath-like string, and adds each element of this string to * a new classpath, if the components exist. Components that don't exist, * aren't added. We do this, because jikes issues warnings for * non-existant files/dirs in his classpath, and these warnings are pretty * annoying. * * @param target - target classpath * @param source - source classpath to get file objects. */ private void addExistingToClasspath(StringBuffer target, String source) { StringTokenizer tok = new StringTokenizer(source, System.getProperty("path.separator"), false); while (tok.hasMoreTokens()) { File f = getProject().resolveFile(tok.nextToken()); if (f.exists()) { target.append(File.pathSeparator); target.append(f.getAbsolutePath()); } else { log("Dropping from classpath: " + f.getAbsolutePath(), Project.MSG_VERBOSE); } } } /** * Enumerated class corresponding to the trace attribute. */ public static class TraceAttr extends EnumeratedAttribute { /** {@inheritDoc}. */ public String[] getValues() { return new String[]{"trace", "trace1", "trace2", "notrace"}; } } /** * Enumerated class corresponding to the verbose attribute. */ public static class VerboseAttr extends EnumeratedAttribute { /** {@inheritDoc}. */ public String[] getValues() { return new String[]{"verbose", "verbose0", "verbose1", "verbose2", "verbose3", "verbose4", "verbose5", "noverbose"}; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -