Alter property of an object used inside the called method in Java












0















I have a java, spring web application using maven as build mechanism. Consider the following code (over-simplified version of my situation) where a controller calls a service to perform some operation and it turn calls some DAO methods to perform some actions in DB.



class MyController extends Controller {
public ModelAndView handleRequest(... request) {
boolean performCheck = Boolean.valueOf(request.getParameter("doCheck"));
myService.doSomeAction(object)
return ...;
}
}

class MyService {
public void doSomeAction(Object o){
myDao.doSomething(o);
}
}

class MyDao exterds HibernateDaoSuppot {
boolean check;

public void doSomething(Object o){
if(check == true){
// some action
} else {
// some other action
}

}
}


My question is how can I alter the value of the check boolean in the Dao method, based on the value I receive in the controller without explicitly passing the boolean through all the layers? I work with a legacy code with lot of business logics and the business team is not confident of making too many modifications to the existing code. However I am free to add any no of classes or aspects to perform the same.



I have tried reading the call stack in the DAO method and determine the boolean several layers above, but I do not feel good about working with the call stack and I am afraid that some future changes in the app architecture or JVM changes can mess up the call stack.










share|improve this question























  • Not sure why you need to change value of singleton class field from RestController. This can result in unidentified bugs.

    – Sukhpal Singh
    Nov 14 '18 at 10:26











  • @SukhpalSingh Yes, your are right. But I do not have any other option to perform two different actions in DAO layer. As I said, this is a code that I have not written, but I am asked to implement this check without any major code changes

    – Sathish
    Nov 14 '18 at 10:59











  • If check variable is introduced by you, then create a new object of type CheckObject (please name something better) and use that from your controller layer. Then you can check using instanceof.

    – Sukhpal Singh
    Nov 14 '18 at 11:38
















0















I have a java, spring web application using maven as build mechanism. Consider the following code (over-simplified version of my situation) where a controller calls a service to perform some operation and it turn calls some DAO methods to perform some actions in DB.



class MyController extends Controller {
public ModelAndView handleRequest(... request) {
boolean performCheck = Boolean.valueOf(request.getParameter("doCheck"));
myService.doSomeAction(object)
return ...;
}
}

class MyService {
public void doSomeAction(Object o){
myDao.doSomething(o);
}
}

class MyDao exterds HibernateDaoSuppot {
boolean check;

public void doSomething(Object o){
if(check == true){
// some action
} else {
// some other action
}

}
}


My question is how can I alter the value of the check boolean in the Dao method, based on the value I receive in the controller without explicitly passing the boolean through all the layers? I work with a legacy code with lot of business logics and the business team is not confident of making too many modifications to the existing code. However I am free to add any no of classes or aspects to perform the same.



I have tried reading the call stack in the DAO method and determine the boolean several layers above, but I do not feel good about working with the call stack and I am afraid that some future changes in the app architecture or JVM changes can mess up the call stack.










share|improve this question























  • Not sure why you need to change value of singleton class field from RestController. This can result in unidentified bugs.

    – Sukhpal Singh
    Nov 14 '18 at 10:26











  • @SukhpalSingh Yes, your are right. But I do not have any other option to perform two different actions in DAO layer. As I said, this is a code that I have not written, but I am asked to implement this check without any major code changes

    – Sathish
    Nov 14 '18 at 10:59











  • If check variable is introduced by you, then create a new object of type CheckObject (please name something better) and use that from your controller layer. Then you can check using instanceof.

    – Sukhpal Singh
    Nov 14 '18 at 11:38














0












0








0








I have a java, spring web application using maven as build mechanism. Consider the following code (over-simplified version of my situation) where a controller calls a service to perform some operation and it turn calls some DAO methods to perform some actions in DB.



class MyController extends Controller {
public ModelAndView handleRequest(... request) {
boolean performCheck = Boolean.valueOf(request.getParameter("doCheck"));
myService.doSomeAction(object)
return ...;
}
}

class MyService {
public void doSomeAction(Object o){
myDao.doSomething(o);
}
}

class MyDao exterds HibernateDaoSuppot {
boolean check;

public void doSomething(Object o){
if(check == true){
// some action
} else {
// some other action
}

}
}


My question is how can I alter the value of the check boolean in the Dao method, based on the value I receive in the controller without explicitly passing the boolean through all the layers? I work with a legacy code with lot of business logics and the business team is not confident of making too many modifications to the existing code. However I am free to add any no of classes or aspects to perform the same.



I have tried reading the call stack in the DAO method and determine the boolean several layers above, but I do not feel good about working with the call stack and I am afraid that some future changes in the app architecture or JVM changes can mess up the call stack.










share|improve this question














I have a java, spring web application using maven as build mechanism. Consider the following code (over-simplified version of my situation) where a controller calls a service to perform some operation and it turn calls some DAO methods to perform some actions in DB.



class MyController extends Controller {
public ModelAndView handleRequest(... request) {
boolean performCheck = Boolean.valueOf(request.getParameter("doCheck"));
myService.doSomeAction(object)
return ...;
}
}

class MyService {
public void doSomeAction(Object o){
myDao.doSomething(o);
}
}

class MyDao exterds HibernateDaoSuppot {
boolean check;

public void doSomething(Object o){
if(check == true){
// some action
} else {
// some other action
}

}
}


My question is how can I alter the value of the check boolean in the Dao method, based on the value I receive in the controller without explicitly passing the boolean through all the layers? I work with a legacy code with lot of business logics and the business team is not confident of making too many modifications to the existing code. However I am free to add any no of classes or aspects to perform the same.



I have tried reading the call stack in the DAO method and determine the boolean several layers above, but I do not feel good about working with the call stack and I am afraid that some future changes in the app architecture or JVM changes can mess up the call stack.







java spring hibernate reflection aop






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 10:09









SathishSathish

5629




5629













  • Not sure why you need to change value of singleton class field from RestController. This can result in unidentified bugs.

    – Sukhpal Singh
    Nov 14 '18 at 10:26











  • @SukhpalSingh Yes, your are right. But I do not have any other option to perform two different actions in DAO layer. As I said, this is a code that I have not written, but I am asked to implement this check without any major code changes

    – Sathish
    Nov 14 '18 at 10:59











  • If check variable is introduced by you, then create a new object of type CheckObject (please name something better) and use that from your controller layer. Then you can check using instanceof.

    – Sukhpal Singh
    Nov 14 '18 at 11:38



















  • Not sure why you need to change value of singleton class field from RestController. This can result in unidentified bugs.

    – Sukhpal Singh
    Nov 14 '18 at 10:26











  • @SukhpalSingh Yes, your are right. But I do not have any other option to perform two different actions in DAO layer. As I said, this is a code that I have not written, but I am asked to implement this check without any major code changes

    – Sathish
    Nov 14 '18 at 10:59











  • If check variable is introduced by you, then create a new object of type CheckObject (please name something better) and use that from your controller layer. Then you can check using instanceof.

    – Sukhpal Singh
    Nov 14 '18 at 11:38

















Not sure why you need to change value of singleton class field from RestController. This can result in unidentified bugs.

– Sukhpal Singh
Nov 14 '18 at 10:26





Not sure why you need to change value of singleton class field from RestController. This can result in unidentified bugs.

– Sukhpal Singh
Nov 14 '18 at 10:26













@SukhpalSingh Yes, your are right. But I do not have any other option to perform two different actions in DAO layer. As I said, this is a code that I have not written, but I am asked to implement this check without any major code changes

– Sathish
Nov 14 '18 at 10:59





@SukhpalSingh Yes, your are right. But I do not have any other option to perform two different actions in DAO layer. As I said, this is a code that I have not written, but I am asked to implement this check without any major code changes

– Sathish
Nov 14 '18 at 10:59













If check variable is introduced by you, then create a new object of type CheckObject (please name something better) and use that from your controller layer. Then you can check using instanceof.

– Sukhpal Singh
Nov 14 '18 at 11:38





If check variable is introduced by you, then create a new object of type CheckObject (please name something better) and use that from your controller layer. Then you can check using instanceof.

– Sukhpal Singh
Nov 14 '18 at 11:38












4 Answers
4






active

oldest

votes


















1














Of course the best is to pass the boolean variable. but instead; you can declare two functions, one for perfromCheck == true and another one for performCheck == false and call either from the controller based on the value of performCheck.






share|improve this answer































    1














    You can add a transient(transient only if object is a persistent object) boolean field in your object.



    Set the value of the boolean in your controller before passing the object to your service.
    You wont even need to maintain a separate check variable in your DAO layer.



    Your controller might look like:



    class MyController extends Controller {
    public ModelAndView handleRequest(... request) {
    object.setPerform(Boolean.valueOf(request.getParameter("doCheck"))); // object has a boolean field named 'perform'
    myService.doSomeAction(object)
    return ...;
    }
    }


    Your MyDao might look like:



    class MyDao exterds HibernateDaoSuppot {
    //boolean check; wont need this

    public void doSomething(Object o){
    if(o.getPerform() == true){
    // some action
    } else {
    // some other action
    }
    }
    }





    share|improve this answer































      1














      As your case: "check" is property of MyDao, you want change it dynamically from a method scope, this may cause Concurrent issues if you use single instance, that's not recommend.



      As mentioned by above user, you can use two MyDao instance in your app, one declare with True and the other with False, and your controller determine which one to use.






      share|improve this answer































        0














        You could make the boolean in the controller as a private class attribute and then use a getter to access its value from Dao.






        share|improve this answer























          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297647%2falter-property-of-an-object-used-inside-the-called-method-in-java%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          Of course the best is to pass the boolean variable. but instead; you can declare two functions, one for perfromCheck == true and another one for performCheck == false and call either from the controller based on the value of performCheck.






          share|improve this answer




























            1














            Of course the best is to pass the boolean variable. but instead; you can declare two functions, one for perfromCheck == true and another one for performCheck == false and call either from the controller based on the value of performCheck.






            share|improve this answer


























              1












              1








              1







              Of course the best is to pass the boolean variable. but instead; you can declare two functions, one for perfromCheck == true and another one for performCheck == false and call either from the controller based on the value of performCheck.






              share|improve this answer













              Of course the best is to pass the boolean variable. but instead; you can declare two functions, one for perfromCheck == true and another one for performCheck == false and call either from the controller based on the value of performCheck.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 14 '18 at 10:18









              Muhammad MoustafaMuhammad Moustafa

              212




              212

























                  1














                  You can add a transient(transient only if object is a persistent object) boolean field in your object.



                  Set the value of the boolean in your controller before passing the object to your service.
                  You wont even need to maintain a separate check variable in your DAO layer.



                  Your controller might look like:



                  class MyController extends Controller {
                  public ModelAndView handleRequest(... request) {
                  object.setPerform(Boolean.valueOf(request.getParameter("doCheck"))); // object has a boolean field named 'perform'
                  myService.doSomeAction(object)
                  return ...;
                  }
                  }


                  Your MyDao might look like:



                  class MyDao exterds HibernateDaoSuppot {
                  //boolean check; wont need this

                  public void doSomething(Object o){
                  if(o.getPerform() == true){
                  // some action
                  } else {
                  // some other action
                  }
                  }
                  }





                  share|improve this answer




























                    1














                    You can add a transient(transient only if object is a persistent object) boolean field in your object.



                    Set the value of the boolean in your controller before passing the object to your service.
                    You wont even need to maintain a separate check variable in your DAO layer.



                    Your controller might look like:



                    class MyController extends Controller {
                    public ModelAndView handleRequest(... request) {
                    object.setPerform(Boolean.valueOf(request.getParameter("doCheck"))); // object has a boolean field named 'perform'
                    myService.doSomeAction(object)
                    return ...;
                    }
                    }


                    Your MyDao might look like:



                    class MyDao exterds HibernateDaoSuppot {
                    //boolean check; wont need this

                    public void doSomething(Object o){
                    if(o.getPerform() == true){
                    // some action
                    } else {
                    // some other action
                    }
                    }
                    }





                    share|improve this answer


























                      1












                      1








                      1







                      You can add a transient(transient only if object is a persistent object) boolean field in your object.



                      Set the value of the boolean in your controller before passing the object to your service.
                      You wont even need to maintain a separate check variable in your DAO layer.



                      Your controller might look like:



                      class MyController extends Controller {
                      public ModelAndView handleRequest(... request) {
                      object.setPerform(Boolean.valueOf(request.getParameter("doCheck"))); // object has a boolean field named 'perform'
                      myService.doSomeAction(object)
                      return ...;
                      }
                      }


                      Your MyDao might look like:



                      class MyDao exterds HibernateDaoSuppot {
                      //boolean check; wont need this

                      public void doSomething(Object o){
                      if(o.getPerform() == true){
                      // some action
                      } else {
                      // some other action
                      }
                      }
                      }





                      share|improve this answer













                      You can add a transient(transient only if object is a persistent object) boolean field in your object.



                      Set the value of the boolean in your controller before passing the object to your service.
                      You wont even need to maintain a separate check variable in your DAO layer.



                      Your controller might look like:



                      class MyController extends Controller {
                      public ModelAndView handleRequest(... request) {
                      object.setPerform(Boolean.valueOf(request.getParameter("doCheck"))); // object has a boolean field named 'perform'
                      myService.doSomeAction(object)
                      return ...;
                      }
                      }


                      Your MyDao might look like:



                      class MyDao exterds HibernateDaoSuppot {
                      //boolean check; wont need this

                      public void doSomething(Object o){
                      if(o.getPerform() == true){
                      // some action
                      } else {
                      // some other action
                      }
                      }
                      }






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 14 '18 at 10:23









                      abj1305abj1305

                      18310




                      18310























                          1














                          As your case: "check" is property of MyDao, you want change it dynamically from a method scope, this may cause Concurrent issues if you use single instance, that's not recommend.



                          As mentioned by above user, you can use two MyDao instance in your app, one declare with True and the other with False, and your controller determine which one to use.






                          share|improve this answer




























                            1














                            As your case: "check" is property of MyDao, you want change it dynamically from a method scope, this may cause Concurrent issues if you use single instance, that's not recommend.



                            As mentioned by above user, you can use two MyDao instance in your app, one declare with True and the other with False, and your controller determine which one to use.






                            share|improve this answer


























                              1












                              1








                              1







                              As your case: "check" is property of MyDao, you want change it dynamically from a method scope, this may cause Concurrent issues if you use single instance, that's not recommend.



                              As mentioned by above user, you can use two MyDao instance in your app, one declare with True and the other with False, and your controller determine which one to use.






                              share|improve this answer













                              As your case: "check" is property of MyDao, you want change it dynamically from a method scope, this may cause Concurrent issues if you use single instance, that's not recommend.



                              As mentioned by above user, you can use two MyDao instance in your app, one declare with True and the other with False, and your controller determine which one to use.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 14 '18 at 10:40









                              Moon.HouMoon.Hou

                              307




                              307























                                  0














                                  You could make the boolean in the controller as a private class attribute and then use a getter to access its value from Dao.






                                  share|improve this answer




























                                    0














                                    You could make the boolean in the controller as a private class attribute and then use a getter to access its value from Dao.






                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      You could make the boolean in the controller as a private class attribute and then use a getter to access its value from Dao.






                                      share|improve this answer













                                      You could make the boolean in the controller as a private class attribute and then use a getter to access its value from Dao.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 14 '18 at 10:21







                                      user10651229





































                                          draft saved

                                          draft discarded




















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid



                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.


                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297647%2falter-property-of-an-object-used-inside-the-called-method-in-java%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Xamarin.iOS Cant Deploy on Iphone

                                          Glorious Revolution

                                          Dulmage-Mendelsohn matrix decomposition in Python