private void processPackage(CtPackage package_) {
   Set<CtPackage> subPackages = package_.getPackages();
   if (subPackages != null) {
     for (CtPackage subPackage : subPackages) {
       processPackage(subPackage);
     }
   }
   for (CtType<?> type : package_.getTypes()) {
     process(type);
   }
 }
Пример #2
0
  /**
   * Gets a created package.
   *
   * @param qualifiedName the package to search
   * @return a found package or null
   */
  public CtPackage get(String qualifiedName) {
    if (qualifiedName.contains(CtType.INNERTTYPE_SEPARATOR)) {
      throw new RuntimeException("Invalid package name " + qualifiedName);
    }
    StringTokenizer token = new StringTokenizer(qualifiedName, CtPackage.PACKAGE_SEPARATOR);
    CtPackage current = rootPackage;
    if (token.hasMoreElements()) {
      current = current.getPackage(token.nextToken());
      while (token.hasMoreElements() && current != null) {
        current = current.getPackage(token.nextToken());
      }
    }

    return current;
  }
Пример #3
0
 @Test
 public void testBasicAPIUsage() throws Exception {
   // this test shows a basic usage of the Launcher API without command line
   // and asserts there is no exception
   SpoonAPI spoon = new Launcher();
   spoon.addInputResource("src/test/resources/spoon/test/api");
   spoon.setSourceOutputDirectory("target/spooned");
   spoon.run();
   Factory factory = spoon.getFactory();
   for (CtPackage p : factory.Package().getAll()) {
     spoon.getEnvironment().debugMessage("package: " + p.getQualifiedName());
   }
   for (CtType<?> s : factory.Class().getAll()) {
     spoon.getEnvironment().debugMessage("class: " + s.getQualifiedName());
   }
 }
Пример #4
0
 private List<CtPackage> getSubPackageList(CtPackage pack) {
   List<CtPackage> packs = new ArrayList<CtPackage>();
   packs.add(pack);
   for (CtPackage p : pack.getPackages()) {
     packs.addAll(getSubPackageList(p));
   }
   return packs;
 }
Пример #5
0
  /**
   * Dans cette méthode on vérifie si la classe/méthode de l'élément analysé correspondent et si la
   * vérification particulière correspond.On retourne le résultat sou forme de boolean
   *
   * @param candidate l'élément analysé
   * @return résultat de la vérification
   */
  @Override
  public boolean isToBeProcessed(CtElement candidate) {
    CtPackage p = candidate.getParent(CtPackage.class);
    CtClass c = candidate.getParent(CtClass.class);
    CtMethod m = candidate.getParent(CtMethod.class);
    try {
      if (pecularVerify(candidate)) {
        if (verifyPackage(p.getSimpleName())) {
          if (verifyClass(c.getSimpleName())) {
            if (verifyMethod(m.getSignature())) {
              return verifyNbApplication();
            }
          }
        }
      }

    } catch (NullPointerException e) {
      return false;
    }
    return false;
  }
Пример #6
0
  /**
   * Gets or creates a package.
   *
   * @param qualifiedName the full name of the package
   */
  public CtPackage getOrCreate(String qualifiedName) {
    StringTokenizer token = new StringTokenizer(qualifiedName, CtPackage.PACKAGE_SEPARATOR);
    CtPackage last = rootPackage;

    while (token.hasMoreElements()) {
      String name = token.nextToken();
      CtPackage next = last.getPackage(name);
      if (next == null) {
        next = factory.Core().createPackage();
        next.setSimpleName(name);
        next.setParent(last);
        last.getPackages().add(next);
      }
      last = next;
    }

    return last;
  }
Пример #7
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);
      }
    }
  }
Пример #8
0
 /** Creates a reference to an existing package. */
 public CtPackageReference createReference(CtPackage pack) {
   if (pack == null) {
     throw new IllegalArgumentException();
   }
   return createReference(pack.getQualifiedName());
 }
Пример #9
0
 /**
  * Creates a new package sub-factory.
  *
  * @param factory the parent factory
  */
 public PackageFactory(Factory factory) {
   super(factory);
   rootPackage = new CtRootPackage();
   rootPackage.setFactory(factory);
 }
Пример #10
0
 /**
  * Creates a new package (see also {@link #getOrCreate(String)}).
  *
  * @param parent the parent package (can be null)
  * @param simpleName the package's simple name (no dots)
  * @return the newly created package
  */
 public CtPackage create(CtPackage parent, String simpleName) {
   return getOrCreate(parent.toString() + CtPackage.PACKAGE_SEPARATOR + simpleName);
 }