When I try to convert one python code to java code to handle QWeb dynamic object, it is awesome to use CGLIB to create dynamic java bean.
Here is simple implement:
package org.easyerp.utils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import net.sf.cglib.beans.BeanGenerator;
import net.sf.cglib.beans.BeanMap;
public class DynamicBeanFactory {
private Map<String, Object> propertyValueMap = new HashMap<>();
private Map<String, Class> propertyTypeMap = new HashMap<>();
public DynamicBeanFactory() {
}
public void setBeanProperty(String name, Class type, Object value) {
this.propertyValueMap.put(name, value);
this.propertyTypeMap.put(name, type);
}
public DynamicBeanBuilder createBeanBuilder() {
DynamicBeanBuilder beanBuilder = new DynamicBeanBuilder(propertyTypeMap);
propertyValueMap.entrySet().parallelStream().forEach(entry -> beanBuilder.setProperty(entry.getKey(), entry.getValue()));
return beanBuilder;
}
}
----------------------------------
package org.easyerp.utils;
import java.util.Map;
import net.sf.cglib.beans.BeanGenerator;
import net.sf.cglib.beans.BeanMap;
public class DynamicBeanBuilder {
private Object instance;
private BeanMap beanMap;
public DynamicBeanBuilder(Map<String, Class> properties) {
BeanGenerator factory = new BeanGenerator();
properties.forEach((name, classType) -> factory.addProperty(name, classType));
this.instance = factory.create();
this.beanMap = BeanMap.create(instance);
}
public void setProperty(String name, Object value) {
beanMap.put(name, value);
}
public Object getProperty(String name) {
return beanMap.get(name);
}
public Object getBean() {
return instance;
}
}
----------------------
PythonInterpreter interp = new PythonInterpreter();
DynamicBeanFactory factory = new DynamicBeanFactory();
factory.setBeanProperty("db", List.class, Arrays.asList("dbd","A", "B"));
interp.set("request", factory.createBeanBuilder().getBean());
PyObject result = interp.eval("request.db");
The output result will be like:
result: [dbd, A, B]
Here is simple implement:
package org.easyerp.utils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import net.sf.cglib.beans.BeanGenerator;
import net.sf.cglib.beans.BeanMap;
public class DynamicBeanFactory {
private Map<String, Object> propertyValueMap = new HashMap<>();
private Map<String, Class> propertyTypeMap = new HashMap<>();
public DynamicBeanFactory() {
}
public void setBeanProperty(String name, Class type, Object value) {
this.propertyValueMap.put(name, value);
this.propertyTypeMap.put(name, type);
}
public DynamicBeanBuilder createBeanBuilder() {
DynamicBeanBuilder beanBuilder = new DynamicBeanBuilder(propertyTypeMap);
propertyValueMap.entrySet().parallelStream().forEach(entry -> beanBuilder.setProperty(entry.getKey(), entry.getValue()));
return beanBuilder;
}
}
----------------------------------
package org.easyerp.utils;
import java.util.Map;
import net.sf.cglib.beans.BeanGenerator;
import net.sf.cglib.beans.BeanMap;
public class DynamicBeanBuilder {
private Object instance;
private BeanMap beanMap;
public DynamicBeanBuilder(Map<String, Class> properties) {
BeanGenerator factory = new BeanGenerator();
properties.forEach((name, classType) -> factory.addProperty(name, classType));
this.instance = factory.create();
this.beanMap = BeanMap.create(instance);
}
public void setProperty(String name, Object value) {
beanMap.put(name, value);
}
public Object getProperty(String name) {
return beanMap.get(name);
}
public Object getBean() {
return instance;
}
}
----------------------
PythonInterpreter interp = new PythonInterpreter();
DynamicBeanFactory factory = new DynamicBeanFactory();
factory.setBeanProperty("db", List.class, Arrays.asList("dbd","A", "B"));
interp.set("request", factory.createBeanBuilder().getBean());
PyObject result = interp.eval("request.db");
The output result will be like:
result: [dbd, A, B]
Comments
Post a Comment