org.hibernate.hql.internal.ast.tree.ConstructorNode Maven / Gradle / Ivy
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008-2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors. All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301 USA
*/
package org.hibernate.hql.internal.ast.tree;
import java.lang.reflect.Constructor;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import antlr.SemanticException;
import antlr.collections.AST;
import org.hibernate.PropertyNotFoundException;
import org.hibernate.QueryException;
import org.hibernate.hql.internal.ast.DetailedSemanticException;
import org.hibernate.internal.util.ReflectHelper;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.transform.AliasToBeanConstructorResultTransformer;
import org.hibernate.transform.ResultTransformer;
import org.hibernate.transform.Transformers;
import org.hibernate.type.PrimitiveType;
import org.hibernate.type.Type;
/**
* Represents a constructor (new) in a SELECT.
*
* @author josh
*/
public class ConstructorNode extends SelectExpressionList implements AggregatedSelectExpression {
private Class resultType;
private Constructor constructor;
private Type[] constructorArgumentTypes;
private boolean isMap;
private boolean isList;
public ResultTransformer getResultTransformer() {
if ( constructor != null ) {
return new AliasToBeanConstructorResultTransformer( constructor );
}
else if ( isMap ) {
return Transformers.ALIAS_TO_ENTITY_MAP;
}
else if ( isList ) {
return Transformers.TO_LIST;
}
throw new QueryException( "Unable to determine proper dynamic-instantiation tranformer to use." );
}
private String[] aggregatedAliases;
public String[] getAggregatedAliases() {
if ( aggregatedAliases == null ) {
aggregatedAliases = buildAggregatedAliases();
}
return aggregatedAliases;
}
private String[] buildAggregatedAliases() {
SelectExpression[] selectExpressions = collectSelectExpressions();
String[] aliases = new String[selectExpressions.length] ;
for ( int i=0; i