Skip to content

[Bug]: Imports are removed #5086

Open
@ghost

Description

Describe the bug

Imports are removed when using Sniper Printer and autoImports set to true.

Source code you are trying to analyze/transform

https://github.com/DaveJarvis/nts

Source code for your Spoon processing

import spoon.Launcher;
import spoon.refactoring.Refactoring;
import spoon.support.sniper.SniperJavaPrettyPrinter;

import java.io.File;
import java.util.regex.Pattern;

public class App {
  public static void main( final String[] args ) {
    final var outputDir = new File("/tmp/nts-new");
    final var spoon = new Launcher();

    final var env = spoon.getEnvironment();
    env.setPrettyPrinterCreator(
      () -> new SniperJavaPrettyPrinter( env )
    );
    env.setAutoImports( true );

    spoon.setSourceOutputDirectory( outputDir );

    spoon.addInputResource( "/tmp/nts/app/src/main/java/nts/app" );
    spoon.addInputResource( "/tmp/nts/dvi/src/main/java/nts/dvi" );
    spoon.addInputResource( "/tmp/nts/png/src/main/java/nts/png" );
    spoon.addInputResource( "/tmp/nts/svg/src/main/java/nts/svg" );
    spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/align" );
    spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/base" );
    spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/builder" );
    spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/command" );
    spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/hyph" );
    spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/io" );
    spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/math" );
    spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/noad" );
    spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/node" );
    spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/render" );
    spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/tex" );
    spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/tfm" );
    spoon.addInputResource( "/tmp/nts/tex/src/main/java/nts/typo" );
    spoon.addInputResource( "/tmp/nts/tex/src/test/java/nts/io" );

    final var model = spoon.buildModel();
    final var p = Pattern.compile( "^(?!Kt).*" );

    for( final var clazz : model.getAllTypes() ) {
      final var name = clazz.getSimpleName();
      final var m = p.matcher( name );

      if( m.matches() ) {
        final var newName = "Kt" + name;

        System.out.println( "rename " + name + " to " + newName );
        Refactoring.changeTypeName( clazz, newName );
      }
    }

    if( outputDir.exists() || outputDir.mkdirs() ) {
      spoon.prettyprint();
    }
  }
}

Actual output

package nts.tfm;/**
 * |IndexMultimap| can store and retrieve |int| values associated to particular |int| key. There can
 * be more values associated to the same key. This class can be replaced by any generic associative
 * container which provides one to many mapping.
 */
public class KtIndexMultimap {

  /** The internal representation of (key, value) pair. */
  protected static class Pair {

    /** The key */
    int key;

    /** The value */
    int val;

    /**
     * Makes new |Pair| with given key and value.
     *
     * @param k the key
     * @param v the value
     */
    Pair(int k, int v) {
      key = k;
      val = v;
    }
  }

  /*
   * The (key, value) pairs are kept in |Vector| sorted acording the key.
   */

  /** Internal storage of (key, value) pairs */
  private Vector<Pair> data = new Vector<Pair>();

  /**
   * The number of (key, value) pairs kept.
   *
   * @return the number of stored pairs.
   */
  protected final int size() {
    return data.size();
  }

  /**
   * (key, value) pair at given position.
   *
   * @param i the position of pair to be examined.
   * @return the pair at given position.
   */
  protected final Pair at(int i) {
    return data.elementAt(i);
  }

  /**
   * Insert a (key, value) pair at the given position.
   *
   * @param i the pair to be inserted.
   * @param i the position to be inserted to.
   */
  protected final void insert(Pair p, int i) {
    data.insertElementAt(p, i);
  }

  /*
   * Searching is implemented by binary search algorithm. If any
   * (key, value) pair withe given key is present in the sorted sequence, it
   * returns the position of one of such pairs. If not it returns the
   * position where a new pair with given key should be inserted to keep the
   * the sequence sorted.
   */

  /**
   * Gives the position where a (key, value) pair with given key is stored or where it should be
   * stored if there is no such pair.
   *
   * @param key the key searched for.
   * @return the position.
   */
  protected final int search(int key) {
    int beg = 0;
    int end = size();
    while (beg < end) {
      int med = (beg + end) / 2;
      Pair p = at(med);
      if (key < p.key) end = med;
      else if (key > p.key) beg = med + 1;
      else return med;
    }
    return beg;
  }

  /*
   * When adding a new (key, value) pair the position of some pair with
   * given key is found first, then it is inserted after all such pairs
   * (or to founded position if there is none such pair).
   */

  /**
   * Adds a new (key, value) pair.
   *
   * @param key the key of the new pair.
   * @param val the value of the new pair.
   */
  public void add(int key, int val) {
    synchronized (data) {
      int pos = search(key);
      while (pos < size() && at(pos).key == key) pos++;
      insert(new Pair(key, val), pos);
    }
  }

  /*
   * The class |Enum| is similar to interface |Enumeration| in the sense
   * that it provides methods for geting next value in a sequnce and for
   * testing the availability of the next value. But instead of |Object|s
   * it works with |ints|.
   */

  /** Class |Enum| provides the sequence of all values associated to particular key. */
  public class Enum {

    /** the current position in the sequence of pairs */
    private int pos;

    /** the key for which the values are required */
    private final int key;

    /**
     * Makes new |Enum| for given key.
     *
     * @param k the key for which the values are required.
     */
    /*
     * The constructor is private so only the enclosing class can
     * instantiate it.
     */
    private Enum(int k) {
      synchronized (data) {
        key = k;
        pos = search(key);
        while (pos > 0 && at(pos - 1).key == key) pos--;
      }
    }

    /**
     * Tests if there is another associated value.
     *
     * @return |true| if next value is available, |false| otherwise.
     */
    public final boolean hasMore() {
      return pos < size() && at(pos).key == key;
    }

    /**
     * Gives the next value from the sequence of associated values.
     *
     * @return the next value.
     */
    public final int next() {
      return at(pos++).val;
    }
  }

  /**
   * Gives the sequence of all keys associated to the given key.
   *
   * @param key the given key.
   * @return the object representing the sequence of associated values.
   */
  public Enum forKey(int key) {
    return new Enum(key);
  }
}

Expected output

// Copyright 2001 by
// DANTE e.V. and any individual authors listed elsewhere in this file.
//
// This file is part of the NTS system.
// ------------------------------------
//
// It may be distributed and/or modified under the
// conditions of the NTS Public License (NTSPL), either version 1.0
// of this license or (at your option) any later version.
// The latest version of this license is in
//    http://www.dante.de/projects/nts/ntspl.txt
// and version 1.0 or later is part of all distributions of NTS
// version 1.0-beta or later.
//
// The list of all files belonging to the NTS distribution is given in
// the file `manifest.txt'.
//
// Filename: nts/tfm/IndexMultimap.java
// $Id: IndexMultimap.java,v 1.1.1.1 1999/06/05 04:18:58 ksk Exp $
package nts.tfm;

import java.util.Vector;

/**
 * |IndexMultimap| can store and retrieve |int| values associated to particular |int| key. There can
 * be more values associated to the same key. This class can be replaced by any generic associative
 * container which provides one to many mapping.
 */
public class KtIndexMultimap {

  /** The internal representation of (key, value) pair. */
  protected static class Pair {

    /** The key */
    int key;

    /** The value */
    int val;

    /**
     * Makes new |Pair| with given key and value.
     *
     * @param k the key
     * @param v the value
     */
    Pair(int k, int v) {
      key = k;
      val = v;
    }
  }

  /*
   * The (key, value) pairs are kept in |Vector| sorted acording the key.
   */

  /** Internal storage of (key, value) pairs */
  private Vector<Pair> data = new Vector<Pair>();

  /**
   * The number of (key, value) pairs kept.
   *
   * @return the number of stored pairs.
   */
  protected final int size() {
    return data.size();
  }

  /**
   * (key, value) pair at given position.
   *
   * @param i the position of pair to be examined.
   * @return the pair at given position.
   */
  protected final Pair at(int i) {
    return data.elementAt(i);
  }

  /**
   * Insert a (key, value) pair at the given position.
   *
   * @param i the pair to be inserted.
   * @param i the position to be inserted to.
   */
  protected final void insert(Pair p, int i) {
    data.insertElementAt(p, i);
  }

  /*
   * Searching is implemented by binary search algorithm. If any
   * (key, value) pair withe given key is present in the sorted sequence, it
   * returns the position of one of such pairs. If not it returns the
   * position where a new pair with given key should be inserted to keep the
   * the sequence sorted.
   */

  /**
   * Gives the position where a (key, value) pair with given key is stored or where it should be
   * stored if there is no such pair.
   *
   * @param key the key searched for.
   * @return the position.
   */
  protected final int search(int key) {
    int beg = 0;
    int end = size();
    while (beg < end) {
      int med = (beg + end) / 2;
      Pair p = at(med);
      if (key < p.key) end = med;
      else if (key > p.key) beg = med + 1;
      else return med;
    }
    return beg;
  }

  /*
   * When adding a new (key, value) pair the position of some pair with
   * given key is found first, then it is inserted after all such pairs
   * (or to founded position if there is none such pair).
   */

  /**
   * Adds a new (key, value) pair.
   *
   * @param key the key of the new pair.
   * @param val the value of the new pair.
   */
  public void add(int key, int val) {
    synchronized (data) {
      int pos = search(key);
      while (pos < size() && at(pos).key == key) pos++;
      insert(new Pair(key, val), pos);
    }
  }

  /*
   * The class |Enum| is similar to interface |Enumeration| in the sense
   * that it provides methods for geting next value in a sequnce and for
   * testing the availability of the next value. But instead of |Object|s
   * it works with |ints|.
   */

  /** Class |Enum| provides the sequence of all values associated to particular key. */
  public class Enum {

    /** the current position in the sequence of pairs */
    private int pos;

    /** the key for which the values are required */
    private final int key;

    /**
     * Makes new |Enum| for given key.
     *
     * @param k the key for which the values are required.
     */
    /*
     * The constructor is private so only the enclosing class can
     * instantiate it.
     */
    private Enum(int k) {
      synchronized (data) {
        key = k;
        pos = search(key);
        while (pos > 0 && at(pos - 1).key == key) pos--;
      }
    }

    /**
     * Tests if there is another associated value.
     *
     * @return |true| if next value is available, |false| otherwise.
     */
    public final boolean hasMore() {
      return (pos < size() && at(pos).key == key);
    }

    /**
     * Gives the next value from the sequence of associated values.
     *
     * @return the next value.
     */
    public final int next() {
      return at(pos++).val;
    }
  }

  /**
   * Gives the sequence of all keys associated to the given key.
   *
   * @param key the given key.
   * @return the object representing the sequence of associated values.
   */
  public Enum forKey(int key) {
    return new Enum(key);
  }
}

Spoon Version

Latest

JVM Version

18

What operating system are you using?

Linux

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions