Hi,
I just thought that might be interesting.
Reading this post I got an idea and wrote a class, that uses reflection to create JavaScript object out of Java object.
For example for the following class: class test{
public
String s = "testString";
public String nulS = null;
public int tr = 99;
private ArrayList dates = new ArrayList();
public long[] t = new long[]{1L,2L};
public ArrayList getDates() {
if(dates.size() == 0){
for(int i=0;i<2;i++){
dates.add(new Date());
}
}
return dates;
}
}
it creates: testObj = new Array();
testObj['s']='testString';
testObj['nulS'] = null;
testObj['tr']= 99.0;
testObj['t'] = new Array();
testObj['t'][0]= 1.0;
testObj['t'][1]= 2.0;
testObj['getDates'] = new Array();
testObj['getDates'][0]= new Date(1078439971890);
testObj['getDates'][1]= new Date(1078439971890);
method main the class below contains a
test - you don't really need it.
This class will transform all public fields if they are not defined in java.lang.Object.
It will transform public method, if method have return type other than void, don't have parameters and not defined in java.lang.Object.
Both methods and fields are transformed into fields.(see 'getDates' in the example).
Here is the source code: package com;
import java.util.Collection;
import java.util.Iterator;
import java.util.Date;
import java.util.ArrayList;
import java.lang.reflect.*;
/**
* User: yfuksenk
* Date: Mar 4, 2004
* Time: 3:35:14 PM
*/
public class JavaToScript {
public static void main(String[] args){
class test{
public String s = "testString";
public String nulS = null;
public int tr = 99;
private ArrayList dates = new ArrayList();
public long[] t = new long[]{1L,2L};
public ArrayList getDates() {
if(dates.size() == 0){
for(int i=0;i<2;i++){
dates.add(new Date());
}
}
return dates;
}
public void setDates(ArrayList dates) {
this.dates = dates;
}
}
test obj = new test();
Method t = null;
try {
t = obj.getClass().getMethod("notify",null);
System.out.println(t.getReturnType().getName());
} catch (NoSuchMethodException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
System.out.println(getJavaScriptObject("testObj", obj));
}
public static final String getJavaScriptObject(String jsName,Object obj){
if(obj == null) return jsName + " = null;\n";
String result = null;
String declClass = null;
Boolean definesToString = new Boolean(false);
try {
Method method = obj.getClass().getDeclaredMethod("toString",null);
declClass = method.getDeclaringClass().getName();
//System.out.println("Found toString " + aClass.getName());
definesToString = new Boolean( method != null
&& Modifier.isPublic(method.getModifiers()));
} catch (NoSuchMethodException e) {}
if(obj instanceof Collection){
Collection cl = (Collection) obj;
StringBuffer sb = new StringBuffer("\n" + jsName + " = new Array();");
int j = 0;
for (Iterator iterator = cl.iterator(); iterator.hasNext()
{
Object o = (Object) iterator.next();
sb.append(getJavaScriptObject("\n" + jsName + "[" + j + "]", o));
j++;
}
result = sb.toString();
}
else if(obj.getClass().isArray()){
StringBuffer sb = new StringBuffer("\n" + jsName + " = new Array();");
int length = Array.getLength(obj);
for (int i = 0; i < length; i++) {
sb.append(getJavaScriptObject("\n" + jsName + "[" + i + "]", Array.get(obj,i)));
}
result = sb.toString();
}
else if(obj.getClass().getName().equals("java.util.Date")){
result = "\n" + jsName + "= new Date("+ ((Date) obj).getTime() + ");\n";
}
else if(obj instanceof Number){
result = "\n" + jsName + "= " + ((Number) obj).doubleValue() + ";\n";
}
else if(obj instanceof Boolean){
result = "\n" + jsName + "= " + ((Boolean) obj).booleanValue() + ";\n";
}
else if(definesToString.booleanValue()){
result = "\n" + jsName + "='" + obj.toString().replaceAll("\'","\\\'").replaceAll("\n","\\n") + "';\n";
}
else{
StringBuffer sb = getJsObject(jsName, obj);
result = sb.toString();
}
return result;
}
private static StringBuffer getJsObject(String jsName, Object obj) {
Class aClass = obj.getClass();
StringBuffer sb = new StringBuffer("\n" + jsName + " = new Array();");
Field[] fields = aClass.getFields();
for (int f = 0; f < fields.length; f++) {
Field field = fields[f];
if(!Modifier.isPublic(field.getModifiers()) || field.getDeclaringClass().getName().equals("java.lang.Object")) continue;
try {
String jScript = getJavaScriptObject(jsName+"['" + field.getName() + "']", field.get(obj));
sb.append(jScript);
} catch (IllegalAccessException e) { }
}
Method[] methods = aClass.getMethods();
for (int m = 0; m < methods.length; m++) {
Method method = methods[m];
if(!Modifier.isPublic(method.getModifiers())
|| method.getReturnType().equals("void")
|| method.getParameterTypes().length > 0
|| method.getDeclaringClass().getName().equals("java.lang.Object")) continue;
String jScript = null;
try {
Object methodCallResult = method.invoke(obj,null);
jScript = getJavaScriptObject(jsName+"['" + method.getName() + "']", methodCallResult);
sb.append(jScript);
} catch (IllegalAccessException e) {} catch (InvocationTargetException e) {}
}
return sb;
}
}