HHH-17613 - Adding recursion; avoiding regular expression

This commit is contained in:
Cedomir Igaly 2024-02-18 13:03:15 +01:00 committed by Marco Belladelli
parent ce805efc0b
commit e427ff47a9
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.Objects;
import java.util.Set; import java.util.Set;
import java.util.TreeSet; import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.hibernate.jpamodelgen.model.ImportContext; import org.hibernate.jpamodelgen.model.ImportContext;
@ -24,8 +22,6 @@ import org.hibernate.jpamodelgen.model.ImportContext;
*/ */
public class ImportContextImpl implements 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> imports = new TreeSet<>();
private final Set<String> staticImports = new TreeSet<>(); private final Set<String> staticImports = new TreeSet<>();
private final Map<String, String> simpleNames = new HashMap<>(); private final Map<String, String> simpleNames = new HashMap<>();
@ -78,10 +74,26 @@ public class ImportContextImpl implements ImportContext {
} }
} }
else if ( result.startsWith( "?" ) ) { else if ( result.startsWith( "?" ) ) {
final Matcher m = P_IMPORT.matcher( result ); int index = 1;
if ( m.matches() ) { while ( index < result.length() && Character.isWhitespace( result.charAt( index ) ) ) {
preamble = m.group( 1 ); index++;
result = Objects.requireNonNullElse( m.group( 2 ), "" ); }
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 ) );
}
} }
} }