KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > bridge > MethodElementImpl


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.netbeans.modules.java.bridge;
21
22 import javax.jmi.reflect.InvalidObjectException;
23
24 import java.beans.*;
25 import java.util.*;
26 import javax.jmi.reflect.RefBaseObject;
27 import org.openide.src.*;
28
29 import org.netbeans.api.mdr.events.*;
30
31 import org.netbeans.jmi.javamodel.Method;
32 import org.netbeans.jmi.javamodel.IsOfType;
33 import org.netbeans.jmi.javamodel.TypeReference;
34 import org.netbeans.jmi.javamodel.ArrayReference;
35 import org.netbeans.jmi.javamodel.PrimitiveType;
36
37 /**
38  * The class implements properties specific to a method.
39  *
40  * @author Svatopluk Dedic, Petr Hamernik, Jaroslav Tulach
41  * @version 0.1
42  * @since 24/11/2000
43  */

44 class MethodElementImpl extends CallableImpl implements MethodElement.Impl {
45
46     private ElementImpl.ElementListener methodListener;
47     
48     private static final long serialVersionUID = 8831749368310053231L;
49     
50     // Construction
51
///////////////////////////////////////////////////////////////////////////////////
52

53     MethodElementImpl(DefaultLangModel model, Method method) {
54         super(model, method);
55     }
56     
57     public void connectListener () {
58         methodListener = new MethodListener (this);
59         methodListener.connect ();
60     }
61     
62     protected void createFromModel(Element el) throws SourceException {
63         super.createFromModel(el);
64         MethodElement m = (MethodElement)el;
65         setReturn(m.getReturn());
66     }
67     
68     // Getters
69
///////////////////////////////////////////////////////////////////////////////
70

71     /**
72      * Retrieves the return type of the method.
73      * @return the return type description.
74      */

75     public Type getReturn() {
76         repository.beginTrans(false);
77         try {
78             if (javaElement.isValid()) {
79                 setClassPath();
80                 javaElement.refImmediateComposite(); // check if javaElement is valid
81
org.netbeans.jmi.javamodel.Type t = ((Method)javaElement).getType();
82                 if (t instanceof PrimitiveType) {
83                     return descrToType(t);
84                 }
85                 return typeReferenceToType (((Method)javaElement).getTypeName ());
86             } else {
87                 return Type.VOID;
88             }
89         } finally {
90             repository.endTrans(false);
91         }
92     }
93     
94     // Setters
95
///////////////////////////////////////////////////////////////////////////////
96

97     /**
98      * Sets the return type.
99      */

100     public void setReturn(Type newType) throws SourceException {
101         checkWritable(true);
102         checkDocument();
103         boolean failed = true;
104         repository.beginTrans (true);
105         try {
106             if (javaElement.isValid()) {
107                 setClassPath();
108                 Type oldType = getReturn ();
109                 newType = resolveType(newType);
110                 if (compareSourceTypes(oldType, newType)) {
111                     failed = false;
112                     return;
113                 }
114                 PropertyChangeEvent evt = new PropertyChangeEvent(getElement(), PROP_RETURN, oldType, newType);
115
116                 checkReturnTypeConstraints(newType);
117                 checkVetoablePropertyChange(evt);
118
119                 if (newType.isPrimitive()) {
120                     ((Method) javaElement).setType(typeToDescr(newType));
121                 } else {
122                     ((Method) javaElement).setTypeName(typeToTypeReference(newType));
123                 }
124                 failed = false;
125             } else {
126                 failed = false;
127                 throwIsInvalid ();
128             }
129         } finally {
130             repository.endTrans (failed);
131         }
132     }
133     
134     public void fireReturnChange (Type oldType, Type newType) {
135         oldType = resolveType (oldType);
136         newType = resolveType (newType);
137         if (compareSourceTypes(oldType, newType))
138             return;
139         
140         PropertyChangeEvent evt = new PropertyChangeEvent(getElement(), PROP_RETURN, oldType, newType);
141         fireOwnPropertyChange(evt);
142         
143         MethodElement elem = (MethodElement) cloneSelf ();
144         try {
145             MethodElement.Impl impl = (MethodElement.Impl) elem.getCookie (Element.Impl.class);
146             impl.setReturn (oldType);
147         } catch (SourceException e) {
148             e.printStackTrace ();
149         }
150         notifyConnectionChange (elem);
151     }
152     
153     // Utility and helper methods.
154
///////////////////////////////////////////////////////////////////////////////
155
protected final Element createWrapperElement() {
156         return new MethodElement(this, getDeclaringClass());
157     }
158     
159     protected void checkReturnTypeConstraints(Type t) throws SourceException {
160         // no checking, no constraings on the rettype.
161
}
162
163     // Serialization support
164
///////////////////////////////////////////////////////////////////////////////
165
public Object JavaDoc readResolve() {
166         return null;
167     }
168     
169     public String JavaDoc toString() {
170         return "MethodElementImpl[" + getName().getSourceName() + ", " + // NOI18N
171
getParameters().length + " args]"; // NOI18N
172
}
173     
174     protected Element cloneSelf() {
175         MethodElement clone = new MethodElement();
176         copyCallableProperties(clone);
177         try {
178             clone.setReturn(getReturn());
179         } catch (SourceException ex) {
180         }
181         return clone;
182     }
183     
184     // ..........................................................................
185

186     static class MethodListener extends CallableImpl.CallableListener implements MDRChangeListener {
187
188         private Type type;
189         
190         MethodListener (MethodElementImpl impl) {
191             super (impl);
192         }
193         
194         public void connect () {
195             if (REGISTER_LISTENER) {
196                 super.connect ();
197                 TypeReference typeRef = ((Method) javaElement).getTypeName();
198                 if (typeRef != null) {
199                     type = ((MethodElementImpl) getImpl ()).typeReferenceToType (typeRef);
200                 } else {
201                     type = null;
202                 }
203             }
204         }
205         
206         public void doChange(MDRChangeEvent event) {
207             super.doChange (event);
208             if (event instanceof AttributeEvent) {
209                 AttributeEvent attrEvent = (AttributeEvent) event;
210                 String JavaDoc attrName = attrEvent.getAttributeName();
211                 Object JavaDoc source = event.getSource();
212                 if (attrName.equals("typeName")) { // NOI18N
213
Type oldType = type;
214                     TypeReference typeRef = (TypeReference) attrEvent.getNewElement();
215                     if (typeRef != null) {
216                         type = ((MethodElementImpl) getImpl()).typeReferenceToType(typeRef);
217                     } else {
218                         type = null;
219                     }
220                     ((MethodElementImpl) impl).fireReturnChange(oldType, type);
221                 }
222             }
223         }
224
225     }
226
227 }
228
Popular Tags