org.nutz.lang.Mirror Java Examples

The following examples show how to use org.nutz.lang.Mirror. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LiteRpcInjectFactory.java    From nutzcloud with Apache License 2.0 6 votes vote down vote up
public Object afterBorn(Object obj, String beanName) {
    Mirror<Object> mirror = Mirror.me(obj);
    Field[] fields = Mirror.me(obj).getFields();
    for (Field field : fields) {
        RpcInject rpcInject = field.getAnnotation(RpcInject.class);
        if (rpcInject == null)
            continue;
        AbstractRpcRefProxy proxy = Mirror.me(rpcInject.by()).born();
        proxy.setField(field);
        proxy.setIoc(ioc);
        proxy.setRpcInject(rpcInject);
        proxy.setObject(obj);
        proxy.setLiteRpc(liteRpc);
        proxy.setKlass(field.getType());
        proxy.setConf(conf);
        Object t = Proxy.newProxyInstance(classLoader, new Class[] {field.getType()}, proxy);
        mirror.setValue(obj, field.getName(), t);
        proxy.afterInject();
        rpcProxys.put(field.toGenericString(), proxy);
    }
    return obj;
}
 
Example #2
Source File: BeanUtil.java    From flash-waimai with MIT License 5 votes vote down vote up
public static  void copyProperties(Object src,Object dest){
    Mirror mirrorSrc = Mirror.me(src.getClass());
    Mirror mirrorDest = Mirror.me(dest.getClass());
    Field[] fieldsSrc = mirrorSrc.getFields();
    for(Field field:fieldsSrc){
        try {
            Field fieldDest = mirrorDest.getField(field.getName());
            mirrorDest.setValue(dest,fieldDest,mirrorSrc.getValue(src,field));
        }catch (Exception e){

        }
    }
}
 
Example #3
Source File: BeanConfigures.java    From nutzwx with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "rawtypes" })
public static final <T> T configure(T t, Object obj) {
	if (t == null)
		return t;
	if (obj == null)
		return t;
	Map<String, Object> map = null;
	if (obj instanceof Map) {
		map = (Map<String, Object>)obj;
	} else {
		map = asConfigureMap(obj);
	}
	Mirror mirror = Mirror.me(t);
	Mirror m2 = Mirror.me(obj);
	for(Field field : mirror.getFields()) {
		Object value = null;
		if (map != null)
			value = map.get(field.getName());
		else {
			try {
				value = m2.getValue(obj, field.getName());
			} catch (FailToGetValueException e) {
				// skip;
			}
		}
		if (value != null) {
			mirror.setValue(t, field.getName(), value);
		}
	}
	return t;
}
 
Example #4
Source File: Wxs.java    From nutzwx with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
public static void mapField(StringBuilder sb, Class<?> klass, Field field) {
    sb.append("\r\n");
    String fieldName = field.getName();
    String className = klass.getSimpleName();
    Mirror mirror = Mirror.me(field.getType());
    String getterTmpl = "return (${fieldType})get(\"${fieldName}\")";
    if (mirror.isPrimitiveNumber()) {
        if (mirror.isBoolean()) {
            getterTmpl = "return getBoolean(\"${fieldName}\", false)";
        } else {
            getterTmpl = "return get"
                         + Strings.upperFirst(mirror.getType().getSimpleName())
                         + "(\"${fieldName}\", 0)";
        }
    }

    Tmpl tmpl = Tmpl.parse("    public ${className} set${upperFieldName}(${fieldType} ${fieldName}){\r\n"
                           + "        put(\"${fieldName}\", ${fieldName});\r\n"
                           + "        return this;\r\n"
                           + "    }\r\n"
                           + "\r\n"
                           + "    public ${fieldType} get${upperFieldName}(){\r\n"
                           + "        "
                           + getterTmpl
                           + ";\r\n"
                           + "    }\r\n");
    NutMap ctx = new NutMap().setv("className", className).setv("fieldName", fieldName);
    ctx.setv("upperFieldName", Strings.upperFirst(fieldName));
    ctx.setv("fieldType", field.getType().getSimpleName());
    sb.append(tmpl.render(ctx));
}