Ejemplo n.º 1
0
 protected void initInputClassLoader() {
   ClassLoader cl = Thread.currentThread().getContextClassLoader();
   if (buildOnlyOutdatedFiles && getBinaryOutputDirectory() != null) {
     CompilerClassLoader ccl = getCompilerClassLoader(cl);
     if (ccl == null) {
       try {
         Launcher.LOGGER.debug(
             "setting classloader for " + getBinaryOutputDirectory().toURI().toURL());
         Thread.currentThread()
             .setContextClassLoader(
                 new CompilerClassLoader(
                     new URL[] {getBinaryOutputDirectory().toURI().toURL()},
                     factory.getEnvironment().getInputClassLoader()));
       } catch (Exception e) {
         Launcher.LOGGER.error(e.getMessage(), e);
       }
     }
   } else {
     if (!hasClassLoader(
         Thread.currentThread().getContextClassLoader(),
         factory.getEnvironment().getInputClassLoader())) {
       Thread.currentThread()
           .setContextClassLoader(factory.getEnvironment().getInputClassLoader());
     }
   }
 }
Ejemplo n.º 2
0
 protected void replace(CtElement element) {
   try {
     replaceIn(this, element, getParent());
   } catch (CtUncomparableException e1) {
     // do nothing
   } catch (Exception e1) {
     Launcher.LOGGER.error(e1.getMessage(), e1);
   }
 }
Ejemplo n.º 3
0
 // this function is used to hack the JDT compiler...
 protected File createTmpJavaFile(File folder) {
   File f = new File(folder, "Tmp.java");
   if (f.exists()) {
     return f;
   }
   try {
     FileUtils.writeStringToFile(f, "class Tmp {}");
     f.deleteOnExit();
   } catch (Exception e) {
     Launcher.LOGGER.error(e.getMessage(), e);
   }
   return f;
 }
Ejemplo n.º 4
0
  /** Creates the {@link SpoonFolder} corresponding to the given file. */
  public static SpoonFolder createFolder(File f) throws FileNotFoundException {
    if (!f.exists()) {
      throw new FileNotFoundException(f.toString() + " does not exist");
    }
    try {
      if (f.isDirectory()) {
        return new FileSystemFolder(f);
      }
      if (isArchive(f)) {
        return new ZipFolder(f);
      }
    } catch (IOException e) {
      Launcher.LOGGER.error(e.getMessage(), e);
    }

    return null;
  }
Ejemplo n.º 5
0
  protected void generateProcessedSourceFilesUsingCUs() {

    factory.getEnvironment().debugMessage("Generating source using compilation units...");
    // Check output directory
    if (outputDirectory == null) {
      throw new RuntimeException("You should set output directory before generating source files");
    }
    // Create spooned directory
    if (outputDirectory.isFile()) {
      throw new RuntimeException("Output must be a directory");
    }
    if (!outputDirectory.exists()) {
      if (!outputDirectory.mkdirs()) {
        throw new RuntimeException("Error creating output directory");
      }
    }

    try {
      outputDirectory = outputDirectory.getCanonicalFile();
    } catch (IOException e1) {
      throw new SpoonException(e1);
    }

    factory.getEnvironment().debugMessage("Generating source files to: " + outputDirectory);

    List<File> printedFiles = new ArrayList<File>();
    for (spoon.reflect.cu.CompilationUnit cu : factory.CompilationUnit().getMap().values()) {

      factory
          .getEnvironment()
          .debugMessage("Generating source for compilation unit: " + cu.getFile());

      CtType<?> element = cu.getMainType();

      CtPackage pack = element.getPackage();

      // create package directory
      File packageDir;
      if (pack.getQualifiedName().equals(CtPackage.TOP_LEVEL_PACKAGE_NAME)) {
        packageDir = new File(outputDirectory.getAbsolutePath());
      } else {
        // Create current package directory
        packageDir =
            new File(
                outputDirectory.getAbsolutePath()
                    + File.separatorChar
                    + pack.getQualifiedName().replace('.', File.separatorChar));
      }
      if (!packageDir.exists()) {
        if (!packageDir.mkdirs()) {
          throw new RuntimeException("Error creating output directory");
        }
      }

      // print type
      try {
        File file =
            new File(
                packageDir.getAbsolutePath()
                    + File.separatorChar
                    + element.getSimpleName()
                    + DefaultJavaPrettyPrinter.JAVA_FILE_EXTENSION);
        file.createNewFile();

        // the path must be given relatively to to the working directory
        InputStream is = getCompilationUnitInputStream(cu.getFile().getPath());

        IOUtils.copy(is, new FileOutputStream(file));

        if (!printedFiles.contains(file)) {
          printedFiles.add(file);
        }

      } catch (Exception e) {
        Launcher.LOGGER.error(e.getMessage(), e);
      }
    }
  }