1 19 20 package org.netbeans.modules.java.codegen; 21 22 import java.util.*; 23 import java.lang.reflect.Modifier ; 24 25 import javax.swing.text.Position ; 26 import javax.swing.text.StyledDocument ; 27 28 import org.openide.src.*; 29 import org.openide.text.CloneableEditorSupport; 30 import org.openide.text.PositionBounds; 31 import org.openide.text.PositionRef; 32 33 import org.netbeans.modules.java.bridge.Binding; 34 35 40 class Method extends Member implements Binding.Method { 41 boolean constructor; 42 String preexistingBody; 43 44 public Method(ConstructorElement el, SourceText s) { 45 super(el, s); 46 constructor = (!(el instanceof MethodElement)); 47 } 48 49 public void create(PositionBounds b) throws SourceException { 50 super.create(b); 51 preexistingBody = null; 52 } 53 54 protected int classifyProperty(String name) { 55 if (name == PROP_BODY) 56 return CLASS_BODY; 57 else 58 return CLASS_HEADER; 59 } 60 61 private MethodElement cloneMethod() { 62 MethodElement el = new MethodElement(); 63 copyProperties(el); 64 try { 65 el.setReturn(((MethodElement)getElement()).getReturn()); 66 } catch (SourceException ex) { 67 } 69 return el; 70 } 71 72 protected Element cloneElement() { 73 Element orig = getElement(); 74 ConstructorElement el = orig instanceof MethodElement ? 75 new MethodElement() : new ConstructorElement(); 76 copyProperties(el); 77 return el; 78 } 79 80 protected void copyProperties(ConstructorElement target) { 81 ConstructorElement my = (ConstructorElement)getElement(); 82 try { 83 target.setName(my.getName()); 84 target.setParameters(my.getParameters()); 85 target.setModifiers(my.getModifiers()); 86 target.setExceptions(my.getExceptions()); 87 if (my instanceof MethodElement) 88 ((MethodElement)target).setReturn(((MethodElement)my).getReturn()); 89 } catch (SourceException ex) { 90 } 92 } 93 94 95 97 public void changeExceptions(Identifier[] exceptions) throws SourceException { 98 if (!source.isGeneratorEnabled()) 99 return; 100 ConstructorElement el = (ConstructorElement)cloneElement(); 101 el.setExceptions(exceptions); 102 regenerateHeader(el); 103 } 104 105 107 public void changeParameters(MethodParameter[] params) throws SourceException { 108 if (!source.isGeneratorEnabled()) 109 return; 110 ConstructorElement el = (ConstructorElement)cloneElement(); 111 el.setParameters(params); 112 regenerateHeader(el); 113 } 114 115 117 public void changeReturnType(Type type) throws SourceException { 118 if (!source.isGeneratorEnabled()) 119 return; 120 MethodElement el = cloneMethod(); 121 el.setReturn(type); 122 regenerateHeader(el); 123 } 124 125 128 public void changeModifiers(int newMods) throws SourceException { 129 if (!source.isGeneratorEnabled()) 130 return; 131 132 ConstructorElement el = (ConstructorElement)cloneElement(); 133 boolean isAbstract = Modifier.isAbstract(el.getModifiers()); 134 boolean newAbstract = Modifier.isAbstract(newMods); 135 el.setModifiers(newMods); 136 regenerateHeader(el); 137 if (isAbstract!=newAbstract) { 138 if (newAbstract) 139 el.setBody(null); 140 else { 141 if (((ConstructorElement)getElement()).getBody()!=null) 142 return; 143 el.setBody(""); 144 } 145 regenerateBody(el); 146 } 147 } 148 149 protected void regenerateBody(Element bean) throws SourceException { 150 ConstructorElement now = (ConstructorElement)bean; 151 ConstructorElement el = (ConstructorElement)getElement(); 152 boolean existed = el.getBody() != null; 153 String newBody = now.getBody(); 154 155 if (existed && newBody == null) { 156 makeAbstract(); 157 } else if (!existed && newBody != null) { 158 createBody(newBody); 159 } else if (existed) { 160 changeBody(newBody); 161 } 162 } 163 164 166 public void createBody(String bodyText) throws SourceException { 167 if (!source.isGeneratorEnabled()) 168 return; 169 final StyledDocument doc = findDocument(); 170 final String s = CodeGenerator.normalizeBody( 171 ElementBinding.convertNewlines(bodyText), true); 172 173 source.runAtomic(getElement(), new ExceptionRunnable() { 174 public void run() throws Exception { 175 int afterHeader = headerBounds.getEnd().getOffset(); 176 String text = CodeGenerator.formatText(doc, afterHeader, 177 " " + s); int end = afterHeader + text.length(); 179 180 CloneableEditorSupport supp = source.getEditorSupport(); 181 doc.insertString(afterHeader, text, null); 182 bodyBounds = new PositionBounds( 183 supp.createPositionRef(afterHeader + 1, Position.Bias.Forward), 184 supp.createPositionRef(end, Position.Bias.Backward) 185 ); 186 doc.remove(end, wholeBounds.getEnd().getOffset() - end); 187 } 188 }); 189 } 190 191 193 public String getBodyContent() throws SourceException { 194 if (preexistingBody != null) 195 return preexistingBody; 196 197 if (bodyBounds != null) { 198 return CodeGenerator.readTextBounds(bodyBounds); 199 } else { 200 return null; 201 } 202 } 203 204 207 public void makeAbstract() throws SourceException { 208 final StyledDocument doc = findDocument(); 209 210 if (!source.isGeneratorEnabled()) 211 return; 212 source.runAtomic(getElement(), new ExceptionRunnable() { 213 public void run() throws Exception { 214 int afterHeader = headerBounds.getEnd().getOffset(); 215 int end = wholeBounds.getEnd().getOffset(); 216 217 doc.insertString(afterHeader, ";", null); doc.remove(afterHeader + 1, end - afterHeader); 219 bodyBounds = null; 220 221 } 224 }); 225 } 226 227 230 public void changeBody(String bodyString) throws SourceException { 231 if (!source.isGeneratorEnabled()) 232 return; 233 final String normalized = CodeGenerator.normalizeBody( 234 ElementBinding.convertNewlines(bodyString), false)+"}"; final ElementBinding b = this; 236 237 source.runAtomic(getElement(), new ExceptionRunnable() { 238 public void run() throws Exception { 239 String formatted = CodeGenerator.formatText( 240 CodeGenerator.getDocument(b), 241 bodyBounds.getBegin(), 242 normalized); 243 String formattedBody = formatted.substring(0,formatted.lastIndexOf('}')); 244 CodeGenerator.fillTextBounds(bodyBounds,formattedBody); 245 } 246 }); 247 } 248 249 252 public void updateFrom(Binding other) { 253 } 254 255 public void copyBody(String bodyString) { 256 preexistingBody = bodyString; 257 } 258 } 259 | Popular Tags |