Java Code Examples for org.codehaus.groovy.control.CompilerConfiguration#setTargetDirectory()

The following examples show how to use org.codehaus.groovy.control.CompilerConfiguration#setTargetDirectory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestGroovyCapabilityBindings.java    From arcusplatform with Apache License 2.0 7 votes vote down vote up
@Test
public void testPlatformDeviceConnectionImport() {
   CapabilityRegistry registry = ServiceLocator.getInstance(CapabilityRegistry.class);

   CompilerConfiguration config = new CompilerConfiguration();
   config.setTargetDirectory(new File(TMP_DIR));
   config.addCompilationCustomizers(new DriverCompilationCustomizer(registry));

   GroovyScriptEngine engine = new GroovyScriptEngine(new ClasspathResourceConnector());
   engine.setConfig(config);
   DriverBinding binding = new DriverBinding(
           ServiceLocator.getInstance(CapabilityRegistry.class),
           new GroovyDriverFactory(engine, registry, ImmutableSet.of(new ControlProtocolPlugin()))
   );
   GroovyDriverBuilder builder = binding.getBuilder();
   assertNotNull(builder.importCapability(GroovyDrivers.PLATFORM_DEVICE_CONNECTION_CAPABILITY));
}
 
Example 2
Source File: TestMethodUtilTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private ModuleNode createGroovyModuleNode() {
    final CompilerConfiguration conf = new CompilerConfiguration();
    conf.setTargetDirectory(compileDir);
    final CompilationUnit cu
            = new CompilationUnit(
                    new GroovyClassLoader(
                            getClass().getClassLoader()
                    )
            );
    cu.configure(conf);
    final SourceUnit sn = cu.addSource("AGroovyClass.groovy", groovyScript.toString());
    try {
        cu.compile();
    } catch (Exception e) {
        //this groovy compile bit didn't work when running tests
        //but did work when debugging them, so odd, but the AST is in
        //place either way, and is all we need for this
        this.log(e.getMessage());
    }
    final ModuleNode mn = sn.getAST();
    return mn;
}
 
Example 3
Source File: GroovyScriptEngineFactory.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
private GroovyScriptEngine create() {
  // custom the configuration of the compiler
  CompilerConfiguration cc = new CompilerConfiguration();
  cc.setTargetDirectory(new File(applicationDirectories.getTemporaryDirectory(), "groovy-classes"));
  cc.setSourceEncoding("UTF-8");
  cc.setScriptBaseClass(ScriptWithCleanup.class.getName());
  cc.addCompilationCustomizers(secureASTCustomizer());
  GroovyClassLoader gcl = new GroovyClassLoader(classLoader, cc);

  engine = new GroovyScriptEngine(gcl);

  // HACK: For testing
  log.info("Created engine: {}", engine);

  return engine;
}
 
Example 4
Source File: CompilerTest.java    From groovy with Apache License 2.0 5 votes vote down vote up
protected void setUp() throws Exception {
    File dir = new File("target/test-generated-classes");
    dir.mkdirs();

    CompilerConfiguration config = new CompilerConfiguration();
    config.setTargetDirectory(dir);
    config.setDebug(dumpClass);

    compiler = new Compiler(config);
}
 
Example 5
Source File: FileSystemCompilerTest.java    From groovy with Apache License 2.0 5 votes vote down vote up
protected void setUp() throws Exception {
    File dir = new File("target/test-generated-classes");
    dir.mkdirs();
    Map options = new HashMap();
    options.put("stubDir", dir);

    CompilerConfiguration configuration = new CompilerConfiguration();
    configuration.setTargetDirectory(dir);
    configuration.setVerbose(dumpClass);
    configuration.setJointCompilationOptions(options);

    compiler = new FileSystemCompiler(configuration);
}
 
Example 6
Source File: TestGroovyCapabilityBindings.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
@Test
public void testCompile() throws Exception {
   CompilerConfiguration config = new CompilerConfiguration();
   config.setTargetDirectory(new File(TMP_DIR));
   config.addCompilationCustomizers(new DriverCompilationCustomizer(ServiceLocator.getInstance(CapabilityRegistry.class)));
   org.codehaus.groovy.tools.Compiler compiler = new org.codehaus.groovy.tools.Compiler(config);
   compiler.compile(new File("src/test/resources/Metadata.capability"));
   ClassLoader loader = new ClassLoader() {

      @Override
      protected Class<?> findClass(String name) throws ClassNotFoundException {
         File f = new File(TMP_DIR + "/" + name.replaceAll("\\.", "/") + ".class");
         if(!f.exists()) {
            throw new ClassNotFoundException();
         }
         try (FileInputStream is = new FileInputStream(f)) {
            byte [] bytes = IOUtils.toByteArray(is);
            return defineClass(name, bytes, 0, bytes.length);
         }
         catch(Exception e) {
            throw new ClassNotFoundException("Unable to load " + name, e);
         }
      }

   };
   Class<?> metadataClass = loader.loadClass("Metadata");
   System.out.println(metadataClass);
   System.out.println("Superclass: " + metadataClass.getSuperclass());
   System.out.println("Interfaces: " + Arrays.asList(metadataClass.getInterfaces()));
   System.out.println("Methods: ");
   for(Method m: Arrays.asList(metadataClass.getMethods())) {
      System.out.println("\t" + m);
   }

   GroovyCapabilityBuilder builder =
         new GroovyCapabilityBuilder(ServiceLocator.getInstance(CapabilityRegistry.class))
            .withName("Metadata");
   CapabilityEnvironmentBinding binding = new CapabilityEnvironmentBinding(builder);
   Script s = (Script) metadataClass.getConstructor(Binding.class).newInstance(binding);
   s.setMetaClass(new CapabilityScriptMetaClass(s.getClass()));
   s.getMetaClass().initialize();
   s.setBinding(binding);
   s.run();
   System.out.println("Definition: " + builder.create().getCapabilityDefinition());
}
 
Example 7
Source File: TestGroovyDriverBindings.java    From arcusplatform with Apache License 2.0 4 votes vote down vote up
@Test
//   @Ignore
   public void testCompile() throws Exception {
      CapabilityRegistry registry = ServiceLocator.getInstance(CapabilityRegistry.class);

      CompilerConfiguration config = new CompilerConfiguration();
      config.setTargetDirectory(new File(TMP_DIR));
      config.addCompilationCustomizers(new DriverCompilationCustomizer(registry));
      org.codehaus.groovy.tools.Compiler compiler = new org.codehaus.groovy.tools.Compiler(config);
      compiler.compile(new File("src/test/resources/Metadata.driver"));
      ClassLoader loader = new ClassLoader() {

         @Override
         protected Class<?> findClass(String name) throws ClassNotFoundException {
            File f = new File(TMP_DIR + "/" + name.replaceAll("\\.", "/") + ".class");
            if(!f.exists()) {
               throw new ClassNotFoundException();
            }
            try (FileInputStream is = new FileInputStream(f)) {
               byte [] bytes = IOUtils.toByteArray(is);
               return defineClass(name, bytes, 0, bytes.length);
            }
            catch(Exception e) {
               throw new ClassNotFoundException("Unable to load " + name, e);
            }
         }

      };
      Class<?> metadataClass = loader.loadClass("Metadata");
      System.out.println(metadataClass);
      System.out.println("Superclass: " + metadataClass.getSuperclass());
      System.out.println("Interfaces: " + Arrays.asList(metadataClass.getInterfaces()));
      System.out.println("Methods: ");
      for(Method m: Arrays.asList(metadataClass.getMethods())) {
         System.out.println("\t" + m);
      }

      GroovyScriptEngine engine = new GroovyScriptEngine(new ClasspathResourceConnector());
      engine.setConfig(config);
      DriverBinding binding = new DriverBinding(
            ServiceLocator.getInstance(CapabilityRegistry.class),
            new GroovyDriverFactory(engine, registry, ImmutableSet.of(new ControlProtocolPlugin()))
      );
      Script s = (Script) metadataClass.getConstructor(Binding.class).newInstance(binding);
      s.setMetaClass(new DriverScriptMetaClass(s.getClass()));
      s.setBinding(binding);
      s.run();
      System.out.println("Definition: " + binding.getBuilder().createDefinition());
   }
 
Example 8
Source File: FileSystemCompiler.java    From groovy with Apache License 2.0 4 votes vote down vote up
public CompilerConfiguration toCompilerConfiguration() throws IOException {
    // Setup the configuration data
    CompilerConfiguration configuration = new CompilerConfiguration();

    if (classpath != null) {
        configuration.setClasspath(classpath);
    }

    if (targetDir != null && targetDir.getName().length() > 0) {
        configuration.setTargetDirectory(targetDir);
    }

    configuration.setParameters(parameterMetadata);
    configuration.setPreviewFeatures(previewFeatures);
    configuration.setSourceEncoding(encoding);
    configuration.setScriptBaseClass(scriptBaseClass);

    // joint compilation parameters
    if (jointCompilation) {
        Map<String, Object> compilerOptions = new HashMap<>();
        compilerOptions.put("flags", javacFlags());
        compilerOptions.put("namedValues", javacNamedValues());
        configuration.setJointCompilationOptions(compilerOptions);
    }

    final List<String> transformations = new ArrayList<>();
    if (compileStatic) {
        transformations.add("ast(groovy.transform.CompileStatic)");
    }
    if (typeChecked) {
        transformations.add("ast(groovy.transform.TypeChecked)");
    }
    if (!transformations.isEmpty()) {
        processConfigScriptText(buildConfigScriptText(transformations), configuration);
    }

    String configScripts = System.getProperty("groovy.starter.configscripts", null);
    if (configScript != null || (configScripts != null && !configScripts.isEmpty())) {
        List<String> scripts = new ArrayList<>();
        if (configScript != null) {
            scripts.add(configScript);
        }
        if (configScripts != null) {
            scripts.addAll(StringGroovyMethods.tokenize(configScripts, ','));
        }
        processConfigScripts(scripts, configuration);
    }

    return configuration;
}