Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ga-sakamoto opened this issue Jan 22, 2020 · 0 comments
Open

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

ga-sakamoto opened this issue Jan 22, 2020 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@ga-sakamoto
Copy link

現状,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();
        }
    }
}
@ga-sakamoto ga-sakamoto added the enhancement New feature or request label Jan 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants