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