December 08, 2004

Stack trace translation using annotations

Code generators and compilers that use Java as an intermediate language (e.g, JSP compilers) can take advantage of the new Java annotations feature for translating stack traces.

Why is this necessary? Any runtime error in the form of an exception will have its stack trace expressed in terms of the intermediate java code. Troubleshooting the problem could become a nightmare, unless the line and number information is presented in the context of the components and methods originally written by the developer.

In simple terms, the idea is to place line number information in metadata blocks in every class generated by the compiler. This information is accessed during runtime to interpret and translate stack traces back into something the developer will understand.

Let's consider a hypothetical component called "MyObj" that has two methods: "methodA" and "methodB". Here's an example of what the intermediate Java code might look like:

@TranslationTable(lines = {
  @Entry(line=11,component="MyObj",method="methodA",sourceLine=241),
  @Entry(line=12,component="MyObj",method="methodA",sourceLine=247),
  @Entry(line=13,component="MyObj",method="methodA",sourceLine=249),
  @Entry(line=18,component="MyObj",method="methodB",sourceLine=340)
})
public class AutogeneratedObject
{
  void autogeneratedMethod1()
  {
     int a = 1;
     int b = 2;
     autogeneratedMethod2();
  }

  void autogeneratedMethod2()
  {
     throw new RuntimeException();
  }
}


The metadata block is made of a tag (@TranslationTable) that contains a list of sub-tags (@Entry). Each entry represents a line of significant code in the generated class. Information about the line number in the generated class and the name of the original component, method and line number it maps to are stored in each entry.

Here's the definition of the Entry tag:

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
public @interface Entry
{
  int line();

  String component();
  String method() default "[unknown]";
  int sourceLine();
}


And here's the TranslationTable tag:

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface TranslationTable
{
  Entry[] lines();
}


The stack trace translator takes each stack frame, obtains the associated class, queries the class for it's TranslationTable metadata and looks up the TranslationTableEntry for the line reported by the stack frame.

This is what the stack trace translator might look like. I know, I know, the implementation very inefficient (linear traversals of the translation tables, no caching)... I'm just trying to make a point here :)

StackTraceElement[] translate(StackTraceElement[] elements)
{
  List result = new ArrayList();

  for (StackTraceElement element : elements) {
     try {
       Class clazz = Class.forName(element.getClassName());
       TranslationTable table = (TranslationTable) clazz.getAnnotation(TranslationTable.class);

       boolean found = false;
       if (table != null) {
         for (Entry line : table.lines()) {
           if (line.line() == element.getLineNumber()) {
             result.add(new StackTraceElement(line.component(), line.method(), line.component(), line.sourceLine()));
             found = true;
             break;
           }
         }
       }

       if (table == null || !found) {
         result.add(element);
       }
     }
     catch (ClassNotFoundException e) {
       e.printStackTrace();
     }

  }

  return result.toArray(new StackTraceElement[0]);
}


If you're not in the mood for trying out the code, I already did it for you :). Here's a regular Java stack trace:

java.lang.RuntimeException
  at AutogeneratedObject.autogeneratedMethod2(Test.java:18)
  at AutogeneratedObject.autogeneratedMethod1(Test.java:13)
  at Test.main(Test.java:25)


And the translated version:

MyObj.methodB(340)
MyObj.methodA(249)
Test.main(25)

13 Comments:

Blogger AlekS said...

This comment has been removed by a blog administrator.

February 21, 2005 2:25 AM  
Blogger AlekS said...

Very good idea and this probably should be standardized ..

February 21, 2005 2:25 AM  
Blogger nasha said...

Why was there no follow on bankruptcy then? The bailout of AIG FP went to (wow power leveling) hedge funds that bound credit swaps on Lehman failing or others betting on rating (wow power leveling) declines. AIG has drained over 100 billion from the government. Which had to go to (wow power leveling) those who bet on failures and downgrades. Many of whom (power leveling)were hedge funds. I-banks that had offsetting swaps needed the money from the AIG bailout or they would have been caught. Its an (wow powerleveling) insiders game and it takes just a little bit too much time for most people to think (wow gold) through where the AIG 100 billion bailout money went to, hedge funds and players, many of whom hire from the top ranks of DOJ, Fed, Treasury, etc. ZHANG XIAO CHEN

April 19, 2009 10:02 PM  
Blogger Kayla said...

Cell phones from simple communication became a full-featured entertainment terminal . Can be said that cell phone china new milestone in the industry. This fully demonstrates that, cheap cell phones entertainment has become the most sought after consumer applications and has become the focus of the mobil phones market.

September 24, 2009 9:19 PM  
Blogger Kayla said...

Present-day society, the rapid development of science and technology makes cell phones wholesale is no longer a simple phone communications equipment only. Wholesale cell phones have many features, entertainment, scalability, and so have a good expression of the discount cell phones will be to maximize the effectiveness of multimedia technology.

September 24, 2009 9:19 PM  
Blogger Kayla said...

www.crazypurchase.com Web site for the majority of the china wholesalers, and buy products wholesale to provide information and buy wholesale from china information exchange platform dedicated to becoming China Wholesale.

September 24, 2009 9:19 PM  
Blogger Adi said...

Oes Tsetnoc | Mengembalikan Jati Diri Bangsa | Kenali dan Kunjungi Objek Wisata di Pandeglang | Oes tsetnoc | Online Marketing | Electronics Gadgets | etips solution | Travel Guide

October 27, 2009 12:40 AM  
Blogger The Jack said...

Thanks ever so much, very useful article. If you do not mind, please visit my article related to pandeglang district in Banten, Indonesia at Kenali dan Kunjungi Objek Wisata di Pandeglang or Kenali dan Kunjungi Objek Wisata di Pandeglang second and also Kenali dan Kunjungi Objek Wisata di Pandeglang Objek Wisata Air Terjun Curug Gendang or related to a leadership at Mengembalikan Jati Diri Bangsa and Oes Tsetnoc or Oes Tsetnoc the second and our hard work at Kerja Keras Adalah Energi Kita that's right Kerja Keras Adalah Energi Kita, and Kenali dan Kunjungi Objek Wisata di Pandeglang Memasuki Babak Akhir also Objek Wisata Taman Wisata Alam Carita, Kenali dan Kunjungi Objek Wisata di Pandeglang, or Kenali dan Kunjungi Objek Wisata di Pandeglang, also Kenali dan Kunjungi Objek Wisata di Pandeglang, or Kenali dan Kunjungi Objek Wisata di Pandeglang, also Kenali dan Kunjungi Objek Wisata di Pandeglang, or Kenali dan Kunjungi Objek Wisata di Pandeglang, also Kenali dan Kunjungi Objek Wisata di Pandeglang, or Kenali dan Kunjungi Objek Wisata di Pandeglang, very smart thank you!

October 28, 2009 11:03 PM  
Blogger Daniela said...

Find your great Travel News and sing the songs at Free Song Lyric or you can watch the drama at Korea Drama Online one of great korea drama is A Love to Kill if you go to travel to Indonesia learn Learn Indonesia Language first! And find your home cari rumah or make a blog Belajar membuat Blog find your home again rumah dijual and again at jual rumah then if you want buy a new laptop see the Laptop Price List or you can buy a New Blackberry and then take care your Health & Jewerly.

November 14, 2009 7:52 PM  
Blogger nike said...

Clothing has always been a thing that has been given a great importance by human beings. It displays the attitude that the people exhibit.
ed hardy clothes
ed hardy shirts
Lots of brands have been existent in the clothing industry and a famous one among them is the Ed hardy clothing brand.
ed hardy jackets
ed hardy hoodies
The brand got its name from the famous American tattoo artist Ed Hardy. He was a very famous tattoo artist and has published many books on tattooing techniques.
ed hardy boots
ed hardy polo shirts
But his tattooing turned into a brand by the efforts of a company called Christian Audiger. This company was a very famous and very powerful company in the field of clothing.
ed hardy shoes
ed hardy jeans
They felt that it would be appropriate to create a brand called Ed hardy and use Hardy's art as the main selling point for the brand. This venture had turned out to be a very successful one and Ed hardy clothing is one of the most famous brands in the clothing industry.
ed hardy outerwear
ed hardy long sleeve shirts
The brand became very famous because it was worn by many famous celebrities like Madonna, Britney spears and also Sylvester Stallone.
ed hardy bags
ed hardy winter boots
The brand has clothes for men, women, and kids. They have also diversified their business by having a lot of accessories to support their clothing business.
ed hardy love kills slowly shirts
ed hardy love kills slowly shoes
ed hardy love kills slowly boots
for men they have a variety of products such as active wear, denim, outwear, swim trucks, sweaters, t-shirts, tops etc.
ed hardy trousers
ed hardy mens
the accessories include things such as belts, caps, scarves, shoes, socks, jeweler, sunglasses, ties and even wallets.
The art works is a unique combination of American and Japanese cultures. The unique selling proposition of the brand is the way in which the company has used the art works of Ed hardy.
ed hardy womens
ed hardy t shirts
ed hardy sunglasses

December 08, 2009 6:55 PM  
Blogger nike said...

ed hardy clothes
ed hardy shirts
ed hardy jackets
ed hardy hoodies
ed hardy boots
ed hardy polo shirts
ed hardy shoes
ed hardy jeans
ed hardy outerwear
ed hardy long sleeve shirts
ed hardy bags
ed hardy winter boots
ed hardy handbags
ed hardy love kills slowly shirts
ed hardy love kills slowly shoes
ed hardy love kills slowly boots
ed hardy trousers
ed hardy mens
ed hardy womens
ed hardy t shirts
ed hardy sunglasses
ghd hair straighteners mk4
hair straightners
ghd iv styler hair straightener
ghd hair straightners
cheap ghd hair straighteners

December 08, 2009 6:56 PM  
Blogger nike said...

Nice work and thanks!
Running
Adidas currently manufactures several running shoesNike shoes, including the adiStar Control 5, the adiStar Ride
Cheap nike shoes
Discount nike shoes
the Supernova Sequence and the Supernova Cushion 7, among others.
Nike shox r4
nike shox shoes
Adidas also uses kangaroo leather to make their more expensive shoes.
Association football
One of the main focuses of Adidas is football kit and associated equipment.
puma cat
cheap sport shoes
Adidas also provides apparel and equipment for all teams in Major League Soccer. Adidas remain a major company in the supply of team kits for international football teams.
cheap nike shox
cheap nike max
Adidas also makes referee kits that are used in international competition and by many countries and leagues in the world. In the United States, referees wear the Adidas kits in MLS matches even though the primary referee supplier is Official Sports.
nike tn dollar
nike running shoes
The company has been an innovator in the area of footwear for the sport with notable examples including development of the Copa Mondial moulded boot on firm dry pitches for forty years.
nike air max tn
puma shoes
Adidas became renowned for advancing the "Predator" boot design.This design featured a ribbed rubber structure for the upper leather of the shoe, used to accent the movement of the ball.
discount puma shoes
puma mens shoes
The Predator also features the Craig Johnston invented "Traxion" sole. As the development and popularity of Football continued Adidas played a leading role in shaping the style of the play itself.
puma running shoes
puma shoes
FIFA, the sports governing body, commissioned specially designed footballs for use in its own World Cup tournaments to favour more attacking play.
ghd hair straighteners mk4
hair straightners
ghd iv styler hair straightener
ghd hair straightners
cheap ghd hair straighteners

December 09, 2009 4:53 PM  
Blogger uwanna said...

gucci replica handbags
men gucci shoes
Gucci men sneakers
Gucci men moccasins
gucci women sneakers
gucci women boots
Gucci men boots
Gucci
Gucci shop
Gucci bags
Gucci shoes
Replica GUCCI SHOES
wholesale gucci shoes
cheap Gucci handbags
Gucci ON sale
Gucci Belts
Gucci small accessories
Gucci hats & scarves
Gucci wallets
Gucci Handbags
Women Gucci shoes
Men Gucci shoes
discount gucci shoes
cheap Gucci shoes

February 04, 2010 9:37 PM  

Post a Comment

Links to this post:

Create a Link

<< Home