예제 #1
0
 private void definePackage(String className) {
   int i = className.lastIndexOf('.');
   if (i != -1) {
     String pkgName = className.substring(0, i);
     java.lang.Package pkg = getPackage(pkgName);
     if (pkg == null) {
       definePackage(pkgName, null, null, null, null, null, null, null);
     }
   }
 }
예제 #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;
  }