Exemplo n.º 1
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);
    }
  }
Exemplo n.º 2
0
    protected Class createClass(byte[] code, ClassNode classNode) {
      BytecodeProcessor bytecodePostprocessor = unit.getConfiguration().getBytecodePostprocessor();
      byte[] fcode = code;
      if (bytecodePostprocessor != null) {
        fcode = bytecodePostprocessor.processBytecode(classNode.getName(), fcode);
      }
      GroovyClassLoader cl = getDefiningClassLoader();
      Class theClass =
          cl.defineClass(
              classNode.getName(), fcode, 0, fcode.length, unit.getAST().getCodeSource());
      this.loadedClasses.add(theClass);

      if (generatedClass == null) {
        ModuleNode mn = classNode.getModule();
        SourceUnit msu = null;
        if (mn != null) msu = mn.getContext();
        ClassNode main = null;
        if (mn != null) main = (ClassNode) mn.getClasses().get(0);
        if (msu == su && main == classNode) generatedClass = theClass;
      }

      return theClass;
    }
Exemplo n.º 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;
  }