[hibernate-commits] Hibernate SVN: r11034 - in branches/HQL_ANTLR_2/Hibernate3: src/org/hibernate/hql/ast/resolve src/org/hibernate/hql/ast/util test/org/hibernate/test/hql test/org/hibernate/test/hql/redesign test/org/hibernate/test/hql/util

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Tue Jan 9 08:43:14 EST 2007


Author: steve.ebersole at jboss.com
Date: 2007-01-09 08:43:12 -0500 (Tue, 09 Jan 2007)
New Revision: 11034

Added:
   branches/HQL_ANTLR_2/Hibernate3/test/org/hibernate/test/hql/util/
   branches/HQL_ANTLR_2/Hibernate3/test/org/hibernate/test/hql/util/PathHelperTest.java
Modified:
   branches/HQL_ANTLR_2/Hibernate3/src/org/hibernate/hql/ast/resolve/HqlResolver.java
   branches/HQL_ANTLR_2/Hibernate3/src/org/hibernate/hql/ast/util/ASTUtil.java
   branches/HQL_ANTLR_2/Hibernate3/src/org/hibernate/hql/ast/util/PathHelper.java
   branches/HQL_ANTLR_2/Hibernate3/test/org/hibernate/test/hql/redesign/ResolverTest.java
Log:
cleanup

Modified: branches/HQL_ANTLR_2/Hibernate3/src/org/hibernate/hql/ast/resolve/HqlResolver.java
===================================================================
--- branches/HQL_ANTLR_2/Hibernate3/src/org/hibernate/hql/ast/resolve/HqlResolver.java	2007-01-09 11:39:15 UTC (rev 11033)
+++ branches/HQL_ANTLR_2/Hibernate3/src/org/hibernate/hql/ast/resolve/HqlResolver.java	2007-01-09 13:43:12 UTC (rev 11034)
@@ -254,18 +254,6 @@
 		throw new QueryException( "Unrecognized join type [" + joinType.getText() + "]" );
 	}
 
-	protected String reconstitutePathString(AST propertyReference) {
-		AST child = propertyReference.getFirstChild();
-		String prefix = "";
-		StringBuffer buffer = new StringBuffer();
-		while ( child != null ) {
-			buffer.append( prefix ).append( child.getText() );
-			prefix = ".";
-			child = child.getNextSibling();
-		}
-		return buffer.toString();
-	}
-
 	protected AST handleSelectedPropertyRef(AST propertyRef) {
 		if ( propertyRef.getType() == PROPERTY_REF ) {
 			PropertyReference ref = ( PropertyReference ) propertyRef;

Modified: branches/HQL_ANTLR_2/Hibernate3/src/org/hibernate/hql/ast/util/ASTUtil.java
===================================================================
--- branches/HQL_ANTLR_2/Hibernate3/src/org/hibernate/hql/ast/util/ASTUtil.java	2007-01-09 11:39:15 UTC (rev 11033)
+++ branches/HQL_ANTLR_2/Hibernate3/src/org/hibernate/hql/ast/util/ASTUtil.java	2007-01-09 13:43:12 UTC (rev 11034)
@@ -11,47 +11,61 @@
 /**
  * Provides utility methods for AST traversal and manipulation.
  *
- * @author Joshua Davis (pgmjsd at sourceforge.net)
+ * @author Joshua Davis
+ * @author Steve Ebersole
  */
 public final class ASTUtil {
 	/**
-	 * Private empty constructor.
-	 * (or else checkstyle says: 'warning: Utility classes should not have a public or default constructor.')
+	 * Disallow instantiation.
 	 *
-	 * @deprecated (tell clover to ignore this)
+	 * @deprecated (tellclovertoignorethis)
 	 */
 	private ASTUtil() {
 	}
 
 	/**
 	 * Creates a single node AST.
+	 * <p/>
+	 * TODO : remove this; this is silly...
 	 *
 	 * @param astFactory The factory.
-	 * @param type       The node type.
-	 * @param text       The node text.
+	 * @param type The node type.
+	 * @param text The node text.
+	 *
 	 * @return AST - A single node tree.
 	 */
 	public static AST create(ASTFactory astFactory, int type, String text) {
-		AST node = astFactory.create( type, text );
-		return node;
+		return astFactory.create( type, text );
 	}
 
 	/**
-	 * Creates a single node AST as a sibling.
+	 * Creates a single node AST as a sibling of the passed prevSibling,
+	 * taking care to reorganize the tree correctly to account for this
+	 * newly created node.
 	 *
-	 * @param astFactory  The factory.
-	 * @param type        The node type.
-	 * @param text        The node text.
+	 * @param astFactory The factory.
+	 * @param type The node type.
+	 * @param text The node text.
 	 * @param prevSibling The previous sibling.
-	 * @return AST - A single node tree.
+	 *
+	 * @return The created AST node.
 	 */
 	public static AST createSibling(ASTFactory astFactory, int type, String text, AST prevSibling) {
 		AST node = astFactory.create( type, text );
-		node.setNextSibling( prevSibling.getNextSibling() );
-		prevSibling.setNextSibling( node );
-		return node;
+		return insertSibling( node, prevSibling );
 	}
 
+	/**
+	 * Inserts a node into a child subtree as a particularly positioned
+	 * sibling taking care to properly reorganize the tree to account for this
+	 * new addition.
+	 *
+	 * @param node The node to insert
+	 * @param prevSibling The previous node at the sibling position
+	 * where we want this node inserted.
+	 *
+	 * @return The return is the same as the node parameter passed in.
+	 */
 	public static AST insertSibling(AST node, AST prevSibling) {
 		node.setNextSibling( prevSibling.getNextSibling() );
 		prevSibling.setNextSibling( node );
@@ -62,11 +76,12 @@
 	 * Creates a 'binary operator' subtree, given the information about the
 	 * parent and the two child nodex.
 	 *
-	 * @param factory    The AST factory.
+	 * @param factory The AST factory.
 	 * @param parentType The type of the parent node.
 	 * @param parentText The text of the parent node.
-	 * @param child1     The first child.
-	 * @param child2     The second child.
+	 * @param child1 The first child.
+	 * @param child2 The second child.
+	 *
 	 * @return AST - A new sub-tree of the form "(parent child1 child2)"
 	 */
 	public static AST createBinarySubtree(ASTFactory factory, int parentType, String parentText, AST child1, AST child2) {
@@ -79,10 +94,11 @@
 	 * Creates a single parent of the specified child (i.e. a 'unary operator'
 	 * subtree).
 	 *
-	 * @param factory    The AST factory.
+	 * @param factory The AST factory.
 	 * @param parentType The type of the parent node.
 	 * @param parentText The text of the parent node.
-	 * @param child      The child.
+	 * @param child The child.
+	 *
 	 * @return AST - A new sub-tree of the form "(parent child)"
 	 */
 	public static AST createParent(ASTFactory factory, int parentType, String parentText, AST child) {
@@ -107,7 +123,8 @@
 	 * Finds the first node of the specified type in the chain of children.
 	 *
 	 * @param parent The parent
-	 * @param type   The type to find.
+	 * @param type The type to find.
+	 *
 	 * @return The first node of the specified type, or null if not found.
 	 */
 	public static AST findTypeInChildren(AST parent, int type) {
@@ -122,6 +139,7 @@
 	 * Returns the last direct child of 'n'.
 	 *
 	 * @param n The parent
+	 *
 	 * @return The last direct child of 'n'.
 	 */
 	public static AST getLastChild(AST n) {
@@ -132,6 +150,7 @@
 	 * Returns the last sibling of 'a'.
 	 *
 	 * @param a The sibling.
+	 *
 	 * @return The last sibling of 'a'.
 	 */
 	private static AST getLastSibling(AST a) {
@@ -147,6 +166,7 @@
 	 * Returns the 'list' representation with some brackets around it for debugging.
 	 *
 	 * @param n The tree.
+	 *
 	 * @return The list representation of the tree.
 	 */
 	public static String getDebugString(AST n) {
@@ -161,7 +181,8 @@
 	 * Find the previous sibling in the parent for the given child.
 	 *
 	 * @param parent the parent node
-	 * @param child  the child to find the previous sibling of
+	 * @param child the child to find the previous sibling of
+	 *
 	 * @return the previous sibling of the child
 	 */
 	public static AST findPreviousSibling(AST parent, AST child) {
@@ -181,7 +202,7 @@
 	 * Makes the child node a sibling of the parent, reconnecting all siblings.
 	 *
 	 * @param parent the parent
-	 * @param child  the child
+	 * @param child the child
 	 */
 	public static void makeSiblingOfParent(AST parent, AST child) {
 		AST prev = findPreviousSibling( parent, child );
@@ -230,7 +251,7 @@
 	 * Inserts the child as the first child of the parent, all other children are shifted over to the 'right'.
 	 *
 	 * @param parent the parent
-	 * @param child  the new first child
+	 * @param child the new first child
 	 */
 	public static void insertChild(AST parent, AST child) {
 		if ( parent.getFirstChild() == null ) {
@@ -243,6 +264,13 @@
 		}
 	}
 
+	private static ASTArray createAstArray(ASTFactory factory, int size, int parentType, String parentText, AST child1) {
+		ASTArray array = new ASTArray( size );
+		array.add( factory.create( parentType, parentText ) );
+		array.add( child1 );
+		return array;
+	}
+
 	/**
 	 * Filters nodes out of a tree.
 	 */
@@ -251,6 +279,7 @@
 		 * Returns true if the node should be filtered out.
 		 *
 		 * @param n The node.
+		 *
 		 * @return true if the node should be filtered out, false to keep the node.
 		 */
 		boolean exclude(AST n);
@@ -267,17 +296,7 @@
 		public abstract boolean include(AST node);
 	}
 
-	private static ASTArray createAstArray(ASTFactory factory, int size, int parentType, String parentText, AST child1) {
-		ASTArray array = new ASTArray( size );
-		array.add( factory.create( parentType, parentText ) );
-		array.add( child1 );
-		return array;
-	}
-
 	public static List collectChildren(AST root, FilterPredicate predicate) {
-//		List children = new ArrayList();
-//		collectChildren( children, root, predicate );
-//		return children;
 		return new CollectingNodeVisitor( predicate ).collect( root );
 	}
 
@@ -305,14 +324,4 @@
 			return collectedNodes;
 		}
 	}
-
-	private static void collectChildren(List children, AST root, FilterPredicate predicate) {
-		for ( AST n = root.getFirstChild(); n != null; n = n.getNextSibling() ) {
-			if ( predicate == null || !predicate.exclude( n ) ) {
-				children.add( n );
-			}
-			collectChildren( children, n, predicate );
-		}
-	}
-
 }

Modified: branches/HQL_ANTLR_2/Hibernate3/src/org/hibernate/hql/ast/util/PathHelper.java
===================================================================
--- branches/HQL_ANTLR_2/Hibernate3/src/org/hibernate/hql/ast/util/PathHelper.java	2007-01-09 11:39:15 UTC (rev 11033)
+++ branches/HQL_ANTLR_2/Hibernate3/src/org/hibernate/hql/ast/util/PathHelper.java	2007-01-09 13:43:12 UTC (rev 11034)
@@ -11,9 +11,12 @@
 import org.apache.commons.logging.LogFactory;
 
 /**
- * Provides utility methods for paths.
+ * Provides utility methods for dealing with path expressions.
+ * <p/>
+ * Note that these utilities do not properly account for index operations.
  *
- * @author josh Sep 14, 2004 8:16:29 AM
+ * @author Joshua Davis
+ * @author Steve Ebersole
  */
 public final class PathHelper {
 
@@ -48,6 +51,37 @@
 		return lhs;
 	}
 
+	/**
+	 * Provides the inverse functionality of {@link #parsePath}.  In other words, for any path
+	 * 'p' not involving index operations, p == reconstitutePathString( parsePath( p, someASTFactory ) ).
+	 *
+	 * @param pathAST The path AST structure.
+	 * @return The corresponding path string.
+	 */
+	public static String rebuildPathExpression(AST pathAST) {
+		final StringBuffer buffer = new StringBuffer();
+		visitExpression( pathAST, buffer );
+		return buffer.toString();
+	}
+
+	private static void visitExpression(AST expression, StringBuffer buffer) {
+		if ( HqlSqlTokenTypes.DOT == expression.getType() ) {
+			visitDot( expression.getFirstChild(), expression.getFirstChild().getNextSibling(), buffer );
+		}
+		else if ( HqlSqlTokenTypes.IDENT == expression.getType() ) {
+			visitIdent( expression, buffer );
+		}
+	}
+
+	private static void visitDot(AST lhs, AST rhs, StringBuffer buffer) {
+		visitExpression( lhs, buffer );
+		buffer.append( '.' ).append( rhs.getText() );
+	}
+
+	private static void visitIdent(AST ident, StringBuffer buffer) {
+		buffer.append( ident.getText() );
+	}
+
 	public static String getAlias(String path) {
 		return StringHelper.root( path );
 	}

Modified: branches/HQL_ANTLR_2/Hibernate3/test/org/hibernate/test/hql/redesign/ResolverTest.java
===================================================================
--- branches/HQL_ANTLR_2/Hibernate3/test/org/hibernate/test/hql/redesign/ResolverTest.java	2007-01-09 11:39:15 UTC (rev 11033)
+++ branches/HQL_ANTLR_2/Hibernate3/test/org/hibernate/test/hql/redesign/ResolverTest.java	2007-01-09 13:43:12 UTC (rev 11034)
@@ -228,7 +228,7 @@
 			}
 		}
 		public static void assertJoinCount(int expected, AST tree) {
-			JoinCounter.assertJoinCount( "incorrect join count", expected, tree );
+			JoinCounter.assertJoinCount( "unexpected join count", expected, tree );
 		}
 		public static void assertJoinCount(String failMessage, int expected, AST tree) {
 			JoinCounter counter = new JoinCounter();
@@ -246,7 +246,7 @@
 			}
 		}
 		public static void assertSelectExpressionCount(int expected, AST tree) {
-			SelectExpressionCounter.assertSelectExpressionCount( "incorrect select expression count", expected, tree );
+			SelectExpressionCounter.assertSelectExpressionCount( "unexpected select expression count", expected, tree );
 		}
 		public static void assertSelectExpressionCount(String failMessage, int expected, AST tree) {
 			SelectExpressionCounter counter = new SelectExpressionCounter();

Added: branches/HQL_ANTLR_2/Hibernate3/test/org/hibernate/test/hql/util/PathHelperTest.java
===================================================================
--- branches/HQL_ANTLR_2/Hibernate3/test/org/hibernate/test/hql/util/PathHelperTest.java	2007-01-09 11:39:15 UTC (rev 11033)
+++ branches/HQL_ANTLR_2/Hibernate3/test/org/hibernate/test/hql/util/PathHelperTest.java	2007-01-09 13:43:12 UTC (rev 11034)
@@ -0,0 +1,28 @@
+package org.hibernate.test.hql.util;
+
+import junit.framework.TestCase;
+
+import org.hibernate.hql.ast.util.PathHelper;
+import org.hibernate.hql.ast.util.ASTPrinter;
+import org.hibernate.hql.ast.HqlASTFactory;
+import org.hibernate.hql.antlr.HqlTokenTypes;
+
+import antlr.collections.AST;
+
+/**
+ * {@inheritDoc}
+ *
+ * @author Steve Ebersole
+ */
+public class PathHelperTest extends TestCase {
+
+	private HqlASTFactory astFactory = new HqlASTFactory();
+	private ASTPrinter printer = new ASTPrinter( HqlTokenTypes.class );
+
+	public void testSimpleDotStructures() {
+		String path = "a.b.c";
+		AST pathAST = PathHelper.parsePath( path, astFactory );
+		System.out.println( printer.showAsString( pathAST, "Path AST" ) );
+		assertEquals( path, PathHelper.rebuildPathExpression( pathAST ) );
+	}
+}




More information about the hibernate-commits mailing list