1 package org.hibernate.hql.ast.tree; 3 4 import java.lang.reflect.Constructor ; 5 import java.util.Arrays ; 6 import java.util.List ; 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 22 public class ConstructorNode extends SelectExpressionList implements SelectExpression { 23 24 private Constructor 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 [] getAliases() { 38 SelectExpression[] selectExpressions = collectSelectExpressions(); 39 String [] aliases = new String [selectExpressions.length] ; 40 for ( int i=0; i<selectExpressions.length; i++ ) { 41 String 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 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 return getFirstChild().getNextSibling(); 59 } 60 61 64 public Type getDataType() { 65 75 throw new UnsupportedOperationException ( "getDataType() is not supported by ConstructorNode!" ); 76 } 77 78 public void prepare() throws SemanticException { 79 constructorArgumentTypes = resolveConstructorArgumentTypes(); 80 String 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 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 resolveConstructor(String path) throws SemanticException { 107 String importedClassName = getSessionFactoryHelper().getImportedClassName( path ); 108 String className = StringHelper.isEmpty( importedClassName ) ? path : importedClassName; 109 if ( className == null ) { 110 throw new SemanticException( "Unable to locate class [" + path + "]" ); 111 } 112 try { 113 Class holderClass = ReflectHelper.classForName( className ); 114 return ReflectHelper.getConstructor( holderClass, constructorArgumentTypes ); 115 } 116 catch ( ClassNotFoundException e ) { 117 throw new DetailedSemanticException( "Unable to locate class [" + className + "]", e ); 118 } 119 catch ( PropertyNotFoundException e ) { 120 throw new DetailedSemanticException( "Unable to locate appropriate constructor on class [" + className + "]", e ); 123 } 124 } 125 126 public Constructor getConstructor() { 127 return constructor; 128 } 129 130 public List 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 return true; 149 } 150 151 public void setAlias(String alias) { 152 throw new UnsupportedOperationException ("constructor may not be aliased"); 153 } 154 155 public String getAlias() { 156 throw new UnsupportedOperationException ("constructor may not be aliased"); 157 } 158 } 159 | Popular Tags |