0

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.

user3334871
  • 1,251
  • 2
  • 14
  • 36
  • 4
    Let's get one thing out the way: [that's not how you compare Strings.](http://stackoverflow.com/q/513832/1079354) – Makoto Apr 21 '16 at 17:03
  • @Makoto and if this was production code, I would agree with you :) I'm just typing this out as a quick example to show what I am trying to do. – user3334871 Apr 21 '16 at 17:04
  • 4
    You don't need to use reflection. You can use a `Map`, which you would almost certainly use in a reflective approach anyway. What's the type of `env1IntrAVO` etc.? – Radiodef Apr 21 '16 at 17:06
  • 2
    I'd say you don't need a Map; you would want to use dependency injection here instead. – Makoto Apr 21 '16 at 17:07
  • @Makoto That may also be true. I don't know Maven so I don't know what they're actually doing here. – Radiodef Apr 21 '16 at 17:09
  • Thanks. I wasn't too sure on reflection anyways, it was just suggested by another developer, so I was trying to see if it fit. – user3334871 Apr 21 '16 at 17:10
  • @Radiodef env1IntrAVO is just an object class used to store the input values from a submitted form – user3334871 Apr 21 '16 at 17:11
  • 1
    @Radiodef: Maven has very little to do with this except allowing one to import the DI library of choice. – Makoto Apr 21 '16 at 17:13
  • why can't you just use polymorphism? – snerd Apr 21 '16 at 17:14
  • @Makoto Yes, see, I wouldn't know. :) – Radiodef Apr 21 '16 at 17:14
  • @DavidHoliday: Polymorphism would get them halfway there. Yes, they'd have a unified API with which to access the field they care about, but they'd still need to do the checks to see which entity they could instantiate. – Makoto Apr 21 '16 at 17:16
  • @user3334871 The reason that I ask for the type of `env1IntrAVO` etc. is that we need its type name to write code examples. – Radiodef Apr 21 '16 at 17:16
  • 1
    I think this question is very unclear. If your object structure includes IntrAnet and IntErnet, then all of this behaviour should be in those objects, probably, and I can't conceive of how reflection could possibly help here. Reflection to _do what?_ – DavidS Apr 21 '16 at 17:48

2 Answers2

1

If I were you I would:

First make all these objects implements an interface of type:

public interface Duration {
    String getStartTime();
    String getEndTime();
}

Then I would load all these objects into a Map<String, Duration> with ${domain}/${env} as key

And finally my code would be something like:

Duration duration = map.get(String.format("%s/%s", domain, env));
String startTime = duration.getStartTime();
String endTime = duration.getEndTime();
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
-1

Sure you can use reflection even polymorphism is what it should be.

Here is example:

public class Work {

    public Object f="w";
    public static void main(String[] args) throws Exception, IllegalAccessException {
             Work v=new Work();
             Object q=v.getValue(v, "f");
             System.out.println(q);
    }

    public Object getValue(Object source, String fieldName) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
        Class  aClass = source.getClass();
        java.lang.reflect.Field field = aClass.getField(fieldName);
        Object value=field.get(source);
        return value;
    }
}

using this approach your entire code will be just 2 lines:

String startTime = getValue(overallClassVO,"startTime");
String endTime = getValue(overallClassVO,"endTine");

Object overallClassVO will have these fields startTime and endTime no matter which environment it is.

Alex
  • 4,457
  • 2
  • 20
  • 59