66
77import org .antlr .v4 .runtime .CharStreams ;
88import org .antlr .v4 .runtime .CommonTokenStream ;
9+ import org .checkerframework .checker .nullness .qual .NonNull ;
910import org .checkerframework .checker .nullness .qual .Nullable ;
1011import org .hibernate .engine .spi .SessionFactoryImplementor ;
1112import org .hibernate .grammars .graph .GraphLanguageLexer ;
1213import org .hibernate .grammars .graph .GraphLanguageParser ;
14+ import org .hibernate .grammars .graph .GraphLanguageParser .GraphContext ;
1315import org .hibernate .graph .InvalidGraphException ;
1416import org .hibernate .graph .internal .RootGraphImpl ;
1517import org .hibernate .graph .spi .GraphImplementor ;
2224 * @author Steve Ebersole
2325 */
2426public class GraphParsing {
27+
2528 public static <T > RootGraphImplementor <T > parse (
26- Class <T > entityClass ,
29+ EntityDomainType <T > entityDomainType ,
2730 String graphText ,
2831 SessionFactoryImplementor sessionFactory ) {
2932 if ( graphText == null ) {
3033 return null ;
3134 }
3235
33- final var lexer = new GraphLanguageLexer ( CharStreams .fromString ( graphText ) );
34- final var parser = new GraphLanguageParser ( new CommonTokenStream ( lexer ) );
35- final var graphContext = parser .graph ();
36-
36+ final var graphContext = parseText ( graphText );
3737 if ( graphContext .typeIndicator () != null ) {
3838 // todo : an alternative here would be to simply validate that the entity type
3939 // from the text matches the passed one...
40- throw new InvalidGraphException ( "Expecting graph text to not include an entity name : " + graphText );
40+ throw new InvalidGraphException ( "Expecting graph text to not include an entity name: " + graphText );
4141 }
4242
43- final var entityType = sessionFactory .getJpaMetamodel ().entity ( entityClass );
44- return parse ( entityType , graphContext .attributeList (), sessionFactory );
43+ return parse ( entityDomainType , graphContext .attributeList (), sessionFactory );
4544 }
4645
4746 public static <T > RootGraphImplementor <T > parse (
48- EntityDomainType <T > entityDomainType ,
47+ Class <T > entityClass ,
4948 String graphText ,
5049 SessionFactoryImplementor sessionFactory ) {
51- if ( graphText == null ) {
52- return null ;
53- }
54-
55- final var lexer = new GraphLanguageLexer ( CharStreams .fromString ( graphText ) );
56- final var parser = new GraphLanguageParser ( new CommonTokenStream ( lexer ) );
57- final var graphContext = parser .graph ();
58-
59- if ( graphContext .typeIndicator () != null ) {
60- // todo : an alternative here would be to simply validate that the entity type
61- // from the text matches the passed one...
62- throw new InvalidGraphException ( "Expecting graph text to not include an entity name : " + graphText );
63- }
64-
65- return parse ( entityDomainType , graphContext .attributeList (), sessionFactory );
50+ return parse ( sessionFactory .getJpaMetamodel ().entity ( entityClass ),
51+ graphText , sessionFactory );
6652 }
6753
6854 public static RootGraphImplementor <?> parse (
6955 String entityName ,
7056 String graphText ,
7157 SessionFactoryImplementor sessionFactory ) {
72- if ( graphText == null ) {
73- return null ;
74- }
75-
76- final var lexer = new GraphLanguageLexer ( CharStreams .fromString ( graphText ) );
77- final var parser = new GraphLanguageParser ( new CommonTokenStream ( lexer ) );
78- final var graphContext = parser .graph ();
79-
80- if ( graphContext .typeIndicator () != null ) {
81- // todo : an alternative here would be to simply validate that the entity type
82- // from the text matches the passed one...
83- throw new InvalidGraphException ( "Expecting graph text to not include an entity name : " + graphText );
84- }
85-
86- final var entityType = sessionFactory .getJpaMetamodel ().entity ( entityName );
87- return parse ( entityType , graphContext .attributeList (), sessionFactory );
58+ return parse ( sessionFactory .getJpaMetamodel ().entity ( entityName ),
59+ graphText , sessionFactory );
8860 }
8961
9062 public static RootGraphImplementor <?> parse (
@@ -94,12 +66,9 @@ public static RootGraphImplementor<?> parse(
9466 return null ;
9567 }
9668
97- final var lexer = new GraphLanguageLexer ( CharStreams .fromString ( graphText ) );
98- final var parser = new GraphLanguageParser ( new CommonTokenStream ( lexer ) );
99- final var graphContext = parser .graph ();
100-
69+ final var graphContext = parseText ( graphText );
10170 if ( graphContext .typeIndicator () == null ) {
102- throw new InvalidGraphException ( "Expecting graph text to include an entity name : " + graphText );
71+ throw new InvalidGraphException ( "Expecting graph text to include an entity name: " + graphText );
10372 }
10473
10574 final String entityName = graphContext .typeIndicator ().TYPE_NAME ().getText ();
@@ -121,24 +90,19 @@ public static <T> RootGraphImplementor<T> parse(
12190 return parse ( null , rootType , attributeListContext , entityNameResolver );
12291 }
12392
93+ private static @ NonNull GraphContext parseText (String graphText ) {
94+ final var lexer = new GraphLanguageLexer ( CharStreams .fromString ( graphText ) );
95+ final var parser = new GraphLanguageParser ( new CommonTokenStream ( lexer ) );
96+ return parser .graph ();
97+ }
98+
12499 public static <T > RootGraphImplementor <T > parse (
125100 @ Nullable String name ,
126101 EntityDomainType <T > rootType ,
127102 GraphLanguageParser .AttributeListContext attributeListContext ,
128103 EntityNameResolver entityNameResolver ) {
129104 final RootGraphImpl <T > targetGraph = new RootGraphImpl <>( name , rootType );
130-
131- final var visitor = new GraphParser ( entityNameResolver );
132- visitor .getGraphStack ().push ( targetGraph );
133- try {
134- visitor .visitAttributeList ( attributeListContext );
135- }
136- finally {
137- visitor .getGraphStack ().pop ();
138-
139- assert visitor .getGraphStack ().isEmpty ();
140- }
141-
105+ visitGraph ( targetGraph , entityNameResolver , attributeListContext );
142106 return targetGraph ;
143107 }
144108
@@ -150,25 +114,28 @@ public static void parseInto(
150114 GraphImplementor <?> targetGraph ,
151115 CharSequence graphString ,
152116 SessionFactoryImplementor sessionFactory ) {
153- final var lexer = new GraphLanguageLexer ( CharStreams .fromString ( graphString .toString () ) );
154- final var parser = new GraphLanguageParser ( new CommonTokenStream ( lexer ) );
155- final var graphContext = parser .graph ();
156-
117+ final var graphContext = parseText ( graphString .toString () );
157118 if ( graphContext .typeIndicator () != null ) {
158119 // todo : throw an exception? Log warning? Ignore?
159120 // for now, ignore
160121 }
122+ visitGraph ( targetGraph ,
123+ sessionFactory .getJpaMetamodel ()::findEntityType ,
124+ graphContext .attributeList () );
125+ }
161126
127+ private static void visitGraph (
128+ GraphImplementor <?> targetGraph ,
129+ EntityNameResolver entityNameResolver ,
130+ GraphLanguageParser .AttributeListContext attributeList ) {
162131 // Build an instance of this class as a visitor
163- final var visitor = new GraphParser ( sessionFactory );
164-
132+ final var visitor = new GraphParser ( entityNameResolver );
165133 visitor .getGraphStack ().push ( targetGraph );
166134 try {
167- visitor .visitAttributeList ( graphContext . attributeList () );
135+ visitor .visitAttributeList ( attributeList );
168136 }
169137 finally {
170138 visitor .getGraphStack ().pop ();
171-
172139 assert visitor .getGraphStack ().isEmpty ();
173140 }
174141 }
0 commit comments