/** * create a new instance by the given constructor. */ public static <T> T newInstance(Constructor<T> constructor){ return newInstance(constructor, Arrays2.EMPTY_BOOLEAN_ARRAY); }
protected static Map createMap(Class<?> mapType){ if(mapType.equals(Map.class)){ return new LinkedHashMap(); } return (Map)Reflection.newInstance(mapType); }
protected Collection newCollection(Class c) { if(List.class.equals(c)) { return new ArrayList(); } if(Set.class.equals(c)) { return new LinkedHashSet(); } return (Collection)Reflection.newInstance(c); }
public static <T> T copyNew(T from) { if(null == from) { return null; } T to = (T)Reflection.newInstance(from.getClass()); copyProperties(from, to); return to; }
@Override public boolean convertFrom(Object value, Class<?> targetType, Type genericType, Out<Object> out, ConvertContext context) throws Throwable { if(value instanceof Map) { Map from = (Map)value; Map to; if(null == genericType) { to = (Map)Reflection.newInstance(targetType); to.putAll(from); }else { to = doConvert(context, from, targetType, genericType); } out.set(to); return true; } return false; } }
/** * Returns the config extension of creates a new one if not exists. */ default <T> T getOrCreateExtension(Class<T> type) { T e = getExtension(type); if(null == e) { e = Reflection.newInstance(type); setExtension(e); } return e; }
/** * Returns the config extension of creates a new one if not exists. */ default <T> T getOrCreateExtension(Class<? super T> type, Class<T> instanceType) { T e = (T)getExtension(type); if(null == e) { e = Reflection.newInstance(instanceType); setExtension(type, e); } return e; }
@Override public boolean convertFrom(Object value, Class<?> targetType, Type genericType, Out<Object> out, ConvertContext context) throws Throwable { // if(Modifier.isAbstract(targetType.getModifiers()) || Modifier.isInterface(targetType.getModifiers())){ // return false; // } if(value instanceof Map){ out.set(convertFromMap(targetType, genericType, (Map)value, context)); return true; } if(ValueParsable.class.isAssignableFrom(targetType)) { Object bean = Reflection.newInstance(targetType); ((ValueParsable)bean).parseValue(value); out.set(bean); return true; } if(value instanceof CharSequence && StringParsable.class.isAssignableFrom(targetType)) { Object bean = Reflection.newInstance(targetType); ((StringParsable)bean).parseString(value.toString()); out.set(bean); return true; } return false; }
@SuppressWarnings("unchecked") protected Collection<Object> createCollection(BeanDefinitionBase bd, ValueDefinition vd,Collection<Object> col){ try { Class<?> definedType = vd.getDefinedType(); if(definedType != null){ if(definedType.isInterface() || Modifier.isAbstract(definedType.getModifiers())){ boolean isAssignableFrom = definedType.isAssignableFrom(vd.getDefinedValue().getClass()); if(!isAssignableFrom){ throw new BeanDefinitionException("Error create instance of collection type '" + vd.getDefinedType() + "' bean " + bd + " cause "+definedType+" is interface or abstract class and the value type " + vd.getDefinedValue().getClass().getName() + " is not the subclass of "+definedType); }else{ definedType = vd.getDefinedValue().getClass(); } } return (Collection<Object>)Reflection.newInstance(definedType); } } catch (ReflectException e) { throw new BeanDefinitionException("Error create instance of collection type '" + vd.getDefinedType() + "' bean " + bd, e); } if(col instanceof List){ return new ArrayList<Object>(); }else if(col instanceof Set){ return new ArrayList<Object>(); }else{ throw new BeanDefinitionException("Invalid collection type '" + col.getClass().getName() + "', only 'list' and 'set' are supported, please check the bean " + bd); } }
@SuppressWarnings({ "unchecked", "rawtypes" }) protected DataSource createDataSourceByClass(PoolProperties props) { String className = props.getDataSourceClassName(); DataSource ds = (DataSource)Reflection.newInstance(Classes.forName(className)); Properties dataSourceProperties = props.getDataSourceProperties(); if(null != dataSourceProperties && !dataSourceProperties.isEmpty()) { Beans.setPropertiesNestable(BeanType.of(ds.getClass()), ds, (Map)Props.toMap(dataSourceProperties)); } return ds; }
@Override public Object newInstance(ConvertContext context, Class<?> type, Type genericType, Map<String, Object> map) { JsonType jsonType = type.getAnnotation(JsonType.class); if(null == jsonType) { return null; } String propertyName = Strings.firstNotEmpty(jsonType.property(), jsonType.meta().getDefaultPropertyName()); Object propertyValue = map.get(propertyName); if(null == propertyValue) { return null; } String name = (String)propertyValue; if(jsonType.meta() == JsonType.MetaType.CLASS_NAME) { return Reflection.newInstance(Classes.forName(name)); }else { for(JsonType.SubType subType : jsonType.types()) { if(subType.name().equals(name)) { return Reflection.newInstance(subType.type()); } } } return null; }
@Override public Object read(Type type, Class contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { if(type instanceof Class<?>) { return readInternal((Class)type, inputMessage); }else { ParameterizedType parameterizedType = (ParameterizedType)type; Class typeArgument = (Class)parameterizedType.getActualTypeArguments()[0]; Collection c = newCollection((Class)parameterizedType.getRawType()); try(InputStream is = inputMessage.getBody()) { try(InputStreamReader reader = new InputStreamReader(is, getCharset(inputMessage))) { try { JsonArray a = JSON.parse(reader).asJsonArray(); a.forEach(item -> { if(null == item) { c.add(null); }else { Object o = Reflection.newInstance(typeArgument); ((JsonParsable)o).parseJson(item); c.add(o); } }); }catch (Exception e) { throw new HttpMessageNotReadableException(e.getMessage(), e); } } } return c; } }
@Override protected Object readInternal(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { try(InputStream is = inputMessage.getBody()) { if(clazz.isArray()) { try(InputStreamReader reader = new InputStreamReader(is, getCharset(inputMessage))) { try { JsonArray a = JSON.parse(reader).asJsonArray(); Object o = Array.newInstance(clazz.getComponentType(), a.length()); for(int i=0;i<a.length();i++) { Object item = Reflection.newInstance(clazz.getComponentType()); ((JsonParsable)item).parseJson(a.getObject(i)); Array.set(o, i, item); } return o; }catch (Exception e) { throw new HttpMessageNotReadableException(e.getMessage(), e); } } }else { Object o = Reflection.newInstance(clazz); try { ((JsonParsable)o).parseJson(IO.readString(is, getCharset(inputMessage))); return o; }catch (Exception e) { throw new HttpMessageNotReadableException(e.getMessage(), e); } } } }
createdMap = (Map)Reflection.newInstance(clzz);
@SuppressWarnings("unchecked") public <T> T newInstance() throws ReflectException{ if(null == defaultConstructor){ throw new ReflectException(Strings.format("there is no default constructor available in class '{0}'",getName())); } if(defaultConstructorInner){ return (T)defaultConstructor.newInstance(Reflection.newInstance(clazz.getEnclosingClass())); }else{ if(null != accessor && accessor.canNewInstance()){ return (T)accessor.newInstance(); }else{ return (T)defaultConstructor.newInstance((Object[])null); } } }
AppPropertyLoader loader = (AppPropertyLoader) Reflection.newInstance(cls);
protected Object readModel(OrmContext context,ResultSet rs,ResultSetMapping rsm,Class<? extends Model> modelClass) throws SQLException { Model model = Reflection.newInstance(modelClass); model.init(context, rsm.getPrimaryEntityMapping()); DbDialect dialect = context.getDb().getDialect(); for(int i=0;i<rsm.getColumnCount();i++){ ResultColumnMapping cm = rsm.getColumnMapping(i); FieldMapping fm = cm.getFieldMapping(); String name = null != fm ? fm.getFieldName() : cm.getNormalizedName(); Object value = readColumnValue(dialect, rs, cm, fm, i+1); model.set(name, value); if(Strings.isNotEmpty(cm.getAliasName()) && !name.equals(cm.getAliasName())){ model.set(cm.getAliasName(), value); } } return model; }
bean = Reflection.newInstance(bd.getConstructor(),doResolveArgs(bd, constructorArguments));