SHARE
TWEET

Untitled

a guest Mar 29th, 2014 44 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package so_22729226;
  2.  
  3. import groovy.lang.GroovyClassLoader;
  4. import groovy.lang.GroovyObject;
  5.  
  6. import java.security.CodeSource;
  7.  
  8. import org.codehaus.groovy.control.CompilationFailedException;
  9. import org.codehaus.groovy.control.CompilationUnit;
  10. import org.codehaus.groovy.control.CompilationUnit.SourceUnitOperation;
  11. import org.codehaus.groovy.control.CompilerConfiguration;
  12. import org.codehaus.groovy.control.Phases;
  13. import org.codehaus.groovy.control.SourceUnit;
  14.  
  15. public class GroovyScriptTestRun {
  16.        
  17.  
  18.         public static class DefaultImportClassLoader extends GroovyClassLoader {
  19.                 public DefaultImportClassLoader(ClassLoader cl){
  20.                         super(cl);
  21.                 }
  22.        
  23.                 public CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource codeSource) {
  24.                         CompilationUnit cu = super.createCompilationUnit(config, codeSource);
  25.                         cu.addPhaseOperation(new SourceUnitOperation() {
  26.                                 public void call(SourceUnit source) throws CompilationFailedException {
  27.                                         //source.getAST().addImport("Test",ClassHelper.make("org.junit.Test"));
  28.                                         source.getAST().addImportPackage("org.junit.");
  29.                                 }}, Phases.CONVERSION);
  30.        
  31.                         return cu;
  32.                 }
  33.                
  34.         }
  35.  
  36.         public static void main(String[] args) throws InstantiationException, IllegalAccessException{
  37. //              GroovyClassLoader loader = new GroovyClassLoader();
  38.                 GroovyClassLoader loader = new GroovyScriptTestRun.DefaultImportClassLoader( GroovyScriptTestRun.class.getClassLoader());
  39.         Class groovyClass = loader.parseClass("class so_22729226_Test { def run(){ print Test.class } }");
  40.         System.out.println("Script Class Name ->"+groovyClass.getSimpleName());        
  41.         GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
  42.         //must print "interface org.junit.Test"
  43.         groovyObject.invokeMethod("run", null);
  44.         }
  45.  
  46. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top