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

Create Rules for SimpleEvaluationContext #192

Closed
wants to merge 5 commits into from
Closed
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 @@ -41,3 +41,29 @@
- title: 'Spring 6.0 migration guide'
url: https://github.com/spring-projects/spring-framework/wiki/Spring-Framework-6.0-Release-Notes#core-container

- ruleID: spring-framework-5.x-to-6.0-core-container-00020
category: potential
effort: 1
labels:
- konveyor.io/source=spring5
- konveyor.io/target=spring6+
when:
or:
- java.referenced:
location: FIELD
pattern: org.springframework.expression.spel.support.SimpleEvaluationContext
- java.referenced:
location: CONSTRUCTOR_CALL
pattern: org.springframework.expression.spel.support.SimpleEvaluationContext
description: Replace SimpleEvaluationContext with StandardEvaluationContext when working with array operations.
message: |
`SimpleEvaluationContext` now disallows creating arrays inside expressions which aligns with how regular constructor resolution works.

Code that relies on creating arrays within expressions using `SimpleEvaluationContext` might malfunction or produce unexpected results.

It is recommended to switch to `org.springframework.expression.spel.standard.StandardEvaluationContext` as it offers the full SpEL functionality,
including array creation within expressions as well as improved performance.
links:
- title: 'Spring 6.0 migration guide'
url: https://github.com/spring-projects/spring-framework/wiki/Spring-Framework-6.0-Release-Notes#core-container

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package java.org.konveyor.spel;

import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.SimpleEvaluationContext;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DynamicQueryService {

public String generateSql(List<Integer> ids) {
ExpressionParser parser = new SpelExpressionParser();
SimpleEvaluationContext context = new SimpleEvaluationContext();

context.setRootObject(ids);
Expression expression = parser.parseExpression("'IN (' + #root.join(\',\') + ')'");
String inClause = (String) expression.getValue(context);
return "SELECT * FROM users WHERE user_id " + inClause;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package java.org.konveyor.spel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.*;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class SecondaryQueryService {

@Autowired
private SimpleEvaluationContext evaluationContext;

public String generateSql(List<Integer> ids) {
ExpressionParser parser = new SpelExpressionParser();

evaluationContext.setRootObject(ids);
Expression expression = parser.parseExpression("'IN (' + #root.join(\',\') + ')'");
String inClause = (String) expression.getValue(evaluationContext);
return "SELECT * FROM users WHERE user_id " + inClause;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ tests:
mode: "source-only"
hasIncidents:
exactly: 2
- ruleID: spring-framework-5.x-to-6.0-core-container-00020
testCases:
- name: tc-2
analysisParams:
mode: "source-only"
hasIncidents:
exactly: 2
Loading