1 package org.hibernate.hql; 3 4 import java.lang.reflect.Constructor ; 5 import java.util.Arrays ; 6 import java.util.HashMap ; 7 import java.util.Map ; 8 9 import org.hibernate.QueryException; 10 11 14 public final class HolderInstantiator { 15 16 18 private final Constructor constructor; 19 private final boolean returnMaps; 20 private final boolean returnLists; 21 private final String [] queryReturnAliases; 22 23 public HolderInstantiator( 24 Constructor constructor, 25 boolean returnMaps, 26 boolean returnLists, 27 String [] queryReturnAliases 28 ) { 29 this.constructor = constructor; 30 this.returnLists = returnLists; 31 this.returnMaps = returnMaps; 32 this.queryReturnAliases = queryReturnAliases; 33 } 34 35 public boolean isRequired() { 36 return constructor!=null || returnLists || returnMaps; 37 } 38 39 public Object instantiate(Object [] row) { 40 if ( constructor != null ) { 41 try { 42 return constructor.newInstance( row ); 43 } 44 catch ( Exception e ) { 45 throw new QueryException( 46 "could not instantiate: " + 47 constructor.getDeclaringClass().getName(), 48 e ); 49 } 50 } 51 else if ( returnMaps ) { 52 Map map = new HashMap (); 53 for ( int j = 0; j < row.length; j++ ) { 54 map.put( queryReturnAliases[j], row[j] ); 55 } 56 return map; 57 } 58 else if ( returnLists ) { 59 return Arrays.asList(row); 60 } 61 else { 62 return row; 63 } 64 } 65 66 } 67 | Popular Tags |