1 19 package org.netbeans.test.j2ee.refactoring; 20 21 import java.io.*; 22 import java.io.File ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import org.netbeans.api.mdr.MDRepository; 26 import org.netbeans.jmi.javamodel.JavaClass; 27 import org.netbeans.jmi.javamodel.Type; 28 import org.netbeans.jmi.javamodel.TypeClass; 29 import org.netbeans.jmi.javamodel.TypeParameter; 30 import org.netbeans.jmi.javamodel.UnresolvedClass; 31 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 32 import org.netbeans.modules.refactoring.classpath.RefactoringClassPathImplementation; 33 import org.netbeans.modules.j2ee.ddloaders.multiview.EjbJarMultiViewDataObject; 34 import org.openide.ErrorManager; 35 import org.openide.cookies.SaveCookie; 36 37 import javax.swing.*; 38 39 40 public class Utility { 41 42 public static void beginTrans(boolean writeAccess) { 43 getDefaultRepository().beginTrans(writeAccess); 44 } 45 46 public static void endTrans(boolean rollback) { 47 getDefaultRepository().endTrans(rollback); 48 } 49 50 public static void endTrans() { 51 getDefaultRepository().endTrans(); 52 } 53 54 public static MDRepository getDefaultRepository() { 55 return JavaMetamodel.getDefaultRepository(); 56 } 57 58 public static void prepareTest() { 59 beginTrans(true); 60 JavaMetamodel.getManager().setClassPath(RefactoringClassPathImplementation.getDefault()); 62 } 63 64 public static void finishTest() { 65 endTrans(false); 66 } 67 68 public static File getFile(File classPathDataDir, String fileName) { 69 return new File (classPathDataDir, fileName); 70 } 71 72 75 public static JavaClass findClass(String s) { 76 JavaClass result; 77 int i = 20; 78 do { 79 result = (JavaClass) JavaMetamodel.getManager().getDefaultExtent().getType().resolve(s); 80 if (result instanceof UnresolvedClass) { 81 try { 82 Thread.sleep(1000); 83 } catch (InterruptedException e) { 84 endTrans(); 85 e.printStackTrace(); 86 return null; 87 } 88 } 89 i--; 90 } while ((result instanceof UnresolvedClass) && i > 0); 91 if (result instanceof UnresolvedClass) { 92 throw new IllegalStateException ("Class " + s + " not found."); 93 } 94 return result; 95 } 96 97 100 public static Type findType(String s, TypeClass typeProxy, JavaClass jc) { 101 Type result; 102 int i = 20; 103 do { 104 result = typeProxy.resolve(s); 105 if (result instanceof UnresolvedClass) { 106 List l = jc.getTypeParameters(); 107 for (Iterator it = l.iterator(); it.hasNext();) { 108 TypeParameter tp = (TypeParameter) (it.next()); 109 if (tp.getName().equals(s)) { 110 return tp; 111 } 112 } 113 try { 114 Thread.sleep(1000); 115 } catch (InterruptedException e) { 116 endTrans(); 117 e.printStackTrace(); 118 return null; 119 } 120 } 121 i--; 122 } while ((result instanceof UnresolvedClass) && i > 0); 123 if (result instanceof UnresolvedClass) { 124 throw new IllegalStateException ("Type " + s + " not found."); 125 } 126 return result; 127 } 128 129 public static void copyFile(File src, File trg) throws Exception { 130 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src)); 131 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(trg)); 132 byte[] buffer = new byte[1024]; 133 int length = 1; 134 while (length > 0) { 135 length = bis.read(buffer); 136 if (length <= 0) { 137 break; 138 } 139 bos.write(buffer, 0, length); 140 } 141 bis.close(); 142 bos.close(); 143 } 144 145 public static void waitForAWTDispatchThread() { 146 final boolean[] finished = new boolean[]{false}; 147 SwingUtilities.invokeLater(new Runnable () { 148 public void run() { 149 finished[0] = true; 150 } 151 }); 152 while (!finished[0]) { 153 try { 154 Thread.sleep(200); 155 } catch (Exception e) { 156 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 157 } 158 } 159 } 160 161 public static boolean saveDD(EjbJarMultiViewDataObject ddo) { 162 for (int i = 0; i < 200; i++) { 163 waitForAWTDispatchThread(); 164 SaveCookie saveCookie = (SaveCookie) ddo.getCookie(SaveCookie.class); 165 if (saveCookie != null) { 166 try { 167 saveCookie.save(); 168 } catch (IOException e) { 169 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 170 } 171 return true; 172 } 173 try { 174 Thread.sleep(200); 175 } catch (Exception e) { 176 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e); 177 } 178 } 179 return false; 180 } 181 182 } 183 | Popular Tags |