1 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 44 class MethodElementImpl extends CallableImpl implements MethodElement.Impl { 45 46 private ElementImpl.ElementListener methodListener; 47 48 private static final long serialVersionUID = 8831749368310053231L; 49 50 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 71 75 public Type getReturn() { 76 repository.beginTrans(false); 77 try { 78 if (javaElement.isValid()) { 79 setClassPath(); 80 javaElement.refImmediateComposite(); 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 97 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 protected final Element createWrapperElement() { 156 return new MethodElement(this, getDeclaringClass()); 157 } 158 159 protected void checkReturnTypeConstraints(Type t) throws SourceException { 160 } 162 163 public Object readResolve() { 166 return null; 167 } 168 169 public String toString() { 170 return "MethodElementImpl[" + getName().getSourceName() + ", " + getParameters().length + " args]"; } 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 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 attrName = attrEvent.getAttributeName(); 211 Object source = event.getSource(); 212 if (attrName.equals("typeName")) { 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 |