예제 #1
0
파일: Groovyc.java 프로젝트: endSly/groovy
  protected GroovyClassLoader buildClassLoaderFor() {
    // GROOVY-5044
    if (!fork && !getIncludeantruntime()) {
      throw new IllegalArgumentException(
          "The includeAntRuntime=false option is not compatible with fork=false");
    }
    ClassLoader parent =
        getIncludeantruntime()
            ? getClass().getClassLoader()
            : new AntClassLoader(
                new RootLoader(EMPTY_URL_ARRAY, null), getProject(), getClasspath());
    if (parent instanceof AntClassLoader) {
      AntClassLoader antLoader = (AntClassLoader) parent;
      String[] pathElm = antLoader.getClasspath().split(File.pathSeparator);
      List<String> classpath = configuration.getClasspath();
      /*
       * Iterate over the classpath provided to groovyc, and add any missing path
       * entries to the AntClassLoader.  This is a workaround, since for some reason
       * 'directory' classpath entries were not added to the AntClassLoader' classpath.
       */
      for (String cpEntry : classpath) {
        boolean found = false;
        for (String path : pathElm) {
          if (cpEntry.equals(path)) {
            found = true;
            break;
          }
        }
        /*
         * fix for GROOVY-2284
         * seems like AntClassLoader doesn't check if the file
         * may not exist in the classpath yet
         */
        if (!found && new File(cpEntry).exists()) {
          try {
            antLoader.addPathElement(cpEntry);
          } catch (BuildException e) {
            log.warn("The classpath entry " + cpEntry + " is not a valid Java resource");
          }
        }
      }
    }

    GroovyClassLoader loader = new GroovyClassLoader(parent, configuration);
    if (!forceLookupUnnamedFiles) {
      // in normal case we don't need to do script lookups
      loader.setResourceLoader(
          new GroovyResourceLoader() {
            public URL loadGroovySource(String filename) throws MalformedURLException {
              return null;
            }
          });
    }
    return loader;
  }
예제 #2
0
 /**
  * creates a GroovyClassLoader.
  *
  * @param parent the parent class loader
  * @param config the compiler configuration
  * @param useConfigurationClasspath determines if the configurations classpath should be added
  */
 public GroovyClassLoader(
     ClassLoader parent, CompilerConfiguration config, boolean useConfigurationClasspath) {
   super(EMPTY_URL_ARRAY, parent);
   if (config == null) config = CompilerConfiguration.DEFAULT;
   this.config = config;
   if (useConfigurationClasspath) {
     for (String path : config.getClasspath()) {
       this.addClasspath(path);
     }
   }
 }
예제 #3
0
 /**
  * Copy constructor. Use this if you have a mostly correct configuration for your compilation but
  * you want to make a some changes programmatically. An important reason to prefer this approach
  * is that your code will most likely be forward compatible with future changes to this
  * configuration API.<br>
  * An example of this copy constructor at work:<br>
  *
  * <pre>
  *    // In all likelihood there is already a configuration in your code's context
  *    // for you to copy, but for the sake of this example we'll use the global default.
  *    CompilerConfiguration myConfiguration = new CompilerConfiguration(CompilerConfiguration.DEFAULT);
  *    myConfiguration.setDebug(true);
  * </pre>
  *
  * @param configuration The configuration to copy.
  */
 public CompilerConfiguration(CompilerConfiguration configuration) {
   setWarningLevel(configuration.getWarningLevel());
   setOutput(configuration.getOutput());
   setTargetDirectory(configuration.getTargetDirectory());
   setClasspathList(new LinkedList(configuration.getClasspath()));
   setVerbose(configuration.getVerbose());
   setDebug(configuration.getDebug());
   setTolerance(configuration.getTolerance());
   setScriptBaseClass(configuration.getScriptBaseClass());
   setRecompileGroovySource(configuration.getRecompileGroovySource());
   setMinimumRecompilationInterval(configuration.getMinimumRecompilationInterval());
   setTargetBytecode(configuration.getTargetBytecode());
   setDefaultScriptExtension(configuration.getDefaultScriptExtension());
   setSourceEncoding(configuration.getSourceEncoding());
   setOutput(configuration.getOutput());
   setTargetDirectory(configuration.getTargetDirectory());
   Map jointCompilationOptions = configuration.getJointCompilationOptions();
   if (jointCompilationOptions != null) {
     jointCompilationOptions = new HashMap(jointCompilationOptions);
   }
   setJointCompilationOptions(jointCompilationOptions);
   setPluginFactory(configuration.getPluginFactory());
 }