Skip to content

Commit

Permalink
🐛 Adding parsing of non WEB-INF classes as dependencies (#553)
Browse files Browse the repository at this point in the history
fixes #533

Signed-off-by: Shawn Hurley <[email protected]>
  • Loading branch information
shawn-hurley committed Mar 26, 2024
1 parent 882b21f commit 423277f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
33 changes: 33 additions & 0 deletions provider/internal/java/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ func (p *javaServiceClient) discoverDepsFromJars(path string, ll map[uri.URI][]k
depToLabels: p.depToLabels,
m2RepoPath: getMavenLocalRepoPath(p.mvnSettingsFile),
seen: map[string]bool{},
initialPath: path,
}
filepath.WalkDir(path, w.walkDirForJar)
}
Expand All @@ -331,6 +332,7 @@ type walker struct {
deps map[uri.URI][]provider.DepDAGItem
depToLabels map[string]*depLabelItem
m2RepoPath string
initialPath string
seen map[string]bool
}

Expand Down Expand Up @@ -370,6 +372,37 @@ func (w *walker) walkDirForJar(path string, info fs.DirEntry, err error) error {
},
}
}
if strings.HasSuffix(info.Name(), ".class") {
// If the class is in WEB-INF we assume this is apart of the application
relPath, _ := filepath.Rel(w.initialPath, path)
relPath = filepath.Dir(relPath)
if strings.Contains(relPath, "WEB-INF") {
return nil
}
if _, ok := w.seen[relPath]; ok {
return nil
}
d := provider.Dep{
Name: info.Name(),
}
artifact, _ := toFilePathDependency(context.Background(), filepath.Join(relPath, info.Name()))
if (artifact != javaArtifact{}) {
d.Name = fmt.Sprintf("%s.%s", artifact.GroupId, artifact.ArtifactId)
d.Version = artifact.Version
d.Labels = addDepLabels(w.depToLabels, d.Name)
d.ResolvedIdentifier = artifact.sha1
// when we can successfully get javaArtifact from a jar
// we added it to the pom and it should be in m2Repo path
d.FileURIPrefix = fmt.Sprintf("file://%s", filepath.Join("java-project", "src", "main",
strings.Replace(artifact.GroupId, ".", "/", -1), artifact.ArtifactId))
}
w.deps[uri.URI(filepath.Join(relPath))] = []provider.DepDAGItem{
{
Dep: d,
},
}
w.seen[relPath] = true
}
return nil
}

Expand Down
45 changes: 43 additions & 2 deletions provider/internal/java/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,13 @@ func explode(ctx context.Context, log logr.Logger, archivePath, projectPath stri
if _, err := io.Copy(dstFile, archiveFile); err != nil {
return "", decompileJobs, dependencies, err
}
seenDirArtificat := map[string]interface{}{}
switch {
// when it's a .class file, decompile it into java project
case strings.HasSuffix(f.Name, ClassFile):
// when it's a .class file and it is in the web-inf, decompile it into java project
// This is the users code.
case strings.HasSuffix(f.Name, ClassFile) &&
(strings.Contains(f.Name, "WEB-INF") || strings.Contains(f.Name, "META-INF")):

// full path in the java project for the decompd file
destPath := filepath.Join(
projectPath, "src", "main", "java",
Expand All @@ -302,6 +306,30 @@ func explode(ctx context.Context, log logr.Logger, archivePath, projectPath stri
packaging: ClassFile,
},
})
// when it's a .class file and it is not in the web-inf, decompile it into java project
// This is some dependency that is not packaged as dependency.
case strings.HasSuffix(f.Name, ClassFile) &&
!(strings.Contains(f.Name, "WEB-INF") || strings.Contains(f.Name, "META-INF")):
destPath := filepath.Join(
projectPath, "src", "main", "java",
strings.Replace(filePath, destDir, "", -1))
destPath = strings.TrimSuffix(destPath, ClassFile) + ".java"
decompileJobs = append(decompileJobs, decompileJob{
inputPath: filePath,
outputPath: destPath,
artifact: javaArtifact{
packaging: ClassFile,
},
})
if _, ok := seenDirArtificat[filepath.Dir(f.Name)]; !ok {
dep, err := toFilePathDependency(ctx, f.Name)
if err != nil {
log.V(8).Error(err, "error getting dependcy for path", "path", destPath)
continue
}
dependencies = append(dependencies, dep)
seenDirArtificat[filepath.Dir(f.Name)] = nil
}
// when it's a java file, it's already decompiled, move it to project path
case strings.HasSuffix(f.Name, JavaFile):
destPath := filepath.Join(
Expand Down Expand Up @@ -558,3 +586,16 @@ func constructArtifactFromSHA(jarFile string) (javaArtifact, error) {
}
return dep, fmt.Errorf("failed to construct artifact from maven lookup")
}

func toFilePathDependency(ctx context.Context, filePath string) (javaArtifact, error) {
dep := javaArtifact{}
// Move up one level to the artifact. we are assuming that we get the full class file here.
// For instance the dir /org/springframework/boot/loader/jar/Something.class.
// in this cass the artificat is: Group: org.springframework.boot.loader, Artifact: Jar
dir := filepath.Dir(filePath)
dep.ArtifactId = filepath.Base(dir)
dep.GroupId = strings.Replace(filepath.Dir(dir), "/", ".", -1)
dep.Version = "0.0.0"
return dep, nil

}

0 comments on commit 423277f

Please sign in to comment.