|
1 | 1 | package com.jaipilot.cli.classpath; |
2 | 2 |
|
| 3 | +import java.io.IOException; |
| 4 | +import java.nio.charset.StandardCharsets; |
| 5 | +import java.nio.file.Files; |
3 | 6 | import java.nio.file.Path; |
| 7 | +import java.util.ArrayList; |
| 8 | +import java.util.LinkedHashSet; |
| 9 | +import java.util.List; |
4 | 10 | import java.util.Optional; |
5 | 11 | import java.util.Set; |
6 | 12 | import java.util.concurrent.ConcurrentHashMap; |
@@ -111,4 +117,204 @@ public ResolvedSource resolveSourceOrThrow( |
111 | 117 | "Source was not available." |
112 | 118 | ))); |
113 | 119 | } |
| 120 | + |
| 121 | + public Optional<ResolvedSource> resolveSourceByFqcn( |
| 122 | + String fqcnOrImport, |
| 123 | + Path projectRoot, |
| 124 | + Path moduleRoot, |
| 125 | + ResolutionOptions options |
| 126 | + ) { |
| 127 | + ResolutionOptions normalizedOptions = options == null ? ResolutionOptions.defaults() : options; |
| 128 | + String normalizedFqcn = ClassNameParser.normalizeFqcn(fqcnOrImport); |
| 129 | + ResolvedClasspath classpath = classpathResolver.resolveTestClasspath(projectRoot, moduleRoot, normalizedOptions); |
| 130 | + |
| 131 | + Optional<ResolvedSource> workspaceSource = resolveExactWorkspaceJava( |
| 132 | + normalizedFqcn, |
| 133 | + List.copyOf(workspaceSourceRoots(classpath)), |
| 134 | + classpath.moduleRoot() |
| 135 | + ); |
| 136 | + if (workspaceSource.isPresent()) { |
| 137 | + return workspaceSource; |
| 138 | + } |
| 139 | + |
| 140 | + Optional<ResolvedSource> generatedSource = resolveExactWorkspaceJava( |
| 141 | + normalizedFqcn, |
| 142 | + generatedSourceRoots(classpath.moduleRoot()), |
| 143 | + classpath.moduleRoot() |
| 144 | + ); |
| 145 | + if (generatedSource.isPresent()) { |
| 146 | + return generatedSource; |
| 147 | + } |
| 148 | + |
| 149 | + for (String candidateBinaryName : binaryNameCandidates(normalizedFqcn)) { |
| 150 | + ClassResolutionResult classResult = locate( |
| 151 | + candidateBinaryName, |
| 152 | + projectRoot, |
| 153 | + moduleRoot, |
| 154 | + normalizedOptions |
| 155 | + ); |
| 156 | + if (classResult.kind() == LocationKind.NOT_FOUND) { |
| 157 | + continue; |
| 158 | + } |
| 159 | + Optional<ResolvedSource> resolved = resolveSource(classResult, projectRoot, moduleRoot, normalizedOptions); |
| 160 | + if (resolved.isPresent()) { |
| 161 | + return resolved; |
| 162 | + } |
| 163 | + } |
| 164 | + return Optional.empty(); |
| 165 | + } |
| 166 | + |
| 167 | + private Optional<ResolvedSource> resolveExactWorkspaceJava( |
| 168 | + String normalizedFqcn, |
| 169 | + List<Path> roots, |
| 170 | + Path moduleRoot |
| 171 | + ) { |
| 172 | + for (String sourceEntry : sourceEntryCandidates(normalizedFqcn)) { |
| 173 | + for (Path root : roots) { |
| 174 | + Path candidate = root.resolve(sourceEntry).toAbsolutePath().normalize(); |
| 175 | + if (!Files.isRegularFile(candidate)) { |
| 176 | + continue; |
| 177 | + } |
| 178 | + return Optional.of(new ResolvedSource( |
| 179 | + normalizedFqcn, |
| 180 | + SourceOrigin.WORKSPACE_FILE, |
| 181 | + candidate, |
| 182 | + readSourceFile(candidate, moduleRoot) |
| 183 | + )); |
| 184 | + } |
| 185 | + } |
| 186 | + return Optional.empty(); |
| 187 | + } |
| 188 | + |
| 189 | + private List<Path> workspaceSourceRoots(ResolvedClasspath classpath) { |
| 190 | + LinkedHashSet<Path> roots = new LinkedHashSet<>(); |
| 191 | + roots.addAll(classpath.mainSourceRoots()); |
| 192 | + roots.addAll(classpath.testSourceRoots()); |
| 193 | + return List.copyOf(roots); |
| 194 | + } |
| 195 | + |
| 196 | + private List<Path> generatedSourceRoots(Path moduleRoot) { |
| 197 | + if (moduleRoot == null) { |
| 198 | + return List.of(); |
| 199 | + } |
| 200 | + |
| 201 | + LinkedHashSet<Path> roots = new LinkedHashSet<>(); |
| 202 | + roots.add(moduleRoot.resolve("target/generated-sources")); |
| 203 | + roots.add(moduleRoot.resolve("target/generated-test-sources")); |
| 204 | + roots.add(moduleRoot.resolve("target")); |
| 205 | + roots.add(moduleRoot.resolve("build/generated/sources")); |
| 206 | + roots.add(moduleRoot.resolve("build/generated/test-sources")); |
| 207 | + roots.add(moduleRoot.resolve("build/generated")); |
| 208 | + return roots.stream() |
| 209 | + .map(Path::toAbsolutePath) |
| 210 | + .map(Path::normalize) |
| 211 | + .toList(); |
| 212 | + } |
| 213 | + |
| 214 | + private List<String> sourceEntryCandidates(String normalizedFqcn) { |
| 215 | + LinkedHashSet<String> candidates = new LinkedHashSet<>(); |
| 216 | + candidates.add(normalizedFqcn.replace('.', '/') + ".java"); |
| 217 | + |
| 218 | + int firstDollar = normalizedFqcn.indexOf('$'); |
| 219 | + if (firstDollar > 0) { |
| 220 | + String outer = normalizedFqcn.substring(0, firstDollar); |
| 221 | + candidates.add(outer.replace('.', '/') + ".java"); |
| 222 | + } |
| 223 | + |
| 224 | + String current = normalizedFqcn; |
| 225 | + while (true) { |
| 226 | + int lastDot = current.lastIndexOf('.'); |
| 227 | + if (lastDot <= 0) { |
| 228 | + break; |
| 229 | + } |
| 230 | + String outer = current.substring(0, lastDot); |
| 231 | + String segment = lastSegment(outer); |
| 232 | + if (segment.isEmpty() || !Character.isUpperCase(segment.charAt(0))) { |
| 233 | + break; |
| 234 | + } |
| 235 | + candidates.add(outer.replace('.', '/') + ".java"); |
| 236 | + current = outer; |
| 237 | + } |
| 238 | + return List.copyOf(candidates); |
| 239 | + } |
| 240 | + |
| 241 | + private List<String> binaryNameCandidates(String normalizedFqcn) { |
| 242 | + LinkedHashSet<String> candidates = new LinkedHashSet<>(); |
| 243 | + candidates.add(normalizedFqcn); |
| 244 | + |
| 245 | + int firstDollar = normalizedFqcn.indexOf('$'); |
| 246 | + if (firstDollar > 0) { |
| 247 | + candidates.add(normalizedFqcn.substring(0, firstDollar)); |
| 248 | + } |
| 249 | + |
| 250 | + List<Integer> trailingInnerBoundaries = trailingInnerBoundaries(normalizedFqcn); |
| 251 | + for (int depth = 1; depth <= trailingInnerBoundaries.size(); depth++) { |
| 252 | + char[] chars = normalizedFqcn.toCharArray(); |
| 253 | + for (int index = 0; index < depth; index++) { |
| 254 | + chars[trailingInnerBoundaries.get(index)] = '$'; |
| 255 | + } |
| 256 | + candidates.add(new String(chars)); |
| 257 | + } |
| 258 | + |
| 259 | + String current = normalizedFqcn; |
| 260 | + while (true) { |
| 261 | + int lastDot = current.lastIndexOf('.'); |
| 262 | + if (lastDot <= 0) { |
| 263 | + break; |
| 264 | + } |
| 265 | + String outer = current.substring(0, lastDot); |
| 266 | + String segment = lastSegment(outer); |
| 267 | + if (segment.isEmpty() || !Character.isUpperCase(segment.charAt(0))) { |
| 268 | + break; |
| 269 | + } |
| 270 | + candidates.add(outer); |
| 271 | + current = outer; |
| 272 | + } |
| 273 | + return List.copyOf(candidates); |
| 274 | + } |
| 275 | + |
| 276 | + private List<Integer> trailingInnerBoundaries(String normalizedFqcn) { |
| 277 | + List<Integer> boundaries = new ArrayList<>(); |
| 278 | + int searchFrom = normalizedFqcn.length() - 1; |
| 279 | + while (true) { |
| 280 | + int boundary = normalizedFqcn.lastIndexOf('.', searchFrom); |
| 281 | + if (boundary <= 0) { |
| 282 | + break; |
| 283 | + } |
| 284 | + String segmentBeforeBoundary = segmentBefore(normalizedFqcn, boundary); |
| 285 | + if (segmentBeforeBoundary.isEmpty() || !Character.isUpperCase(segmentBeforeBoundary.charAt(0))) { |
| 286 | + break; |
| 287 | + } |
| 288 | + boundaries.add(boundary); |
| 289 | + searchFrom = boundary - 1; |
| 290 | + } |
| 291 | + return boundaries; |
| 292 | + } |
| 293 | + |
| 294 | + private String segmentBefore(String value, int boundaryIndex) { |
| 295 | + int previousBoundary = value.lastIndexOf('.', boundaryIndex - 1); |
| 296 | + return value.substring(previousBoundary + 1, boundaryIndex); |
| 297 | + } |
| 298 | + |
| 299 | + private String lastSegment(String value) { |
| 300 | + int boundary = value.lastIndexOf('.'); |
| 301 | + if (boundary < 0) { |
| 302 | + return value; |
| 303 | + } |
| 304 | + return value.substring(boundary + 1); |
| 305 | + } |
| 306 | + |
| 307 | + private String readSourceFile(Path sourcePath, Path moduleRoot) { |
| 308 | + try { |
| 309 | + return Files.readString(sourcePath, StandardCharsets.UTF_8); |
| 310 | + } catch (IOException exception) { |
| 311 | + throw new ClasspathResolutionException(new ResolutionFailure( |
| 312 | + ResolutionFailureCategory.SOURCE_NOT_AVAILABLE, |
| 313 | + null, |
| 314 | + moduleRoot, |
| 315 | + "read source " + sourcePath, |
| 316 | + exception.getMessage() |
| 317 | + ), exception); |
| 318 | + } |
| 319 | + } |
114 | 320 | } |
0 commit comments