KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > puppycrawl > tools > checkstyle > checks > usage > transmogrify > MethodDef


1
2 // Transmogrify License
3
//
4
// Copyright (c) 2001, ThoughtWorks, Inc.
5
// All rights reserved.
6
// Redistribution and use in source and binary forms, with or without
7
// modification, are permitted provided that the following conditions
8
// are met:
9
// - Redistributions of source code must retain the above copyright notice,
10
// this list of conditions and the following disclaimer.
11
// - Redistributions in binary form must reproduce the above copyright
12
// notice, this list of conditions and the following disclaimer in the
13
// documentation and/or other materials provided with the distribution.
14
// Neither the name of the ThoughtWorks, Inc. nor the names of its
15
// contributors may be used to endorse or promote products derived from this
16
// software without specific prior written permission.
17
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
19
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28

29 package com.puppycrawl.tools.checkstyle.checks.usage.transmogrify;
30
31
32
33 import java.util.List JavaDoc;
34 import java.util.Vector JavaDoc;
35
36
37 /**
38  * <code>MethodDef</code> contains all the pertinent information for
39  * a method, including return type, formal parameters, and exceptions
40  * thrown
41  *
42  * @see ClassDef
43  * @see MethodSignature
44  */

45 public class MethodDef extends DefaultScope implements IMethod {
46
47   private IClass returnType;
48   private List JavaDoc exceptions;
49
50   private List JavaDoc parameters;
51
52   public MethodDef(String JavaDoc name, Scope parentScope, SymTabAST node) {
53     super(name, parentScope, node);
54     parameters = new Vector JavaDoc();
55   }
56
57   /**
58    * Returns the <code>ClassDef</code> for the return type of this method.
59    *
60    * @return the <code>ClassDef</code> for the return type of this method
61    */

62   public IClass getType() {
63     return returnType;
64   }
65
66   /**
67    * Sets the return type of this method.
68    *
69    * @param type the <code>ClassDef</code> for the return type
70    */

71   public void setType(IClass type) {
72     returnType = type;
73   }
74
75   /**
76    * Adds a parameter to the collection of formal parameters
77    *
78    * @param parameter the <code>VariableDef</code> to add
79    */

80   public void addParameter(VariableDef parameter) {
81     parameters.add( parameter );
82     addDefinition(parameter);
83   }
84
85   /**
86    * Whether this method has the same signature as the given signature.
87    *
88    * @param signature the <code>MethodSignature</code> to compare
89    *
90    * @return whether the signatures are equal
91    */

92   public boolean hasSameSignature(ISignature signature) {
93     return getSignature().equals(signature);
94   }
95
96   /**
97    * Whether this method has a signature compatible with the given signature.
98    *
99    * @param signature the signature being compared
100    * @return whether the signatures are compatible
101    */

102   public boolean hasCompatibleSignature(ISignature signature) {
103     return signature.isCompatibleWith(getSignature());
104   }
105
106   /**
107    * Returns the signature of this method.
108    *
109    * @return the signature of this method
110    */

111   public ISignature getSignature() {
112     Vector JavaDoc argTypes = new Vector JavaDoc();
113
114     for (int i = 0; i < parameters.size(); i++) {
115       argTypes.add(getParameterAt(i).getType());
116     }
117
118     return new MethodSignature(argTypes);
119   }
120
121   /**
122    * Gets the <i>i</i>th parameter of this method
123    *
124    * @param i the index of the parameter
125    *
126    * @return the <code>VariableDef</code> of the <i>i</i>th parameter
127    */

128   private VariableDef getParameterAt( int i ) {
129     return (VariableDef)(parameters.get( i ));
130   }
131
132   /**
133    * Adds an exception that this method throws.
134    *
135    * @param exception the exception to add
136    */

137   public void addException(IClass exception) {
138     if (exceptions == null) {
139       exceptions = new Vector JavaDoc();
140     }
141
142     exceptions.add(exception);
143   }
144
145   /**
146    * Returns the exceptions this method throws
147    *
148    * @return the exceptions this method throws
149    */

150   public IClass[] getExceptions() {
151     return (IClass[])exceptions.toArray(new IClass[0]);
152   }
153
154   public String JavaDoc getQualifiedName() {
155     return super.getQualifiedName() + getSignature();
156   }
157 }
Popular Tags