View Javadoc

1   /** 2007, Digitalis Informatica. All rights reserved.
2    *
3    * Distribuicao e Gestao de Informatica, Lda.
4    * Estrada de Paco de Arcos num.9 - Piso -1
5    * 2780-666 Paco de Arcos
6    * Telefone: (351) 21 4408990
7    * Fax: (351) 21 4408999
8    * http://www.digitalis.pt
9    */
10  package pt.digitalis.dif.codegen.util;
11  
12  import pt.digitalis.dif.utils.ObjectFormatter;
13  
14  /**
15   * Defines a method enhancement object.
16   *
17   * @author Pedro Viegas <a href="mailto:pviegas@digitalis.pt">pviegas@digitalis.pt</a>
18   * @author Rodrigo Gonçalves <a href="mailto:rgoncalves@digitalis.pt">rgoncalves@digitalis.pt</a><br/>
19   * @created Dec 10, 2007
20   */
21  public class ClassMethodEnhancement {
22  
23      /** Defines if source should be appended or overwritten on each addition operation. T is for append, F for overwrite. */
24      private boolean incrementalMode;
25  
26      /** The name of the method to enhance. */
27      private String methodName;
28  
29      /** The method source. */
30      private StringBuilder source = new StringBuilder();
31  
32      /** the finalizer code if needed */
33      String terminatorSource = "";
34  
35      /**
36       * Builds a new method enhancement object.
37       *
38       * @param methodName
39       *            the name of the method
40       * @param incrementalMode
41       *            defines the incremental mode
42       */
43      public ClassMethodEnhancement(String methodName, boolean incrementalMode) {
44          this.methodName = methodName;
45          this.incrementalMode = incrementalMode;
46      }
47  
48      /**
49       * Adds source to the method. If it is a non-incremental source method it will overwrite previous source code
50       *
51       * @param source
52       *            the source to add
53       */
54      public void addSource(String source) {
55          if (isIncrementalMode())
56              this.source.append(source);
57          else
58              this.source = new StringBuilder(source);
59      }
60  
61      /**
62       * Returns the method name.
63       *
64       * @return the method name
65       */
66      public String getMethodName() {
67          return methodName;
68      }
69  
70      /**
71       * Returns the source.
72       *
73       * @return the source
74       */
75      public String getSource() {
76          if (terminatorSource == null || "".equals(terminatorSource))
77              return source.toString();
78          else
79              return source.toString() + "\n" + terminatorSource;
80      }
81  
82      /**
83       * Inspector for the 'incrementalMode' property.
84       *
85       * @return the incrementalMode property value
86       */
87      public boolean isIncrementalMode() {
88          return incrementalMode;
89      }
90  
91      /**
92       * Modifier for the 'source' attribute.
93       *
94       * @param source the new source value to set
95       */
96      public void setSource(StringBuilder source)
97      {
98          this.source = source;
99      }
100 
101     /**
102      * Adds source as the methods terminator (finalizer) code. it will overwrite previous source terminator code
103      *
104      * @param source
105      *            the source to add
106      */
107     public void setTerminator(String source) {
108         this.terminatorSource = source;
109     }
110 
111 
112     /**
113      * @see java.lang.Object#toString()
114      */
115     @Override
116     public String toString() {
117         ObjectFormatter formatter = new ObjectFormatter();
118 
119         formatter.addItem("Method Name", this.methodName);
120         formatter.addItem("Incremental", incrementalMode);
121         formatter.addItem("Source", source);
122         formatter.addItem("Terminator Source", terminatorSource);
123 
124         return formatter.getFormatedObject();
125     }
126 }