Alter property of an object used inside the called method in Java
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
add a comment |
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
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
Ifcheck
variable is introduced by you, then create a new object of typeCheckObject
(please name something better) and use that from your controller layer. Then you can check usinginstanceof
.
– Sukhpal Singh
Nov 14 '18 at 11:38
add a comment |
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
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
java spring hibernate reflection aop
asked Nov 14 '18 at 10:09
Sathish
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
Ifcheck
variable is introduced by you, then create a new object of typeCheckObject
(please name something better) and use that from your controller layer. Then you can check usinginstanceof
.
– Sukhpal Singh
Nov 14 '18 at 11:38
add a comment |
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
Ifcheck
variable is introduced by you, then create a new object of typeCheckObject
(please name something better) and use that from your controller layer. Then you can check usinginstanceof
.
– 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
add a comment |
4 Answers
4
active
oldest
votes
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.
add a comment |
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
}
}
}
add a comment |
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.
add a comment |
You could make the boolean in the controller as a private class attribute and then use a getter to access its value from Dao.
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 14 '18 at 10:18
Muhammad Moustafa
212
212
add a comment |
add a comment |
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
}
}
}
add a comment |
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
}
}
}
add a comment |
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
}
}
}
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
}
}
}
answered Nov 14 '18 at 10:23
abj1305
17510
17510
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 14 '18 at 10:40
Moon.Hou
307
307
add a comment |
add a comment |
You could make the boolean in the controller as a private class attribute and then use a getter to access its value from Dao.
add a comment |
You could make the boolean in the controller as a private class attribute and then use a getter to access its value from Dao.
add a comment |
You could make the boolean in the controller as a private class attribute and then use a getter to access its value from Dao.
You could make the boolean in the controller as a private class attribute and then use a getter to access its value from Dao.
answered Nov 14 '18 at 10:21
user10651229
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 typeCheckObject
(please name something better) and use that from your controller layer. Then you can check usinginstanceof
.– Sukhpal Singh
Nov 14 '18 at 11:38