Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

UNIX HELP You are part of a team working on a large C++ program. The source code

ID: 3877313 • Letter: U

Question

UNIX HELP You are part of a team working on a large C++ program. The source code for this program is kept in the directory ~cs252/Assignments/commandsAsst/project.
After a certain amount of debate, the team has decided to change the order of the parameters of the function binarySearch. Someone else is actually changing the definition of that function. Your job is to find all the places where that function is called and to make repairs as necessary.
What command would you give to list all calls to that function? You should show the lines of code containing each call and the name of the file from in which each such line occurs. (It’s OK to produce a few incidental uses of the function name as well, because you will be inspecting the output before actually making the changes.)
Same problem, but suppose that all you wanted was a list of the source code file names in which the function was mentioned?
*Note: I don’t have access to the directory. UNIX HELP You are part of a team working on a large C++ program. The source code for this program is kept in the directory ~cs252/Assignments/commandsAsst/project.
After a certain amount of debate, the team has decided to change the order of the parameters of the function binarySearch. Someone else is actually changing the definition of that function. Your job is to find all the places where that function is called and to make repairs as necessary.
What command would you give to list all calls to that function? You should show the lines of code containing each call and the name of the file from in which each such line occurs. (It’s OK to produce a few incidental uses of the function name as well, because you will be inspecting the output before actually making the changes.)
Same problem, but suppose that all you wanted was a list of the source code file names in which the function was mentioned?
*Note: I don’t have access to the directory. UNIX HELP You are part of a team working on a large C++ program. The source code for this program is kept in the directory ~cs252/Assignments/commandsAsst/project.
After a certain amount of debate, the team has decided to change the order of the parameters of the function binarySearch. Someone else is actually changing the definition of that function. Your job is to find all the places where that function is called and to make repairs as necessary.
What command would you give to list all calls to that function? You should show the lines of code containing each call and the name of the file from in which each such line occurs. (It’s OK to produce a few incidental uses of the function name as well, because you will be inspecting the output before actually making the changes.)
Same problem, but suppose that all you wanted was a list of the source code file names in which the function was mentioned?
*Note: I don’t have access to the directory.

Explanation / Answer

Answer:

Problem can be solved using Throwable and StackTraceElement[] class.It is the power of any object oriented programming. Chain of responsibilty design which every object oriented programming language follow and due to that we can easily identify all teh places where fuction is called and make corrective action.

In chain of resposibilities, each object in the chain will have it’s own implementation to process the request, either full or partial or to send it to the next object in the chain.

We can understand the above program and find the solution using below code.

Suppose we have two classes in hierarchy ( using  inheritance concept ) Base class and Drive class. both are calling method1 and now due to some enhancement we need to chnage the signature of that method and for the same we need to identify the all the classes which are invoked it as the problem given in above question.

To solve this problem, we need to introduce one method we has to be invoked within method1. in our case it is doSomething().Please check the Callee.java class. You can check the implementation of dosomething method.

main responsibiltiy of this method is to print the parent caller method and class name from which it is being invoked and from method1, it is being invoked so method1 is parent method for doSomething. this two methos are making chain and that's why it calling chain of resposibitiy. Now all the class object which are invoking method1, will also invoke doSomething method. Now using below two lines of code, we can list down all the class name or method name fromw which method1 is being invoked.

Throwable t = new Throwable();
StackTraceElement[] elements = t.getStackTrace();

Please refer the Callee class file.

You need to run Base class main method to run the below code.

Please let me know if you face any chnallanges to make the understanding on above code.

Base.java

-----------------

package chegg.knowmethod;

public class Base {

   public void method1() {
       new Callee().doSomething();
   }

   public static void main(String[] args) {
       Base caller = new Base();
       caller.method1();

       caller = new CallerSub();
       caller.method1();
   }

}

Drive.java

----------------

package chegg.knowmethod;

public class Drive extends Base {

   public void method1() {
       new Callee().doSomething();
   }
}

Callee.java

------------------

package chegg.knowmethod;

public class Callee {
  
   public void doSomething() {
       Throwable t = new Throwable();
       StackTraceElement[] elements = t.getStackTrace();

       String calleeMethod = elements[0].getMethodName();
       String callerMethodName = elements[1].getMethodName();
       String callerClassName = elements[1].getClassName();

       System.out.println("CallerClassName=" + callerClassName
               + " , Caller method name: " + callerMethodName);
       System.out.println("Callee method name: " + calleeMethod);
   }
}

Output

----------------

CallerClassName=chegg.knowmethod.Base , Caller method name: method1
Callee method name: doSomething
CallerClassName=chegg.knowmethod.Drive , Caller method name: method1
Callee method name: doSomething

Please provide your valuable feedback.