javacompiler.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 701 行 · 第 1/2 页
JAVA
701 行
/* * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved * * This file is part of Resin(R) Open Source * * Each copy or derived work must preserve the copyright notice and this * notice unmodified. * * Resin Open Source is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * Resin Open Source is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty * of NON-INFRINGEMENT. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License * along with Resin Open Source; if not, write to the * * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * * @author Scott Ferguson */package com.caucho.java;import com.caucho.bytecode.ByteCodeParser;import com.caucho.bytecode.JavaClass;import com.caucho.bytecode.SourceDebugExtensionAttribute;import com.caucho.config.*;import com.caucho.loader.DynamicClassLoader;import com.caucho.log.Log;import com.caucho.make.Make;import com.caucho.server.util.CauchoSystem;import com.caucho.util.CharBuffer;import com.caucho.util.L10N;import com.caucho.vfs.Encoding;import com.caucho.vfs.IOExceptionWrapper;import com.caucho.vfs.Path;import com.caucho.vfs.ReadStream;import com.caucho.vfs.WriteStream;import java.io.IOException;import java.util.ArrayList;import java.util.Arrays;import java.util.logging.Level;import java.util.logging.Logger;import java.util.regex.Pattern;/** * Compiles Java source, returning the loaded class. */public class JavaCompiler { static final L10N L = new L10N(JavaCompiler.class); static final Logger log = Log.open(JavaCompiler.class); private static final Object LOCK = new Object(); // Parent class loader. Used to grab the classpath. private ClassLoader _loader; // Executable name of the compiler private String _compiler; private String _sourceExt = ".java"; private Path _classDir; private Path _sourceDir; private boolean _compileParent = true; private String _extraClassPath; private String _classPath; protected String _charEncoding; protected ArrayList<String> _args; private int _maxBatch = 64; protected long _maxCompileTime = 120 * 1000L; private JavaCompiler() { } /** * Creates a new compiler. */ public static JavaCompiler create() { return create(Thread.currentThread().getContextClassLoader()); } /** * Creates a new compiler. * * @param loader the parent class loader for the compiler. */ public static JavaCompiler create(ClassLoader loader) { JavacConfig config = JavacConfig.getLocalConfig(); String javac = config.getCompiler(); JavaCompiler javaCompiler = new JavaCompiler(); if (loader == null) loader = Thread.currentThread().getContextClassLoader(); javaCompiler.setClassLoader(loader); javaCompiler.setCompiler(javac); javaCompiler.setArgs(config.getArgs()); javaCompiler.setEncoding(config.getEncoding()); javaCompiler.setMaxBatch(config.getMaxBatch()); return javaCompiler; } /** * Sets the class loader used to load the compiled class and to grab * the classpath from. */ public void setClassLoader(ClassLoader loader) { _loader = loader; } /** * Sets the class loader used to load the compiled class and to grab * the classpath from. */ public ClassLoader getClassLoader() { return _loader; } /** * Sets the compiler name, e.g. jikes. */ public void setCompiler(String compiler) { _compiler = compiler; } /** * Gets the compiler name, e.g. jikes. */ public String getCompiler() { if (_compiler == null) _compiler = "javac"; return _compiler; } /** * Sets the directory where compiled classes go. * * @param path representing the class dir. */ public void setClassDir(Path path) { try { path.mkdirs(); } catch (IOException e) { } _classDir = path; } /** * Returns the directory where compiled classes go. */ Path getClassDir() { if (_classDir != null) return _classDir; else return CauchoSystem.getWorkPath(); } /** * Returns the path to the class directory. */ String getClassDirName() { return getClassDir().getNativePath(); } /** * Sets the directory the java source comes from. * * @param path representing the source dir. */ public void setSourceDir(Path path) { _sourceDir = path; } /** * Returns the directory where compiled classes go. */ public Path getSourceDir() { if (_sourceDir != null) return _sourceDir; else return getClassDir(); } /** * Returns the path to the class directory. */ String getSourceDirName() { return getSourceDir().getNativePath(); } /** * Sets the source extension. */ public void setSourceExtension(String ext) { _sourceExt = ext; } /** * Gets the source extension. */ public String getSourceExtension() { return _sourceExt; } /** * Sets the class path for compilation. Normally, the classpath from * the class loader will be sufficient. */ public void setClassPath(String classPath) { _classPath = classPath; } /** * Sets an extra class path for compilation. */ public void setExtraClassPath(String classPath) { _extraClassPath = classPath; } public void setCompileParent(boolean compileParent) { _compileParent = compileParent; } /** * Returns the classpath for the compiler. */ public String getClassPath() { String classPath = null;//_classPath; if (classPath != null) return classPath; if (classPath == null && _loader instanceof DynamicClassLoader) { classPath = ((DynamicClassLoader) _loader).getClassPath(); } else if (classPath == null) classPath = CauchoSystem.getClassPath(); String srcDirName = getSourceDirName(); String classDirName = getClassDirName(); char sep = CauchoSystem.getPathSeparatorChar(); if (_extraClassPath != null) classPath = classPath + sep + _extraClassPath; // Adding the srcDir lets javac and jikes find source files if (! srcDirName.equals(classDirName)) classPath = srcDirName + sep + classPath; classPath = classDirName + sep + classPath; return classPath; } /** * Sets any additional arguments for the compiler. */ public void setArgs(String argString) { try { if (argString != null) { String []args = Pattern.compile("[\\s,]+").split(argString); _args = new ArrayList<String>(); for (int i = 0; i < args.length; i++) { if (! args[i].equals("")) _args.add(args[i]); } } } catch (Exception e) { log.log(Level.WARNING, e.toString(), e); } } /** * Returns the ArrayList of arguments. */ public ArrayList<String> getArgs() { return _args; } /** * Sets the Java encoding for the compiler. */ public void setEncoding(String encoding) { _charEncoding = encoding; String javaEncoding = Encoding.getJavaName(encoding); if ("ISO8859_1".equals(javaEncoding)) _charEncoding = null; } /** * Returns the encoding. */ public String getEncoding() { return _charEncoding; } /** * Returns the maximum time allowed for an external compilation. */ public long getMaxCompileTime() { return _maxCompileTime; } /**
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?