예제 #1
0
  protected boolean buildTemplates() {
    if (templates.getAllJavaFiles().isEmpty()) {
      return true;
    }
    JDTBatchCompiler batchCompiler = createBatchCompiler();
    List<String> args = new ArrayList<String>();
    args.add("-1." + javaCompliance);
    if (encoding != null) {
      args.add("-encoding");
      args.add(encoding);
    }
    args.add("-preserveAllLocals");
    args.add("-enableJavadoc");
    args.add("-noExit");
    // args.add("-verbose");
    // args.add("-d");
    // args.add("none");
    // args.add("-g");
    // args.add("-nowarn");

    File f = null;

    if (this.templateClasspath != null && this.templateClasspath.length > 0) {
      args.add("-cp");
      args.add(this.computeTemplateClasspath());

      // Set<String> paths = new HashSet<String>();
      // String sourcePaths = "";
      // for (SpoonFolder file : templates.getSubFolders()) {
      // if (file.isArchive()) {
      // sourcePaths += file.getPath() + File.pathSeparator;
      // }
      // }
      // for (SpoonFile file : files) {
      // if (!paths.contains(file.getFileSystemParent().getPath())) {
      // sourcePaths += file.getParent().getPath()
      // + File.pathSeparator;
      // }
      // paths.add(file.getPath());
      // }
      // args.add("-sourcepath");
      // args.add(sourcePaths.substring(0, sourcePaths.length() - 1));
      // args.addAll(paths);
      // args.add(".");
      for (SpoonFolder file : templates.getSubFolders()) {
        if (file.isArchive()) {
          // JDT bug HACK
          f = createTmpJavaFile(file.getFileSystemParent());
        }
      }
      args.addAll(toStringList(templates.getAllJavaFiles()));
    } else {
      // when no class path is defined, we are probably in test and we try
      // to get as much source as we can compiled
      args.add(".");
    }

    getFactory().getEnvironment().debugMessage("template build args: " + args);
    // printUsage();
    // System.out.println("=>" + args);
    batchCompiler.configure(args.toArray(new String[0]));
    CompilationUnitDeclaration[] units = batchCompiler.getUnits(templates.getAllJavaFiles());

    if (f != null && f.exists()) {
      f.delete();
    }

    // here we build the model in the template factory
    JDTTreeBuilder builder = new JDTTreeBuilder(factory);
    for (CompilationUnitDeclaration unit : units) {
      unit.traverse(builder, unit.scope);
    }

    return probs.size() == 0;
  }
예제 #2
0
  protected boolean buildSources() {
    if (sources.getAllJavaFiles().isEmpty()) {
      return true;
    }
    initInputClassLoader();
    // long t=System.currentTimeMillis();
    // Build input
    JDTBatchCompiler batchCompiler = createBatchCompiler();
    List<String> args = new ArrayList<String>();
    args.add("-1." + javaCompliance);
    if (encoding != null) {
      args.add("-encoding");
      args.add(encoding);
    }
    args.add("-preserveAllLocals");
    args.add("-enableJavadoc");
    args.add("-noExit");
    // args.add("-d");
    // args.add("none");

    if (getSourceClasspath() != null) {
      addClasspathToJDTArgs(args);
    } else {
      ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
      if (currentClassLoader instanceof URLClassLoader) {
        URL[] urls = ((URLClassLoader) currentClassLoader).getURLs();
        if (urls != null && urls.length > 0) {
          String classpath = ".";
          for (URL url : urls) {
            classpath += File.pathSeparator + url.getFile();
          }
          if (classpath != null) {
            args.add("-cp");
            args.add(classpath);
          }
        }
      }
    }
    // args.add("-nowarn");
    // Set<String> paths = new HashSet<String>();
    // for (SpoonFile file : files) {
    // // We can not use file.getPath() because of in-memory code or files
    // // within archives
    // paths.add(file.getFileSystemParent().getPath());
    // }
    args.addAll(toStringList(sources.getAllJavaFiles()));

    getFactory().getEnvironment().debugMessage("build args: " + args);

    batchCompiler.configure(args.toArray(new String[0]));

    List<SpoonFile> filesToBuild = sources.getAllJavaFiles();
    if (buildOnlyOutdatedFiles) {
      if (outputDirectory.exists()) {
        @SuppressWarnings("unchecked")
        Collection<File> outputFiles =
            FileUtils.listFiles(outputDirectory, new String[] {"java"}, true);
        keepOutdatedFiles(filesToBuild, outputFiles);
      } else {
        keepOutdatedFiles(filesToBuild, new ArrayList<File>());
      }
    }
    CompilationUnitDeclaration[] units = batchCompiler.getUnits(filesToBuild);

    // here we build the model
    JDTTreeBuilder builder = new JDTTreeBuilder(factory);
    for (CompilationUnitDeclaration unit : units) {
      unit.traverse(builder, unit.scope);
    }

    return probs.size() == 0;
  }