コード例 #1
0
  /**
   * Compile the specified Groovy source files, applying any {@link CompilerAutoConfiguration}s. All
   * classes defined in the files will be returned from this method.
   *
   * @param file the file to compile
   * @return compiled classes
   * @throws CompilationFailedException
   * @throws IOException
   */
  public Class<?>[] compile(File... file) throws CompilationFailedException, IOException {

    this.loader.clearCache();
    List<Class<?>> classes = new ArrayList<Class<?>>();

    CompilerConfiguration configuration = this.loader.getConfiguration();

    CompilationUnit compilationUnit = new CompilationUnit(configuration, null, this.loader);
    SourceUnit sourceUnit =
        new SourceUnit(file[0], configuration, this.loader, compilationUnit.getErrorCollector());
    ClassCollector collector = this.loader.createCollector(compilationUnit, sourceUnit);
    compilationUnit.setClassgenCallback(collector);

    compilationUnit.addSources(file);

    addAstTransformations(compilationUnit);

    compilationUnit.compile(Phases.CLASS_GENERATION);
    for (Object loadedClass : collector.getLoadedClasses()) {
      classes.add((Class<?>) loadedClass);
    }
    ClassNode mainClassNode = (ClassNode) compilationUnit.getAST().getClasses().get(0);
    Class<?> mainClass = null;
    for (Class<?> loadedClass : classes) {
      if (mainClassNode.getName().equals(loadedClass.getName())) {
        mainClass = loadedClass;
      }
    }
    if (mainClass != null) {
      classes.remove(mainClass);
      classes.add(0, mainClass);
    }

    return classes.toArray(new Class<?>[classes.size()]);
  }
コード例 #2
0
  /**
   * Loads the given class node returning the implementation Class.
   *
   * <p>WARNING: this compilation is not synchronized
   *
   * @param classNode
   * @return a class
   */
  public Class defineClass(ClassNode classNode, String file, String newCodeBase) {
    CodeSource codeSource = null;
    try {
      codeSource =
          new CodeSource(new URL("file", "", newCodeBase), (java.security.cert.Certificate[]) null);
    } catch (MalformedURLException e) {
      // swallow
    }

    CompilationUnit unit = createCompilationUnit(config, codeSource);
    ClassCollector collector = createCollector(unit, classNode.getModule().getContext());
    try {
      unit.addClassNode(classNode);
      unit.setClassgenCallback(collector);
      unit.compile(Phases.CLASS_GENERATION);
      definePackage(collector.generatedClass.getName());
      return collector.generatedClass;
    } catch (CompilationFailedException e) {
      throw new RuntimeException(e);
    }
  }
コード例 #3
0
  private Class doParseClass(GroovyCodeSource codeSource) {
    validate(codeSource);
    Class answer; // Was neither already loaded nor compiling, so compile and add to cache.
    CompilationUnit unit = createCompilationUnit(config, codeSource.getCodeSource());
    if (recompile != null && recompile || recompile == null && config.getRecompileGroovySource()) {
      unit.addFirstPhaseOperation(
          TimestampAdder.INSTANCE, CompilePhase.CLASS_GENERATION.getPhaseNumber());
    }
    SourceUnit su = null;
    File file = codeSource.getFile();
    if (file != null) {
      su = unit.addSource(file);
    } else {
      URL url = codeSource.getURL();
      if (url != null) {
        su = unit.addSource(url);
      } else {
        su = unit.addSource(codeSource.getName(), codeSource.getScriptText());
      }
    }

    ClassCollector collector = createCollector(unit, su);
    unit.setClassgenCallback(collector);
    int goalPhase = Phases.CLASS_GENERATION;
    if (config != null && config.getTargetDirectory() != null) goalPhase = Phases.OUTPUT;
    unit.compile(goalPhase);

    answer = collector.generatedClass;
    String mainClass = su.getAST().getMainClassName();
    for (Object o : collector.getLoadedClasses()) {
      Class clazz = (Class) o;
      String clazzName = clazz.getName();
      definePackage(clazzName);
      setClassCacheEntry(clazz);
      if (clazzName.equals(mainClass)) answer = clazz;
    }
    return answer;
  }