in java is it possible to make a method aware of who called it (without changing parameters) and then return something else?
public class MyClassA {
public static final String someStirng = "this is some String"
public String getSomeString ()
{
return someString;
}
}
public class MyClassB extends MyClassA {
public static final String otherstring = "This is other string"
public SomeBean getContents()
{
SomeBean s = new someBean();
//if this method gets called from MyCallingClassOther then
// i want s.setContents(otherstring)
s.setContents(getSomeString());
return s;
}
}
public class MyCallingClass {
public String callingclassContents ()
{
MyClassB myb = new MyClassB();
return ((SomeBean)myb.getContents()).getFirstName();
}
}
public class MyCallingClassOther {
public String callingOtherContents ()
{
MyClassB myb = new MyclassB();
return ((SomeBean)myb.getContents()).getFirstName();
}
}
so when getContents()
method of MyClassB
gets called from MyCallingClassOther
then I want a different thing returned.
Only code I can change is the body of getContents()
(cant change parms). or I can change body of callingOtherContents()
in MyCallingClassOther
.
This is small piece of a bigger puzzle I am trying to solve ...which obviously was designed poorly. This is kind of a hackway.
Also I'd like to see how it is possible in some other languages?
and If you are wondering why I cant change parameters...thats because I do not want to change anything in callingclassContents()
of MyCallingClass