Open Source Repository

Home /hibernate/hibernate-3.2.4.ga | Repository Home



org/hibernate/hql/ast/tree/ConstructorNode.java
// $Id: ConstructorNode.java 7460 2005-07-12 20:27:29Z steveebersole $
package org.hibernate.hql.ast.tree;

import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.List;

import org.hibernate.PropertyNotFoundException;
import org.hibernate.hql.ast.DetailedSemanticException;
import org.hibernate.type.Type;
import org.hibernate.util.ReflectHelper;
import org.hibernate.util.StringHelper;

import antlr.SemanticException;
import antlr.collections.AST;

/**
 * Represents a constructor (new) in a SELECT.
 *
 @author josh Sep 24, 2004 6:46:08 PM
 */
public class ConstructorNode extends SelectExpressionList implements SelectExpression {

  private Constructor constructor;
  private Type[] constructorArgumentTypes;
  private boolean isMap;
  private boolean isList;
  
  public boolean isMap() {
    return isMap;
  }
  
  public boolean isList() {
    return isList;
  }
  
  public String[] getAliases() {
    SelectExpression[] selectExpressions = collectSelectExpressions();
    String[] aliases = new String[selectExpressions.length;
    for int i=0; i<selectExpressions.length; i++ ) {
      String alias = selectExpressions[i].getAlias();
      aliases[i= alias==null ? Integer.toString(i: alias;
    }
    return aliases;
  }

  public void setScalarColumnText(int ithrows SemanticException {
    SelectExpression[] selectExpressions = collectSelectExpressions();
    // Invoke setScalarColumnText on each constructor argument.
    for int j = 0; j < selectExpressions.length; j++ ) {
      SelectExpression selectExpression = selectExpressions[j];
      selectExpression.setScalarColumnText);
    }
  }

  protected AST getFirstSelectExpression() {
    // Collect the select expressions, skip the first child because it is the class name.
    return getFirstChild().getNextSibling();
  }

  /**
   @deprecated (tell clover to ignore this method)
   */
  public Type getDataType() {
/*
    // Return the type of the object created by the constructor.
    AST firstChild = getFirstChild();
    String text = firstChild.getText();
    if ( firstChild.getType() == SqlTokenTypes.DOT ) {
      DotNode dot = ( DotNode ) firstChild;
      text = dot.getPath();
    }
    return getSessionFactoryHelper().requireEntityType( text );
*/
    throw new UnsupportedOperationException"getDataType() is not supported by ConstructorNode!" );
  }

  public void prepare() throws SemanticException {
    constructorArgumentTypes = resolveConstructorArgumentTypes();
    String path = ( ( PathNode getFirstChild() ).getPath();
    if "map".equalspath.toLowerCase() ) ) {
      isMap = true;
    }
    else if "list".equalspath.toLowerCase() ) ) {
      isList = true;
    }
    else {
      constructor = resolveConstructor(path);
    }
  }

  private Type[] resolveConstructorArgumentTypes() throws SemanticException {
    SelectExpression[] argumentExpressions = collectSelectExpressions();
    if argumentExpressions == null ) {
      // return an empty Type array
      return new Type[]{};
    }

    Type[] types = new Type[argumentExpressions.length];
    for int x = 0; x < argumentExpressions.length; x++ ) {
      types[x= argumentExpressions[x].getDataType();
    }
    return types;
  }

  private Constructor resolveConstructor(String paththrows SemanticException {
    String importedClassName = getSessionFactoryHelper().getImportedClassNamepath );
    String className = StringHelper.isEmptyimportedClassName ? path : importedClassName;
    if className == null ) {
      throw new SemanticException"Unable to locate class [" + path + "]" );
    }
    try {
      Class holderClass = ReflectHelper.classForNameclassName );
      return ReflectHelper.getConstructorholderClass, constructorArgumentTypes );
    }
    catch ClassNotFoundException e ) {
      throw new DetailedSemanticException"Unable to locate class [" + className + "]", e );
    }
    catch PropertyNotFoundException e ) {
      // this is the exception returned by ReflectHelper.getConstructor() if it cannot
      // locate an appropriate constructor
      throw new DetailedSemanticException"Unable to locate appropriate constructor on class [" + className + "]", e );
    }
  }
  
  public Constructor getConstructor() {
    return constructor;
  }

  public List getConstructorArgumentTypeList() {
    return Arrays.asListconstructorArgumentTypes );
  }

  public FromElement getFromElement() {
    return null;
  }

  public boolean isConstructor() {
    return true;
  }

  public boolean isReturnableEntity() throws SemanticException {
    return false;
  }

  public boolean isScalar() {
    // Constructors are always considered scalar results.
    return true;
  }
  
  public void setAlias(String alias) {
    throw new UnsupportedOperationException("constructor may not be aliased");
  }
  
  public String getAlias() {
    throw new UnsupportedOperationException("constructor may not be aliased");
  }
}