Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
allow querying complex expressions in JPA content assist
  • Loading branch information
danthe1st committed Feb 21, 2023
commit 4d2edf9acaa559d015b0d5bdb281d7d6faef0d4e
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,11 @@ private static String buildSignature(String methodName, Map<String, DomainProper
List<String> parameters = parseResult.parameters();
for(int i = 0; i < parameters.size(); i++){
String param = parameters.get(i);
if (properties.containsKey(param)) {
signatureBuilder.append(properties.get(param).getType().getSimpleName());
} else {
DomainType type = findExpressionType(properties, param);
if (type == null) {
signatureBuilder.append("Object");
}else {
signatureBuilder.append(type.getSimpleName());
}
signatureBuilder.append(" ");
signatureBuilder.append(StringUtils.uncapitalize(param));
Expand All @@ -126,6 +127,27 @@ private static String buildSignature(String methodName, Map<String, DomainProper
return signatureBuilder.toString();
}

private static DomainType findExpressionType(Map<String, DomainProperty> properties, String param) {
String[] splitByUnderscore = param.split("_");
if(properties.containsKey(splitByUnderscore[0])) {
DomainType type = properties.get(splitByUnderscore[0]).getType();
for (int j = 1; j < splitByUnderscore.length && type != null; j++) {
type = findMatchingParameter(splitByUnderscore[j], type);
}
return type;
}
return null;
}

private static DomainType findMatchingParameter(String name, DomainType type) {
for(DomainProperty prop : type.getProperties()){
if (prop.getName().equals(name)) {
return prop.getType();
}
}
return null;
}

private static void addQueryStartProposals(Collection<ICompletionProposal> completions, String prefix, int offset) {
for(QueryMethodSubject queryMethodSubject : QueryMethodSubject.QUERY_METHOD_SUBJECTS){
String toInsert = queryMethodSubject.key() + "By";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ void testPrefixSensitiveCompletionsCompleteMethod() throws Exception {
@Test
void testPrefixSensitiveCompletionsCompleteMethodReturnTypePresent() throws Exception {
checkCompletions("List<Customer> findByFirstNameAndLastName", "List<Customer> findByFirstNameAndLastName(String firstName, String lastName);");
checkCompletions("boolean existsByFirstNameAndLastName", "boolean existsByFirstNameAndLastName(String firstName, String lastName);");
}

@Test
Expand Down Expand Up @@ -135,6 +136,12 @@ void testPropertyProposals() throws Exception {
"findByLastName");
}

@Test
void findByComplexExpression() throws Exception {
checkCompletions("findByResponsibleEmployee", "List<Customer> findByResponsibleEmployee(Employee responsibleEmployee);");
checkCompletions("findByResponsibleEmployee_SocialSecurityNumber", "List<Customer> findByResponsibleEmployee_SocialSecurityNumber(Long responsibleEmployee_SocialSecurityNumber);");
}

private void checkCompletions(String alredyPresent, String... expectedCompletions) throws Exception {
prepareCase("{\n}", "{\n\t" + alredyPresent + "<*>");
assertContainsAnnotationCompletions(Arrays.stream(expectedCompletions).map(expected -> "\t" + expected + "<*>").toArray(String[]::new));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,39 @@

@SpringBootApplication
public class Application {

private static final Logger log = LoggerFactory.getLogger(Application.class);

public static void main(String[] args) {
SpringApplication.run(Application.class);
}

@Bean
public CommandLineRunner demo(CustomerRepository repository) {
return args -> {
Employee employee = new Employee("Margot", "Al-Harazi");
Employee employee = new Employee(1234, "Margot", "Al-Harazi");
// save a couple of customers
repository.save(new Customer("Jack", "Bauer", employee));
repository.save(new Customer("Chloe", "O'Brian", employee));
repository.save(new Customer("Kim", "Bauer", employee));
repository.save(new Customer("David", "Palmer", employee));
repository.save(new Customer("Michelle", "Dessler", employee));

// fetch all customers
log.info("Customers found with findAll():");
log.info("-------------------------------");
for(Customer customer : repository.findAll()){
log.info(customer.toString());
}
log.info("");

// fetch an individual customer by ID
Customer customer = repository.findOne(1L);
log.info("Customer found with findOne(1L):");
log.info("--------------------------------");
log.info(customer.toString());
log.info("");

// fetch customers by last name
log.info("Customer found with findByLastName('Bauer'):");
log.info("--------------------------------------------");
Expand All @@ -51,5 +51,5 @@ public CommandLineRunner demo(CustomerRepository repository) {
log.info("");
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,39 @@

@Entity
public class Employee {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private Long socialSecurityNumber;
private String firstName;
private String lastName;

protected Employee() {
}

public Employee(String firstName, String lastName) {

public Employee(long socialSecurityNumber, String firstName, String lastName) {
this.socialSecurityNumber = socialSecurityNumber;
this.firstName = firstName;
this.lastName = lastName;
}

@Override
public String toString() {
return String.format(
"Employee[id=%d, firstName='%s', lastName='%s']",
id, firstName, lastName
"Employee[socialSecurityNumber=%d, firstName='%s', lastName='%s']",
socialSecurityNumber, firstName, lastName
);
}

// end::sample[]
public Long getId() {
return id;

public Long getSocialSecurityNumber() {
return socialSecurityNumber;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}
Expand Down