I know your first reaction will be "why on earth would you do this, your method names are clearly ridiculous" but it's because I'm using Spring Boot JPA where you can use the method name to construct the query for you by reflection (it's amazing). But when querying based on a few different columns and the entity variables have longish names the method name ends up being quite long and hard to read, so I was wondering if there is a way of splitting it across multiple lines?
At the moment I have something like (simplified for this question):
public List<Employee> findByFirstNameAndLastNameAndAgeBetweenAndBirthdayBefore(String firstName, String lastName, Integer minAge, Integer maxAge, Date maxBirthday);
And I would like it in my code to be more like:
public List<Employee> findBy
FirstName
AndLastName
AndAgeBetween
AndBirthdayBefore
(String firstName, String lastName, Integer minAge, Integer maxAge, Date maxBirthday);
Is this at all possible?
A method name is an identifier, which is made up of IdentifierChars.
IdentifierChars are defined as starting with a Java Letter, and thereafter may be a Java Letter or Digit. Those are described in the Javadoc of Character.isJavaIdentifierPart (and isJavaIdentifierStart):
A character may be part of a Java identifier if any of the following are true:
- it is a letter it is a currency symbol (such as '$') it is a
- connecting punctuation character (such as '_')
- it is a digit
- it is a numeric letter (such as a Roman numeral character)
- it is a combining mark
- it is a non-spacing mark
isIdentifierIgnorable(codePoint)returns true
And isIdentifierIgnorable(int) says (emphasis mine):
The following Unicode characters are ignorable in a Java identifier or a Unicode identifier:
- ISO control characters that are not whitespace
- '\u0000' through '\u0008'
- '\u000E' through '\u001B'
- '\u007F' through '\u009F'
- all characters that have the FORMAT general category value
So no, you can't have newlines in a method name.
Well, that is an invalid method declaration, Java doesn't allow you to extend method name to next line but even if it does it is still not a good practice.
I guess you trying to do this because of long method names, and even Spring Data JPA documentation suggests that if your method names are growing large you consider writing the query by using @Query annotation or use Query DSL.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With