KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > apt > core > internal > generatedfile > CompilationUnitHelper


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 /*******************************************************************************
13  * Copyright (c) 2006, 2007 BEA Systems, Inc.
14  * All rights reserved. This program and the accompanying materials
15  * are made available under the terms of the Eclipse Public License v1.0
16  * which accompanies this distribution, and is available at
17  * http://www.eclipse.org/legal/epl-v10.html
18  *
19  * Contributors:
20  * wharley@bea.com - refactored, and reinstated reconcile-time type gen
21  *******************************************************************************/

22
23 /**
24  * Helper utilities to create, modify, save, and discard compilation units and their
25  * working copies. Basically, calls to ICompilationUnit.
26  * These are encapsulated here not so much because the code is complex, but rather to
27  * make it very clear what the algorithms are (as opposed to distributing these calls
28  * throughout other code). All calls to the Java Model involved in generating types
29  * should go through methods here.
30  */

31 public class CompilationUnitHelper
32 {
33
34     /**
35      * Update the contents of a working copy and commit it to disk.
36      * @throws JavaModelException
37      */

38     public void commitNewContents(ICompilationUnit wc, String JavaDoc contents, IProgressMonitor monitor) throws JavaModelException {
39         IBuffer b = wc.getBuffer();
40         b.setContents(contents);
41         wc.commitWorkingCopy(true, monitor);
42     }
43     
44     /**
45      * Get an in-memory working copy. This does not create the type or package on disk.
46      * <p>
47      * The methods called by this routine are all read-only with respect to the resource
48      * tree, so they do not require taking any scheduling locks. Therefore we think
49      * it's safe to call this method within a synchronized block.
50      * @param typeName the fully qualified type name, e.g., "foo.Bar"
51      * @param root the package fragment root within which the type will be created
52      * @return a working copy that is ready to be modified. The working copy may not
53      * yet be backed by a file on disk.
54      */

55     public ICompilationUnit getWorkingCopy(String JavaDoc typeName, IPackageFragmentRoot root)
56     {
57         String JavaDoc[] names = parseTypeName(typeName);
58         String JavaDoc pkgName = names[0];
59         String JavaDoc 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); //$NON-NLS-1$
69
return null;
70         }
71         if (AptPlugin.DEBUG_GFM) AptPlugin.trace(
72                 "Created working copy: root = " + //$NON-NLS-1$
73
root + ",\n\tfragment = " + pkgFragment + ",\n\twc = " + workingCopy); //$NON-NLS-1$ //$NON-NLS-2$
74
return workingCopy;
75     }
76
77     /**
78      * Discard a working copy, ie, remove it from memory. Each call to
79      * {@link #getWorkingCopy(String typeName, IPackageFragmentRoot root)}
80      * must be balanced with exactly one call to this method.
81      */

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()); //$NON-NLS-1$
88
try {
89             wc.discardWorkingCopy();
90         } catch (JavaModelException e) {
91             AptPlugin.log(e, "Unable to discard working copy: " + wc.getElementName()); //$NON-NLS-1$
92
}
93     }
94     
95     /**
96      * Update the contents of an existing working copy.
97      *
98      * @param contents
99      * the new text.
100      * @param reconcile
101      * true if the changes should be reconciled.
102      * @return true if the contents were modified as a result.
103      */

104     public boolean updateWorkingCopyContents(String JavaDoc 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()); //$NON-NLS-1$
113
return false;
114         }
115         // We need to do this diff to tell our caller whether this is a modification.
116
// It's not obvious to me that the caller actually needs to know, so
117
// this might just be a needless performance sink. - WHarley 11/06
118
modified = !contents.equals(b.getContents());
119
120         b.setContents(contents);
121         if (AptPlugin.DEBUG_GFM_MAPS) AptPlugin.trace(
122                 "updated contents of working copy: " //$NON-NLS-1$
123
+ wc.getElementName() + " modified = " + modified); //$NON-NLS-1$
124
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()); //$NON-NLS-1$
129
}
130         }
131         return modified;
132     }
133
134     /**
135      * Create a package fragment on disk.
136      * @param pkgName the name of the package.
137      * @param root the package fragment root under which to place the package.
138      * @param progressMonitor
139      * @return a package fragment, or null if there was an error.
140      */

141     public IPackageFragment createPackageFragment(String JavaDoc 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); //$NON-NLS-1$
148
}
149         
150         return pkgFrag;
151     }
152
153     /**
154      * Given a fully qualified type name, generate the package name and the local filename
155      * including the extension. For instance, type name <code>foo.bar.Baz</code> is
156      * turned into package <code>foo.bar</code> and filename <code>Baz.java</code>.
157      *
158      * @param qualifiedName
159      * a fully qualified type name
160      * @return a String array containing {package name, filename}
161      */

162     private String JavaDoc[] parseTypeName(String JavaDoc qualifiedName) {
163         String JavaDoc[] names = new String JavaDoc[2];
164         String JavaDoc pkgName;
165         String JavaDoc 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"; //$NON-NLS-1$
172
}
173         else
174         {
175             pkgName = ""; //$NON-NLS-1$
176
fname = qualifiedName + ".java"; //$NON-NLS-1$
177
}
178         names[0] = pkgName;
179         names[1] = fname;
180         return names;
181     }
182
183 }
184
Popular Tags