jeudi 21 avril 2016

How would I use Java reflection in this case?

I am working on a Maven project that includes a few object classes. My code centers around controlling start times and end times for a particular functionality in three different environments that are separate in IntrAnet and IntErnet domains. So my object structure looks something like:

IntrAnet:
  env1:
    startTime:
    endTime:
  env2:
    startTime:
    endTime
IntErnet:
  env1:
    startTime:
    endTime:
...

Now, in my controller class, I want to use the start time and end time depending on what environment and domain the user is in. So I have code that looks like:

if(domain == "IntrAnet") {
   if(env == "env1") {
     String startTime = overallClassVO.env1IntrAVO.getStartTime();
     String endTime = overallClassVO.env1IntrAVO.getEndTime();
     ...
   }
   if(env == "env2") {
     String startTime = overallClassVO.env2IntrAVO.getStartTime();
     String endTime = overallClassVO.env2IntrAVO.getEndTime();
     ...
   }
}
if(domain == "IntErnet") {
   if(env == "env1") {
     String startTime = overallClassVO.env1IntErVO.getStartTime();
     String endTime = overallClassVO.env1IntErVO.getEndTime();
     ...
   }
   if(env == "env2") {
     String startTime = overallClassVO.env2IntErVO.getStartTime();
     String endTime = overallClassVO.env2IntErO.getEndTime();
     ...
   }
}

My code is a little more complex, but that is the general idea. I know reflection is useful in simplifying repetitive code by calling classes based on the object during runtime, but I am wondering if I can use reflection in this case.





Aucun commentaire:

Enregistrer un commentaire