Skip to content

Commit

Permalink
✨ Allow removing exploded Java bins (#765)
Browse files Browse the repository at this point in the history
Fixes https://issues.redhat.com/browse/MTA-4484

---------

Signed-off-by: Emily McMullan <emcmulla@redhat.com>
  • Loading branch information
eemcmullan authored Jan 29, 2025
1 parent 66002c2 commit 35bb8a3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
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)
}

0 comments on commit 35bb8a3

Please sign in to comment.