|
6 | 6 | import java.nio.file.Files; |
7 | 7 | import java.nio.file.Path; |
8 | 8 | import java.util.ArrayList; |
| 9 | +import java.util.HashSet; |
9 | 10 | import java.util.List; |
| 11 | +import java.util.Set; |
| 12 | +import java.util.stream.Collectors; |
10 | 13 |
|
11 | 14 | import org.eclipse.core.resources.IProject; |
12 | 15 | import org.eclipse.core.resources.ResourcesPlugin; |
@@ -188,28 +191,85 @@ private String analyzeCoverage( String execFilePath, String projectName ) throws |
188 | 191 | reader.read(); |
189 | 192 | } |
190 | 193 |
|
| 194 | + Set<String> executedClassNames = executionDataStore.getContents().stream() |
| 195 | + .map( data -> data.getName() ) |
| 196 | + .collect( Collectors.toSet() ); |
| 197 | + |
| 198 | + if ( executedClassNames.isEmpty() ) |
| 199 | + { |
| 200 | + return "\n--- Coverage ---\nNo execution data found in coverage file.\n"; |
| 201 | + } |
| 202 | + |
191 | 203 | CoverageBuilder coverageBuilder = new CoverageBuilder(); |
192 | 204 | Analyzer analyzer = new Analyzer( executionDataStore, coverageBuilder ); |
| 205 | + Set<String> analyzedClasses = new HashSet<>(); |
193 | 206 |
|
194 | 207 | IProject[] allProjects = ResourcesPlugin.getWorkspace().getRoot().getProjects(); |
195 | 208 | for ( IProject project : allProjects ) |
196 | 209 | { |
197 | | - if ( !project.isOpen() || !project.hasNature( JavaCore.NATURE_ID ) ) |
| 210 | + try |
198 | 211 | { |
199 | | - continue; |
| 212 | + if ( !project.isOpen() || !project.hasNature( JavaCore.NATURE_ID ) ) |
| 213 | + { |
| 214 | + continue; |
| 215 | + } |
| 216 | + IJavaProject javaProject = JavaCore.create( project ); |
| 217 | + File outputLocation = project.getLocation().append( |
| 218 | + javaProject.getOutputLocation().removeFirstSegments( 1 ) ).toFile(); |
| 219 | + if ( !outputLocation.exists() ) |
| 220 | + { |
| 221 | + continue; |
| 222 | + } |
| 223 | + analyzeMatchingClasses( outputLocation, outputLocation, executedClassNames, analyzedClasses, analyzer ); |
200 | 224 | } |
201 | | - IJavaProject javaProject = JavaCore.create( project ); |
202 | | - File outputLocation = project.getLocation().append( |
203 | | - javaProject.getOutputLocation().removeFirstSegments( 1 ) ).toFile(); |
204 | | - if ( outputLocation.exists() ) |
| 225 | + catch ( Exception e ) |
205 | 226 | { |
206 | | - analyzer.analyzeAll( outputLocation ); |
| 227 | + if ( logger != null ) |
| 228 | + { |
| 229 | + logger.warn( "Skipping project '" + project.getName() + "' during coverage analysis: " + e.getMessage() ); |
| 230 | + } |
207 | 231 | } |
208 | 232 | } |
209 | 233 |
|
210 | 234 | return formatReport( coverageBuilder, projectName ); |
211 | 235 | } |
212 | 236 |
|
| 237 | + private void analyzeMatchingClasses( File root, File dir, Set<String> executedClassNames, |
| 238 | + Set<String> analyzedClasses, Analyzer analyzer ) throws IOException |
| 239 | + { |
| 240 | + File[] files = dir.listFiles(); |
| 241 | + if ( files == null ) |
| 242 | + { |
| 243 | + return; |
| 244 | + } |
| 245 | + for ( File file : files ) |
| 246 | + { |
| 247 | + if ( file.isDirectory() ) |
| 248 | + { |
| 249 | + analyzeMatchingClasses( root, file, executedClassNames, analyzedClasses, analyzer ); |
| 250 | + } |
| 251 | + else if ( file.getName().endsWith( ".class" ) ) |
| 252 | + { |
| 253 | + String relativePath = root.toPath().relativize( file.toPath() ).toString().replace( '\\', '/' ); |
| 254 | + String className = relativePath.substring( 0, relativePath.length() - 6 ); |
| 255 | + if ( executedClassNames.contains( className ) && analyzedClasses.add( className ) ) |
| 256 | + { |
| 257 | + try ( FileInputStream fis = new FileInputStream( file ) ) |
| 258 | + { |
| 259 | + analyzer.analyzeAll( fis, className ); |
| 260 | + } |
| 261 | + catch ( Exception e ) |
| 262 | + { |
| 263 | + if ( logger != null ) |
| 264 | + { |
| 265 | + logger.warn( "Skipping class '" + className + "': " + e.getMessage() ); |
| 266 | + } |
| 267 | + } |
| 268 | + } |
| 269 | + } |
| 270 | + } |
| 271 | + } |
| 272 | + |
213 | 273 | private String formatReport( CoverageBuilder coverageBuilder, String projectName ) |
214 | 274 | { |
215 | 275 | StringBuilder sb = new StringBuilder(); |
|
0 commit comments