JavaObject { public static String ConvertToRCode(String name, Object javaobject, boolean useList, boolean useEquals) throws IllegalAccessException { StringBuilder builder = new StringBuilder(); StringBuilder tempbuffer = new StringBuilder(); String className, varName; Object o; Field f; Field[] fields = javaobject.getClass().getFields(); if (useList) { if (useEquals) { builder.append(name).append(" = list("); } else { builder.append(name).append(" <- list("); } } for (int i = 0; i < fields.length; i++) { f = fields[i]; className = f.getType().getCanonicalName(); varName = f.getName(); o = f.get(javaobject); switch (className) { case "int": case "float": case "double": case "long": case "short": builder.append(varName).append("=").append(o); break; case "java.lang.String": builder.append(varName).append("=").append("\"").append(o).append("\""); break; case "boolean": builder.append(varName).append("=").append(o.toString().toUpperCase()); break; case "int[]": tempbuffer.setLength(0); RCodeUtils.addIntArray(tempbuffer, varName, (int[]) o, true); builder.append(tempbuffer.toString()); break; case "double[]": tempbuffer.setLength(0); RCodeUtils.addDoubleArray(tempbuffer, varName, (double[]) o, true); builder.append(tempbuffer.toString()); break; case "float[]": tempbuffer.setLength(0); RCodeUtils.addFloatArray(tempbuffer, varName, (float[]) o, true); builder.append(tempbuffer.toString()); break; case "short[]": tempbuffer.setLength(0); RCodeUtils.addShortArray(tempbuffer, varName, (short[]) o, true); builder.append(tempbuffer.toString()); break; case "long[]": tempbuffer.setLength(0); RCodeUtils.addLongArray(tempbuffer, varName, (long[]) o, true); builder.append(tempbuffer.toString()); break; case "boolean[]": tempbuffer.setLength(0); RCodeUtils.addLogicalArray(tempbuffer, varName, (boolean[]) o, true); builder.append(tempbuffer.toString()); break; case "java.lang.String[]": tempbuffer.setLength(0); RCodeUtils.addStringArray(tempbuffer, varName, (String[]) o, true); builder.append(tempbuffer.toString()); break; case "java.lang.Object": tempbuffer.setLength(0); tempbuffer.append(JavaObject.ConvertToRCode(name, o, useList, true)); builder.append(tempbuffer.toString()); break; default: builder.append(varName).append("=").append("\"").append("Unsupported data type: ").append(className).append(" in JavaObject").append("\""); break; } if (useList) { if (i < fields.length - 1) { builder.append(", "); } } else { builder.append(";"); } } if (useList) { builder.append(")\n"); } else { builder.append("\n"); } return (builder.toString()); } JavaObject(String name, Object o); Object getObject(); void setObject(Object object); String getName(); void setName(String name); static String ConvertToRCode(String name, Object javaobject, boolean useList, boolean useEquals); String produceRCode(boolean useEquals);  }
@Test public void ConvertJavaPlainObjectToRCode() throws IllegalAccessException { TestClass tc = new TestClass(); tc.b = true; tc.d = 6.789; tc.f = 4.5f; tc.i = 3; tc.l = 7L; tc.s = "s1"; String code = JavaObject.ConvertToRCode("Test", tc, true, true); assertEquals("Test = list(i=3, f=4.5, d=6.789, b=TRUE, l=7, s=\"s1\")\n", code); }
@Test public void ConvertJavaPlainObjectToRCodeWithArrays() throws IllegalAccessException { TestClassWithArrays tc = new TestClassWithArrays(); tc.b = true; tc.ba = new boolean[]{true,false,true,true}; tc.d = 3.4; tc.da = new double[]{1.0,10.0,100.0}; tc.f = 5.6f; tc.i = 9; tc.ia = new int[]{1,2,3,4,5}; tc.l = 999L; tc.s = "H..."; tc.sa = new String[]{"One", "Two", "Three"}; String code = JavaObject.ConvertToRCode("Test", tc, true, true); assertEquals( "Test = list(ia=c(1, 2, 3, 4, 5), da=c(1.0, 10.0, 100.0), sa=c(\"One\", \"Two\", \"Three\"), ba=c(TRUE, FALSE, TRUE, TRUE), i=9, f=5.6, d=3.4, b=TRUE, l=999, s=\"H...\")\n", code); }