Пример #1
0
 private void validate(GroovyCodeSource codeSource) {
   if (codeSource.getFile() == null) {
     if (codeSource.getScriptText() == null) {
       throw new IllegalArgumentException("Script text to compile cannot be null!");
     }
   }
 }
Пример #2
0
 /**
  * Parses the given code source into a Java class. If there is a class file for the given code
  * source, then no parsing is done, instead the cached class is returned.
  *
  * @param shouldCacheSource if true then the generated class will be stored in the source cache
  * @return the main class defined in the given script
  */
 public Class parseClass(GroovyCodeSource codeSource, boolean shouldCacheSource)
     throws CompilationFailedException {
   synchronized (sourceCache) {
     Class answer = sourceCache.get(codeSource.getName());
     if (answer != null) return answer;
     answer = doParseClass(codeSource);
     if (shouldCacheSource) sourceCache.put(codeSource.getName(), answer);
     return answer;
   }
 }
Пример #3
0
 /**
  * Parses the given text into a Java class capable of being run
  *
  * @param text the text of the script/class to parse
  * @param fileName the file name to use as the name of the class
  * @return the main class defined in the given script
  */
 public Class parseClass(final String text, final String fileName)
     throws CompilationFailedException {
   GroovyCodeSource gcs =
       AccessController.doPrivileged(
           new PrivilegedAction<GroovyCodeSource>() {
             public GroovyCodeSource run() {
               return new GroovyCodeSource(text, fileName, "/groovy/script");
             }
           });
   gcs.setCachable(false);
   return parseClass(gcs);
 }
Пример #4
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;
  }
Пример #5
0
 public Class parseClass(GroovyCodeSource codeSource) throws CompilationFailedException {
   return parseClass(codeSource, codeSource.isCachable());
 }