1 7 package com.bull.eclipse.jonas; 8 9 import java.lang.reflect.Array ; 10 import java.lang.reflect.InvocationTargetException ; 11 import java.util.ArrayList ; 12 import java.util.HashMap ; 13 import java.util.Iterator ; 14 import java.util.Vector ; 15 16 import org.eclipse.core.resources.IProject; 17 import org.eclipse.core.resources.ResourcesPlugin; 18 import org.eclipse.core.runtime.IProgressMonitor; 19 import org.eclipse.jdt.core.Flags; 20 import org.eclipse.jdt.core.ICompilationUnit; 21 import org.eclipse.jdt.core.IMethod; 22 import org.eclipse.jdt.core.IPackageFragment; 23 import org.eclipse.jdt.core.IType; 24 import org.eclipse.jdt.core.JavaCore; 25 import org.eclipse.jdt.core.Signature; 26 import org.eclipse.jdt.core.ToolFactory; 27 import org.eclipse.jdt.core.formatter.CodeFormatter; 28 import org.eclipse.jface.dialogs.ProgressMonitorDialog; 29 import org.eclipse.jface.operation.IRunnableWithProgress; 30 import org.eclipse.jface.text.Document; 31 import org.eclipse.jface.text.IDocument; 32 import org.eclipse.jface.text.Region; 33 import org.eclipse.jface.text.formatter.ContentFormatter; 34 import org.eclipse.jface.text.formatter.ContextBasedFormattingStrategy; 35 import org.eclipse.jface.text.formatter.FormattingContextProperties; 36 import org.eclipse.jface.text.formatter.IFormattingStrategy; 37 import org.eclipse.jface.text.source.SourceViewerConfiguration; 38 import org.eclipse.jface.viewers.IStructuredContentProvider; 39 import org.eclipse.jface.viewers.LabelProvider; 40 import org.eclipse.jface.viewers.Viewer; 41 import org.eclipse.text.edits.TextEdit; 42 import org.eclipse.ui.IWorkbenchWindow; 43 import org.eclipse.ui.PlatformUI; 44 import org.eclipse.ui.dialogs.ListSelectionDialog; 45 46 import com.bull.eclipse.jonas.utils.UtilServicesClass; 47 48 50 public class EjbGenInterfaces { 51 private ICompilationUnit cu; 52 private String [] szMethode_ = 53 { 54 "ejbCreate", 55 "ejbPostCreate", 56 "ejbRemove", 57 "ejbLoad", 58 "ejbStore", 59 "ejbActivate", 60 "ejbPassivate", 61 "ejbPassivate", 62 "setEntityContext", 63 "unsetEntityContext", 64 "setSessionContext", 65 "unsetSessionContext", 66 "afterBegin", 67 "afterCompletion", 68 "beforeCompletion", 69 "ejbFind" }; 70 71 public void run(ICompilationUnit cu) throws Exception { 72 this.cu = cu; 74 75 final String s = cu.getElementName().replaceAll("EJB.java", ""); 77 final String sEJB = cu.getElementName().replaceAll(".java", ""); 78 79 IPackageFragment pkg = (IPackageFragment) cu.getParent(); 81 String jonasprojName = cu.getParent().getJavaProject().getElementName(); 82 83 final IProject proj = 85 ResourcesPlugin.getWorkspace().getRoot().getProject(jonasprojName); 86 87 UtilServicesClass.saveFiles(); 88 89 final IMethod[] sel = getMethodesDialog(sEJB); 90 if (sel == null) 91 return; 92 93 96 97 IRunnableWithProgress worker = new IRunnableWithProgress() { 98 public void run(IProgressMonitor monitor) 99 throws InvocationTargetException { 100 try { 101 102 monitor.setTaskName( 103 JonasPluginResources.GEN_INTERFACES_TITLE); 104 105 monitor.beginTask( 107 JonasPluginResources.GEN_INTERFACES_MSG_LOCAL, 108 5); 109 updEjbLocalFile(s + "Local", sel); 110 monitor.worked(1); 111 112 monitor.setTaskName( 113 JonasPluginResources.GEN_INTERFACES_MSG_REMOTE); 114 updEjbLocalFile(s, sel); 115 monitor.worked(2); 116 117 monitor.setTaskName( 119 JonasPluginResources.GEN_INTERFACES_MSG_HOME); 120 updEjbHomeFile(s + "Home"); 121 monitor.worked(3); 122 123 monitor.setTaskName( 124 JonasPluginResources.GEN_INTERFACES_MSG_LOCALH); 125 updEjbHomeFile(s + "LocalHome"); 126 monitor.worked(4); 127 128 monitor.setTaskName( 129 JonasPluginResources.GEN_INTERFACES_MSG_LOCALH); 130 updEjbBeanFile(sEJB); 131 monitor.worked(5); 132 133 } catch (Exception e) { 134 System.out.println("Excepion : " + e.getMessage()); 135 } finally { 136 monitor.done(); 137 } 138 } 139 }; 140 141 144 try { 145 new ProgressMonitorDialog( 146 PlatformUI 147 .getWorkbench() 148 .getActiveWorkbenchWindow() 149 .getShell()) 150 .run( 151 false, 152 false, 153 worker); 154 } catch (InterruptedException e) { 155 e.printStackTrace(); 156 } catch (InvocationTargetException e) { 157 e.printStackTrace(); 158 } 159 160 } 161 162 private void updEjbBeanFile(String sUnit) throws Exception { 163 boolean bFound = false; 164 165 IPackageFragment pkg = (IPackageFragment) cu.getParent(); 166 ICompilationUnit unitA = null; 168 try { 169 unitA = pkg.getCompilationUnit(sUnit + ".java"); 170 if (!unitA.getResource().exists()) 171 return; 172 173 } catch (Exception ex) { 174 return; 176 } 177 178 IType itCible = unitA.getType(sUnit); 179 IMethod[] ime = itCible.getMethods(); 181 182 int iCount = 0; 183 for (int j = 0; j < ime.length; j++) { 184 if (ime[j].getElementName().equals("ejbPostCreate")) 185 iCount++; 186 } 187 188 int crCount = 0; 189 for (int j = 0; j < ime.length; j++) { 190 if (ime[j].getElementName().equals("ejbCreate")) 191 crCount++; 192 } 193 194 if (iCount == 0) { 198 for (int j = 0; j < ime.length; j++) { 200 if (ime[j].getElementName().equals("ejbCreate")) { 201 itCible.createMethod( 203 MakeEJBPostCreateMethode(ime[j]), 204 null, 205 true, 206 null); 207 208 } 209 } 210 } 211 212 226 CodeFormat(unitA); 228 229 } 230 231 private void updEjbLocalFile(String sUnit, IMethod[] imeSelected) 232 throws Exception { 233 234 boolean bFound = false; 235 236 IPackageFragment pkg = (IPackageFragment) cu.getParent(); 237 ICompilationUnit unitA = null; 239 try { 240 unitA = pkg.getCompilationUnit(sUnit + ".java"); 241 if (!unitA.getResource().exists()) 242 return; 243 244 } catch (Exception ex) { 245 return; 247 } 248 249 IType itCible = unitA.getType(sUnit); 250 IMethod[] ime = itCible.getMethods(); 251 int nbMethodesInFacade = ime.length; 252 int nbMethodesInBean = imeSelected.length; 253 ArrayList candidateMeth = new ArrayList (); 254 int h = 0; 255 256 for (int j = 0; j < nbMethodesInBean; j++) { 257 for (h = 0; h < nbMethodesInFacade; h++) { 258 if ((ime[h].getElementName().equals(imeSelected[j] 259 .getElementName())) 260 && (ime[h].getSignature().equals(imeSelected[j] 261 .getSignature()))) { 262 System.out.println("Méthode déjà dans la façade = " 263 + imeSelected[j].getElementName()); 264 break; 265 } 266 } 267 if ((h == nbMethodesInFacade) && (imeSelected[j] != null)) 268 candidateMeth.add(imeSelected[j]); 269 } 270 271 for (int j = 0; j < nbMethodesInFacade; j++) { 272 for (h = 0; h < nbMethodesInBean; h++) { 273 if ((ime[j].getElementName().equals(imeSelected[h] 274 .getElementName()) 275 && (ime[j].getSignature().equals(imeSelected[h] 276 .getSignature())))) { 277 System.out.println("Méthode déjà dans la façade = " 278 + imeSelected[j].getElementName()); 279 break; 280 } 281 } 282 if ((h == nbMethodesInBean) && (ime[j] != null)) 283 ime[j].delete(true,null); 284 } 285 286 System.out.println("Liste des mèthodes publiques"); 287 IType[] it = cu.getAllTypes(); 288 289 294 306 for (int i = 0; i < candidateMeth.size(); i++) { 309 System.out.println( 310 "liste des methodes retenues : " 311 + ((IMethod)candidateMeth.get(i)).getElementName()); 312 } 313 314 for (int i = 0; i < candidateMeth.size(); i++) { 315 String s = MakeMethode(((IMethod)candidateMeth.get(i)), false, sUnit); 318 System.out.println("ajout meth : " + s); 319 itCible.createMethod(s, null, true, null); 320 321 } 323 } 327 328 private void updEjbHomeFile(String sUnit) throws Exception { 329 330 IPackageFragment pkg = (IPackageFragment) cu.getParent(); 331 332 ICompilationUnit unitA = null; 334 try { 335 unitA = pkg.getCompilationUnit(sUnit + ".java"); 336 if (!unitA.getResource().exists()) 337 return; 338 339 } catch (Exception ex) { 340 return; 342 } 343 344 IType itCible = unitA.getType(sUnit); 345 346 IMethod[] ime = itCible.getMethods(); 348 for (int j = 0; j < ime.length; j++) { 349 if (ime[j].getElementName().startsWith("Find")) 350 continue; 351 if (ime[j].getElementName().startsWith("create")) 352 ime[j].delete(true, null); 353 } 354 355 System.out.println("Liste des mèthodes publiques"); 356 String beanName = cu.getElementName(); 357 Vector listeMethodes = getMethodes(beanName.substring(0,beanName.length()-5),true); 358 for (int j = 0; j < listeMethodes.size(); j++) { 359 360 if (((IMethod)listeMethodes.get(j)).getFlags() != Flags.AccPublic) 362 continue; 363 364 if (((IMethod)listeMethodes.get(j)).getFlags() == Flags.AccAbstract) 366 continue; 367 368 String szMethode = ((IMethod)listeMethodes.get(j)).getElementName(); 369 370 373 if (!szMethode.startsWith("ejbCreate")) 374 continue; 375 376 String s = MakeMethode(((IMethod)listeMethodes.get(j)), true, sUnit); 378 System.out.println("ajout meth : " + s); 379 itCible.createMethod(s, null, true, null); 380 381 } 382 CodeFormat(unitA); 384 } 385 386 private String MakeMethode(IMethod ime, boolean bHome, String EJBName) 387 throws Exception { 388 389 StringBuffer sb = new StringBuffer ("public "); 390 String sig = ime.getReturnType(); 391 if (bHome) 393 sig = EJBName.replaceAll("Home", ""); 394 395 String sName = ime.getElementName(); 396 397 if (sName.startsWith("ejbCreate")) 398 sName = "create"; 399 else if (sName.startsWith("ejbFind")) 400 sName = sName.replaceAll("ejbFind", "Find"); 401 402 sb.append(getType(sig) + " "); 403 sb.append(sName + "("); 404 String [] spars = ime.getParameterNames(); 405 String [] sparstype = ime.getParameterTypes(); 406 for (int k = 0; k < ime.getNumberOfParameters(); k++) { 407 if (k != 0) 408 sb.append(", "); 409 sb.append(getType(sparstype[k])); 410 sb.append(" "); 411 sb.append(spars[k]); 412 } 413 sb.append(")"); 414 HashMap hMap = new HashMap (10); 418 String [] exc = ime.getExceptionTypes(); 419 420 for (int u = 0; u < exc.length; u++) { 421 String s = getType(exc[u]); 422 hMap.put(s, s); 423 } 424 String szRemote = "java.rmi.RemoteException"; 425 String szRemote_ = "RemoteException"; 426 427 if (EJBName.indexOf("Local") != -1 && hMap.get(szRemote) != null) 429 hMap.remove(szRemote); 430 431 if (EJBName.indexOf("Local") != -1 && hMap.get(szRemote_) != null) 432 hMap.remove(szRemote_); 433 434 if (EJBName.indexOf("Local") == -1) { 436 if ((hMap.get(szRemote) == null) && (hMap.get(szRemote_) == null)) 437 hMap.put(szRemote, szRemote); 438 } 439 if (hMap.size() != 0) { 441 sb.append(" throws "); 442 Iterator it = hMap.values().iterator(); 443 while (it.hasNext()) { 444 sb.append((String ) it.next()); 445 if (it.hasNext()) 446 sb.append(", "); 447 } 448 } 449 450 sb.append(";\n\n"); 451 452 return sb.toString(); 453 } 454 455 private String MakeEJBPostCreateMethode(IMethod ime) throws Exception { 456 457 StringBuffer sb = new StringBuffer ("public void ejbPostCreate ("); 458 String [] spars = ime.getParameterNames(); 459 String [] sparstype = ime.getParameterTypes(); 460 for (int k = 0; k < ime.getNumberOfParameters(); k++) { 461 if (k != 0) 462 sb.append(", "); 463 sb.append(getType(sparstype[k])); 464 sb.append(" "); 465 sb.append(spars[k]); 466 } 467 String [] exc = ime.getExceptionTypes(); 468 sb.append(")"); 469 if (exc.length != 0) { 470 sb.append(" throws "); 471 for (int u = 0; u < exc.length; u++) { 472 if (u != 0) 473 sb.append(", "); 474 sb.append(getType(exc[u])); 475 } 476 } 477 478 sb.append(" {}\n\n"); 479 return sb.toString(); 480 481 } 482 483 private void CodeFormat(ICompilationUnit unit) throws Exception { 484 } 505 506 private boolean MethodeToIgnore(String szMethode) { 507 for (int m_ = 0; m_ < szMethode_.length; m_++) { 508 if (szMethode.equalsIgnoreCase(szMethode_[m_])) 509 return true; 510 if (szMethode.startsWith("ejbFind")) 511 return true; 512 513 } 514 return false; 515 } 516 517 private String getType(String s) { 518 519 if (s.equalsIgnoreCase("v")) 520 return "void"; 521 522 if (s.equalsIgnoreCase("b")) 523 return "byte"; 524 if (s.equalsIgnoreCase("[b")) 525 return "byte []"; 526 527 if (s.equalsIgnoreCase("c")) 528 return "char"; 529 if (s.equalsIgnoreCase("[c")) 530 return "char []"; 531 532 if (s.equalsIgnoreCase("d")) 533 return "double"; 534 if (s.equalsIgnoreCase("[d")) 535 return "double []"; 536 537 if (s.equalsIgnoreCase("f")) 538 return "float"; 539 if (s.equalsIgnoreCase("[f")) 540 return "float []"; 541 542 if (s.equalsIgnoreCase("i")) 543 return "int"; 544 if (s.equalsIgnoreCase("[i")) 545 return "int []"; 546 547 if (s.equalsIgnoreCase("j")) 548 return "long"; 549 if (s.equalsIgnoreCase("[j")) 550 return "long []"; 551 552 if (s.equalsIgnoreCase("s")) 553 return "short"; 554 if (s.equalsIgnoreCase("[s")) 555 return "short []"; 556 557 if (s.equalsIgnoreCase("z")) 558 return "boolean"; 559 if (s.equalsIgnoreCase("[z")) 560 return "boolean []"; 561 562 if (s.startsWith("Q")) { 563 return s.substring(1, s.length() - 1); 564 } 565 566 if (s.startsWith("[Q")) { 567 return s.substring(2, s.length() - 1) + " []"; 568 } 569 570 return s; 571 572 } 573 574 private Vector getMethodes(String sUnit, boolean withejbMethods) throws Exception { 575 576 IWorkbenchWindow[] wk = PlatformUI.getWorkbench().getWorkbenchWindows(); 577 IPackageFragment pkg = (IPackageFragment) cu.getParent(); 578 ICompilationUnit unitA = null; 580 ICompilationUnit unitB = null; 581 IType itCibleA = null; 582 IType itCibleB = null; 583 try { 584 unitA = pkg.getCompilationUnit(sUnit + ".java"); 585 itCibleA = unitA.getType(sUnit); 586 String superclass = itCibleA.getSuperclassName(); 587 if ((!"java.lang.Object".equals(superclass)) && (superclass != null) ) { 588 unitB = pkg.getCompilationUnit(superclass + ".java"); 589 itCibleB = unitB.getType(superclass); 590 } 591 if (!unitA.getResource().exists()) 592 return null; 593 594 } catch (Exception ex) { 595 System.out.println("Exception : " + ex.getMessage()); 597 return null; 598 } 599 600 IMethod[] imeA = itCibleA.getMethods(); 602 IMethod[] imeB = null; 603 if (itCibleB != null) 604 imeB = itCibleB.getMethods(); 605 606 Vector vmet = new Vector (10); 607 608 for (int i = 0; i < imeA.length; i++) { 614 System.out.println(" method : " + imeA[i].getElementName()); 615 616 if ((imeA[i].getFlags() & Flags.AccPublic) == 0) 618 continue; 619 if ((imeA[i].getFlags() & Flags.AccAbstract) != 0) 621 continue; 622 623 if (MethodeToIgnore(imeA[i].getElementName()) && !withejbMethods) 625 continue; 626 System.out.println( 627 " candidate method : " + imeA[i].getElementName()); 628 629 String szSign = MakeMethode(imeA[i], false, ""); 630 631 vmet.add(imeA[i]); 632 } 633 634 if (imeB != null) 635 for (int i = 0; i < imeB.length; i++) { 636 System.out.println(" method : " + imeB[i].getElementName()); 637 638 if ((imeB[i].getFlags() & Flags.AccPublic) == 0) 640 continue; 641 if ((imeB[i].getFlags() & Flags.AccAbstract) != 0) 643 continue; 644 645 if (MethodeToIgnore(imeB[i].getElementName()) && !withejbMethods) 647 continue; 648 System.out.println(" candidate method : " 649 + imeB[i].getElementName()); 650 651 String szSign = MakeMethode(imeB[i], false, ""); 652 653 vmet.add(imeB[i]); 654 } 655 return vmet; 656 } 657 658 659 private IMethod[] getMethodesDialog(String sUnit) throws Exception { 663 664 Vector vmet = getMethodes(sUnit,false); 665 if (vmet.size() == 0) 667 return null; 668 669 final Object [] szMethfull = vmet.toArray(); 670 672 ListSelectionDialog dlg = 673 new ListSelectionDialog( 674 PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), 675 new Object (), 676 new IStructuredContentProvider() { 677 678 public void dispose() { 679 } 680 681 public Object [] getElements(Object inputElement) { 682 683 return szMethfull; 684 } 685 686 public void inputChanged(Viewer viewer1, Object obj, Object obj1) { 687 int i = 0; 688 } 689 690 }, new LabelProvider(), "Select methods from BEAN"); 691 692 dlg.setTitle("Preselect Methods for Remote/Local Interfaces"); 693 dlg.open(); 694 695 final Object [] results_upd = (Object []) dlg.getResult(); 696 int ret = dlg.getReturnCode(); 697 if (ret == 1) 699 return null; 700 if (results_upd == null) 701 return null; 702 if (results_upd.length == 0) 703 return null; 704 705 Vector imeRet = new Vector (10); 706 707 for (int i = 0; i < results_upd.length; i++) { 708 imeRet.add(results_upd[i]); 716 } 717 718 return (IMethod[]) imeRet.toArray(new IMethod[0]); 719 720 } 721 722 class myMeth { 724 private String szSign = null; 725 private int iOrder = 0; 726 myMeth(String szSign, int iOrder) { 727 this.szSign = szSign; 728 this.iOrder = iOrder; 729 } 730 public int getOrder() { 731 return this.iOrder; 732 } 733 public String getMethode() { 734 return this.szSign; 735 } 736 } 737 } 738 | Popular Tags |