|
| 1 | +// cli/detectors/java.go |
| 2 | +package detectors |
| 3 | + |
| 4 | +import ( |
| 5 | + "bufio" |
| 6 | + "errors" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +type JavaDetector struct { |
| 13 | + confidence int |
| 14 | +} |
| 15 | + |
| 16 | +func (d *JavaDetector) Detect(dir string) (*ProjectInfo, error) { |
| 17 | + // Check for Java project files |
| 18 | + packageFiles := []string{ |
| 19 | + "pom.xml", |
| 20 | + "build.gradle", |
| 21 | + "build.gradle.kts", |
| 22 | + } |
| 23 | + |
| 24 | + var foundFile string |
| 25 | + for _, f := range packageFiles { |
| 26 | + if fileExists(filepath.Join(dir, f)) { |
| 27 | + foundFile = f |
| 28 | + break |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + if foundFile == "" { |
| 33 | + d.confidence = 0 |
| 34 | + return nil, errors.New("no Java project file found") |
| 35 | + } |
| 36 | + |
| 37 | + deps := collectJavaDeps(filepath.Join(dir, foundFile), foundFile) |
| 38 | + framework := detectJavaFramework(deps, dir, foundFile) |
| 39 | + |
| 40 | + d.confidence = 95 |
| 41 | + |
| 42 | + return &ProjectInfo{ |
| 43 | + Language: "java", |
| 44 | + Framework: framework, |
| 45 | + PackageFile: foundFile, |
| 46 | + RootDir: dir, |
| 47 | + Dependencies: deps, |
| 48 | + }, nil |
| 49 | +} |
| 50 | + |
| 51 | +func (d *JavaDetector) Confidence() int { |
| 52 | + return d.confidence |
| 53 | +} |
| 54 | + |
| 55 | +func detectJavaFramework(deps []string, dir, packageFile string) string { |
| 56 | + // Check dependencies for known frameworks |
| 57 | + for _, dep := range deps { |
| 58 | + lower := strings.ToLower(dep) |
| 59 | + switch { |
| 60 | + case strings.Contains(lower, "spring-boot"): |
| 61 | + return "spring-boot" |
| 62 | + case strings.Contains(lower, "quarkus"): |
| 63 | + return "quarkus" |
| 64 | + case strings.Contains(lower, "micronaut"): |
| 65 | + return "micronaut" |
| 66 | + case strings.Contains(lower, "jakarta.ee") || strings.Contains(lower, "javax.servlet"): |
| 67 | + return "jakarta-ee" |
| 68 | + case strings.Contains(lower, "dropwizard"): |
| 69 | + return "dropwizard" |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // Also check the file content directly for Spring Boot parent POM |
| 74 | + if packageFile == "pom.xml" { |
| 75 | + data, err := os.ReadFile(filepath.Join(dir, packageFile)) |
| 76 | + if err == nil { |
| 77 | + content := strings.ToLower(string(data)) |
| 78 | + if strings.Contains(content, "spring-boot-starter-parent") || |
| 79 | + strings.Contains(content, "spring-boot-starter") { |
| 80 | + return "spring-boot" |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return "" |
| 86 | +} |
| 87 | + |
| 88 | +func collectJavaDeps(path, packageFile string) []string { |
| 89 | + switch { |
| 90 | + case packageFile == "pom.xml": |
| 91 | + return parsePomXML(path) |
| 92 | + case strings.HasPrefix(packageFile, "build.gradle"): |
| 93 | + return parseGradle(path) |
| 94 | + } |
| 95 | + return nil |
| 96 | +} |
| 97 | + |
| 98 | +// parsePomXML does a minimal parse to extract dependency artifact IDs from pom.xml |
| 99 | +func parsePomXML(path string) []string { |
| 100 | + f, err := os.Open(path) |
| 101 | + if err != nil { |
| 102 | + return nil |
| 103 | + } |
| 104 | + defer f.Close() |
| 105 | + |
| 106 | + var deps []string |
| 107 | + scanner := bufio.NewScanner(f) |
| 108 | + inDependency := false |
| 109 | + var currentGroup, currentArtifact string |
| 110 | + |
| 111 | + for scanner.Scan() { |
| 112 | + line := strings.TrimSpace(scanner.Text()) |
| 113 | + |
| 114 | + if strings.Contains(line, "<dependency>") { |
| 115 | + inDependency = true |
| 116 | + currentGroup = "" |
| 117 | + currentArtifact = "" |
| 118 | + continue |
| 119 | + } |
| 120 | + |
| 121 | + if strings.Contains(line, "</dependency>") { |
| 122 | + if inDependency && (currentGroup != "" || currentArtifact != "") { |
| 123 | + dep := currentGroup |
| 124 | + if currentArtifact != "" { |
| 125 | + if dep != "" { |
| 126 | + dep += ":" |
| 127 | + } |
| 128 | + dep += currentArtifact |
| 129 | + } |
| 130 | + deps = append(deps, dep) |
| 131 | + } |
| 132 | + inDependency = false |
| 133 | + continue |
| 134 | + } |
| 135 | + |
| 136 | + if inDependency { |
| 137 | + if groupID := extractXMLValue(line, "groupId"); groupID != "" { |
| 138 | + currentGroup = groupID |
| 139 | + } |
| 140 | + if artifactID := extractXMLValue(line, "artifactId"); artifactID != "" { |
| 141 | + currentArtifact = artifactID |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + return deps |
| 147 | +} |
| 148 | + |
| 149 | +// parseGradle does a minimal parse to extract dependencies from build.gradle |
| 150 | +func parseGradle(path string) []string { |
| 151 | + f, err := os.Open(path) |
| 152 | + if err != nil { |
| 153 | + return nil |
| 154 | + } |
| 155 | + defer f.Close() |
| 156 | + |
| 157 | + var deps []string |
| 158 | + scanner := bufio.NewScanner(f) |
| 159 | + |
| 160 | + for scanner.Scan() { |
| 161 | + line := strings.TrimSpace(scanner.Text()) |
| 162 | + |
| 163 | + // Match patterns like: implementation 'group:artifact:version' |
| 164 | + // or: implementation "group:artifact:version" |
| 165 | + for _, keyword := range []string{"implementation", "api", "compileOnly", "runtimeOnly", "testImplementation"} { |
| 166 | + if strings.HasPrefix(line, keyword+" ") || strings.HasPrefix(line, keyword+"(") { |
| 167 | + dep := extractGradleDep(line) |
| 168 | + if dep != "" { |
| 169 | + deps = append(deps, dep) |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + return deps |
| 176 | +} |
| 177 | + |
| 178 | +// extractXMLValue extracts the text content of a simple XML element |
| 179 | +func extractXMLValue(line, tag string) string { |
| 180 | + openTag := "<" + tag + ">" |
| 181 | + closeTag := "</" + tag + ">" |
| 182 | + |
| 183 | + startIdx := strings.Index(line, openTag) |
| 184 | + endIdx := strings.Index(line, closeTag) |
| 185 | + |
| 186 | + if startIdx >= 0 && endIdx > startIdx { |
| 187 | + return strings.TrimSpace(line[startIdx+len(openTag) : endIdx]) |
| 188 | + } |
| 189 | + return "" |
| 190 | +} |
| 191 | + |
| 192 | +// extractGradleDep extracts the dependency coordinate from a Gradle dependency line |
| 193 | +func extractGradleDep(line string) string { |
| 194 | + // Find the quoted string (single or double) |
| 195 | + for _, quote := range []string{"'", "\""} { |
| 196 | + start := strings.Index(line, quote) |
| 197 | + if start >= 0 { |
| 198 | + end := strings.Index(line[start+1:], quote) |
| 199 | + if end >= 0 { |
| 200 | + return line[start+1 : start+1+end] |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | + return "" |
| 205 | +} |
0 commit comments