예제 #1
0
 /**
  * (Re)Compiles the given source. This method starts the compilation of a given source, if the
  * source has changed since the class was created. For this isSourceNewer is called.
  *
  * @param source the source pointer for the compilation
  * @param className the name of the class to be generated
  * @param oldClass a possible former class
  * @return the old class if the source wasn't new enough, the new class else
  * @throws CompilationFailedException if the compilation failed
  * @throws IOException if the source is not readable
  * @see #isSourceNewer(URL, Class)
  */
 protected Class recompile(URL source, String className, Class oldClass)
     throws CompilationFailedException, IOException {
   if (source != null) {
     // found a source, compile it if newer
     if ((oldClass != null && isSourceNewer(source, oldClass)) || (oldClass == null)) {
       synchronized (sourceCache) {
         String name = source.toExternalForm();
         sourceCache.remove(name);
         if (isFile(source)) {
           try {
             return parseClass(
                 new GroovyCodeSource(new File(source.toURI()), config.getSourceEncoding()));
           } catch (URISyntaxException e) {
             // do nothing and fall back to the other version
           }
         }
         return parseClass(source.openStream(), name);
       }
     }
   }
   return oldClass;
 }
예제 #2
0
 /**
  * Parses the given file into a Java class capable of being run
  *
  * @param file the file name to parse
  * @return the main class defined in the given script
  */
 public Class parseClass(File file) throws CompilationFailedException, IOException {
   return parseClass(new GroovyCodeSource(file, config.getSourceEncoding()));
 }