HHH-17613 - Adding recursion; avoiding regular expression

This commit is contained in:
Cedomir Igaly 2024-02-18 13:03:15 +01:00 committed by Gavin King
parent da41e5b4be
commit b17aa9674f
1 changed files with 20 additions and 8 deletions

View File

@ -11,8 +11,6 @@ import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.hibernate.jpamodelgen.model.ImportContext;
@ -24,8 +22,6 @@ import org.hibernate.jpamodelgen.model.ImportContext;
*/
public class ImportContextImpl implements ImportContext {
private static final Pattern P_IMPORT = Pattern.compile( "(\\?\\s+\\w+\\s+)(.+)" );
private final Set<String> imports = new TreeSet<>();
private final Set<String> staticImports = new TreeSet<>();
private final Map<String, String> simpleNames = new HashMap<>();
@ -78,10 +74,26 @@ public class ImportContextImpl implements ImportContext {
}
}
else if ( result.startsWith( "?" ) ) {
final Matcher m = P_IMPORT.matcher( result );
if ( m.matches() ) {
preamble = m.group( 1 );
result = Objects.requireNonNullElse( m.group( 2 ), "" );
int index = 1;
while ( index < result.length() && Character.isWhitespace( result.charAt( index ) ) ) {
index++;
}
if ( index < result.length() ) {
int nextIndex = -1;
if ( result.substring( index ).startsWith( "extends" ) ) {
nextIndex = index + 7;
}
else if ( result.substring( index ).startsWith( "super" ) ) {
nextIndex = index + 5;
}
if ( nextIndex > 0 && nextIndex < result.length() && Character.isWhitespace( result.charAt( nextIndex ) ) ) {
index = nextIndex;
while ( Character.isWhitespace( result.charAt( index ) ) ) {
index++;
}
preamble = result.substring( 0, index );
result = importType( result.substring( index ) );
}
}
}