From b17aa9674f23b90efdc26cad7198f037352504fa Mon Sep 17 00:00:00 2001 From: Cedomir Igaly Date: Sun, 18 Feb 2024 13:03:15 +0100 Subject: [PATCH] HHH-17613 - Adding recursion; avoiding regular expression --- .../jpamodelgen/ImportContextImpl.java | 28 +++++++++++++------ 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/ImportContextImpl.java b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/ImportContextImpl.java index 0d778f40c0..d20897c9ec 100644 --- a/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/ImportContextImpl.java +++ b/tooling/metamodel-generator/src/main/java/org/hibernate/jpamodelgen/ImportContextImpl.java @@ -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 imports = new TreeSet<>(); private final Set staticImports = new TreeSet<>(); private final Map 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 ) ); + } } }