1 11 12 package org.eclipse.jdt.apt.core.internal.generatedfile; 13 14 import java.util.ArrayList ; 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 30 public class ClasspathUtil { 31 32 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 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 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 int j = 0; 101 for ( int i=0; i<cp.length; i++ ) 102 { 103 if (! cp[i].getPath().equals( workspaceRelativePath ) ) 104 { 105 106 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 cp[j] = cp[i]; 122 } 123 else 124 { 125 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 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"); } 143 } 144 } 145 146 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 ArrayList <IPath> exclusions = new ArrayList <IPath>(); 160 for ( int i = 0; i< cp.length; i++ ) 161 { 162 if ( cp[i].getPath().isPrefixOf( path ) ) 163 { 164 IPath projectRelativePath = folder.getProjectRelativePath().addTrailingSeparator(); 166 167 IPath[] oldExclusions = cp[i].getExclusionPatterns(); 169 170 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 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 !found; 213 } 214 215 218 private ClasspathUtil() { 219 } 220 221 222 } 223 | Popular Tags |