I was having some problem when trying to set the field values by using Reflection library in Java. What I am trying to do is whenever the field value is -9999 or "Z", I then modify it to 0 or "". Here is my code:
public static void initializeObject(vtModule.acct obj){
ReflectionUtils.doWithFields(obj.getClass(), field -> {
log.debug("Field name: " + field.getName());
log.debug("Field value: "+ field.get(obj));
if(field.get(obj) instanceof Integer) {
if (field.get(obj).equals(-9999)) {
log.debug("COME IN -9999");
field.setAccessible(true);
field.setInt(field.get(obj), 0);
}
}
if(field.get(obj) instanceof String) {
if (field.get(obj).equals("Z")) {
log.debug("COME IN Z");
field.setAccessible(true);
field.set(field.get(obj), "");
}
}
});
}
My class object as such:
public final class acct {
public String date_inactive;
public String purchase_order;
public int child_count;
public char vip_code;
}
The part where the code initialize the object with default values which I am not supposed to change, that is the reason why I have to come out with another function to convert the -9999 value to 0 and "Z" value to "":
static vtModule.acct initAcct(vtModule.acct account) {
account.date_inactive="";
account.purchase_order="";
account.child_count=-9999;
account.vip_code='Z';
}
I managed to print out the field name as well as go into the if statement to check for -9999 value. However, I am getting error message when trying to set the value to that particular field:
java.lang.IllegalArgumentException: Can not set int field vtModule.acct.child_count to java.lang.Integer
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)
at sun.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58)
at sun.reflect.UnsafeIntegerFieldAccessorImpl.setInt(UnsafeIntegerFieldAccessorImpl.java:128)
at java.lang.reflect.Field.setInt(Field.java:949)
at test.ManageUtil.lambda$initializeObject$0(ManageUtil.java:498)
at org.springframework.util.ReflectionUtils.doWithFields(ReflectionUtils.java:731)
at org.springframework.util.ReflectionUtils.doWithFields(ReflectionUtils.java:710)
at test.ManageUtil.initializeObject(ManageUtil.java:491)
at test.ManageBPOImpl.save(ManageBPOImpl.java:3064)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.camel.support.ObjectHelper.invokeMethodSafe(ObjectHelper.java:201)
at org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:423)
at org.apache.camel.component.bean.MethodInfo$1.doProceed(MethodInfo.java:244)
at org.apache.camel.component.bean.MethodInfo$1.proceed(MethodInfo.java:215)
at org.apache.camel.component.bean.AbstractBeanProcessor.process(AbstractBeanProcessor.java:148)
at org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:55)
at org.apache.camel.processor.errorhandler.RedeliveryErrorHandler$RedeliveryState.run(RedeliveryErrorHandler.java:476)
at org.apache.camel.impl.engine.DefaultReactiveExecutor$Worker.schedule(DefaultReactiveExecutor.java:185)
at org.apache.camel.impl.engine.DefaultReactiveExecutor.scheduleMain(DefaultReactiveExecutor.java:59)
at org.apache.camel.processor.Pipeline.process(Pipeline.java:87)
at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:228)
at org.apache.camel.impl.engine.DefaultAsyncProcessorAwaitManager.process(DefaultAsyncProcessorAwaitManager.java:78)
at org.apache.camel.support.AsyncProcessorSupport.process(AsyncProcessorSupport.java:40)
at org.apache.camel.component.jms.EndpointMessageListener.onMessage(EndpointMessageListener.java:111)
at org.springframework.jms.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:736)
at org.springframework.jms.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:696)
at org.springframework.jms.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:674)
at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.doReceiveAndExecute(AbstractPollingMessageListenerContainer.java:318)
at org.springframework.jms.listener.AbstractPollingMessageListenerContainer.receiveAndExecute(AbstractPollingMessageListenerContainer.java:257)
at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.invokeListener(DefaultMessageListenerContainer.java:1189)
at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.executeOngoingLoop(DefaultMessageListenerContainer.java:1179)
at org.springframework.jms.listener.DefaultMessageListenerContainer$AsyncMessageListenerInvoker.run(DefaultMessageListenerContainer.java:1076)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Aucun commentaire:
Enregistrer un commentaire