Search in sources :

Example 1 with JavaObjectArg

use of org.apache.storm.generated.JavaObjectArg in project storm by apache.

the class Thrift method instantiateJavaObject.

public static Object instantiateJavaObject(JavaObject obj) {
    List<JavaObjectArg> args = obj.get_args_list();
    Class[] paraTypes = new Class[args.size()];
    Object[] paraValues = new Object[args.size()];
    for (int i = 0; i < args.size(); i++) {
        JavaObjectArg arg = args.get(i);
        paraValues[i] = arg.getFieldValue();
        if (arg.getSetField().equals(JavaObjectArg._Fields.INT_ARG)) {
            paraTypes[i] = Integer.class;
        } else if (arg.getSetField().equals(JavaObjectArg._Fields.LONG_ARG)) {
            paraTypes[i] = Long.class;
        } else if (arg.getSetField().equals(JavaObjectArg._Fields.STRING_ARG)) {
            paraTypes[i] = String.class;
        } else if (arg.getSetField().equals(JavaObjectArg._Fields.BOOL_ARG)) {
            paraTypes[i] = Boolean.class;
        } else if (arg.getSetField().equals(JavaObjectArg._Fields.BINARY_ARG)) {
            paraTypes[i] = ByteBuffer.class;
        } else if (arg.getSetField().equals(JavaObjectArg._Fields.DOUBLE_ARG)) {
            paraTypes[i] = Double.class;
        } else {
            paraTypes[i] = Object.class;
        }
    }
    try {
        Class clazz = Class.forName(obj.get_full_class_name());
        Constructor cons = clazz.getConstructor(paraTypes);
        return cons.newInstance(paraValues);
    } catch (Exception e) {
        LOG.error("java object instantiation failed", e);
    }
    return null;
}
Also used : JavaObjectArg(org.apache.storm.generated.JavaObjectArg) Constructor(java.lang.reflect.Constructor) JavaObject(org.apache.storm.generated.JavaObject) ComponentObject(org.apache.storm.generated.ComponentObject)

Aggregations

Constructor (java.lang.reflect.Constructor)1 ComponentObject (org.apache.storm.generated.ComponentObject)1 JavaObject (org.apache.storm.generated.JavaObject)1 JavaObjectArg (org.apache.storm.generated.JavaObjectArg)1