/plugins/JavaSideKick/tags/javasidekick-2-6-0/src/sidekick/java/node/MethodNode.java

#
Java | 173 lines | 95 code | 22 blank | 56 comment | 26 complexity | f477cbc0b4026fbec758281bc0fe075a MD5 | raw file

✨ Summary
  1. /*
  2. Copyright (c) 2005, Dale Anson
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without modification,
  5. are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright notice,
  9. this list of conditions and the following disclaimer in the documentation
  10. and/or other materials provided with the distribution.
  11. * Neither the name of the <ORGANIZATION> nor the names of its contributors
  12. may be used to endorse or promote products derived from this software without
  13. specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  21. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. package sidekick.java.node;
  26. import java.util.*;
  27. // an extension of TigerNode for a method
  28. public class MethodNode extends TigerNode implements Parameterizable {
  29. String typeParams = null;
  30. List formalParams = null;
  31. Type returnType = null;
  32. public MethodNode() {
  33. super();
  34. }
  35. public MethodNode( String name, int modifiers, String typeParams, List formalParams, Type returnType ) {
  36. super(name, modifiers);
  37. this.typeParams = typeParams;
  38. this.formalParams = formalParams;
  39. setReturnType(returnType);
  40. }
  41. public int getOrdinal() {
  42. return METHOD;
  43. }
  44. public void setFormalParams( List p ) {
  45. formalParams = p;
  46. }
  47. /**
  48. * @return raw value for formal params
  49. */
  50. public List getFormalParams() {
  51. return formalParams;
  52. }
  53. /**
  54. * Returns a string showing the formal parameters for this method. The
  55. * returned string is a comma separated list of parameter types, if
  56. * <code>withNames</code> is true, then the returned string is a comma
  57. * separated list of type:name.
  58. * <p>
  59. * Example: method is "void getX(int a, int b)",
  60. * <code>withNames</code> is false, returned string is "int,int".
  61. * <p>
  62. * Example: method is "void getX(int a, int b)",
  63. * <code>withNames</code> is true, returned string is "int a,int b".
  64. * @param withNames should returned string include the formal parameter names
  65. * @param typeAsSuffix if true and if withNames is true, name and type will
  66. * be reversed, e.g. method is "void getX(int a, int b), returned string is
  67. * "a : int, b : int"
  68. * @param includeFinal if true, include any "final" modifier, e.g. method is
  69. * "void getX(int a, final int b)", returned string is "int, final int", or
  70. * if withNames is true, "int a, final int b", and if typeAsSuffix is true,
  71. * "a : int, b : final int"
  72. * @return parameters as string, see above
  73. */
  74. public String getFormalParams( boolean withNames, boolean typeAsSuffix, boolean includeFinal, boolean includeTypeArgs ) {
  75. if (formalParams == null || formalParams.size() == 0)
  76. return "";
  77. StringBuffer sb = new StringBuffer();
  78. for (Iterator it = formalParams.iterator(); it.hasNext(); ) {
  79. Parameter param = (Parameter)it.next();
  80. if (typeAsSuffix) {
  81. if (includeFinal && param.isFinal())
  82. sb.append("final ");
  83. sb.append(param.getType());
  84. if (includeTypeArgs)
  85. sb.append(param.type.typeArgs);
  86. if (param.isVarArg())
  87. sb.append("...");
  88. if (withNames)
  89. sb.append(" : ").append(param.getType());
  90. }
  91. else {
  92. if (withNames)
  93. sb.append(param.getName()).append(" : ");
  94. if (includeFinal && param.isFinal())
  95. sb.append("final ");
  96. sb.append(param.getType());
  97. if (includeTypeArgs)
  98. sb.append(param.getTypeParams());
  99. if (param.isVarArg())
  100. sb.append("...");
  101. }
  102. if (it.hasNext())
  103. sb.append(", ");
  104. }
  105. return sb.toString();
  106. }
  107. public void setTypeParams( String p ) {
  108. typeParams = p;
  109. }
  110. public String getTypeParams() {
  111. return typeParams == null ? "" : typeParams;
  112. }
  113. public void setThrows( List t ) {
  114. if (t == null) {
  115. return;
  116. }
  117. for (Iterator it = t.iterator(); it.hasNext(); ) {
  118. TigerNode tn = (TigerNode)it.next();
  119. ThrowsNode thn = new ThrowsNode(tn.getName());
  120. thn.setStartLocation(tn.getStartLocation());
  121. thn.setEndLocation(tn.getEndLocation());
  122. addChild(thn);
  123. }
  124. }
  125. /**
  126. * Overridden to return true if the node is a ThrowsNode.
  127. */
  128. public boolean canAdd( TigerNode node ) {
  129. //return node.getOrdinal() == TigerNode.THROWS || node.getOrdinal() == TigerNode.BLOCK;
  130. return true;
  131. }
  132. public void setReturnType( Type t ) {
  133. returnType = t;
  134. }
  135. public Type getReturnType() {
  136. return returnType;
  137. }
  138. public String toString() {
  139. StringBuffer sb = new StringBuffer();
  140. sb.append( super.toString() );
  141. if (getTypeParams() != null)
  142. sb.append(getTypeParams());
  143. sb.append("(").append( getFormalParams( true, false, true, true ) ).append(")");
  144. if ( returnType != null )
  145. sb.append( ": " ).append( returnType );
  146. return sb.toString();
  147. }
  148. }