Example #1
0
  protected InputStream getCompilationUnitInputStream(String path) {
    Environment env = factory.getEnvironment();
    spoon.reflect.cu.CompilationUnit cu = factory.CompilationUnit().getMap().get(path);
    List<CtType<?>> toBePrinted = cu.getDeclaredTypes();

    PrettyPrinter printer = null;

    if (printer == null) {
      printer = new DefaultJavaPrettyPrinter(env);
    }
    printer.calculate(cu, toBePrinted);

    return new ByteArrayInputStream(printer.getResult().toString().getBytes());
  }
Example #2
0
  @SuppressWarnings("unchecked")
  void enter(CtElement e, ASTNode node) {
    stack.push(new ASTPair(e, node));
    if (!(e instanceof CtPackage)
        || (compilationUnitSpoon.getFile() != null
            && compilationUnitSpoon
                .getFile()
                .getName()
                .equals(DefaultJavaPrettyPrinter.JAVA_PACKAGE_DECLARATION))) {
      if (compilationunitdeclaration != null) {
        e.setPosition(this.jdtTreeBuilder.getPositionBuilder().buildPositionCtElement(e, node));
      }
    }

    ASTPair pair = stack.peek();
    CtElement current = pair.element;

    if (current instanceof CtExpression) {
      while (!casts.isEmpty()) {
        ((CtExpression<?>) current).addTypeCast(casts.remove(0));
      }
    }
    if (current instanceof CtStatement && !this.label.isEmpty()) {
      ((CtStatement) current).setLabel(this.label.pop());
    }

    try {
      if (e instanceof CtTypedElement
          && !(e instanceof CtConstructorCall)
          && node instanceof Expression) {
        if (((CtTypedElement<?>) e).getType() == null) {
          ((CtTypedElement<Object>) e)
              .setType(
                  this.jdtTreeBuilder
                      .getReferencesBuilder()
                      .getTypeReference(((Expression) node).resolvedType));
        }
      }
    } catch (UnsupportedOperationException ignore) {
      // For some element, we throw an UnsupportedOperationException when we call setType().
    }
  }
Example #3
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);
      }
    }
  }