• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package annotator.find;
2 
3 import type.Type;
4 
5 /**
6  * Specifies an insertion of a cast into a source file. Stores the type of cast
7  * to insert in addition to the annotation and location.
8  * <p>
9  * In order to restrict the cast to only the specified expression, a cast
10  * insertion is of the form:
11  *
12  * <pre>
13  * ((<em>cast type</em>) (<em>original expression</em>))
14  * </pre>
15  *
16  * This insertion inserts the cast type and parentheses that go before the
17  * original expression. A {@link CloseParenthesisInsertion} must be used
18  * after the expression to close the parentheses left open by this insertion.
19  */
20 public class CastInsertion extends Insertion {
21 
22   /**
23    * The type to cast to.
24    */
25   private Type type;
26 
27   /**
28    * Whether insertion is to take place on a bare array literal.
29    */
30   public boolean onArrayLiteral = false;
31 
32   /**
33    * Creates a new CastInsertion.
34    *
35    * @param criteria where to insert the text
36    * @param type the un-annotated type to cast to
37    */
CastInsertion(Criteria criteria, Type type)38   public CastInsertion(Criteria criteria, Type type) {
39     super(criteria, false);
40     this.type = type;
41   }
42 
43   /**
44    * Gets the type for this insertion. It is assumed that the returned value will be modified
45    * to update the type to be inserted.
46    * @return the type
47    */
getType()48   public Type getType() {
49       return type;
50   }
51 
setType(Type t)52   protected void setType(Type t) {
53       type = t;
54   }
55 
56   /** {@inheritDoc} */
57   @Override
getText(boolean comments, boolean abbreviate)58   protected String getText(boolean comments, boolean abbreviate) {
59     String result = onArrayLiteral
60         ? "((new " + typeToString(type, comments, abbreviate) + " "
61         : "((" + typeToString(type, comments, abbreviate) + ") (";
62     return result;
63   }
64 
65   /** {@inheritDoc} */
66   @Override
addLeadingSpace(boolean gotSeparateLine, int pos, char precedingChar)67   protected boolean addLeadingSpace(boolean gotSeparateLine, int pos,
68       char precedingChar) {
69     // Don't add a leading space if this cast is on the index of an array access.
70     return super.addLeadingSpace(gotSeparateLine, pos, precedingChar)
71            && precedingChar != '[';
72   }
73 
74   /** {@inheritDoc} */
75   @Override
addTrailingSpace(boolean gotSeparateLine)76   protected boolean addTrailingSpace(boolean gotSeparateLine) {
77     // Never add a trailing space after the first part of a cast insertion.
78     return false;
79   }
80 
isOnArrayLiteral()81   public boolean isOnArrayLiteral() {
82     return onArrayLiteral;
83   }
84 
setOnArrayLiteral(boolean onArrayLiteral)85   public void setOnArrayLiteral(boolean onArrayLiteral) {
86     this.onArrayLiteral = onArrayLiteral;
87   }
88 
89   /** {@inheritDoc} */
90   @Override
getKind()91   public Kind getKind() {
92     return Kind.CAST;
93   }
94 }
95