compilingloader.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 886 行 · 第 1/2 页

JAVA
886
字号
	if (! subSource.exists()) {	  String tail = subSource.getTail();	  boolean doRemove = true;	  if (tail.indexOf('$') > 0) {	    String subTail = tail.substring(0, tail.indexOf('$')) + _sourceExt;	    Path subJava = subSource.getParent().lookup(subTail);	    if (subJava.exists())	      doRemove = false;	  }	  if (doRemove) {	    log.finer(L.l("removing obsolete class `{0}'.", subClass.getPath()));	    subClass.remove();	  }	}      }    }  }  /**   * Returns the classes which need compilation.   */  private void findAllModifiedClasses(String name,				      Path sourceDir,				      Path classDir,				      String sourcePath,				      ArrayList<String> sources)    throws IOException, ClassNotFoundException  {    String []list;    try {      list = sourceDir.list();    } catch (IOException e) {      return;    }    for (int i = 0; list != null && i < list.length; i++) {      if (list[i].startsWith("."))        continue;      if (_excludedDirectories.contains(list[i]))        continue;            Path subSource = sourceDir.lookup(list[i]);      if (subSource.isDirectory()) {	findAllModifiedClasses(name + list[i] + "/", subSource,			       classDir.lookup(list[i]), sourcePath, sources);      }      else if (list[i].endsWith(_sourceExt)) {	int tail = list[i].length() - _sourceExt.length();	String prefix = list[i].substring(0, tail);	Path subClass = classDir.lookup(prefix + ".class");	if (subClass.getLastModified() < subSource.getLastModified()) {	  sources.add(name + list[i]);	}      }    }    if (! _requireSource)      return;        try {      list = classDir.list();    } catch (IOException e) {      return;    }    for (int i = 0; list != null && i < list.length; i++) {      if (list[i].startsWith("."))        continue;      if (_excludedDirectories.contains(list[i]))        continue;            Path subClass = classDir.lookup(list[i]);      if (list[i].endsWith(".class")) {	String prefix = list[i].substring(0, list[i].length() - 6);	Path subSource = sourceDir.lookup(prefix + _sourceExt);	if (! subSource.exists()) {	  String tail = subSource.getTail();	  boolean doRemove = true;	  if (tail.indexOf('$') > 0) {	    String subTail = tail.substring(0, tail.indexOf('$')) + _sourceExt;	    Path subJava = subSource.getParent().lookup(subTail);	    if (subJava.exists())	      doRemove = false;	  }	  if (doRemove) {	    log.finer(L.l("removing obsolete class '{0}'.", subClass.getPath()));	    subClass.remove();	  }	}      }    }  }  /**   * Loads the specified class, compiling if necessary.   */  @Override  protected ClassEntry getClassEntry(String name, String pathName)    throws ClassNotFoundException  {    synchronized (this) {      Path classFile = _classDir.lookup(pathName);      String javaName = name.replace('.', '/') + _sourceExt;      Path javaFile = _sourceDir.lookup(javaName);      String tail = javaFile.getTail();      for (int i = 0; i < INNER_CLASS_SEPARATORS.length; i++) {	char sep = INNER_CLASS_SEPARATORS[i];	if (name.indexOf(sep) > 0) {	  String subName = name.substring(0, name.indexOf(sep));	  String subJavaName = subName.replace('.', '/') + _sourceExt;	  Path subJava = _sourceDir.lookup(subJavaName);	  if (subJava.exists()) {	    javaFile = subJava;	  }	}      }      if (_requireSource && ! javaFile.exists()) {	boolean doRemove = true;	if (doRemove) {	  log.finer(L.l("removing obsolete class `{0}'.", classFile.getPath()));	  try {	    classFile.remove();	  } catch (IOException e) {	    log.log(Level.WARNING, e.toString(), e);	  }	  return null;	}      }      if (! classFile.canRead() && ! javaFile.canRead())	return null;      return new CompilingClassEntry(this, getLoader(),				     name, javaFile,				     classFile,				     getCodeSource(classFile));    }  }  /**   * Returns the code source for the directory.   */  protected CodeSource getCodeSource(Path path)  {    return _codeSource;  }  /**   * Checks that the case is okay for the source.   */  boolean checkSource(Path sourceDir, String javaName)  {    try {      while (javaName != null && ! javaName.equals("")) {        int p = javaName.indexOf('/');        String head;              if (p >= 0) {          head = javaName.substring(0, p);          javaName = javaName.substring(p + 1);        }        else {          head = javaName;          javaName = null;        }        String []names = sourceDir.list();        int i;        for (i = 0; i < names.length; i++) {          if (names[i].equals(head))            break;        }        if (i == names.length)          return false;        sourceDir = sourceDir.lookup(head);      }    } catch (IOException e) {      log.log(Level.FINE, e.toString(), e);            return false;    }    return true;  }  /**   * Loads the class from the class file.   *   * @param className the name of the class to load   * @param javaFile the path to the java source   * @param classFile the path to the class file   *   * @return a class entry or null on failure   */  private ClassEntry loadClass(String className, Path javaFile, Path classFile)  {    long length = classFile.getLength();    ClassEntry entry = new CompilingClassEntry(this, getLoader(),					       className, javaFile,					       classFile,					       getCodeSource(classFile));    Class cl = null;    try {      cl = getLoader().loadClass(entry);    } catch (Exception e) {      try {        if (javaFile.canRead())          classFile.remove();      } catch (IOException e1) {      }    }    if (cl != null)      return entry;    else      return null;  }  /**   * Compile the Java source.  Compile errors are encapsulated in a   * ClassNotFound wrapper.   *   * @param javaSource path to the Java source   */  void compileClass(Path javaSource, Path javaClass,		    String sourcePath, boolean isMake)    throws ClassNotFoundException  {    try {      JavaCompiler compiler = JavaCompiler.create(getLoader());      compiler.setClassDir(_classDir);      compiler.setSourceDir(_sourceDir);      if (_encoding != null)        compiler.setEncoding(_encoding);      compiler.setArgs(_args);      compiler.setCompileParent(! isMake);      compiler.setSourceExtension(_sourceExt);      if (_compiler != null)	compiler.setCompiler(_compiler);      //LineMap lineMap = new LineMap(javaFile.getNativePath());      // The context path is obvious from the browser url      //lineMap.add(name.replace('.', '/') + _sourceExt, 1, 1);      // Force this into a relative path so different compilers will work      String prefix = _sourceDir.getPath();      String full = javaSource.getPath();      String source;      if (full.startsWith(prefix)) {        source = full.substring(prefix.length());        if (source.startsWith("/"))          source = source.substring(1);      }      else        source = javaSource.getPath();      /*      if (javaSource.canRead() && javaClass.exists())        javaClass.remove();      */                compiler.compileIfModified(source, null);    } catch (Exception e) {      getLoader().addDependency(new Depend(javaSource));      log.log(Level.FINEST, e.toString(), e);      // Compile errors are wrapped in a special ClassNotFound class      // so the server can give a nice error message      throw new CompileClassNotFound(e);    }  }  /**   * Compile the Java source.  Compile errors are encapsulated in a   * ClassNotFound wrapper.   */  void compileBatch(String []files, boolean isMake)    throws ClassNotFoundException  {    try {      JavaCompiler compiler = JavaCompiler.create(getLoader());      compiler.setClassDir(_classDir);      compiler.setSourceDir(_sourceDir);      if (_encoding != null)        compiler.setEncoding(_encoding);      compiler.setArgs(_args);      compiler.setCompileParent(! isMake);      compiler.setSourceExtension(_sourceExt);      if (_compiler != null)	compiler.setCompiler(_compiler);      //LineMap lineMap = new LineMap(javaFile.getNativePath());      // The context path is obvious from the browser url      //lineMap.add(name.replace('.', '/') + _sourceExt, 1, 1);                compiler.compileBatch(files);    } catch (Exception e) {      getLoader().addDependency(AlwaysModified.create());      // Compile errors are wrapped in a special ClassNotFound class      // so the server can give a nice error message      throw new CompileClassNotFound(e);    }  }  /**   * Returns the path for the given name.   *   * @param name the name of the class   */  @Override  public Path getPath(String name)  {    Path path = _classDir.lookup(name);    if (path != null && path.exists())      return path;    path = _sourceDir.lookup(name);    if (path != null && path.exists())      return path;    return null;  }  /**   * Adds the classpath we're responsible for to the classpath   *   * @param head the overriding classpath   * @return the new classpath   */  @Override  protected void buildClassPath(ArrayList<String> pathList)  {    if (! _classDir.getScheme().equals("file"))      return;    try {      if (! _classDir.isDirectory() && _sourceDir.isDirectory()) {        try {          _classDir.mkdirs();        } catch (IOException e) {        }      }      if (_classDir.isDirectory()) {	String path = _classDir.getNativePath();	if (! pathList.contains(path))	  pathList.add(path);      }          if (! _classDir.equals(_sourceDir)) {	String path = _sourceDir.getNativePath();	if (! pathList.contains(path))	  pathList.add(path);      }    } catch (java.security.AccessControlException e) {      log.log(Level.WARNING, e.toString(), e);    }  }  protected String prefixClassPath(String tail)  {    CharBuffer cb = new CharBuffer();    if (! _classDir.isDirectory() && _sourceDir.isDirectory()) {      try {        _classDir.mkdirs();      } catch (IOException e) {      }    }    if (_classDir.isDirectory()) {      if (cb.length() > 0)        cb.append(CauchoSystem.getPathSeparatorChar());      cb.append(_classDir.getNativePath());    }        if (! _classDir.equals(_sourceDir)) {      if (cb.length() > 0)        cb.append(CauchoSystem.getPathSeparatorChar());      cb.append(_sourceDir.getNativePath());    }    if (cb.length() > 0)      cb.append(CauchoSystem.getPathSeparatorChar());    cb.append(tail);    return cb.close();  }  /*  @Override  protected void buildSourcePath(StringBuilder head)  {    buildClassPath(head);  }  */  public String toString()  {    if (_classDir == null)      return "CompilingLoader[]";    else if (_classDir.equals(_sourceDir))      return "CompilingLoader[src:" + _sourceDir + "]";    else      return ("CompilingLoader[src:" + _sourceDir +              ",class:" + _classDir + "]");  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?