KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > hql > ast > tree > ConstructorNode


1 // $Id: ConstructorNode.java,v 1.1 2005/07/12 20:27:16 steveebersole Exp $
2
package org.hibernate.hql.ast.tree;
3
4 import java.lang.reflect.Constructor JavaDoc;
5 import java.util.Arrays JavaDoc;
6 import java.util.List JavaDoc;
7
8 import org.hibernate.PropertyNotFoundException;
9 import org.hibernate.hql.ast.DetailedSemanticException;
10 import org.hibernate.type.Type;
11 import org.hibernate.util.ReflectHelper;
12 import org.hibernate.util.StringHelper;
13
14 import antlr.SemanticException;
15 import antlr.collections.AST;
16
17 /**
18  * Represents a constructor (new) in a SELECT.
19  *
20  * @author josh Sep 24, 2004 6:46:08 PM
21  */

22 public class ConstructorNode extends SelectExpressionList implements SelectExpression {
23
24     private Constructor JavaDoc constructor;
25     private Type[] constructorArgumentTypes;
26     private boolean isMap;
27     private boolean isList;
28     
29     public boolean isMap() {
30         return isMap;
31     }
32     
33     public boolean isList() {
34         return isList;
35     }
36     
37     public String JavaDoc[] getAliases() {
38         SelectExpression[] selectExpressions = collectSelectExpressions();
39         String JavaDoc[] aliases = new String JavaDoc[selectExpressions.length] ;
40         for ( int i=0; i<selectExpressions.length; i++ ) {
41             String JavaDoc alias = selectExpressions[i].getAlias();
42             aliases[i] = alias==null ? Integer.toString(i) : alias;
43         }
44         return aliases;
45     }
46
47     public void setScalarColumnText(int i) throws SemanticException {
48         SelectExpression[] selectExpressions = collectSelectExpressions();
49         // Invoke setScalarColumnText on each constructor argument.
50
for ( int j = 0; j < selectExpressions.length; j++ ) {
51             SelectExpression selectExpression = selectExpressions[j];
52             selectExpression.setScalarColumnText( j );
53         }
54     }
55
56     protected AST getFirstSelectExpression() {
57         // Collect the select expressions, skip the first child because it is the class name.
58
return getFirstChild().getNextSibling();
59     }
60
61     /**
62      * @deprecated (tell clover to ignore this method)
63      */

64     public Type getDataType() {
65 /*
66         // Return the type of the object created by the constructor.
67         AST firstChild = getFirstChild();
68         String text = firstChild.getText();
69         if ( firstChild.getType() == SqlTokenTypes.DOT ) {
70             DotNode dot = ( DotNode ) firstChild;
71             text = dot.getPath();
72         }
73         return getSessionFactoryHelper().requireEntityType( text );
74 */

75         throw new UnsupportedOperationException JavaDoc( "getDataType() is not supported by ConstructorNode!" );
76     }
77
78     public void prepare() throws SemanticException {
79         constructorArgumentTypes = resolveConstructorArgumentTypes();
80         String JavaDoc path = ( ( PathNode ) getFirstChild() ).getPath();
81         if ( "map".equals( path.toLowerCase() ) ) {
82             isMap = true;
83         }
84         else if ( "list".equals( path.toLowerCase() ) ) {
85             isList = true;
86         }
87         else {
88             constructor = resolveConstructor(path);
89         }
90     }
91
92     private Type[] resolveConstructorArgumentTypes() throws SemanticException {
93         SelectExpression[] argumentExpressions = collectSelectExpressions();
94         if ( argumentExpressions == null ) {
95             // return an empty Type array
96
return new Type[]{};
97         }
98
99         Type[] types = new Type[argumentExpressions.length];
100         for ( int x = 0; x < argumentExpressions.length; x++ ) {
101             types[x] = argumentExpressions[x].getDataType();
102         }
103         return types;
104     }
105
106     private Constructor JavaDoc resolveConstructor(String JavaDoc path) throws SemanticException {
107         String JavaDoc importedClassName = getSessionFactoryHelper().getImportedClassName( path );
108         String JavaDoc className = StringHelper.isEmpty( importedClassName ) ? path : importedClassName;
109         if ( className == null ) {
110             throw new SemanticException( "Unable to locate class [" + path + "]" );
111         }
112         try {
113             Class JavaDoc holderClass = ReflectHelper.classForName( className );
114             return ReflectHelper.getConstructor( holderClass, constructorArgumentTypes );
115         }
116         catch ( ClassNotFoundException JavaDoc e ) {
117             throw new DetailedSemanticException( "Unable to locate class [" + className + "]", e );
118         }
119         catch ( PropertyNotFoundException e ) {
120             // this is the exception returned by ReflectHelper.getConstructor() if it cannot
121
// locate an appropriate constructor
122
throw new DetailedSemanticException( "Unable to locate appropriate constructor on class [" + className + "]", e );
123         }
124     }
125     
126     public Constructor JavaDoc getConstructor() {
127         return constructor;
128     }
129
130     public List JavaDoc getConstructorArgumentTypeList() {
131         return Arrays.asList( constructorArgumentTypes );
132     }
133
134     public FromElement getFromElement() {
135         return null;
136     }
137
138     public boolean isConstructor() {
139         return true;
140     }
141
142     public boolean isReturnableEntity() throws SemanticException {
143         return false;
144     }
145
146     public boolean isScalar() {
147         // Constructors are always considered scalar results.
148
return true;
149     }
150     
151     public void setAlias(String JavaDoc alias) {
152         throw new UnsupportedOperationException JavaDoc("constructor may not be aliased");
153     }
154     
155     public String JavaDoc getAlias() {
156         throw new UnsupportedOperationException JavaDoc("constructor may not be aliased");
157     }
158 }
159
Popular Tags