示例#1
0
  public boolean build() {
    if (factory == null) {
      throw new SpoonException("Factory not initialized");
    }
    if (build) {
      throw new SpoonException("Model already built");
    }
    build = true;

    boolean srcSuccess, templateSuccess;
    factory.getEnvironment().debugMessage("building sources: " + sources.getAllJavaFiles());
    long t = System.currentTimeMillis();
    javaCompliance = factory.getEnvironment().getComplianceLevel();
    srcSuccess = buildSources();

    reportProblems(factory.getEnvironment());

    factory.getEnvironment().debugMessage("built in " + (System.currentTimeMillis() - t) + " ms");
    factory.getEnvironment().debugMessage("building templates: " + templates.getAllJavaFiles());
    t = System.currentTimeMillis();
    templateSuccess = buildTemplates();
    // factory.Template().parseTypes();
    factory.getEnvironment().debugMessage("built in " + (System.currentTimeMillis() - t) + " ms");
    return srcSuccess && templateSuccess;
  }
示例#2
0
 public Set<File> getTemplateSources() {
   Set<File> files = new HashSet<File>();
   for (SpoonFolder file : getTemplates().getSubFolders()) {
     files.add(new File(file.getPath()));
   }
   return files;
 }
示例#3
0
  @Override
  public boolean compileInputSources() {
    initInputClassLoader();
    factory.getEnvironment().debugMessage("compiling input sources: " + sources.getAllJavaFiles());
    long t = System.currentTimeMillis();
    javaCompliance = factory.getEnvironment().getComplianceLevel();

    Main batchCompiler = createBatchCompiler(false);
    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("-proc:none");
    if (getBinaryOutputDirectory() != null) {
      args.add("-d");
      args.add(getBinaryOutputDirectory().getAbsolutePath());
    } else {
      args.add("-d");
      args.add("none");
    }

    String finalClassPath = null;
    if (getSourceClasspath() != null) {
      finalClassPath = computeJdtClassPath();
    } 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) {
            finalClassPath = classpath;
          }
        }
      }
    }

    args.add("-cp");
    args.add(finalClassPath);

    // Set<String> paths = new HashSet<String>();
    // for (SpoonFile file : sources.getAllJavaFiles()) {
    // paths.add(file.getParent().getPath());
    // }
    // args.addAll(paths);

    args.addAll(toStringList(sources.getAllJavaFiles()));

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

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

    factory
        .getEnvironment()
        .debugMessage("compiled in " + (System.currentTimeMillis() - t) + " ms");
    return probs.size() == 0;
  }
示例#4
0
  @Override
  public boolean compile() {
    initInputClassLoader();
    factory
        .getEnvironment()
        .debugMessage("compiling sources: " + factory.CompilationUnit().getMap().keySet());
    long t = System.currentTimeMillis();
    javaCompliance = factory.getEnvironment().getComplianceLevel();

    JDTBatchCompiler batchCompiler = createBatchCompiler(true);
    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("-proc:none");
    if (getBinaryOutputDirectory() != null) {
      args.add("-d");
      args.add(getBinaryOutputDirectory().getAbsolutePath());
    } else {
      args.add("-d");
      args.add("none");
    }

    // args.add("-d");
    // args.add(getDestinationDirectory().toString());

    String finalClassPath = null;
    if (getSourceClasspath() != null) {
      finalClassPath = computeJdtClassPath();
    } 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) {
            finalClassPath = classpath;
          }
        }
      }
    }

    args.add("-cp");
    args.add(finalClassPath);

    if (buildOnlyOutdatedFiles) {

      // ignore the files that are not outdated
      if (outputDirectory.exists()) {
        @SuppressWarnings("unchecked")
        Collection<File> outputFiles =
            FileUtils.listFiles(outputDirectory, new String[] {"java"}, true);
        int offset = outputDirectory.getAbsolutePath().length() + 1;
        Collection<String> relativeOutputPaths = new ArrayList<String>();
        for (File f : outputFiles) {
          relativeOutputPaths.add(f.getAbsolutePath().substring(offset));
        }
        for (SpoonFile sf : sources.getAllJavaFiles()) {
          if (factory.CompilationUnit().getMap().containsKey(sf.getPath())) {
            continue;
          }
          File source = sf.toFile();
          for (String out : relativeOutputPaths) {
            if (source.getAbsolutePath().endsWith(out)) {
              if (source.lastModified() <= new File(outputDirectory, out).lastModified()) {
                batchCompiler.ignoreFile(new File(outputDirectory, out).getAbsolutePath());
              }
            }
          }
        }
      }

      args.add(getBinaryOutputDirectory().getAbsolutePath());

    } else {
      args.addAll(toStringList(sources.getAllJavaFiles()));
    }

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

    // batchCompiler.batchCompiler.useSingleThread = true;

    System.setProperty("jdt.compiler.useSingleThread", "true");

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

    reportProblems(factory.getEnvironment());

    factory
        .getEnvironment()
        .debugMessage("compiled in " + (System.currentTimeMillis() - t) + " ms");
    return probs.size() == 0;
  }
示例#5
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;
  }