KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 2007 BEA Systems, Inc.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * wharley@bea.com - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.jdt.apt.core.internal.generatedfile;
13
14 import java.util.ArrayList JavaDoc;
15
16 import org.eclipse.core.resources.IFolder;
17 import org.eclipse.core.runtime.IPath;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.jdt.apt.core.internal.AptPlugin;
20 import org.eclipse.jdt.core.IClasspathAttribute;
21 import org.eclipse.jdt.core.IClasspathEntry;
22 import org.eclipse.jdt.core.IJavaProject;
23 import org.eclipse.jdt.core.JavaCore;
24 import org.eclipse.jdt.core.JavaModelException;
25
26 /**
27  * Utilities to ensure the generated source folder is (or is not) on the
28  * Java build path as appropriate.
29  */

30 public class ClasspathUtil {
31
32     /**
33      * Given a java project, this function will determine if the specified
34      * folder is a source folder of the java project.
35      *
36      * @param jp - the java project
37      * @param folder - the folder that you want to see if it is a classpath entry for the java project
38      * @return the IClasspathEntry corresponding to folder, or null if none was found.
39      * @throws JavaModelException
40      */

41     public static IClasspathEntry findProjectSourcePath( IJavaProject jp, IFolder folder )
42         throws JavaModelException
43     {
44         IClasspathEntry[] cp = jp.getRawClasspath();
45         IClasspathEntry searchingFor =
46             JavaCore.newSourceEntry(folder.getFullPath());
47         IPath searchingForPath = searchingFor.getPath();
48         for (int i = 0; i < cp.length; i++)
49         {
50             if (cp[i].getPath().equals( searchingForPath ))
51                 return cp[i];
52         }
53         return null;
54     }
55
56     /**
57      * Does the classpath contain the specified path?
58      * @param jp if non-null, get this project's classpath and ignore cp
59      * @param cp if non-null, use this classpath and ignore jp
60      * @param path the entry to look for on the classpath
61      * @param progressMonitor
62      * @return true if classpath contains the path specified.
63      * @throws JavaModelException
64      */

65     public static boolean doesClasspathContainEntry(
66             IJavaProject jp,
67             IClasspathEntry[] cp,
68             IPath path,
69             IProgressMonitor progressMonitor)
70         throws JavaModelException
71     {
72         if( cp == null )
73             cp = jp.getRawClasspath();
74         for (int i = 0; i < cp.length; i++)
75         {
76             if (cp[i].getPath().equals( path ))
77             {
78                 return true;
79             }
80         }
81         return false;
82     }
83     
84     /**
85      * removes a classpath entry from the project
86      */

87     public static void removeFromProjectClasspath( IJavaProject jp, IFolder folder, IProgressMonitor progressMonitor )
88         throws JavaModelException
89     {
90         IClasspathEntry[] cp = jp.getRawClasspath();
91         IPath workspaceRelativePath = folder.getFullPath();
92         boolean found = doesClasspathContainEntry(jp, cp, workspaceRelativePath, progressMonitor);
93         
94         if( found ){
95             IPath projectRelativePath = folder.getProjectRelativePath().addTrailingSeparator();
96     
97             // remove entries that are for the specified folder, account for
98
// multiple entries, and clean up any exclusion entries to the
99
// folder being removed.
100
int j = 0;
101             for ( int i=0; i<cp.length; i++ )
102             {
103                 if (! cp[i].getPath().equals( workspaceRelativePath ) )
104                 {
105                 
106                     // see if we added the generated source dir as an exclusion pattern to some other entry
107
IPath[] oldExclusions = cp[i].getExclusionPatterns();
108                     int m = 0;
109                     for ( int k = 0; k < oldExclusions.length; k++ )
110                     {
111                         if ( !oldExclusions[k].equals( projectRelativePath ) )
112                         {
113                             oldExclusions[m] = oldExclusions[k];
114                             m++;
115                         }
116                     }
117                     
118                     if ( oldExclusions.length == m )
119                     {
120                         // no exclusions changed, so we do't need to create a new entry
121
cp[j] = cp[i];
122                     }
123                     else
124                     {
125                         // we've removed some exclusion, so create a new entry
126
IPath[] newExclusions = new IPath[ m ];
127                         System.arraycopy( oldExclusions, 0, newExclusions, 0, m );
128                         cp[j] = JavaCore.newSourceEntry( cp[i].getPath(), cp[i].getInclusionPatterns(), newExclusions, cp[i].getOutputLocation(), cp[i].getExtraAttributes() );
129                     }
130                     
131                     j++;
132                 }
133             }
134             
135             // now copy updated classpath entries into new array
136
IClasspathEntry[] newCp = new IClasspathEntry[ j ];
137             System.arraycopy( cp, 0, newCp, 0, j);
138             jp.setRawClasspath( newCp, progressMonitor );
139             
140             if( AptPlugin.DEBUG ){
141                 AptPlugin.trace("removed " + workspaceRelativePath + " from classpath"); //$NON-NLS-1$ //$NON-NLS-2$
142
}
143         }
144     }
145
146     /**
147      * returns true if we updated the classpath, false otherwise
148      */

149     public static boolean updateProjectClasspath( IJavaProject jp, IFolder folder, IProgressMonitor progressMonitor )
150         throws JavaModelException
151     {
152         IClasspathEntry[] cp = jp.getRawClasspath();
153         IPath path = folder.getFullPath();
154         boolean found = ClasspathUtil.doesClasspathContainEntry(jp, cp, path, progressMonitor);
155         
156         if (!found)
157         {
158             // update exclusion patterns
159
ArrayList JavaDoc<IPath> exclusions = new ArrayList JavaDoc<IPath>();
160             for ( int i = 0; i< cp.length; i++ )
161             {
162                 if ( cp[i].getPath().isPrefixOf( path ) )
163                 {
164                     // exclusion patterns must be project-relative paths, and must end with a "/"
165
IPath projectRelativePath = folder.getProjectRelativePath().addTrailingSeparator();
166                     
167                     // path is contained in an existing source path, so update existing paths's exclusion patterns
168
IPath[] oldExclusions = cp[i].getExclusionPatterns();
169
170                     // don't add if exclusion pattern already contains src dir
171
boolean add = true;
172                     for ( int j = 0; j < oldExclusions.length; j++ )
173                         if ( oldExclusions[j].equals( projectRelativePath ) )
174                             add = false;
175                     
176                     if ( add )
177                     {
178                         IPath[] newExclusions;
179                         if ( cp[i].getExclusionPatterns() == null )
180                             newExclusions = new IPath[1];
181                         else
182                         {
183                             newExclusions = new IPath[ oldExclusions.length + 1 ];
184                             System.arraycopy( oldExclusions, 0, newExclusions, 0, oldExclusions.length );
185                         }
186                         newExclusions[ newExclusions.length - 1 ] = projectRelativePath;
187                         cp[i] = JavaCore.newSourceEntry(cp[i].getPath(), cp[i].getInclusionPatterns(), newExclusions, cp[i].getOutputLocation(), cp[i].getExtraAttributes());
188                     }
189                     
190                 }
191                 else if ( path.isPrefixOf( cp[i].getPath() ))
192                 {
193                     // new source path contains an existing source path, so add an exclusion pattern for it
194
exclusions.add( cp[i].getPath().addTrailingSeparator() );
195                 }
196             }
197             
198             IPath[] exclusionPatterns = exclusions.toArray( new IPath[exclusions.size()] );
199             final IClasspathAttribute[] attrs = new IClasspathAttribute[1];
200             attrs[0] = JavaCore.newClasspathAttribute(IClasspathAttribute.OPTIONAL, Boolean.toString(true));
201             IClasspathEntry generatedSourceClasspathEntry =
202                 JavaCore.newSourceEntry(folder.getFullPath(), new IPath[] {}, exclusionPatterns, null, attrs );
203             
204             IClasspathEntry[] newCp = new IClasspathEntry[cp.length + 1];
205             System.arraycopy(cp, 0, newCp, 0, cp.length);
206             newCp[newCp.length - 1] = generatedSourceClasspathEntry;
207             
208             jp.setRawClasspath(newCp, progressMonitor );
209         }
210
211         // return true if we updated the project's classpath entries
212
return !found;
213     }
214     
215     /**
216      * All methods static. Clients should not instantiate this class.
217      */

218     private ClasspathUtil() {
219     }
220
221
222 }
223
Popular Tags