|
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; |
13 | 16 | import org.eclipse.core.runtime.ILog; |
14 | 17 | import org.eclipse.core.runtime.Platform; |
15 | 18 | import org.eclipse.e4.core.di.annotations.Creatable; |
| 19 | +import org.eclipse.jdt.core.IClasspathEntry; |
16 | 20 | import org.eclipse.jdt.core.IJavaProject; |
17 | 21 | import org.eclipse.jdt.core.JavaCore; |
18 | 22 | import org.jacoco.core.analysis.Analyzer; |
@@ -188,22 +192,77 @@ private String analyzeCoverage( String execFilePath, String projectName ) throws |
188 | 192 | reader.read(); |
189 | 193 | } |
190 | 194 |
|
| 195 | + Set<String> executedClassNames = executionDataStore.getContents().stream() |
| 196 | + .map( data -> data.getName() ) |
| 197 | + .collect( Collectors.toSet() ); |
| 198 | + |
| 199 | + if ( executedClassNames.isEmpty() ) |
| 200 | + { |
| 201 | + return "\n--- Coverage ---\nNo execution data found in coverage file.\n"; |
| 202 | + } |
| 203 | + |
191 | 204 | CoverageBuilder coverageBuilder = new CoverageBuilder(); |
192 | 205 | Analyzer analyzer = new Analyzer( executionDataStore, coverageBuilder ); |
193 | 206 |
|
| 207 | + List<File> outputLocations = new ArrayList<>(); |
194 | 208 | IProject[] allProjects = ResourcesPlugin.getWorkspace().getRoot().getProjects(); |
195 | 209 | for ( IProject project : allProjects ) |
196 | 210 | { |
197 | | - if ( !project.isOpen() || !project.hasNature( JavaCore.NATURE_ID ) ) |
| 211 | + try |
| 212 | + { |
| 213 | + if ( !project.isOpen() || !project.hasNature( JavaCore.NATURE_ID ) ) |
| 214 | + { |
| 215 | + continue; |
| 216 | + } |
| 217 | + IJavaProject javaProject = JavaCore.create( project ); |
| 218 | + Set<File> projectOutputs = new HashSet<>(); |
| 219 | + File defaultOutput = project.getLocation().append( |
| 220 | + javaProject.getOutputLocation().removeFirstSegments( 1 ) ).toFile(); |
| 221 | + projectOutputs.add( defaultOutput ); |
| 222 | + for ( IClasspathEntry entry : javaProject.getResolvedClasspath( true ) ) |
| 223 | + { |
| 224 | + if ( entry.getEntryKind() == IClasspathEntry.CPE_SOURCE && entry.getOutputLocation() != null ) |
| 225 | + { |
| 226 | + File entryOutput = project.getLocation().append( |
| 227 | + entry.getOutputLocation().removeFirstSegments( 1 ) ).toFile(); |
| 228 | + projectOutputs.add( entryOutput ); |
| 229 | + } |
| 230 | + } |
| 231 | + for ( File output : projectOutputs ) |
| 232 | + { |
| 233 | + if ( output.exists() ) |
| 234 | + { |
| 235 | + outputLocations.add( output ); |
| 236 | + } |
| 237 | + } |
| 238 | + } |
| 239 | + catch ( Exception e ) |
198 | 240 | { |
199 | | - continue; |
| 241 | + // skip projects that can't be resolved |
200 | 242 | } |
201 | | - IJavaProject javaProject = JavaCore.create( project ); |
202 | | - File outputLocation = project.getLocation().append( |
203 | | - javaProject.getOutputLocation().removeFirstSegments( 1 ) ).toFile(); |
204 | | - if ( outputLocation.exists() ) |
| 243 | + } |
| 244 | + |
| 245 | + for ( String className : executedClassNames ) |
| 246 | + { |
| 247 | + String classFilePath = className.replace( '/', File.separatorChar ) + ".class"; |
| 248 | + for ( File outputLocation : outputLocations ) |
205 | 249 | { |
206 | | - analyzer.analyzeAll( outputLocation ); |
| 250 | + File classFile = new File( outputLocation, classFilePath ); |
| 251 | + if ( classFile.exists() ) |
| 252 | + { |
| 253 | + try ( FileInputStream fis = new FileInputStream( classFile ) ) |
| 254 | + { |
| 255 | + analyzer.analyzeAll( fis, className ); |
| 256 | + } |
| 257 | + catch ( Exception e ) |
| 258 | + { |
| 259 | + if ( logger != null ) |
| 260 | + { |
| 261 | + logger.warn( "Skipping class '" + className + "': " + e.getMessage() ); |
| 262 | + } |
| 263 | + } |
| 264 | + break; |
| 265 | + } |
207 | 266 | } |
208 | 267 | } |
209 | 268 |
|
|
0 commit comments