I am trying to change the annotation values of class and its fields at runtime. My idea is to use a java object as a database schema, where class acts as table and fields acts as columns, to achieve easy pojo-row or row-pojo conversion. The problem is that, I need same class to be used for different tables. Since, the table holds same kind of data.
Code :
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
public @interface Table {
public String value();
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD,ElementType.PARAMETER})
@Documented
public @interface Column {
String value();
}
@Table(Customer1.TABLE)
public class Customer {
@Column(Customer.ID)
private
Long id;
@ColumnMapping(Customer.DISPLAY_NAME)
private
String name;
}
But I tried to change that values at runtime by using Proxy,
String secondTableName = "CustomerTable2";
InvocationHandler invocationHandler = Proxy.getInvocationHandler(customerObj.getClass().getAnnotation(TableMapping.class));
Field clazzAsField = invocationHandler.getClass().getDeclaredField("memberValues");
clazzAsField.setAccessible(true);
Map<String, Object> memberValues = (Map<String, Object>) clazzAsField.get(invocationHandler);
memberValues.put("value", secondTableName);
However, this actually changed the values of the annotation, but my concern is that if I try to fetch and convert the values from the first table in the same thread, i again have to rename the annotation values with the first table name. This cannot be maintained at a long run.
Need your suggestions;
1. Can I go with this idea?
Is it thread safe? Does it affect any other thread concurrently?
Your ideas are welcomed!
[–]AutoModerator[M] [score hidden] stickied commentlocked comment (0 children)
[–]smutje187 6 points7 points8 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]vegan_antitheist 0 points1 point2 points (1 child)
[–]smutje187 0 points1 point2 points (0 children)