Home | History | Annotate | Download | only in find
      1 package annotator.find;
      2 
      3 import java.util.List;
      4 import java.util.LinkedHashSet;
      5 import java.util.Set;
      6 
      7 import type.Type;
      8 
      9 public class ConstructorInsertion extends TypedInsertion {
     10   private ReceiverInsertion receiverInsertion = null;
     11   private Set<Insertion> declarationInsertions = new LinkedHashSet<Insertion>();
     12 
     13   /**
     14    * Construct a ConstructorInsertion.
     15    * <p>
     16    * To insert the annotation and the constructor (for example,
     17    * {@code @Anno Type this}) the name should be set to the type to insert.
     18    * This can either be done before calling this constructor, or by modifying
     19    * the return value of {@link #getType()}.
     20    *
     21    * @param type the type to use when inserting the constructor
     22    * @param criteria where to insert the text
     23    * @param innerTypeInsertions the inner types to go on this constructor
     24    */
     25   public ConstructorInsertion(Type type, Criteria criteria,
     26       List<Insertion> innerTypeInsertions) {
     27     super(type, criteria, true, innerTypeInsertions);
     28   }
     29 
     30   /** {@inheritDoc} */
     31   @Override
     32   protected String getText(boolean comments, boolean abbreviate) {
     33     StringBuilder b = new StringBuilder();
     34     if (annotationsOnly) {
     35       // List<String> annotations = type.getAnnotations();
     36       // if (annotations.isEmpty()) { return ""; }
     37       // for (String a : annotations) {
     38       //  b.append(a);
     39       //  b.append(' ');
     40       // }
     41       // return new AnnotationInsertion(b.toString(), getCriteria(),
     42       //    getSeparateLine()).getText(comments, abbreviate);
     43       return "";
     44     } else {
     45       boolean commentAnnotation =
     46           comments && getBaseType().getName().isEmpty();
     47       String typeString = typeToString(type, commentAnnotation, true);
     48       int ix = typeString.lastIndexOf('$');  // FIXME: exclude '$' in source
     49       typeString = typeString.substring(ix+1);
     50 
     51       for (Insertion i : declarationInsertions) {
     52         b.append(i.getText(commentAnnotation, abbreviate)).append("\n");
     53         if (abbreviate) {
     54           packageNames.addAll(i.getPackageNames());
     55         }
     56       }
     57       b.append("public ").append(typeString).append("(");
     58       if (receiverInsertion != null && !receiverInsertion.getInserted()) {
     59         b.append(receiverInsertion.getText(comments, abbreviate));
     60       }
     61       b.append(") { super(); }");
     62       return b.toString();
     63     }
     64   }
     65 
     66   protected ReceiverInsertion getReceiverInsertion() {
     67     return receiverInsertion;
     68   }
     69 
     70   public void addReceiverInsertion(ReceiverInsertion recv) {
     71     if (receiverInsertion == null) {
     72       receiverInsertion = recv;
     73     } else {
     74       receiverInsertion.getInnerTypeInsertions()
     75           .addAll(recv.getInnerTypeInsertions());
     76     }
     77   }
     78 
     79   public void addDeclarationInsertion(Insertion ins) {
     80     declarationInsertions.add(ins);
     81     ins.setInserted(true);
     82   }
     83 
     84   /** {@inheritDoc} */
     85   @Override
     86   protected boolean addLeadingSpace(boolean gotSeparateLine, int pos,
     87       char precedingChar) {
     88     return false;
     89   }
     90 
     91   /** {@inheritDoc} */
     92   @Override
     93   protected boolean addTrailingSpace(boolean gotSeparateLine) {
     94     return false;
     95   }
     96 
     97   /** {@inheritDoc} */
     98   @Override
     99   public Kind getKind() {
    100     return Kind.CONSTRUCTOR;
    101   }
    102 }
    103