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

RemoveBoolVariation recipe with methodPattern for other SDK(Other than LaunchDrakly) is throwing TraitErrors #23

Closed
venuravu opened this issue Jun 9, 2024 · 5 comments · Fixed by #24
Assignees
Labels
bug Something isn't working

Comments

@venuravu
Copy link

venuravu commented Jun 9, 2024

What version of OpenRewrite are you using?

I am using

  • Java v17
  • OpenRewrite v6.14.0
  • Maven/Gradle plugin v6.14.0
  • rewrite-launchdrakly v0.5.0

How are you running OpenRewrite?

I am using the Gradle plugin, and my project is a multi module project.

plugins {
    id 'org.springframework.boot' version '3.0.4'
    id 'io.spring.dependency-management' version '1.1.0'
    id 'java'
    id 'org.openrewrite.rewrite' version '6.14.0'
}

What is the smallest, simplest way to reproduce the problem?

public class ToggleChecker {
	public ToggleChecker() {
		
	}
	
	public  boolean isToggleEnabled(String toggleName,boolean fallback) {
		Map<String,Boolean> toggleMap=new  HashMap<>();
		toggleMap.put("FEATURE_TOGGLE1", true);
		toggleMap.put("FEATURE_TOGGLE2", true);
		toggleMap.put("FEATURE_TOGGLE3", false);
		
		return toggleMap.containsKey(toggleName);
	}
	

}
//Service class
public class ServiceImpl {
	private ToggleChecker toggleChecker=new ToggleChecker();
	public  void methodA() {
		if(toggleChecker.isToggleEnabled("FEATURE_TOGGLE1",false)) {
		  LOG.info("Feature Toggle  enabled"); 
	  }else {
		  LOG.info("Feature Toggle Not enabled");
	  }
	}
	

}

rewrite.yml

type: specs.openrewrite.org/v1beta/recipe
name: com.osd.RemoveBoolVariationExample
displayName: Remove `isToggleEnabled` for feature key example
recipeList:
  - org.openrewrite.launchdarkly.RemoveBoolVariation:
      featureKey: FEATURE_TOGGLE1
      replacementValue: true
      methodPattern: com.osd.util.ToggleChecker isToggleEnabled(String,boolean)

What did you expect to see?

public class ServiceImpl {
	private ToggleChecker toggleChecker=new ToggleChecker();
	public  void methodA() {
		  LOG.info("Feature Toggle  enabled"); 
	
	}
	

What did you see instead?

Exception

What is the full stack trace of any errors you encountered?

* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':microservices:product-service:rewriteDryRun'.
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.lambda$executeIfValid$1(ExecuteActionsTaskExecuter.java:142)
        at org.gradle.internal.Try$Failure.ifSuccessfulOrElse(Try.java:282)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeIfValid(ExecuteActionsTaskExecuter.java:140)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:128)
        at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:77)
        at org.gradle.api.internal.tasks.execution.FinalizePropertiesTaskExecuter.execute(FinalizePropertiesTaskExecuter.java:46)
        at org.gradle.api.internal.tasks.execution.ResolveTaskExecutionModeExecuter.execute(ResolveTaskExecutionModeExecuter.java:51)
        at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:57)
        at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:57)
        at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:36)
        at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.executeTask(EventFiringTaskExecuter.java:77)
        at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.call(EventFiringTaskExecuter.java:55)
        at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.call(EventFiringTaskExecuter.java:52)
        at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:204)
        at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:199)
        at org.gradle.internal.operations.DefaultBuildOperationRunner$2.execute(DefaultBuildOperationRunner.java:66)
        at org.gradle.internal.operations.DefaultBuildOperationRunner$2.execute(DefaultBuildOperationRunner.java:59)
        at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:157)
        at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:59)
        at org.gradle.internal.operations.DefaultBuildOperationRunner.call(DefaultBuildOperationRunner.java:53)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor.call(DefaultBuildOperationExecutor.java:73)
        at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter.execute(EventFiringTaskExecuter.java:52)
        at org.gradle.execution.plan.LocalTaskNodeExecutor.execute(LocalTaskNodeExecutor.java:69)
        at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$InvokeNodeExecutorsAction.execute(DefaultTaskExecutionGraph.java:322)
        at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$InvokeNodeExecutorsAction.execute(DefaultTaskExecutionGraph.java:309)
        at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$BuildOperationAwareExecutionAction.execute(DefaultTaskExecutionGraph.java:302)
        at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$BuildOperationAwareExecutionAction.execute(DefaultTaskExecutionGraph.java:288)
        at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker.execute(DefaultPlanExecutor.java:462)
        at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker.run(DefaultPlanExecutor.java:379)
        at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
        at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:49)
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: Error while visiting com/services/ServiceImpl.java: org.openrewrite.analysis.trait.util.TraitErrorsException: TraitErrors: 
        - Method must be created from class org.openrewrite.java.tree.J$MethodDeclaration but was class org.openrewrite.java.tree.J$Lambda
        - No parent Method found
        - Field must be declared in a class, interface, or anonymous class
  org.openrewrite.analysis.trait.util.TraitErrors.doThrow(TraitErrors.java:43)
  fj.data.Either$RightProjection.on(Either.java:531)
  fj.data.Validation.on(Validation.java:146)
  org.openrewrite.analysis.trait.expr.VarAccessBase$1.visitVariable(VarAccess.java:187)
  org.openrewrite.analysis.trait.expr.VarAccessBase$1.visitVariable(VarAccess.java:175)
  org.openrewrite.java.tree.J$VariableDeclarations$NamedVariable.acceptJava(J.java:5955)
  org.openrewrite.java.tree.J.accept(J.java:59)
  org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
  org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:324)
  org.openrewrite.java.JavaVisitor.visitRightPadded(JavaVisitor.java:1369)
  org.openrewrite.java.JavaVisitor.lambda$visitVariableDeclarations$29(JavaVisitor.java:963)
  org.openrewrite.internal.ListUtils.map(ListUtils.java:176)
  org.openrewrite.java.JavaVisitor.visitVariableDeclarations(JavaVisitor.java:963)
  org.openrewrite.java.tree.J$VariableDeclarations.acceptJava(J.java:5840)
  org.openrewrite.java.tree.J.accept(J.java:59)
  org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
  ...
        at org.openrewrite.gradle.DelegatingProjectParser.unwrapInvocationException(DelegatingProjectParser.java:163)
        at org.openrewrite.gradle.DelegatingProjectParser.dryRun(DelegatingProjectParser.java:113)
        at org.openrewrite.gradle.RewriteDryRunTask.run(RewriteDryRunTask.java:47)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at org.gradle.internal.reflect.JavaMethod.invoke(JavaMethod.java:125)
        at org.gradle.api.internal.project.taskfactory.StandardTaskAction.doExecute(StandardTaskAction.java:58)
        at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:51)
        at org.gradle.api.internal.project.taskfactory.StandardTaskAction.execute(StandardTaskAction.java:29)
        at org.gradle.api.internal.tasks.execution.TaskExecution$3.run(TaskExecution.java:236)
        at org.gradle.internal.operations.DefaultBuildOperationRunner$1.execute(DefaultBuildOperationRunner.java:29)
        at org.gradle.internal.operations.DefaultBuildOperationRunner$1.execute(DefaultBuildOperationRunner.java:26)
        at org.gradle.internal.operations.DefaultBuildOperationRunner$2.execute(DefaultBuildOperationRunner.java:66)
        at org.gradle.internal.operations.DefaultBuildOperationRunner$2.execute(DefaultBuildOperationRunner.java:59)
        at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:157)
        at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:59)
        at org.gradle.internal.operations.DefaultBuildOperationRunner.run(DefaultBuildOperationRunner.java:47)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor.run(DefaultBuildOperationExecutor.java:68)
        at org.gradle.api.internal.tasks.execution.TaskExecution.executeAction(TaskExecution.java:221)
        at org.gradle.api.internal.tasks.execution.TaskExecution.executeActions(TaskExecution.java:204)
        at org.gradle.api.internal.tasks.execution.TaskExecution.executeWithPreviousOutputFiles(TaskExecution.java:187)
        at org.gradle.api.internal.tasks.execution.TaskExecution.execute(TaskExecution.java:165)
        at org.gradle.internal.execution.steps.ExecuteStep.executeInternal(ExecuteStep.java:89)
        at org.gradle.internal.execution.steps.ExecuteStep.access$000(ExecuteStep.java:40)
        at org.gradle.internal.execution.steps.ExecuteStep$1.call(ExecuteStep.java:53)
        at org.gradle.internal.execution.steps.ExecuteStep$1.call(ExecuteStep.java:50)
        at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:204)
        at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:199)
        at org.gradle.internal.operations.DefaultBuildOperationRunner$2.execute(DefaultBuildOperationRunner.java:66)
        at org.gradle.internal.operations.DefaultBuildOperationRunner$2.execute(DefaultBuildOperationRunner.java:59)
        at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:157)
        at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:59)
        at org.gradle.internal.operations.DefaultBuildOperationRunner.call(DefaultBuildOperationRunner.java:53)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor.call(DefaultBuildOperationExecutor.java:73)
        at org.gradle.internal.execution.steps.ExecuteStep.execute(ExecuteStep.java:50)
        at org.gradle.internal.execution.steps.ExecuteStep.execute(ExecuteStep.java:40)
        at org.gradle.internal.execution.steps.RemovePreviousOutputsStep.execute(RemovePreviousOutputsStep.java:68)
        at org.gradle.internal.execution.steps.RemovePreviousOutputsStep.execute(RemovePreviousOutputsStep.java:38)
        at org.gradle.internal.execution.steps.CancelExecutionStep.execute(CancelExecutionStep.java:41)
        at org.gradle.internal.execution.steps.TimeoutStep.executeWithoutTimeout(TimeoutStep.java:74)
        at org.gradle.internal.execution.steps.TimeoutStep.execute(TimeoutStep.java:55)
        at org.gradle.internal.execution.steps.CreateOutputsStep.execute(CreateOutputsStep.java:51)
        at org.gradle.internal.execution.steps.CreateOutputsStep.execute(CreateOutputsStep.java:29)
        at org.gradle.internal.execution.steps.CaptureStateAfterExecutionStep.executeDelegateBroadcastingChanges(CaptureStateAfterExecutionStep.java:124)
        at org.gradle.internal.execution.steps.CaptureStateAfterExecutionStep.execute(CaptureStateAfterExecutionStep.java:80)
        at org.gradle.internal.execution.steps.CaptureStateAfterExecutionStep.execute(CaptureStateAfterExecutionStep.java:58)
        at org.gradle.internal.execution.steps.ResolveInputChangesStep.execute(ResolveInputChangesStep.java:48)
        at org.gradle.internal.execution.steps.ResolveInputChangesStep.execute(ResolveInputChangesStep.java:36)
        at org.gradle.internal.execution.steps.BuildCacheStep.executeWithoutCache(BuildCacheStep.java:181)
        at org.gradle.internal.execution.steps.BuildCacheStep.lambda$execute$1(BuildCacheStep.java:71)
        at org.gradle.internal.Either$Right.fold(Either.java:175)
        at org.gradle.internal.execution.caching.CachingState.fold(CachingState.java:59)
        at org.gradle.internal.execution.steps.BuildCacheStep.execute(BuildCacheStep.java:69)
        at org.gradle.internal.execution.steps.BuildCacheStep.execute(BuildCacheStep.java:47)
        at org.gradle.internal.execution.steps.StoreExecutionStateStep.execute(StoreExecutionStateStep.java:36)
        at org.gradle.internal.execution.steps.StoreExecutionStateStep.execute(StoreExecutionStateStep.java:25)
        at org.gradle.internal.execution.steps.RecordOutputsStep.execute(RecordOutputsStep.java:36)
        at org.gradle.internal.execution.steps.RecordOutputsStep.execute(RecordOutputsStep.java:22)
        at org.gradle.internal.execution.steps.SkipUpToDateStep.executeBecause(SkipUpToDateStep.java:110)
        at org.gradle.internal.execution.steps.SkipUpToDateStep.lambda$execute$2(SkipUpToDateStep.java:56)
        at org.gradle.internal.execution.steps.SkipUpToDateStep.execute(SkipUpToDateStep.java:56)
        at org.gradle.internal.execution.steps.SkipUpToDateStep.execute(SkipUpToDateStep.java:38)
        at org.gradle.internal.execution.steps.ResolveChangesStep.execute(ResolveChangesStep.java:73)
        at org.gradle.internal.execution.steps.ResolveChangesStep.execute(ResolveChangesStep.java:44)
        at org.gradle.internal.execution.steps.legacy.MarkSnapshottingInputsFinishedStep.execute(MarkSnapshottingInputsFinishedStep.java:37)
        at org.gradle.internal.execution.steps.legacy.MarkSnapshottingInputsFinishedStep.execute(MarkSnapshottingInputsFinishedStep.java:27)
        at org.gradle.internal.execution.steps.ResolveCachingStateStep.execute(ResolveCachingStateStep.java:89)
        at org.gradle.internal.execution.steps.ResolveCachingStateStep.execute(ResolveCachingStateStep.java:50)
        at org.gradle.internal.execution.steps.ValidateStep.execute(ValidateStep.java:102)
        at org.gradle.internal.execution.steps.ValidateStep.execute(ValidateStep.java:57)
        at org.gradle.internal.execution.steps.CaptureStateBeforeExecutionStep.execute(CaptureStateBeforeExecutionStep.java:76)
        at org.gradle.internal.execution.steps.CaptureStateBeforeExecutionStep.execute(CaptureStateBeforeExecutionStep.java:50)
        at org.gradle.internal.execution.steps.SkipEmptyWorkStep.executeWithNoEmptySources(SkipEmptyWorkStep.java:254)
        at org.gradle.internal.execution.steps.SkipEmptyWorkStep.execute(SkipEmptyWorkStep.java:91)
        at org.gradle.internal.execution.steps.SkipEmptyWorkStep.execute(SkipEmptyWorkStep.java:56)
        at org.gradle.internal.execution.steps.RemoveUntrackedExecutionStateStep.execute(RemoveUntrackedExecutionStateStep.java:32)
        at org.gradle.internal.execution.steps.RemoveUntrackedExecutionStateStep.execute(RemoveUntrackedExecutionStateStep.java:21)
        at org.gradle.internal.execution.steps.legacy.MarkSnapshottingInputsStartedStep.execute(MarkSnapshottingInputsStartedStep.java:38)
        at org.gradle.internal.execution.steps.LoadPreviousExecutionStateStep.execute(LoadPreviousExecutionStateStep.java:43)
        at org.gradle.internal.execution.steps.LoadPreviousExecutionStateStep.execute(LoadPreviousExecutionStateStep.java:31)
        at org.gradle.internal.execution.steps.AssignWorkspaceStep.lambda$execute$0(AssignWorkspaceStep.java:40)
        at org.gradle.api.internal.tasks.execution.TaskExecution$4.withWorkspace(TaskExecution.java:281)
        at org.gradle.internal.execution.steps.AssignWorkspaceStep.execute(AssignWorkspaceStep.java:40)
        at org.gradle.internal.execution.steps.AssignWorkspaceStep.execute(AssignWorkspaceStep.java:30)
        at org.gradle.internal.execution.steps.IdentityCacheStep.execute(IdentityCacheStep.java:37)
        at org.gradle.internal.execution.steps.IdentityCacheStep.execute(IdentityCacheStep.java:27)
        at org.gradle.internal.execution.steps.IdentifyStep.execute(IdentifyStep.java:44)
        at org.gradle.internal.execution.steps.IdentifyStep.execute(IdentifyStep.java:33)
        at org.gradle.internal.execution.impl.DefaultExecutionEngine$1.execute(DefaultExecutionEngine.java:76)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeIfValid(ExecuteActionsTaskExecuter.java:139)
        at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(ExecuteActionsTaskExecuter.java:128)
        at org.gradle.api.internal.tasks.execution.CleanupStaleOutputsExecuter.execute(CleanupStaleOutputsExecuter.java:77)
        at org.gradle.api.internal.tasks.execution.FinalizePropertiesTaskExecuter.execute(FinalizePropertiesTaskExecuter.java:46)
        at org.gradle.api.internal.tasks.execution.ResolveTaskExecutionModeExecuter.execute(ResolveTaskExecutionModeExecuter.java:51)
        at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(SkipTaskWithNoActionsExecuter.java:57)
        at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(SkipOnlyIfTaskExecuter.java:57)
        at org.gradle.api.internal.tasks.execution.CatchExceptionTaskExecuter.execute(CatchExceptionTaskExecuter.java:36)
        at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.executeTask(EventFiringTaskExecuter.java:77)
        at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.call(EventFiringTaskExecuter.java:55)
        at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter$1.call(EventFiringTaskExecuter.java:52)
        at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:204)
        at org.gradle.internal.operations.DefaultBuildOperationRunner$CallableBuildOperationWorker.execute(DefaultBuildOperationRunner.java:199)
        at org.gradle.internal.operations.DefaultBuildOperationRunner$2.execute(DefaultBuildOperationRunner.java:66)
        at org.gradle.internal.operations.DefaultBuildOperationRunner$2.execute(DefaultBuildOperationRunner.java:59)
        at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:157)
        at org.gradle.internal.operations.DefaultBuildOperationRunner.execute(DefaultBuildOperationRunner.java:59)
        at org.gradle.internal.operations.DefaultBuildOperationRunner.call(DefaultBuildOperationRunner.java:53)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor.call(DefaultBuildOperationExecutor.java:73)
        at org.gradle.api.internal.tasks.execution.EventFiringTaskExecuter.execute(EventFiringTaskExecuter.java:52)
        at org.gradle.execution.plan.LocalTaskNodeExecutor.execute(LocalTaskNodeExecutor.java:69)
        at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$InvokeNodeExecutorsAction.execute(DefaultTaskExecutionGraph.java:322)
        at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$InvokeNodeExecutorsAction.execute(DefaultTaskExecutionGraph.java:309)
        at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$BuildOperationAwareExecutionAction.execute(DefaultTaskExecutionGraph.java:302)
        at org.gradle.execution.taskgraph.DefaultTaskExecutionGraph$BuildOperationAwareExecutionAction.execute(DefaultTaskExecutionGraph.java:288)
        at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker.execute(DefaultPlanExecutor.java:462)
        at org.gradle.execution.plan.DefaultPlanExecutor$ExecutorWorker.run(DefaultPlanExecutor.java:379)
        at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:64)
        at org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:49)
Caused by: java.lang.RuntimeException: Error while visiting com/services/ServiceImpl.java: org.openrewrite.analysis.trait.util.TraitErrorsException: TraitErrors: 
        - Method must be created from class org.openrewrite.java.tree.J$MethodDeclaration but was class org.openrewrite.java.tree.J$Lambda
        - No parent Method found
        - Field must be declared in a class, interface, or anonymous class
  org.openrewrite.analysis.trait.util.TraitErrors.doThrow(TraitErrors.java:43)
  fj.data.Either$RightProjection.on(Either.java:531)
  fj.data.Validation.on(Validation.java:146)
  org.openrewrite.analysis.trait.expr.VarAccessBase$1.visitVariable(VarAccess.java:187)
  org.openrewrite.analysis.trait.expr.VarAccessBase$1.visitVariable(VarAccess.java:175)
  org.openrewrite.java.tree.J$VariableDeclarations$NamedVariable.acceptJava(J.java:5955)
  org.openrewrite.java.tree.J.accept(J.java:59)
  org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
  org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:324)
  org.openrewrite.java.JavaVisitor.visitRightPadded(JavaVisitor.java:1369)
  org.openrewrite.java.JavaVisitor.lambda$visitVariableDeclarations$29(JavaVisitor.java:963)
  org.openrewrite.internal.ListUtils.map(ListUtils.java:176)
  org.openrewrite.java.JavaVisitor.visitVariableDeclarations(JavaVisitor.java:963)
  org.openrewrite.java.tree.J$VariableDeclarations.acceptJava(J.java:5840)
  org.openrewrite.java.tree.J.accept(J.java:59)
  org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
  ...
        at org.openrewrite.gradle.isolated.ResultsContainer$1.lambda$preVisit$0(ResultsContainer.java:115)
        at org.openrewrite.gradle.isolated.ResultsContainer$1.preVisit(ResultsContainer.java:112)
        at org.openrewrite.gradle.isolated.ResultsContainer$1.preVisit(ResultsContainer.java:108)
        at org.openrewrite.gradle.isolated.ResultsContainer_1_JavaVisitor.preVisit(ResultsContainer_1_JavaVisitor.zig:102)
        at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:247)
        at org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:324)
        at org.openrewrite.java.JavaVisitor.visitRightPadded(JavaVisitor.java:1369)
        at org.openrewrite.java.JavaVisitor.lambda$visitVariableDeclarations$29(JavaVisitor.java:963)
        at org.openrewrite.internal.ListUtils.map(ListUtils.java:176)
        at org.openrewrite.java.JavaVisitor.visitVariableDeclarations(JavaVisitor.java:963)
        at org.openrewrite.java.tree.J$VariableDeclarations.acceptJava(J.java:5840)
        at org.openrewrite.java.tree.J.accept(J.java:59)
        at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
        at org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:324)
        at org.openrewrite.java.JavaVisitor.visitRightPadded(JavaVisitor.java:1369)
        at org.openrewrite.java.JavaVisitor.lambda$visitLambda$17(JavaVisitor.java:795)
        at org.openrewrite.internal.ListUtils.map(ListUtils.java:176)
        at org.openrewrite.java.JavaVisitor.visitLambda(JavaVisitor.java:794)
        at org.openrewrite.java.tree.J$Lambda.acceptJava(J.java:3199)
        at org.openrewrite.java.tree.J.accept(J.java:59)
        at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
        at org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:324)
        at org.openrewrite.java.JavaVisitor.visitRightPadded(JavaVisitor.java:1369)
        at org.openrewrite.java.JavaVisitor.lambda$visitContainer$37(JavaVisitor.java:1419)
        at org.openrewrite.internal.ListUtils.map(ListUtils.java:176)
        at org.openrewrite.java.JavaVisitor.visitContainer(JavaVisitor.java:1419)
        at org.openrewrite.java.JavaVisitor.visitMethodInvocation(JavaVisitor.java:922)
        at org.openrewrite.java.tree.J$MethodInvocation.acceptJava(J.java:3945)
        at org.openrewrite.java.tree.J.accept(J.java:59)
        at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
        at org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:324)
        at org.openrewrite.java.JavaVisitor.visitLeftPadded(JavaVisitor.java:1397)
        at org.openrewrite.java.JavaVisitor.visitVariable(JavaVisitor.java:1303)
        at org.openrewrite.java.tree.J$VariableDeclarations$NamedVariable.acceptJava(J.java:5955)
        at org.openrewrite.java.tree.J.accept(J.java:59)
        at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
        at org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:324)
        at org.openrewrite.java.JavaVisitor.visitRightPadded(JavaVisitor.java:1369)
        at org.openrewrite.java.JavaVisitor.lambda$visitVariableDeclarations$29(JavaVisitor.java:963)
        at org.openrewrite.internal.ListUtils.map(ListUtils.java:176)
        at org.openrewrite.java.JavaVisitor.visitVariableDeclarations(JavaVisitor.java:963)
        at org.openrewrite.java.tree.J$VariableDeclarations.acceptJava(J.java:5840)
        at org.openrewrite.java.tree.J.accept(J.java:59)
        at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
        at org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:324)
        at org.openrewrite.java.JavaVisitor.visitRightPadded(JavaVisitor.java:1369)
        at org.openrewrite.java.JavaVisitor.lambda$visitBlock$4(JavaVisitor.java:401)
        at org.openrewrite.internal.ListUtils.map(ListUtils.java:176)
        at org.openrewrite.java.JavaVisitor.visitBlock(JavaVisitor.java:400)
        at org.openrewrite.java.tree.J$Block.acceptJava(J.java:841)
        at org.openrewrite.java.tree.J.accept(J.java:59)
        at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
        at org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:324)
        at org.openrewrite.java.JavaVisitor.visitMethodDeclaration(JavaVisitor.java:883)
        at org.openrewrite.java.tree.J$MethodDeclaration.acceptJava(J.java:3685)
        at org.openrewrite.java.tree.J.accept(J.java:59)
        at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
        at org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:324)
        at org.openrewrite.java.JavaVisitor.visitRightPadded(JavaVisitor.java:1369)
        at org.openrewrite.java.JavaVisitor.lambda$visitBlock$4(JavaVisitor.java:401)
        at org.openrewrite.internal.ListUtils.map(ListUtils.java:176)
        at org.openrewrite.java.JavaVisitor.visitBlock(JavaVisitor.java:400)
        at org.openrewrite.java.tree.J$Block.acceptJava(J.java:841)
        at org.openrewrite.java.tree.J.accept(J.java:59)
        at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
        at org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:324)
        at org.openrewrite.java.JavaVisitor.visitClassDeclaration(JavaVisitor.java:488)
        at org.openrewrite.java.tree.J$ClassDeclaration.acceptJava(J.java:1290)
        at org.openrewrite.java.tree.J.accept(J.java:59)
        at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
        at org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:324)
        at org.openrewrite.java.JavaVisitor.lambda$visitCompilationUnit$10(JavaVisitor.java:501)
        at org.openrewrite.internal.ListUtils.map(ListUtils.java:176)
        at org.openrewrite.java.JavaVisitor.visitCompilationUnit(JavaVisitor.java:501)
        at org.openrewrite.java.tree.J$CompilationUnit.acceptJava(J.java:1568)
        at org.openrewrite.java.tree.J.accept(J.java:59)
        at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
        at org.openrewrite.gradle.isolated.ResultsContainer.getRecipeErrors(ResultsContainer.java:119)
        at org.openrewrite.gradle.isolated.ResultsContainer.getFirstException(ResultsContainer.java:99)
        at org.openrewrite.gradle.isolated.DefaultProjectParser.dryRun(DefaultProjectParser.java:344)
        at org.openrewrite.gradle.isolated.DefaultProjectParser.dryRun(DefaultProjectParser.java:338)
        at org.openrewrite.gradle.DelegatingProjectParser.lambda$dryRun$3(DelegatingProjectParser.java:114)
        at org.openrewrite.gradle.DelegatingProjectParser.unwrapInvocationException(DelegatingProjectParser.java:155)

Are you interested in contributing a fix to OpenRewrite?

@venuravu venuravu added the bug Something isn't working label Jun 9, 2024
@timtebeek timtebeek moved this to Backlog in OpenRewrite Jun 10, 2024
@timtebeek
Copy link
Contributor

Thanks for the report here @venuravu ! This seems to fail around the CursorUtil we use to detect the key passed into the method, not specific to using a different SDK than LaunchDarkly. We would have expected the passed in argument to be a field or constant, and find any references from there. In the sample you're referencing we should have this work if it's a String literal as well; I'll see if that's an easy fix to make.

https://github.com/openrewrite/rewrite-launchdarkly/blob/f1173512dab1b9ff191fba029be1c41ddd74c684/src/main/java/org/openrewrite/launchdarkly/RemoveBoolVariation.java#L80-L85

@shanman190
Copy link
Collaborator

For this one I was kind of wondering if we needed to make sure we were on the right element prior to checking with CursorUtils as I was thinking we may be hitting a different method that accepts a Lambda and erroring out on that.

@timtebeek
Copy link
Contributor

For this one I was kind of wondering if we needed to make sure we were on the right element prior to checking with CursorUtils as I was thinking we may be hitting a different method that accepts a Lambda and erroring out on that.

Only seeing this now; how would you change the CursorUtil call? I didn't immediately spot good options there.

For now I'd gone for some defensive short circuiting as the issue failed to replicate in the minimal test:

@timtebeek timtebeek moved this from Backlog to Ready to Review in OpenRewrite Jun 10, 2024
@timtebeek timtebeek self-assigned this Jun 10, 2024
@shanman190
Copy link
Collaborator

shanman190 commented Jun 10, 2024

@timtebeek, the things that I know right now:

  • taking the example snippet and running it through the original code path succeeds without any issues at all
  • The ConstantFold.findConstantLiteralValue(c, String.class) does properly handle a literal, a local variable, or a constant field (in the same class).

So there has to be something more about @venuravu's original source that's causing rewrite-analysis to have an issue.

I think one way that we may be able to guard the rewrite-analysis CursorUtils call is by only invoking it once we have identified the matching method that we want to inspect first, then checking for the value as a nested if statement.

Maybe something like:

J.MethodInvocation mi = (J.MethodInvocation) super.visitMethodInvocation(method, ctx);
if (methodMatcher.matches(mi)) {
    boolean isFirstArgumentFeatureKey = 
            CursorUtil
                    .findCursorForTree(getCursor(), mi.getArguments().get(0))
                    .bind(c -> ConstantFold.findConstantLiteralValue(c, String.class))
                    .map(featureKey::equals)
                    .orSome(false);
    if (isFirstArgumentFeatureKey) {
        // modify
    }
}

Even with that said though, I'm really curious what other trait about the original class is tripping up rewrite-analysis because we can get that documented and fixed upstream such that this would be a bit more resilient going forward. Maybe @JLLeitschuh may have a suspicion with the stacktrace snippet that's been provided so far?

@timtebeek
Copy link
Contributor

Turns out we've encountered that message before:

So likely the thing to replicate here is having a lambda, but we can work around it with a guard as seen abovee.

@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Jun 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants