java question to decide who called this method - java

I have the following scenario where
both testOne() and testTwo calls same callMe() method.
How do I decide inside callMe() method who called callMe().
public void testOne(){
callMe();
}
public void testTwo(){
callMe();
}
public void callMe(){
System.out.println("I was called by following method."+methodName);
}
Any sort of help is appreciated.

Any solution that has you generating a stacktrace and looking at the second frame is one that is going to lead to pain - what you are essentially doing is bypassing the idea of passing what a function needs to it in order for the function to do it's work.
If you need the name of the caller method, then just pass it as a parameter. If you need some other piece of data to decide what to do in the callMe() method, pass it (as a boolean, int, etc.).
It will confuse other developers working on your code why callMe() has what are essentially secret parameters.
public void testOne(){
callMe("testOne");
}
public void testTwo(){
callMe("testTwo");
}
public void callMe(String methodName){
System.out.println("I was called by following method."+methodName);
}

My best answer is to query the stack trace.
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
String previousMethodName = null;
for (int i = 0; (i < stackTrace.length) && (previousMethodName == null); i++)
{
if (stackTrace[i].getMethodName().equals("callMe") && (i < stackTrace.length - 1))
{
previousMethodName = stackTrace[i + 1].getMethodName();
}
}
if (previousMethodName != null)
{
System.out.println("Previous method = " + previousMethodName);
}

sorry, i meant to answer your question and not comment :( so here it is
i think this already answered question may help you out: Get current stack trace in Java

The simplest approach is to use a parameter
public static void testOne(){
callMe("testOne");
}
public static void testTwo(){
callMe("testTwo");
}
public static void callMe(){
System.out.println("I was called by following method."+methodName);
}
However, you can use the call stack.
public static void callMe(){
String methodName = Thread.currentThread().getStackTrace()[2].getMethodName();
System.out.println("I was called by following method."+methodName);
}

Related

any tools to trace code using eclipse line by line?

It's been long time since I've asked a question so forgive my mistakes.
So, I have a code which I don't understand. It's dealing with interface and super/sub class. I even have the answers to it but I just don't know how it got to the answer. My question is that, how would I learn tracing or is there a way I can see which lines are executed first in Eclipse?
For example, does eclipse or any other tool allows the user to actually see which and why the lines are printing?
Here is my code. I Have the correct answer to it but I just don't know how they traced it. Any help would be appreciated.
interface Silly {
public void narf();
public void poit(Silly s);
}
public class Bird implements Silly {
public static void main(String args[]) {
System.out.println("zero");
Silly s = new SillyBird(1);
Silly s2 = new Loony();
s.poit(s2);
s2.poit(s);
System.out.println("zymurgy");
}
public Bird() {
this(0);
System.out.println("zircon");
}
public Bird(int i) {
System.out.println("zanzibar");
}
public void narf() {
System.out.println("zort");
}
public void poit(Silly s) {
s.narf();
}
}
class SillyBird extends Bird {
public SillyBird() {
System.out.println("duchess");
}
public SillyBird(int i) {
super(i);
}
public void narf() {
System.out.println("drum");
super.narf();
}
}
class Loony extends SillyBird {
public Loony() {
System.out.println("stupendous");
}
public void narf() {
System.out.println("snark");
}
}
The output of the above code was:
zero
zanzibar
zanzibar
zircon
duchess
stupendous
snark
drum
zort
zymurgy
As mentioned by #Abra in the comments you can set a breakpoint at the functions you want to have a look at and use the ‚go into‘ operation to see what is going on in the function at execution. The debugger will show you the state of all locals and globals at each step of the execution

Increment number after method is called Mockito

I have simple class with void method.
public class Simple {
public Void doSomething() {}
}
And I want to increment a number in my test after calling doSomething().
#Test
public void test1() {
int number = 0;
Simple simple = Mockito.mock(Simple.class);
Mockito.when(simple.doSomething()).then(number++);
}
Of course it is causing compilation error. How can I increment number after calling doSomething() ?
Alternative solution:
It's very bad practice but it's alternative solution for best answer below.
private int number = 0;
#Test
public void test1() {
Simple simple = Mockito.mock(Simple.class);
Mockito.when(simple.doSomething()).thenReturn(increment());
}
private Void increment() {
number++;
return null;
}
It is not clear from your question why you would want to have such a thing. The code in this answer is not good practice and I won't recommend it.
The first problem is that your method returns void so you can't stub it with Mockito.when. You need to use Mockito.doWhen.
In this code, I use Mockito.doAnswer to write a custom code inside the answer part:
#Test
public void test1() {
int[] number = { 0 };
Simple simple = Mockito.mock(Simple.class);
Mockito.doAnswer(new Answer<Void>() {
#Override
public Void answer(InvocationOnMock invocation) throws Throwable {
number[0]++;
return null;
}
}).when(simple).doSomething();
}
The trick is to store the number inside a final variable (in this case, an array) and increment its only value in the answer part. Each time doSomething() will be called, it will be incremented and you will have the count inside number[0].
The same code can be made more compact with Java 8:
Mockito.doAnswer(invocation -> number[0]++).when(simple).doSomething();

Call Methods in Sequence

I have 5 different methods like this
public void met1(){}
public void met2(){}
public void met3(){}
public void met4(){}
public void met5(){}
I want to call this method from 1 to 5 is there any convinient way to do this.
I don't want to call one by one or I don't want to put method call inside other method.
How Can I do this??
I believe you could do it with reflection with something like:
YourClass classInstance = new YourClass();
for (int i = 1; i < 6; i++) {
Method yourMethod = YourClass.class.getMethod("met" + i);
method.invoke(instance);
}
Haven't tested it out, so no guarantees.
Have you looked into fluent design patterns? http://www.javacodegeeks.com/2013/01/fluent-object-creation.html
Example would be something like this:
myObject.met1().met2().met3().met4().met5().result();
You can use the Execute Around Method pattern.
public class Resource {
private Resource(){}
public void opc1(){
System.out.println("Opc1");
// use can use cascade pattern( return this)
}
public void opc2(){
System.out.println("Opc2");
// use can use cascade pattern( return this)
}
public void opc3(){
System.out.println("Opc3");
// use can use cascade pattern( return this)
}
public void closeResource(){
System.out.println("Release resource");
}
public static void use(Consumer<Resource> consumer){
Resource resource =new Resource();
consumer.accept(resource);
resource.closeResource(); // force to execute closeResource method
}
public static void main(String[] args) {
Resource.use(resource -> {
resource.opc1();
resource.opc3();
resource.opc2();
});
}
}
More info at https://agiledeveloper.com/
You could do this with reflection as other answers have previously mentioned. Reflection is generally avoided if possible.
In reality the most common design pattern to address your concern would be Chain of Responsibility.
http://en.wikipedia.org/wiki/Chain-of-responsibility_pattern
The only way to call methods by name is through reflection. See this article for how to do this:
How do I invoke a Java method when given the method name as a string?
Something like this should work:
for (int i = 1; i < 6; i++){
java.lang.reflect.Method method;
try {
method = this.getClass().getMethod("met" + i);
method.invoke(this);
} catch (SecurityException e) {
// ...
} catch (NoSuchMethodException e) {
// ...
}
}

Calling another method from a String method after it returns the string

I have something I am trying to figure out. So when I run this method called:
public String showList()
I want it to return the string, but after that call a method called displayMenu, so it will automatically go to the next method when the showList is called. Is this possible?
The showList method: (I want to call another method called public void displayMenu())
public String showList()
{
sortList();
int i = 0;
String retStr = "The nodes in the list are:\n";
LinkedListNode current = front;
while(current != null){
i++;
retStr += "Node " + i + " is: " + current.getData() + "\n";
current = current.getNext();
}
return retStr;
//Would like to call the displayMenu() here, but I can't after the string returns it is unreachable.
}
Note: I don't recommend doing this. I definitely recommend that you call the methods one after another in some controller class. Now that that's done:
A rather complicated method of doing this that hasn't yet been mentioned is by using a Thread. I would not generally do this, but it is worth noting that it can be done. Note that this is dealing with Threads (see tutorial) so I won't guarantee that the method will evaluate after the return.
One way of doing this is as follows:
In the same class as your method include something like the following:
class doSomething implements Runnable{
public void run(){
displayMenu();
}
}
Then, in your showList method, do the following:
public String showList(){
...//some code
(new Thread(new doSomething())).start(); //more efficient: create a
//variable to hold the thread.
return retStr;
}
Example code:
public class test{
public static void main(String[]a){
System.out.print(foo());
}
public static String foo(){
(new Thread(new fooBar())).start();
return "foo";
}
public static void bar(){
System.out.println("bar");
}
static class fooBar implements Runnable{
public void run(){
bar();
}
}
}
Prints:
foobar
you can not write anything after the return statement. Return statement should be the last line of a method.
The thing about a return statement is that when you use it, you're basically saying that "I want to return this value now". Not in 5 lines, not after another function call. Now.
Thus, it doesn't make sense to want to do something in a method after returning a value; you've already basically indicated that there's nothing left to do; you're done and ready to go back to the calling method.
#DarkKnight and #ManZzup have already suggested alternatives; you need to restructure your code so that there's no need for such a construct.
Modify your method calling showList() as shown below. Let's call this method is doSomething()
doSomething(){
String output=showList(..); // This is your existing method call
displayMenu(); // call displaymenu() once showlist() execution is over
}

How to return String in anonymous class for a method returning void

I'm bit confused. I have the following:
public static String showInputDialog() {
Form frm = new Form();
final Command cmd = new Command("Ok");
final TextField txt = new TextField("Enter the text", null, 1024, 0);
frm.addCommand(cmd);
frm.append(txt);
frm.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable d) {
if (c == cmd) {
return txt.getString(); // Error !!
} else {
return null; // Error !!
}
}
});
}
As you can see, I want to return the input dialog string, while the anonymous class method should return void. How can I resolve this problem?
This does not work as you expected.
I see there are already some solutions, but I feel a bit more discussion about what is actually going on might be helpful.
When you call the frm.setCommandListener(new CommandListener() { ... }) the code presents the user with a dialog where she can type in some text and submit, but the code does not stop and wait until the user finishes.
Instead the code continues to execute - without yielding the result. Only after the user finished typing and submits, you get called back to process the result - which might happen much later, or not at all.
I guess you have some code calling this method like:
public void someMethod(int foo, String bar) {
[...]
String result = MyInputForm.showInputDialog();
// do something with the result
System.out.println("hey, got a result "+ result);
[...]
}
Instead you need to reorganize this. First write a helper class handling the result:
public static class MyCallBack {
public MyCallBack(... /* here pass in what you need to process the result*/) {
... remember necessary stuff in instance variables
}
public void processResult(String result) {
// do something with the result
System.out.println("hey, got a result "+ result);
[...]
}
}
then the calling side does just:
public void someMethod(int foo, String bar) {
[...]
MyInputForm.showInputDialog( new MyCallBack(... here pass in stuff ...) );
[...]
}
and the actual code has to be changed to:
public static String showInputDialog(final MyCallBack callback) {
Form frm = new Form();
final Command cmd = new Command("Ok");
final TextField txt = new TextField("Enter the text", null, 1024, 0);
frm.addCommand(cmd);
frm.append(txt);
frm.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable d) {
if (c == cmd) {
return callback.processResult(txt.getString());
} else {
return; // or just omit the else part
}
}
});
}
Two issues:
this way of programming feels pretty backwards, but it is really the way it works.
what feels not right is that I need to define a second helper class aside of the CommandListener. That is really not good style. I hope it can be improved, but as I do not see the complete code (which would be too much information anyway), I have to leave it to you to improve the code and get rid of the clutter. While I feel you want to have a modular, reusable input dialog helper, this might not be the best approach; better define the Form,TextField and Command directly where you need the result and get that running. Make it reusable in a second step after you get it running.
You don't need to return it if you instead do something with the String or store it somewhere, for example:
static String result;
public String commandAction(Command c, Displayable d) {
if (c == cmd) {
result = txt.getString();
} else {
result = null;
}
}
Although you'll have threading issues to deal with.
Given that CommandListener is fixed, 2 possible options are
Use a class member variable in the outer class & assign to that variable instead
private static String myText;
...
public static String showInputDialog() {
...
frm.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable d) {
if (c == cmd) {
myText = txt.getString();
} else {
myText = null;
}
}
});
}
or Create a concrete implementation of your CommandListener and set the return value as a property of the new implementation
I would have a look at making the method/variable in this snippet non-static...
You cant return the string because you dont know when the listener will be called.
You can do something with it once you have the string though.
public static void showInputDialog() {
StringHandler sh = new StringHandler();
frm.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable d) {
if (c == cmd) {
sh.handle(txt.getString());
} else {
sh.handle(null);
}
}
});}
public class StringHandler {
public void handle(String s){
// Do something with that string.
}
}

Categories