Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Allow removing exploded Java bins #765

Merged
merged 2 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const (
MVN_SETTINGS_FILE_INIT_OPTION = "mavenSettingsFile"
GLOBAL_SETTINGS_INIT_OPTION = "mavenCacheDir"
MVN_INSECURE_SETTING = "mavenInsecure"
CLEAN_EXPLODED_BIN_OPTION = "cleanExplodedBin"
JVM_MAX_MEM_INIT_OPTION = "jvmMaxMem"
FERN_FLOWER_INIT_OPTION = "fernFlowerPath"
)
Expand Down Expand Up @@ -336,10 +337,13 @@ func (p *javaProvider) Init(ctx context.Context, log logr.Logger, config provide
}

extension := strings.ToLower(path.Ext(config.Location))
explodedBins := []string{}
switch extension {
case JavaArchive, WebArchive, EnterpriseArchive:
cleanBin, ok := config.ProviderSpecificConfig[CLEAN_EXPLODED_BIN_OPTION].(bool)

depLocation, sourceLocation, err := decompileJava(ctx, log, fernflower,
config.Location, getMavenLocalRepoPath(mavenSettingsFile))
config.Location, getMavenLocalRepoPath(mavenSettingsFile), ok)
if err != nil {
cancelFunc()
return nil, additionalBuiltinConfig, err
Expand All @@ -348,7 +352,13 @@ func (p *javaProvider) Init(ctx context.Context, log logr.Logger, config provide
// for binaries, we fallback to looking at .jar files only for deps
config.DependencyPath = depLocation
isBinary = true

if ok && cleanBin {
log.Info("removing exploded binaries after analysis")
explodedBins = append(explodedBins, depLocation, sourceLocation)
}
}

additionalBuiltinConfig.Location = config.Location
additionalBuiltinConfig.DependencyPath = config.DependencyPath

Expand Down Expand Up @@ -483,6 +493,7 @@ func (p *javaProvider) Init(ctx context.Context, log logr.Logger, config provide
globalSettings: globalSettingsFile,
depsLocationCache: make(map[string]int),
includedPaths: provider.GetIncludedPathsFromConfig(config, false),
cleanExplodedBins: explodedBins,
}

if mode == provider.FullAnalysisMode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type javaServiceClient struct {
depsCache map[uri.URI][]*provider.Dep
depsLocationCache map[string]int
includedPaths []string
cleanExplodedBins []string
}

type depLabelItem struct {
Expand Down Expand Up @@ -222,6 +223,11 @@ func (p *javaServiceClient) Stop() {
if err != nil {
p.log.Info("stopping java provider", "error", err)
}
if len(p.cleanExplodedBins) > 0 {
for _, explodedPath := range p.cleanExplodedBins {
os.RemoveAll(explodedPath)
}
}
}

func (p *javaServiceClient) initialization(ctx context.Context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import (
"strings"
"sync"
"text/template"
"time"

"math/rand"

"github.com/go-logr/logr"
"github.com/konveyor/analyzer-lsp/tracing"
Expand Down Expand Up @@ -159,11 +162,16 @@ func decompile(ctx context.Context, log logr.Logger, filter decompileFilter, wor
// decompileJava unpacks archive at archivePath, decompiles all .class files in it
// creates new java project and puts the java files in the tree of the project
// returns path to exploded archive, path to java project, and an error when encountered
func decompileJava(ctx context.Context, log logr.Logger, fernflower, archivePath string, m2RepoPath string) (explodedPath, projectPath string, err error) {
func decompileJava(ctx context.Context, log logr.Logger, fernflower, archivePath string, m2RepoPath string, cleanBin bool) (explodedPath, projectPath string, err error) {
ctx, span := tracing.StartNewSpan(ctx, "decompile")
defer span.End()

projectPath = filepath.Join(filepath.Dir(archivePath), "java-project")
// only need random project name if there is not dir cleanup after
if cleanBin {
projectPath = filepath.Join(filepath.Dir(archivePath), fmt.Sprintf("java-project-%v", RandomName()))
} else {
projectPath = filepath.Join(filepath.Dir(archivePath), "java-project")
}

decompFilter := alwaysDecompileFilter(true)

Expand Down Expand Up @@ -626,4 +634,14 @@ func toFilePathDependency(_ context.Context, filePath string) (javaArtifact, err
dep.Version = "0.0.0"
return dep, nil

}
}

func RandomName() string {
rand.Seed(int64(time.Now().Nanosecond()))
charset := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
b := make([]byte, 16)
for i := range b {
b[i] = charset[rand.Intn(len(charset))]
}
return string(b)
}
Loading