Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask Java Expert


Home >> Java

1 Introduction

In order to debug an application, it is sometimes useful to know where a given object comes from, or where was it sent to. An object tracer describes the origin of an object and also the places where the object was provided as an argument or returned as a result.

2 Goals

Implementation of a tracer of Java objects. The tracer can be invoked from any point of a Java program, accepting an object as argument. The tracer should immediatly log on System.err the history of that object in terms of the place where it was created, and all places where it was provided as an argument or returned as a result.

The tracer is called using the following form:

ist.meic.pa.Trace.print(object)

This means that y o u need to implement the class ist.meic.pa.Trace withastatic method that accepts one object as argument.

As a result of calling the previous method, the tracer must then present the point of instantiation and the points of call/return in the history of the object, namely:

  •  The constructor call used to create the object, including le name and line number.
  •  All method calls where the object was used as an argument, including le name and line number.
  •  All method calls where the object was returned, including le name and line number.

It should be possible to invoke the tracer in several di erent points of the (traced) program, with several di erent objects. The trace information for that object should follow the time sequence of operations on that object, from the oldest one to the most recent one.

3 Interface

The tracer presents the object history based on a textual representation that is printed on the console. As an example, consider the following Java class de ned in le Test0.java:

1 import ist.meic.pa.Trace;
2
3 class Test {
4
5 public Object foo() {
6 return new String("Foo");
7 }
8
9 public Object bar() {
10 return new String("Bar");
11 }
12
13 public Object identity(Object o) {
14 return o;

15 }
16
17 public void test() {
18 Trace.print(foo());
19 Object b = bar();
20 Trace.print(identity(b));
21 }
22 }
23
24 public class Test0 {
25
26 public static void main(String args[]) {
27 (new Test()).test();
28 }
29 }
The execution of this Java class le produces the following output:
$ java Test0
Tracing for Foo is nonexistent!
Tracing for Bar is nonexistent!

However, by using Javassist, it is possible to transform the bytecode of the class le in a way that allows much more information to be printed. The bytecode transformation is done at load time, by the class TraceVM.

Using this class, the example becomes:

$ java ist.meic.pa.TraceVM Test0

Tracing for Foo
<- java.lang.String(java.lang.String) on Test0.java:6
<- Test.foo() on Test0.java:18
-> ist.meic.pa.Trace.print(java.lang.Object) on Test0.java:18

Tracing for Bar
<- java.lang.String(java.lang.String) on Test0.java:10
<- Test.bar() on Test0.java:19
-> Test.identity(java.lang.Object) on Test0.java:20
<- Test.identity(java.lang.Object) on Test0.java:20
-> ist.meic.pa.Trace.print(java.lang.Object) on Test0.java:20

Note, in the previous example, that the arrows show the direction of the control flow. The arrow <- indicates that the object was returned, either from calling a constructor, or from calling a method. The arrow -> indicates that the object was provided as argument to a constructor or to a method call. After printing the arrow, the tracer prints the invoked constructor or method, and the le name and line number where the invocation was done.

Asasecond example, consider the following le Test1.java:

1 import ist.meic.pa.Trace;
2
3 class Test {
4
5 public Object foo() {
6 return new String("Foo");
7 }
8
9 public Object bar() {
10 return foo();
11 }
12
13 public Object baz() {
14 return bar();
15 }
16
17 public void test() {

18 Trace.print(foo());
19 Trace.print(bar());
20 Trace.print(baz());
21 }
22 }
23
24 public class Test1 {
25
26 public static void main(String args[]) {
27 (new Test()).test();
28 }
29 }

Running the previous class le using the bytecode transformation implemented by TraceVM, produces the following output:

$ java ist.meic.pa.TraceVM Test1
Tracing for Foo
<- java.lang.String(java.lang.String) on Test1.java:6
<- Test.foo() on Test1.java:18
-> ist.meic.pa.Trace.print(java.lang.Object) on Test1.java:18
Tracing for Foo
<- java.lang.String(java.lang.String) on Test1.java:6
<- Test.foo() on Test1.java:10
<- Test.bar() on Test1.java:19
-> ist.meic.pa.Trace.print(java.lang.Object) on Test1.java:19
Tracing for Foo
<- java.lang.String(java.lang.String) on Test1.java:6
<- Test.foo() on Test1.java:10
<- Test.bar() on Test1.java:14
<- Test.baz() on Test1.java:20
-> ist.meic.pa.Trace.print(java.lang.Object) on Test1.java:20

Assignment is to implement the classes ist.meic.pa.Trace and ist.meic.pa.TraceVM. The rst one is used to print the history of an object. The second one is used to transform the bytecode of all classes loaded as a result of loading its rst argument.

In order to implement the required output format, y o u should consider the following templates:

 When there is no tracing information:

Tracing for object is nonexistent!
 When there is tracing information:

Tracing for object
followed, in the case of a constructor or method call returning object as result, by

<- behavior on file :line

or, in the case of a constructor or method call using object as argument

-> behavior on file :line

In the previous templates, object is the printed representation of an object, as produced by its toStringmethod, while behavior is the description of a constructor or method, as implemented b y Javassist API method getLongName. Finally, file is the name of the le and line is a number.

4 Code
Your implementation must work in Java 6 and should use the bytecode manipulation tool Javassist, version 3.18.1-GA.

The written code should have the best possible style, should allow easy reading and should not require excessive comments. It is always preferable to have clearer code with few comments than obscure code with lots of comments.

The code should be modular, divided in functionalities with speci c and reduced responsibilities. Each module should haveashort comment describing its purpose.

You must implement a Java class named ist.meic.pa.Trace that must provide, at least, the method with signature:

static public void print(Object object)

The previous method prints on System.err the trace of the object provided as argument, following the format described previously.

You must also implement a Java class named ist.meic.pa.TraceVM, containingastatic method main that accepts, as arguments, the name of another Java program (i.e., a Java class that also containsastatic method main)) and the arguments that should be provided to that program. The class should (1) operate the necessary transformations to the loaded Java classes so that objects are traced during the execution of the program, and (2) should transfer the control to the main method of the program.

Java, Programming

  • Category:- Java
  • Reference No.:- M9741718

Have any Question?


Related Questions in Java

Retail price calculatorwrite a java program that asks the

Retail Price Calculator Write a JAVA program that asks the user to enter an item's wholesale cost and its markup percentage. It should then display the item's retail price. For example: (If an item's wholesale cost is 5. ...

Operating systems assignment -problem 1 sharing the bridgea

Operating Systems Assignment - Problem 1: Sharing the Bridge A new single lane bridge is constructed to connect the North Island of New Zealand to the South Island of New Zealand. Farmers from each island use the bridge ...

Assessment -java program using array of Assessment -JAVA Program using array of objects

Assessment -JAVA Program using array of objects Objectives This assessment item relates to the course learning outcomes as stated in the Unit Profile. Details For this assignment, you are required to develop a Windowed G ...

Assessment database and multithread programmingtasktask 1

Assessment: Database and Multithread Programming Task Task 1: Grade Processing University grading system maintains a database called "GradeProcessing" that contains number of tables to store, retrieve and manipulate stud ...

Answer the following question whats the difference public

Answer the following Question : What's the difference public inheritance and private inheritance? What can derived classes inherit from base classes? What cannot be inherited from base classes?

Simple order processing systemquestion given the classes

Simple Order Processing System Question: Given the classes Ship (with getter and setter), Speedboat, and SpeedboatTest. Answer the following questions: Refine the whole application (all classes) and create Abstract class ...

Assessment database and multithread programmingtasktask 1

Assessment: Database and Multithread Programming Task Task 1: Grade Processing University grading system maintains a database called "GradeProcessing" that contains number of tables to store, retrieve and manipulate stud ...

Assessment socket programmingtaskwrite a java gui program

Assessment: Socket Programming Task Write a JAVA GUI program that would facilitate text chatting/exchanging between two or multiple computers over the network/internet, using the concept of JAVA socket programming. If yo ...

Project requirementsfor the problem described in the next

Project requirements For the problem described in the next section, you must do the following: 1. include your student ID at the end of all filenames for all java code files. Three classes have been identified in section ...

Chatbotscreate a small networked chat application that is

Chatbots Create a small, networked chat application that is populated by bots. Introduction On an old server park, filled with applications from the early days of the internet, a few servers still run one of the earliest ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As