Skip to content

データ型の初期化コードについて #60

@ga-sakamoto

Description

@ga-sakamoto

現状,RTCBuilderを用いてポートを持つJava版RTCのコードを生成した際に,データ型を初期化するコードとして,毎回以下のコードを出力しています.
こちらのコードは,汎用的に使用できるコードではないかと思いますので,ミドルウェア側に組み込むことはできませんでしょうか?

private void initializeParam(Object target) {
    Class<?> targetClass = target.getClass();
    ClassLoader loader = target.getClass().getClassLoader();
    //
    Field[] fields = targetClass.getFields();
    for(Field field : fields) {
        if(field.getType().isPrimitive()) continue;
        
        try {
            if(field.getType().isArray()) {
                Object arrayValue = null;
                Class<?> clazz = null;
                if(field.getType().getComponentType().isPrimitive()) {
                    clazz = field.getType().getComponentType();
                } else {
                    clazz = loader.loadClass(field.getType().getComponentType().getName());
                }
                arrayValue = Array.newInstance(clazz, 0);
                field.set(target, arrayValue);
                
            } else {
                Constructor<?>[] constList = field.getType().getConstructors();
                if(constList.length==0) {
                    Method[] methodList = field.getType().getMethods();
                    for(Method method : methodList) {
                        if(method.getName().equals("from_int")==false) continue;
                        Object objFld = method.invoke(target, new Object[]{ new Integer(0) });
                        field.set(target, objFld);
                        break;
                    }
                    
                } else {
                    Class<?> classFld = Class.forName(field.getType().getName(), true, loader);
                    Object objFld = classFld.newInstance();
                    initializeParam(objFld);
                    field.set(target, objFld);
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

Metadata

Metadata

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions