Search in sources :

Example 1 with Wrapper

use of org.apache.dubbo.common.bytecode.Wrapper in project dubbo by alibaba.

the class GenericJSONConverter method writeValue.

@Override
@SuppressWarnings("unchecked")
public void writeValue(Object obj, JSONWriter jb, boolean writeClass) throws IOException {
    if (obj == null) {
        jb.valueNull();
        return;
    }
    Class<?> c = obj.getClass();
    Encoder encoder = GLOBAL_ENCODER_MAP.get(c);
    if (encoder != null) {
        encoder.encode(obj, jb);
    } else if (obj instanceof JSONNode) {
        ((JSONNode) obj).writeJSON(this, jb, writeClass);
    } else if (c.isEnum()) {
        jb.valueString(((Enum<?>) obj).name());
    } else if (c.isArray()) {
        int len = Array.getLength(obj);
        jb.arrayBegin();
        for (int i = 0; i < len; i++) {
            writeValue(Array.get(obj, i), jb, writeClass);
        }
        jb.arrayEnd();
    } else if (Map.class.isAssignableFrom(c)) {
        Object key, value;
        jb.objectBegin();
        for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) obj).entrySet()) {
            key = entry.getKey();
            if (key == null) {
                continue;
            }
            jb.objectItem(key.toString());
            value = entry.getValue();
            if (value == null) {
                jb.valueNull();
            } else {
                writeValue(value, jb, writeClass);
            }
        }
        jb.objectEnd();
    } else if (Collection.class.isAssignableFrom(c)) {
        jb.arrayBegin();
        for (Object item : (Collection<Object>) obj) {
            if (item == null) {
                jb.valueNull();
            } else {
                writeValue(item, jb, writeClass);
            }
        }
        jb.arrayEnd();
    } else if (obj instanceof Locale) {
        jb.valueString(obj.toString());
    } else {
        jb.objectBegin();
        Wrapper w = Wrapper.getWrapper(c);
        String[] pns = w.getPropertyNames();
        for (String pn : pns) {
            if ((obj instanceof Throwable) && ("localizedMessage".equals(pn) || "cause".equals(pn) || "suppressed".equals(pn) || "stackTrace".equals(pn))) {
                continue;
            }
            jb.objectItem(pn);
            Object value = w.getPropertyValue(obj, pn);
            if (value == null || value == obj) {
                jb.valueNull();
            } else {
                writeValue(value, jb, writeClass);
            }
        }
        if (writeClass) {
            jb.objectItem(JSONVisitor.CLASS_PROPERTY);
            writeValue(obj.getClass().getName(), jb, writeClass);
        }
        jb.objectEnd();
    }
}
Also used : Locale(java.util.Locale) Wrapper(org.apache.dubbo.common.bytecode.Wrapper) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Wrapper

use of org.apache.dubbo.common.bytecode.Wrapper in project dubbo by alibaba.

the class JSON method json.

private static void json(Object obj, String[] properties, JSONWriter jb, boolean writeClass) throws IOException {
    if (obj == null) {
        jb.valueNull();
    } else {
        Wrapper wrapper = Wrapper.getWrapper(obj.getClass());
        Object value;
        jb.objectBegin();
        for (String prop : properties) {
            jb.objectItem(prop);
            value = wrapper.getPropertyValue(obj, prop);
            if (value == null) {
                jb.valueNull();
            } else {
                DEFAULT_CONVERTER.writeValue(value, jb, writeClass);
            }
        }
        jb.objectEnd();
    }
}
Also used : Wrapper(org.apache.dubbo.common.bytecode.Wrapper)

Example 3 with Wrapper

use of org.apache.dubbo.common.bytecode.Wrapper in project dubbo by alibaba.

the class RpcMessageHandler method reply.

public Object reply(ExchangeChannel channel, RpcMessage msg) throws RemotingException {
    String desc = msg.getMethodDesc();
    Object[] args = msg.getArguments();
    Object impl = mProvider.getImplementation(msg.getClassName());
    Wrapper wrap = Wrapper.getWrapper(impl.getClass());
    try {
        return new MockResult(wrap.invokeMethod(impl, desc, msg.getParameterTypes(), args));
    } catch (NoSuchMethodException e) {
        throw new RemotingException(channel, "Service method not found.");
    } catch (InvocationTargetException e) {
        return new MockResult(e.getTargetException());
    }
}
Also used : Wrapper(org.apache.dubbo.common.bytecode.Wrapper) RemotingException(org.apache.dubbo.remoting.RemotingException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NoSuchMethodException(org.apache.dubbo.common.bytecode.NoSuchMethodException)

Aggregations

Wrapper (org.apache.dubbo.common.bytecode.Wrapper)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Locale (java.util.Locale)1 Map (java.util.Map)1 NoSuchMethodException (org.apache.dubbo.common.bytecode.NoSuchMethodException)1 RemotingException (org.apache.dubbo.remoting.RemotingException)1