1 19 20 package org.netbeans.modules.form.codestructure; 21 22 import java.util.*; 23 import java.lang.reflect.*; 24 25 28 29 class CodeSupport { 30 31 private CodeSupport() { 32 } 33 34 static String getSourceClassName(Class cls) { 35 return cls.getName().replace('$', '.').replace('+', '.').replace('/', '.'); } 37 38 41 static final class MethodStatement extends AbstractCodeStatement { 42 private Method performMethod; 43 private CodeExpression[] parameters; 44 45 public MethodStatement(CodeExpression exp, 46 Method m, 47 CodeExpression[] params) 48 { 49 super(exp); 50 performMethod = m; 51 parameters = params != null ? params : CodeStructure.EMPTY_PARAMS; 52 } 53 54 public Object getMetaObject() { 55 return performMethod; 56 } 57 58 public CodeExpression[] getStatementParameters() { 59 return parameters; 60 } 61 62 public String getJavaCodeString(String parentStr, String [] paramsStr) { 63 StringBuffer buf = new StringBuffer (); 64 65 if (parentStr != null && !parentStr.equals("")) { 66 buf.append(parentStr); 67 buf.append("."); } 69 70 buf.append(performMethod.getName()); 71 buf.append("("); 73 for (int i=0; i < paramsStr.length; i++) { 74 buf.append(paramsStr[i]); 75 if (i+1 < parameters.length) 76 buf.append(", "); } 78 79 buf.append(");"); 82 return buf.toString(); 83 } 84 } 85 86 static final class FieldStatement extends AbstractCodeStatement { 87 private Field assignField; 88 private CodeExpression[] parameters; 89 90 public FieldStatement(CodeExpression exp, 91 Field f, 92 CodeExpression assignedExp) 93 { 94 super(exp); 95 assignField = f; 96 parameters = new CodeExpression[] { assignedExp }; 97 } 98 99 public Object getMetaObject() { 100 return assignField; 101 } 102 103 public CodeExpression[] getStatementParameters() { 104 return parameters; 105 } 106 107 public String getJavaCodeString(String parentStr, String [] paramsStr) { 108 StringBuffer buf = new StringBuffer (); 109 110 if (parentStr != null && !parentStr.equals("")) { 111 buf.append(parentStr); 112 buf.append("."); } 114 115 buf.append(assignField.getName()); 116 buf.append(" = "); buf.append(paramsStr[0]); 118 buf.append(";"); 121 return buf.toString(); 122 } 123 } 124 125 static final class AssignVariableStatement extends AbstractCodeStatement { 126 private CodeVariable variable; 127 128 public AssignVariableStatement(CodeVariable var, CodeExpression exp) { 129 super(exp); 130 variable = var; 131 } 132 133 public Object getMetaObject() { 134 return parentExpression; 135 } 136 137 public CodeExpression[] getStatementParameters() { 138 return parentExpression.getOrigin().getCreationParameters(); 139 } 140 141 public String getJavaCodeString(String parentStr, String [] paramsStr) { 142 StringBuffer buf = new StringBuffer (); 143 int varType = variable.getType(); 144 145 int declareMask = CodeVariable.SCOPE_MASK 146 | CodeVariable.DECLARATION_MASK; 147 if ((varType & declareMask) == CodeVariable.LOCAL) { 148 if ((varType & CodeVariable.FINAL) == CodeVariable.FINAL) 151 buf.append("final "); 153 buf.append(getSourceClassName(variable.getDeclaredType())); 154 buf.append(" "); } 156 157 buf.append(variable.getName()); 158 buf.append(" = "); buf.append(parentExpression.getOrigin().getJavaCodeString( 160 parentStr, paramsStr)); 161 buf.append(";"); 163 return buf.toString(); 164 } 165 } 166 167 static final class DeclareVariableStatement extends AbstractCodeStatement { 168 private CodeVariable variable; 169 170 public DeclareVariableStatement(CodeVariable var) { 171 super(null); 172 variable = var; 173 } 174 175 public Object getMetaObject() { 176 return variable; 177 } 178 179 public CodeExpression[] getStatementParameters() { 180 return CodeStructure.EMPTY_PARAMS; 181 } 182 183 public String getJavaCodeString(String parentStr, String [] paramsStr) { 184 StringBuffer buf = new StringBuffer (); 185 int type = variable.getType(); 186 187 if ((type & CodeVariable.SCOPE_MASK) == CodeVariable.FIELD) { 188 switch (type & CodeVariable.ACCESS_MODIF_MASK) { 189 case CodeVariable.PUBLIC: 190 buf.append("public "); break; 192 case CodeVariable.PRIVATE: 193 buf.append("private "); break; 195 case CodeVariable.PROTECTED: 196 buf.append("protected "); break; 198 } 199 200 if ((type & CodeVariable.STATIC) == CodeVariable.STATIC) 201 buf.append("static "); 203 if ((type & CodeVariable.FINAL) == CodeVariable.FINAL) 204 buf.append("final "); 206 if ((type & CodeVariable.TRANSIENT) == CodeVariable.TRANSIENT) 207 buf.append("transient "); 209 if ((type & CodeVariable.VOLATILE) == CodeVariable.VOLATILE) 210 buf.append("volatile "); } 212 else { if ((type & CodeVariable.FINAL) == CodeVariable.FINAL) 214 buf.append("final "); } 216 217 buf.append(getSourceClassName(variable.getDeclaredType())); 218 buf.append(" "); buf.append(variable.getName()); 220 buf.append(";"); 222 return buf.toString(); 223 } 224 } 225 226 229 static final class ConstructorOrigin implements CodeExpressionOrigin { 230 private Constructor constructor; 231 private CodeExpression[] parameters; 232 233 public ConstructorOrigin(Constructor ctor, CodeExpression[] params) { 234 constructor = ctor; 235 parameters = params != null ? params : CodeStructure.EMPTY_PARAMS; 236 } 237 238 public Class getType() { 239 return constructor.getDeclaringClass(); 240 } 241 242 public CodeExpression getParentExpression() { 243 return null; 244 } 245 246 public Object getMetaObject() { 247 return constructor; 248 } 249 250 public Object getValue() { 251 Object [] params = new Object [parameters.length]; 252 for (int i=0; i < params.length; i++) { 253 CodeExpressionOrigin paramOrigin = parameters[i].getOrigin(); 254 Object value = paramOrigin.getValue(); 255 Class type = paramOrigin.getType(); 256 if (value == null && type.isPrimitive()) 257 return null; 258 params[i] = value; 259 } 260 261 try { 262 return constructor.newInstance(params); 263 } 264 catch (Exception ex) { 265 org.openide.ErrorManager.getDefault().notify(org.openide.ErrorManager.INFORMATIONAL, ex); 266 return null; 267 } 268 } 269 270 public CodeExpression[] getCreationParameters() { 271 return parameters; 272 } 273 274 public String getJavaCodeString(String parentStr, String [] paramsStr) { 275 StringBuffer buf = new StringBuffer (); 276 277 buf.append("new "); buf.append(constructor.getName()); 279 buf.append("("); 281 for (int i=0; i < paramsStr.length; i++) { 282 buf.append(paramsStr[i]); 283 if (i+1 < parameters.length) 284 buf.append(", "); } 286 287 buf.append(")"); 289 return buf.toString(); 290 } 291 } 292 293 static final class MethodOrigin implements CodeExpressionOrigin { 294 private CodeExpression parentExpression; 295 private Method creationMethod; 296 private CodeExpression[] parameters; 297 298 public MethodOrigin(CodeExpression parent, 299 Method m, 300 CodeExpression[] params) 301 { 302 parentExpression = parent; 303 creationMethod = m; 304 parameters = params != null ? params : CodeStructure.EMPTY_PARAMS; 305 } 306 307 public Class getType() { 308 return creationMethod.getReturnType(); 309 } 310 311 public CodeExpression getParentExpression() { 312 return parentExpression; 313 } 314 315 public Object getMetaObject() { 316 return creationMethod; 317 } 318 319 public Object getValue() { 320 return null; 321 } 322 323 public CodeExpression[] getCreationParameters() { 324 return parameters; 325 } 326 327 public String getJavaCodeString(String parentStr, String [] paramsStr) { 328 StringBuffer buf = new StringBuffer (); 329 330 if (parentExpression != null) { 331 if (parentStr != null && !parentStr.equals("")) { 332 buf.append(parentStr); 333 buf.append("."); } 335 } 336 else { buf.append(getSourceClassName(creationMethod.getDeclaringClass())); 338 buf.append("."); } 340 341 buf.append(creationMethod.getName()); 342 buf.append("("); 344 for (int i=0; i < paramsStr.length; i++) { 345 buf.append(paramsStr[i]); 346 if (i+1 < parameters.length) 347 buf.append(", "); } 349 350 buf.append(")"); 352 return buf.toString(); 353 } 354 } 355 356 static final class FieldOrigin implements CodeExpressionOrigin { 357 private CodeExpression parentExpression; 358 private Field originField; 359 360 public FieldOrigin(CodeExpression parent, Field f) { 361 parentExpression = parent; 362 originField = f; 363 } 364 365 public Class getType() { 366 return originField.getType(); 367 } 368 369 public CodeExpression getParentExpression() { 370 return parentExpression; 371 } 372 373 public Object getMetaObject() { 374 return originField; 375 } 376 377 public Object getValue() { 378 return null; 379 } 380 381 public CodeExpression[] getCreationParameters() { 382 return CodeStructure.EMPTY_PARAMS; 383 } 384 385 public String getJavaCodeString(String parentStr, String [] paramsStr) { 386 StringBuffer buf = new StringBuffer (); 387 388 if (parentExpression != null) { 389 if (parentStr != null && !parentStr.equals("")) { 390 buf.append(parentStr); 391 buf.append("."); } 393 } 394 else { buf.append(getSourceClassName(originField.getDeclaringClass())); 396 buf.append("."); } 398 399 buf.append(originField.getName()); 400 401 return buf.toString(); 402 } 403 } 404 405 static final class ValueOrigin implements CodeExpressionOrigin { 406 private Class expressionType; 407 private Object expressionValue; 408 private String javaString; 409 410 public ValueOrigin(Class type, Object value, String javaStr) { 411 expressionType = type; 412 expressionValue = value; 413 javaString = javaStr; 414 } 415 416 public Class getType() { 417 return expressionType; 418 } 419 420 public CodeExpression getParentExpression() { 421 return null; 422 } 423 424 public Object getMetaObject() { 425 return null; 426 } 427 428 public Object getValue() { 429 return expressionValue; 430 } 431 432 public CodeExpression[] getCreationParameters() { 433 return CodeStructure.EMPTY_PARAMS; 434 } 435 436 public String getJavaCodeString(String parentStr, String [] paramsStr) { 437 return javaString; 438 } 439 } 440 441 444 static final class DefaultCodeGroup implements CodeGroup { 446 447 private List statements = new ArrayList(); 448 449 public void addStatement(CodeStatement statement) { 450 statements.add(statement); 451 } 452 453 public void addStatement(int index, CodeStatement statement) { 454 statements.add(index, statement); 455 } 456 457 public void addGroup(CodeGroup group) { 458 statements.add(group); 459 } 460 461 public void addGroup(int index, CodeGroup group) { 462 statements.add(index, group); 463 } 464 465 public CodeStatement getStatement(int index) { 466 Object obj = statements.get(index); 467 if (obj instanceof CodeStatement) 468 return (CodeStatement) obj; 469 if (obj instanceof CodeGroup) 470 return ((CodeGroup)obj).getStatement(0); 471 return null; 472 } 473 474 public int indexOf(Object object) { 475 return statements.indexOf(object); 476 } 477 478 public void remove(Object object) { 479 statements.remove(object); 480 } 481 482 public void remove(int index) { 483 statements.remove(index); 484 } 485 486 public void removeAll() { 487 statements.clear(); 488 } 489 490 public Iterator getStatementsIterator() { 491 return new StatementsIterator(); 492 } 493 494 class StatementsIterator implements Iterator { 495 int index = 0; 496 int count = statements.size(); 497 Iterator subIter; 498 499 public boolean hasNext() { 500 if (subIter != null) { 501 if (subIter.hasNext()) 502 return true; 503 subIter = null; 504 index++; 505 } 506 507 while (index < count) { 508 Object item = statements.get(index); 509 if (item instanceof CodeGroup) { 510 subIter = ((CodeGroup)item).getStatementsIterator(); 511 if (subIter.hasNext()) 512 return true; 513 subIter = null; 514 } 515 else if (item instanceof CodeStatement) 516 return true; 517 index++; 518 } 519 520 return false; 521 } 522 523 public Object next() { 524 if (!hasNext()) 525 throw new NoSuchElementException(); 526 527 return subIter != null ? subIter.next() : 528 statements.get(index++); 529 } 530 531 public void remove() { 532 throw new UnsupportedOperationException (); 533 } 534 } 535 } 536 } 537 | Popular Tags |