KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > src > MethodElement


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.src;
21
22 import java.io.*;
23 import java.lang.reflect.Modifier JavaDoc;
24
25 import org.openide.util.NbBundle;
26
27 /** Representation of a method.
28 * It extends the constructor representation since
29 * all that is added is the return type.
30 *
31 * @author Petr Hamernik
32 */

33 public final class MethodElement extends ConstructorElement {
34     /** Format for the header - used in code generator */
35     private static final ElementFormat HEADER_FORMAT =
36         new ElementFormat("{m,,\" \"}{r} {n}({a}){e,\" throws \",}"); // NOI18N
37

38     static final long serialVersionUID =2366156788906032138L;
39
40     /** Create a method element held in memory. */
41     public MethodElement() {
42         this(new Memory(), null);
43     }
44
45     /** Create a method element.
46     * @param impl implementation of functionality
47     * @param clazz declaring class, or <code>null</code>
48     */

49     public MethodElement(MethodElement.Impl impl, ClassElement clazz) {
50         super(impl, clazz);
51     }
52
53     /** Clone the method.
54     * @return new method with the same values as the original,
55     * but represented in memory
56     */

57     public Object JavaDoc clone () {
58         return new MethodElement (new Memory (this), null);
59     }
60
61     final MethodElement.Impl getMethodImpl() {
62         return (MethodElement.Impl) impl;
63     }
64
65     /** Get the method's return type.
66     * @return the return type
67     */

68     public Type getReturn() {
69         return getMethodImpl().getReturn();
70     }
71
72     /** Set the method's return type.
73     * @param ret the new type
74     * @throws SourceException if impossible
75     */

76     public void setReturn (Type ret) throws SourceException {
77         getMethodImpl().setReturn(ret);
78     }
79
80     /* @return the mask of possible modifiers for this element. */
81     public int getModifiersMask() {
82         if (isDeclaredInInterface()) {
83             return Modifier.PUBLIC | Modifier.ABSTRACT;
84         }
85         else {
86             return Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE |
87                    Modifier.ABSTRACT | Modifier.STATIC | Modifier.FINAL |
88                    Modifier.NATIVE | Modifier.SYNCHRONIZED;
89         }
90     }
91
92     /** Set the name of this member.
93     * @param name the name
94     * @throws SourceException if impossible
95     */

96     public final void setName(Identifier name) throws SourceException {
97         ClassElement c = getDeclaringClass();
98         if (c != null) {
99             MethodParameter[] params = getParameters();
100             Type[] types = new Type[params.length];
101             for (int i = 0; i < types.length; i++)
102                 types[i] = params[i].getType();
103             MethodElement m = c.getMethod(name, types);
104             if ((m != null) && (m != this)) {
105         String JavaDoc msg = NbBundle.getMessage(ElementFormat.class,
106             "FMT_EXC_RenameMethod", c.getName().getName(), name ); // NOI18N
107
throwSourceException(msg);
108             }
109         }
110         super.setName(name);
111     }
112
113     /** Get the printing format.
114     * May be overridden by subclasses.
115     * @return the format
116     */

117     ElementFormat getFormat() {
118         return HEADER_FORMAT;
119     }
120
121     /** Marks the notable point in the writer.
122     * This method calls markMethod.
123     */

124     void printerMark(ElementPrinter printer, int what) throws ElementPrinterInterruptException {
125         printer.markMethod(this, what);
126     }
127
128     /** Pluggable behavior of the method element.
129     * @see MethodElement
130     */

131     public interface Impl extends ConstructorElement.Impl {
132         /** @deprecated Only public by accident. */
133         /* public static final */ long serialVersionUID = 7273573865765501815L;
134         /** Get the method's return type.
135          * @return the return type
136          */

137         public Type getReturn();
138
139         /** Set the method's return type.
140          * @param ret the new type
141          */

142         public void setReturn (Type ret) throws SourceException;
143     }
144
145     /** A key for method elements, for use in hash tables etc.
146     */

147     public static final class Key extends ConstructorElement.Key {
148         /** Name of the method */
149         private Identifier name;
150         private Type returnType;
151         
152         /** Constructs a key by name and parameters.
153         * @param name the method name
154         * @param params the method's parameters
155         */

156         public Key (final Identifier name, final Type[] params) {
157             super(params);
158             this.name = name;
159         }
160     
161     /** Constructs a key that checks also a return type of the method.
162      * Methods with different return types are considered to be different although
163      * they have the same name and parameter types.
164      * @param name name of the method
165      * @param params types of method's arguments
166      * @param returnType return type for the method; if null, the key behaves like
167      * it was constructed using {@link MethodElement.Key#MethodElement.Key(Identifier, Type[])}
168      */

169     public Key (final Identifier name, final Type[] params, final Type returnType) {
170         this(name, params);
171         this.returnType = returnType;
172     }
173
174         /** Constructs a key for a method.
175         * Does not hold any reference to the element.
176         * @param me the method element
177         */

178         public Key (final MethodElement me) {
179             super(me);
180             name = me.getName();
181         }
182
183     /**
184      * Constructs a key that checks for method's name, its argument types and
185      * optionally for a return type, if <I>checkReturn</I> is true.
186      * @param me method to construct the key for
187      * @param checkReturn if true, the key contains and also checks the retunr type.
188      */

189     public Key(final MethodElement me, boolean checkReturn) {
190         this(me);
191         if (checkReturn)
192         returnType = me.getReturn();
193     }
194
195         /* Returns true if parameters are the same */
196         public boolean equals (Object JavaDoc obj) {
197             if (!(obj instanceof Key)) return false;
198         Key other = (Key)obj;
199             if (!name.equals(other.name) || !super.equals(obj)) return false;
200             if (returnType == null || other.returnType == null)
201                 return true;
202             return returnType.equals(other.returnType);
203         }
204
205         /* Computes hashcode as exclusive or of
206         * superclass hashcode and return type string hashcode.
207         */

208         public int hashCode () {
209         // Note: hashcode of the returntype was intentionally left out
210
// from the computation to allow matching against keys with no
211
// return type specified.
212
return super.hashCode() ^ name.getFullName().hashCode();
213         }
214
215     } // end of Key inner class
216

217     static class Memory extends ConstructorElement.Memory implements Impl {
218         /** Type of exception */
219         private Type type;
220
221         static final long serialVersionUID =2015834437815195149L;
222         Memory() {
223             type = Type.VOID;
224         }
225
226         /** Copy constructor */
227         Memory (MethodElement el) {
228             super (el);
229             type = el.getReturn ();
230         }
231
232         /** Setter for the return type */
233         public Type getReturn() {
234             return type;
235         }
236
237         /** @return the return type */
238         public void setReturn (Type ret) {
239             Type t = type;
240             type = ret;
241             firePropertyChange (PROP_RETURN, t, ret);
242         }
243
244         public Object JavaDoc readResolve() {
245             return new MethodElement(this, null);
246         }
247
248     }
249
250 }
251
Popular Tags