I want to implement a generic functionality which would enable that our domain classes are being proxied for the case that its values to be xml compliant (escaping special characters in strings). The domain classes/objects are being generated, so that they can not be changed by me. What I tried to do was following code for the generation of proxies:
public class StringValuesFormatterForXml implements InvocationHandler {
public static interface IA {
String getMa1();
List<? extends IB> getBs();
}
public static class A implements IA {
#Override
public String getMa1() {
return "Ma1";
}
#Override
public List<? extends IB> getBs() {
return Arrays.asList(new B(), new B());
}
}
public static interface IB {
String getMb1();
String getMb2();
}
public static class B implements IB {
#Override
public String getMb1() {
return "Mb1";
}
#Override
public String getMb2() {
return "Mb2";
}
}
Object destObj;
private final Map<String, Method> methods = new HashMap<>();
#SuppressWarnings("unchecked")
public static IA createProxyA(IA destObj) {
return (IA) Proxy.newProxyInstance(destObj.getClass().getClassLoader(), new Class[] {
IA.class
}, new StringValuesFormatterForXml(destObj));
}
#SuppressWarnings("unchecked")
public static Object createProxy(Object destObj, Class<?> clazz) {
return Proxy.newProxyInstance(destObj.getClass().getClassLoader(), new Class[] {
clazz
}, new StringValuesFormatterForXml(destObj));
}
public StringValuesFormatterForXml(Object destObj) {
this.destObj = destObj;
for (Method method : destObj.getClass().getMethods()) {
this.methods.put(method.getName(), method);
}
}
#Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getReturnType().isAssignableFrom(List.class)) {
List<Object> elems = (List<Object>) method.invoke(destObj, args);
List<Object> proxyElems = new ArrayList<Object>();
for (Object obj : elems) {
Object proxyObj = createProxy(obj, obj.getClass());
proxyElems.add(proxyObj);
}
return proxyElems;
}
return method.invoke(destObj, args); // Here I will format the output for xml
}
public static void main(String[] args) {
A orig = new A();
IA proxy1 = createProxyA(orig);
A proxy2 = (A) createProxy(orig, orig.getClass());
}
}
Code in createProxy(orig, orig.getClass()) throws following error java.lang.IllegalArgumentException: StringValuesFormatterForXml$A is not an interface but the code createProxyA(orig) does not. So it seems that I would need to have a separate creator method for every interface which I use. In our domain model there are many classes and I do not want to create for every class separate creator.
Are there any other ways/frameworks which are better suited for my case of proxying objects.
Your createProxy method does work, you just have to pass the class of the interface as second parameter:
A orig = new A();
IA proxy1 = (IA) createProxy(orig, IA.class);
In addition I would recomment you to use the createProxy function as a generic function in order to avoid the obkect cast:
#SuppressWarnings("unchecked")
public static <T> T createProxyB(T destObj, Class<T> clazz) {
return (T) Proxy.newProxyInstance(destObj.getClass().getClassLoader(), new Class[] { clazz },
new StringValuesFormatterForXml(destObj));
}
In this case you can call the function like this:
A orig = new A();
IA proxy1 = createProxyB(orig, IA.class);
Related
How to add a generic object to list in java?
Currently, I have two classes doing the same function and would like to integrate them together
public class MyClass1 {
private List<Object1> myList = new ArrayList<>();
public void addList(Object1 o) {
myList.add(o);
}
}
public class MyClass2 {
private List<Object2> myList = new ArrayList<>();
public void addList(Object2 o) {
myList.add(o);
}
}
something like
public class MyClass {
private List<Object> myList = new ArrayList<>();
public void addList(Object o) {
myList.add(o);
}
}
You could make your own class generic:
public class MyClass<T> {
private List<T> myList = new ArrayList<>();
public void addList(T o) {
myList.add(o);
}
}
You can make both classes Object1 and Object2 implement the same interface 'ObjInterface'
public class MyClass {
private List<ObjInterface> myList = new ArrayList<>();
public void addList(ObjInterface o) {
myList.add(o);
}
}
If you want the class to contain only Object1 or only Object2 and never anything else, you can combine the other two answers:
interface ObjInterface {
// may be empty
}
public class MyClass<T extends ObjInterface> {
private List<T> myList = new ArrayList<>();
public void addList(T o) {
myList.add(o);
}
}
MyClass<Object1> object1only = new MyClass<>();
MyClass<Object2> object2only = new MyClass<>();
and add implements ObjInterface to the definitions of Object1 and Object2.
If you add methods common to both classes to ObjInterface, you can call those methods on the T objects in MyClass, since they're guaranteed to be a subclass of ObjInterface.
I want to pass the getter of a bean as a function. When the function is called the getter should be invoked. Example:
public class MyConverter {
public MyConverter(Function f) {
this.f = f;
}
public void process(DTO dto) {
// I just want to call the function with the dto, and the DTO::getList should be called
List<?> list = f.call(dto);
}
}
public class DTO {
private List<String> list;
public List<String> getList() { return list; }
}
Is that possible with java 8?
If the constructor of MyConverter must take a function, and process must take an object, this is probably the best way:
class MyConverter<T> {
// V takes a thing (in our case a DTO)
// V returns a list of Strings
private Function<T, List<String>> f;
public MyConverter(Function<T, List<String>> f) {
this.f = f;
}
public void process(T processable) {
List<String> list = f.apply(processable);
}
}
MyConverter<DTO> converter = new MyConverter<>(DTO::getList);
DTO dto = new DTO();
converter.process(dto);
I have an interface:
public interface ITransformer<S,T>{
public void transform(S source,T target);
default String getTransformerName(){
Class<S> s;
Class<T> t;
return s.getName() + t.getName(); //*********
}
}
the error message the starred line:
The local variable s may not have been initialized
The local variable t may not have been initialized
I would like to use this method to return a string with [S.classname][T.classname] . Please let me know how to achieve this or is this impossible to do at interface ?
Update: Jan 12
My purpose of doing this is due to the fact that this class will be in framework and I want to reduce the human error as much as possible.. I am changing the code as follows:
public interface ITransformer<S,T>{
public void transform(S source,T target);
public FieldEntry<S, T> getTransformerName();
}
public class FieldEntry<S,T> implements Comparable<FieldEntry> {
private Class<S> s;
private Class<T> t;
public FieldEntry(Class<S> s,Class<T> t){
this.s = s;
this.t = t;
}
public String getEntryName(){
return s.getName() + t.getName();
}
#Override
public int compareTo(FieldEntry entry) {
if(entry == null) throw new IllegalArgumentException("The argument to compare cannot be null!");
return entry.getEntryName().compareTo(this.getEntryName());
}
}
In order to demonstrate why this can’t work, you may change your class to
public interface ITransformer<S,T>{
public void transform(S source,T target);
static <In,Out> ITransformer<In,Out> noOp() {
return (source,target) -> {};
}
static void main(String... arg) {
ITransformer<String,Integer> t1 = noOp();
ITransformer<Long,Thread> t2 = noOp();
System.out.println(t1 == (Object)t2);
}
}
Running this will print true. In other words, both functions are represented by the same instances, so there can’t be and property allowing to recognize their different type.
Generally, when two functions (lambda expressions or method references) exhibit the same behavior, a JVM may represent them by the same implementation type or even the same instance.
Even for non-interface classes, this doesn’t work due to Type Erasure. It only works when you have a reifiable (i.e. non-generic) type extending or implementing a generic type.
It's a little bit dangerous and I wouldn't used this in production (because you should cover in your code all possible use cases of your interface), but you can use reflection for it:
public interface ITransformer<S, T> {
public void transform(S source, T target);
default String getTransformerName() {
Type[] genericInterfaces = this.getClass().getGenericInterfaces();
ParameterizedType parameterizedType = null;
for (Type genericInterface : genericInterfaces) {
if (genericInterface instanceof ParameterizedType) {
ParameterizedType paramInterface = (ParameterizedType) genericInterface;
if (paramInterface.getRawType().equals(ITransformer.class)) {
parameterizedType = paramInterface;
break;
}
}
}
if (parameterizedType == null) {
throw new IllegalStateException("!");
}
return parameterizedType.getActualTypeArguments()[0].getTypeName() + parameterizedType.getActualTypeArguments()[1].getTypeName();
}
}
public class StringToIntegerTransfomer implements ITransformer<String, Integer> {
#Override
public void transform(String source, Integer target) {
}
}
public interface StringToNumberTransfomer<T extends Number> extends ITransformer<String, T> {
}
public class StringToLongTransfomer implements StringToNumberTransfomer<Long>, ITransformer<String, Long> {
#Override
public void transform(String source, Long target) {
}
}
#Test
public void test() {
ITransformer<String, Integer> intTransformer = new StringToIntegerTransfomer();
ITransformer<String, Long> longTransformer = new StringToLongTransfomer();
ITransformer<String, String> stringTransformer = new ITransformer<String, String>() {
#Override
public void transform(String source, String target) {
}
};
ITransformer<String, Double> doubleTransformer = new StringToNumberTransfomer<Double>() {
#Override
public void transform(String source, Double target) {
}
};
System.out.println(String.format("intTransformer: %s", intTransformer.getTransformerName()));
System.out.println(String.format("longTransformer: %s", longTransformer.getTransformerName()));
System.out.println(String.format("stringTransformer: %s", stringTransformer.getTransformerName()));
System.out.println(String.format("doubleTransformer: %s", doubleTransformer.getTransformerName()));
}
Output for this snippet:
intTransformer: java.lang.Stringjava.lang.Integer
longTransformer: java.lang.Stringjava.lang.Long
stringTransformer: java.lang.Stringjava.lang.String
java.lang.IllegalStateException: !
This code has one restriction, you should say implements ITransformer<S, T> for all implementations of ITransformer. That why I have got IllegalStateException for this line ITransformer<String, Double> doubleTransformer = new StringToNumberTransfomer<Double>(). But you can improve this code.
Better option is to use some base implementation of interface and pass source and target classes into constructor:
public interface ITransformer<S, T> {
void transform(S source, T target);
String getTransformerName();
}
public abstract class BaseITransformer<S, T> implements ITransformer<S, T> {
private final Class<S> sourceClass;
private final Class<T> targetClass;
public BaseITransformer(Class<S> sourceClass, Class<T> targetClass) {
this.sourceClass = sourceClass;
this.targetClass = targetClass;
}
public String getTransformerName() {
return sourceClass.getName() + targetClass.getName();
}
}
In Java it is impossible to get a Class<S>, unless you already know which class S is, or something else that knows which class S is gives you one.
I am using JAVA7. I have below method.
public static <T extends BaseClass> T newObject(Class<T> classOfT, String someName)
{
SomeClass data = new SomeClass();
data.setName(someName);
Class[] constructorSignature = new Class[] {SomeClass.class};
Object[] constructorArgs = new Object[] {data};
return returnNewObject(classOfT, constructorSignature, constructorArgs);
}
Now i have to write a generic method which will accept above parameters. Method should be generic.
private static <T extends BaseClass> T returnNewObject(Class<T> classOfT, Class[] constructorSignature, Object[] constructorArgs) throws SomeException
{
try
{
Constructor<T> domainObjectConstructor = classOfT.getDeclaredConstructor(constructorSignature);
return (T)domainObjectConstructor.newInstance(constructorArgs);
}
}
Now how can i avoid below two lines?
Class[] constructorSignature = new Class[] {SomeClass.class};
Object[] constructorArgs = new Object[] {data};
I simply took arrays of Class and Object just to make the returnNewObject method generic.
Now how can i avoid above arrays and still make the returnNewObject method generic?
returnNewObject method should take any class, any data and any class type to copy the data.
I would write it like this.
public static <T extends BaseClass> T newObject(Class<T> classOfT, String someName) {
try {
return classOfT.getConstructor(SomeClass.class)
.newInstance(new SomeClass(someName));
} catch (Exception e) {
throw new SomeException("Failed to create " + classOfT, e);
}
}
You returnNewObject doesn't add much value, it just complicates things.
What you could do if you have just one argument is
private static <T extends BaseClass> T returnNewObject(Class<T> classOfT, Object arg) throws SomeException {
try {
return (T) classOfT.getConstructor(arg.getClass()).newInstance(arg);
}
}
I am in a situation where I want to have a map where the keys are an interface class, and the corresponding value is a class which implements that interface. In other words the key and value type is related.
My current implementation of the method which adds to the map, and gets an instance of the implementation class looks like:
// should be something like Class<T>, Class<? extends T>
static Map<Class<?>, Class<?>> map = new HashMap<Class<?>, Class<?>> ();
public static <T> void add(Class<T> interfaceT,
Class<? extends T> implementationT) {
map.put(interfaceT, implementationT);
}
public static <T> T get(Class<T> interfaceT) {
// cast caused by definition not complete.
Class<T> implementationT = (Class<T>) map.get(interfaceT);
// try catch stuff omitted
T t = implementationT.newInstance();
return t;
}
My question is:
Can I define the "map" variable so the cast in the get(...) method is unneeded? I could not make the " new HashMap<Class<T>, Class<? extends T>>()' work, so either it is impossible or I missed something fundamental :)
Please advise :)
Edit: It turned out that the asSubclass() method on Class did what I wanted :D
Class<?> rawClassFromMap = map.get(interfaceT);
Class<? extends T> implementationT = rawClassFromMap.asSubclass(interfaceT);
It is fine that implementationT is of type "? extends T" as I just need a T object returned.
I like generics. Reminds me of Haskell...
It looks like the goal is something like the "Typesafe Heterogenous Container" described by Josh Bloch in Chapter 5 of Effective Java (item 29). In his case, he's mapping a type (Class<T>) to an (already-instantiated) instance (T).
You can do something similar, using asSubclass instead of cast:
final class Factory
{
private Map<Class<?>, Class<?>> map = new HashMap<Class<?>, Class<?>>();
<T> void map(Class<T> type, Class<? extends T> impl)
{
map.put(type, impl.asSubclass(type));
}
private <T> Class<? extends T> get(Class<T> type)
{
Class<?> impl = map.get(type);
if (impl == null)
throw new IllegalArgumentException("Unknown type: " + type);
return impl.asSubclass(type);
}
<T> T create(Class<T> type)
throws Exception
{
Class<? extends T> impl = get(type);
Constructor<? extends T> ctor = impl.getConstructor();
return ctor.newInstance();
}
}
I would suggest a Proxy. Here's the Java example.
public interface Bike {
public String getWheels();
public int getSize();
}
public class MountainBike implements Bike {
#Override
public int getSize() {
return 24;
}
#Override
public String getWheels() {
return "Treaded";
}
#Override
public String toString() {
String newLine = System.getProperty("line.separator");
StringBuilder sb = new StringBuilder();
sb.append("Type: MOUNTAIN").append(newLine);
sb.append("Wheels: ").append(getWheels()).append(newLine);
sb.append("Size: ").append(getSize()).append(newLine);
return sb.toString();
}
}
public class CruiserBike implements Bike {
#Override
public int getSize() {
return 26;
}
#Override
public String getWheels() {
return "Smooth";
}
#Override
public String toString() {
String newLine = System.getProperty("line.separator");
StringBuilder sb = new StringBuilder();
sb.append("Type: CRUISER").append(newLine);
sb.append("Wheels: ").append(getWheels()).append(newLine);
sb.append("Size: ").append(getSize()).append(newLine);
return sb.toString();
}
}
public class BikeProxy implements InvocationHandler {
private Object obj;
public static Object newInstance(Object obj)
{
return java.lang.reflect.Proxy.newProxyInstance(obj.getClass()
.getClassLoader(), obj.getClass().getInterfaces(),
new BikeProxy(obj));
}
public static <T> T newInstance(String className)
{
try
{
return (T) newInstance(Class.forName(className));
}
catch (ClassNotFoundException e)
{
throw new RuntimeException(e);
}
}
public static <T> T newInstance(Class<T> bikeClass)
{
try
{
return (T) java.lang.reflect.Proxy.newProxyInstance(Bike.class.getClassLoader(), new Class[]{Bike.class},
new BikeProxy(bikeClass.newInstance()));
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
private BikeProxy(Object obj)
{
this.obj = obj;
}
public Object invoke(Object proxy, Method m, Object[] args)
throws Throwable
{
Object result;
try
{
result = m.invoke(obj, args);
}
catch (InvocationTargetException e)
{
throw e.getTargetException();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
return result;
}
}
public class ProxyTester
{
public static void main(String[] args)
{
Bike mountainBike = BikeProxy.newInstance(MountainBike.class);
System.out.println(mountainBike);
Bike mountainBike2 = BikeProxy.newInstance(MountainBike.class.getName());
System.out.println(mountainBike2);
Bike cruiserBike = BikeProxy.newInstance(CruiserBike.class);
System.out.println(cruiserBike);
Bike cruiserBike2 = BikeProxy.newInstance(CruiserBike.class.getName());
System.out.println(cruiserBike2);
}
}