|
| 1 | +package com.github.gradusnikov.eclipse.assistai.mcp.services; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.IOException; |
| 6 | +import java.nio.file.Files; |
| 7 | +import java.nio.file.Path; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import org.eclipse.core.resources.IProject; |
| 12 | +import org.eclipse.core.resources.ResourcesPlugin; |
| 13 | +import org.eclipse.core.runtime.ILog; |
| 14 | +import org.eclipse.core.runtime.Platform; |
| 15 | +import org.eclipse.e4.core.di.annotations.Creatable; |
| 16 | +import org.eclipse.jdt.core.IJavaProject; |
| 17 | +import org.eclipse.jdt.core.JavaCore; |
| 18 | +import org.jacoco.core.analysis.Analyzer; |
| 19 | +import org.jacoco.core.analysis.CoverageBuilder; |
| 20 | +import org.jacoco.core.analysis.IClassCoverage; |
| 21 | +import org.jacoco.core.analysis.ICounter; |
| 22 | +import org.jacoco.core.analysis.IPackageCoverage; |
| 23 | +import org.jacoco.core.data.ExecutionDataReader; |
| 24 | +import org.jacoco.core.data.ExecutionDataStore; |
| 25 | +import org.jacoco.core.data.SessionInfoStore; |
| 26 | + |
| 27 | +import jakarta.inject.Inject; |
| 28 | +import jakarta.inject.Singleton; |
| 29 | + |
| 30 | +@Creatable |
| 31 | +@Singleton |
| 32 | +public class CoverageService |
| 33 | +{ |
| 34 | + private static final String ECLEMMA_CORE_BUNDLE = "org.eclipse.eclemma.core"; |
| 35 | + private static final String COVERAGE_LAUNCH_MODE = "coverage"; |
| 36 | + |
| 37 | + @Inject |
| 38 | + private ILog logger; |
| 39 | + |
| 40 | + public boolean isCoverageAvailable() |
| 41 | + { |
| 42 | + return Platform.getBundle( ECLEMMA_CORE_BUNDLE ) != null; |
| 43 | + } |
| 44 | + |
| 45 | + public String getCoverageLaunchMode() |
| 46 | + { |
| 47 | + return COVERAGE_LAUNCH_MODE; |
| 48 | + } |
| 49 | + |
| 50 | + public String findLatestCoverageFile() |
| 51 | + { |
| 52 | + Path basePath = ResourcesPlugin.getWorkspace().getRoot().getLocation().toFile().toPath() |
| 53 | + .resolve( ".metadata" ).resolve( ".plugins" ).resolve( ECLEMMA_CORE_BUNDLE ); |
| 54 | + |
| 55 | + if ( !Files.exists( basePath ) ) |
| 56 | + { |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + try |
| 61 | + { |
| 62 | + var execFiles = Files.walk( basePath, 2 ) |
| 63 | + .filter( p -> p.toString().endsWith( ".exec" ) ) |
| 64 | + .sorted( ( a, b ) -> { |
| 65 | + try |
| 66 | + { |
| 67 | + return Long.compare( Files.getLastModifiedTime( b ).toMillis(), |
| 68 | + Files.getLastModifiedTime( a ).toMillis() ); |
| 69 | + } |
| 70 | + catch ( IOException e ) |
| 71 | + { |
| 72 | + return 0; |
| 73 | + } |
| 74 | + } ) |
| 75 | + .toList(); |
| 76 | + |
| 77 | + if ( !execFiles.isEmpty() ) |
| 78 | + { |
| 79 | + return execFiles.get( 0 ).toString(); |
| 80 | + } |
| 81 | + } |
| 82 | + catch ( IOException e ) |
| 83 | + { |
| 84 | + logger.error( "Error searching for coverage files", e ); |
| 85 | + } |
| 86 | + return null; |
| 87 | + } |
| 88 | + |
| 89 | + public String formatCoverageInfo( String execFilePath ) |
| 90 | + { |
| 91 | + return formatCoverageInfo( execFilePath, null ); |
| 92 | + } |
| 93 | + |
| 94 | + public String formatCoverageInfo( String execFilePath, String projectName ) |
| 95 | + { |
| 96 | + if ( execFilePath == null ) |
| 97 | + { |
| 98 | + return ""; |
| 99 | + } |
| 100 | + |
| 101 | + if ( projectName != null ) |
| 102 | + { |
| 103 | + try |
| 104 | + { |
| 105 | + return analyzeCoverage( execFilePath, projectName ); |
| 106 | + } |
| 107 | + catch ( Exception e ) |
| 108 | + { |
| 109 | + if ( logger != null ) |
| 110 | + { |
| 111 | + try { logger.error( "Error analyzing coverage data, falling back to basic info", e ); } |
| 112 | + catch ( Exception logEx ) { /* logger not fully initialized */ } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + StringBuilder sb = new StringBuilder(); |
| 118 | + sb.append( "\n--- Coverage ---\n" ); |
| 119 | + sb.append( "Coverage data collected. View in Eclipse via Coverage view.\n" ); |
| 120 | + sb.append( "Coverage data file: " ).append( execFilePath ).append( "\n" ); |
| 121 | + return sb.toString(); |
| 122 | + } |
| 123 | + |
| 124 | + private String analyzeCoverage( String execFilePath, String projectName ) throws Exception |
| 125 | + { |
| 126 | + ExecutionDataStore executionDataStore = new ExecutionDataStore(); |
| 127 | + SessionInfoStore sessionInfoStore = new SessionInfoStore(); |
| 128 | + |
| 129 | + try ( FileInputStream fis = new FileInputStream( execFilePath ) ) |
| 130 | + { |
| 131 | + ExecutionDataReader reader = new ExecutionDataReader( fis ); |
| 132 | + reader.setExecutionDataVisitor( executionDataStore ); |
| 133 | + reader.setSessionInfoVisitor( sessionInfoStore ); |
| 134 | + reader.read(); |
| 135 | + } |
| 136 | + |
| 137 | + CoverageBuilder coverageBuilder = new CoverageBuilder(); |
| 138 | + Analyzer analyzer = new Analyzer( executionDataStore, coverageBuilder ); |
| 139 | + |
| 140 | + IProject[] allProjects = ResourcesPlugin.getWorkspace().getRoot().getProjects(); |
| 141 | + for ( IProject project : allProjects ) |
| 142 | + { |
| 143 | + if ( !project.isOpen() || !project.hasNature( JavaCore.NATURE_ID ) ) |
| 144 | + { |
| 145 | + continue; |
| 146 | + } |
| 147 | + IJavaProject javaProject = JavaCore.create( project ); |
| 148 | + File outputLocation = project.getLocation().append( |
| 149 | + javaProject.getOutputLocation().removeFirstSegments( 1 ) ).toFile(); |
| 150 | + if ( outputLocation.exists() ) |
| 151 | + { |
| 152 | + analyzer.analyzeAll( outputLocation ); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return formatReport( coverageBuilder, projectName ); |
| 157 | + } |
| 158 | + |
| 159 | + private String formatReport( CoverageBuilder coverageBuilder, String projectName ) |
| 160 | + { |
| 161 | + StringBuilder sb = new StringBuilder(); |
| 162 | + sb.append( "\n--- Coverage Report ---\n" ); |
| 163 | + |
| 164 | + int totalLines = 0; |
| 165 | + int coveredLines = 0; |
| 166 | + int totalBranches = 0; |
| 167 | + int coveredBranches = 0; |
| 168 | + |
| 169 | + StringBuilder jsonArray = new StringBuilder(); |
| 170 | + jsonArray.append( "[\n" ); |
| 171 | + boolean first = true; |
| 172 | + |
| 173 | + for ( IPackageCoverage pkg : coverageBuilder.getBundle( projectName ).getPackages() ) |
| 174 | + { |
| 175 | + String packageName = pkg.getName().replace( '/', '.' ); |
| 176 | + totalLines += pkg.getLineCounter().getTotalCount(); |
| 177 | + coveredLines += pkg.getLineCounter().getCoveredCount(); |
| 178 | + totalBranches += pkg.getBranchCounter().getTotalCount(); |
| 179 | + coveredBranches += pkg.getBranchCounter().getCoveredCount(); |
| 180 | + |
| 181 | + for ( IClassCoverage cls : pkg.getClasses() ) |
| 182 | + { |
| 183 | + int clsLines = cls.getLineCounter().getTotalCount(); |
| 184 | + if ( clsLines == 0 ) continue; |
| 185 | + |
| 186 | + List<Integer> uncoveredLines = new ArrayList<>(); |
| 187 | + List<Integer> partiallyCoveredLines = new ArrayList<>(); |
| 188 | + List<Integer> uncoveredBranchLines = new ArrayList<>(); |
| 189 | + List<Integer> partiallyCoveredBranchLines = new ArrayList<>(); |
| 190 | + |
| 191 | + for ( int i = cls.getFirstLine(); i <= cls.getLastLine(); i++ ) |
| 192 | + { |
| 193 | + int lineStatus = cls.getLine( i ).getStatus(); |
| 194 | + if ( lineStatus == ICounter.NOT_COVERED ) |
| 195 | + { |
| 196 | + uncoveredLines.add( i ); |
| 197 | + } |
| 198 | + else if ( lineStatus == ICounter.PARTLY_COVERED ) |
| 199 | + { |
| 200 | + partiallyCoveredLines.add( i ); |
| 201 | + } |
| 202 | + |
| 203 | + int branchTotal = cls.getLine( i ).getBranchCounter().getTotalCount(); |
| 204 | + if ( branchTotal > 0 ) |
| 205 | + { |
| 206 | + int branchStatus = cls.getLine( i ).getBranchCounter().getStatus(); |
| 207 | + if ( branchStatus == ICounter.NOT_COVERED ) |
| 208 | + { |
| 209 | + uncoveredBranchLines.add( i ); |
| 210 | + } |
| 211 | + else if ( branchStatus == ICounter.PARTLY_COVERED ) |
| 212 | + { |
| 213 | + partiallyCoveredBranchLines.add( i ); |
| 214 | + } |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + if ( uncoveredLines.isEmpty() && partiallyCoveredLines.isEmpty() |
| 219 | + && uncoveredBranchLines.isEmpty() && partiallyCoveredBranchLines.isEmpty() ) |
| 220 | + { |
| 221 | + continue; |
| 222 | + } |
| 223 | + |
| 224 | + if ( !first ) jsonArray.append( ",\n" ); |
| 225 | + first = false; |
| 226 | + |
| 227 | + String sourceFile = cls.getSourceFileName() != null ? cls.getSourceFileName() |
| 228 | + : cls.getName().substring( cls.getName().lastIndexOf( '/' ) + 1 ) + ".java"; |
| 229 | + |
| 230 | + jsonArray.append( " {\n" ); |
| 231 | + jsonArray.append( " \"sourcefile\": \"" ).append( sourceFile ).append( "\",\n" ); |
| 232 | + jsonArray.append( " \"package\": \"" ).append( packageName ).append( "\",\n" ); |
| 233 | + jsonArray.append( " \"lines\": {\n" ); |
| 234 | + jsonArray.append( " \"nocovered\": " ).append( toJsonArray( uncoveredLines ) ).append( ",\n" ); |
| 235 | + jsonArray.append( " \"partiallycovered\": " ).append( toJsonArray( partiallyCoveredLines ) ).append( "\n" ); |
| 236 | + jsonArray.append( " },\n" ); |
| 237 | + jsonArray.append( " \"branch\": {\n" ); |
| 238 | + jsonArray.append( " \"nocovered\": " ).append( toJsonArray( uncoveredBranchLines ) ).append( ",\n" ); |
| 239 | + jsonArray.append( " \"partiallycovered\": " ).append( toJsonArray( partiallyCoveredBranchLines ) ).append( "\n" ); |
| 240 | + jsonArray.append( " }\n" ); |
| 241 | + jsonArray.append( " }" ); |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + jsonArray.append( "\n]" ); |
| 246 | + |
| 247 | + if ( totalLines > 0 ) |
| 248 | + { |
| 249 | + int overallLinePct = (int) ( 100.0 * coveredLines / totalLines ); |
| 250 | + sb.append( "Overall: " ).append( overallLinePct ).append( "% line coverage (" ) |
| 251 | + .append( coveredLines ).append( "/" ).append( totalLines ).append( " lines)" ); |
| 252 | + if ( totalBranches > 0 ) |
| 253 | + { |
| 254 | + int overallBranchPct = (int) ( 100.0 * coveredBranches / totalBranches ); |
| 255 | + sb.append( ", " ).append( overallBranchPct ).append( "% branch coverage (" ) |
| 256 | + .append( coveredBranches ).append( "/" ).append( totalBranches ).append( " branches)" ); |
| 257 | + } |
| 258 | + sb.append( "\n\n" ); |
| 259 | + } |
| 260 | + |
| 261 | + if ( first ) |
| 262 | + { |
| 263 | + sb.append( "All classes fully covered.\n" ); |
| 264 | + } |
| 265 | + else |
| 266 | + { |
| 267 | + sb.append( "Classes with incomplete coverage:\n" ); |
| 268 | + sb.append( jsonArray ); |
| 269 | + sb.append( "\n" ); |
| 270 | + } |
| 271 | + |
| 272 | + return sb.toString(); |
| 273 | + } |
| 274 | + |
| 275 | + private String toJsonArray( List<Integer> values ) |
| 276 | + { |
| 277 | + if ( values.isEmpty() ) return "[]"; |
| 278 | + StringBuilder sb = new StringBuilder(); |
| 279 | + sb.append( "[" ); |
| 280 | + for ( int i = 0; i < values.size(); i++ ) |
| 281 | + { |
| 282 | + if ( i > 0 ) sb.append( ", " ); |
| 283 | + sb.append( values.get( i ) ); |
| 284 | + } |
| 285 | + sb.append( "]" ); |
| 286 | + return sb.toString(); |
| 287 | + } |
| 288 | +} |
0 commit comments