1 package org.eclipse.jdt.apt.core.internal.generatedfile; 2 3 import org.eclipse.core.runtime.IProgressMonitor; 4 import org.eclipse.jdt.apt.core.internal.AptPlugin; 5 import org.eclipse.jdt.core.IBuffer; 6 import org.eclipse.jdt.core.ICompilationUnit; 7 import org.eclipse.jdt.core.IPackageFragment; 8 import org.eclipse.jdt.core.IPackageFragmentRoot; 9 import org.eclipse.jdt.core.JavaModelException; 10 import org.eclipse.jdt.core.WorkingCopyOwner; 11 12 22 23 31 public class CompilationUnitHelper 32 { 33 34 38 public void commitNewContents(ICompilationUnit wc, String contents, IProgressMonitor monitor) throws JavaModelException { 39 IBuffer b = wc.getBuffer(); 40 b.setContents(contents); 41 wc.commitWorkingCopy(true, monitor); 42 } 43 44 55 public ICompilationUnit getWorkingCopy(String typeName, IPackageFragmentRoot root) 56 { 57 String [] names = parseTypeName(typeName); 58 String pkgName = names[0]; 59 String fname = names[1]; 60 61 IPackageFragment pkgFragment; 62 ICompilationUnit workingCopy = null; 63 try { 64 pkgFragment = root.getPackageFragment(pkgName ); 65 workingCopy = pkgFragment.getCompilationUnit(fname); 66 workingCopy.becomeWorkingCopy(null); 67 } catch (JavaModelException e) { 68 AptPlugin.log(e, "Unable to become working copy: " + typeName); return null; 70 } 71 if (AptPlugin.DEBUG_GFM) AptPlugin.trace( 72 "Created working copy: root = " + root + ",\n\tfragment = " + pkgFragment + ",\n\twc = " + workingCopy); return workingCopy; 75 } 76 77 82 public void discardWorkingCopy(ICompilationUnit wc) 83 { 84 if (null == wc) 85 return; 86 if (AptPlugin.DEBUG_GFM) AptPlugin.trace( 87 "discarding working copy: " + wc.getElementName()); try { 89 wc.discardWorkingCopy(); 90 } catch (JavaModelException e) { 91 AptPlugin.log(e, "Unable to discard working copy: " + wc.getElementName()); } 93 } 94 95 104 public boolean updateWorkingCopyContents(String contents, ICompilationUnit wc, 105 WorkingCopyOwner wcOwner, boolean reconcile) 106 { 107 boolean modified = true; 108 IBuffer b = null; 109 try { 110 b = wc.getBuffer(); 111 } catch (JavaModelException e) { 112 AptPlugin.log(e, "Unable to get buffer for working copy: " + wc.getElementName()); return false; 114 } 115 modified = !contents.equals(b.getContents()); 119 120 b.setContents(contents); 121 if (AptPlugin.DEBUG_GFM_MAPS) AptPlugin.trace( 122 "updated contents of working copy: " + wc.getElementName() + " modified = " + modified); if (reconcile && modified) { 125 try { 126 wc.reconcile(ICompilationUnit.NO_AST, true, wcOwner, null); 127 } catch (JavaModelException e) { 128 AptPlugin.log(e, "Unable to reconcile generated type: " + wc.getElementName()); } 130 } 131 return modified; 132 } 133 134 141 public IPackageFragment createPackageFragment(String pkgName, IPackageFragmentRoot root, IProgressMonitor progressMonitor) { 142 IPackageFragment pkgFrag = null; 143 try { 144 pkgFrag = root.createPackageFragment(pkgName, true, 145 progressMonitor); 146 } catch (JavaModelException e) { 147 AptPlugin.log(e, "Unable to create package fragment for package " + pkgName); } 149 150 return pkgFrag; 151 } 152 153 162 private String [] parseTypeName(String qualifiedName) { 163 String [] names = new String [2]; 164 String pkgName; 165 String fname; 166 int idx = qualifiedName.lastIndexOf( '.' ); 167 if ( idx > 0 ) 168 { 169 pkgName = qualifiedName.substring( 0, idx ); 170 fname = 171 qualifiedName.substring(idx + 1, qualifiedName.length()) + ".java"; } 173 else 174 { 175 pkgName = ""; fname = qualifiedName + ".java"; } 178 names[0] = pkgName; 179 names[1] = fname; 180 return names; 181 } 182 183 } 184 | Popular Tags |