Home Javatutorials sun.reflect.Reflection Package - Java Tutorials
Questions:Ask|Latest



sun.reflect.Reflection Package - Java Tutorials
Posted on: April 18, 2011 By Deepak Kumar
This page discusses - sun.reflect.Reflection Package - Java Tutorials

sun.reflect.Reflection - Package

2004-04-09 The Java Specialists' Newsletter [Issue 087] - sun.reflect.Reflection

Author: Dr. Heinz M. Kabutz

If you are reading this, and have not subscribed, please consider doing it now by going to our subscribe page. You can subscribe either via email or RSS.

Welcome to the 87th edition of The Java(tm) Specialists' Newsletter. We have a new translation into Greek, thanks to Panos Konstantinidis. The English have an expression: "It's Greek to me.", which means that they could not understand what was being said. The Greeks have a similar saying: "It's Chinese to me." When I was in Greece, my wife's cousin explained it like this: "Chinese is the most difficult language, because of these two sayings." When I was in China last year, I asked whether they had a similar expression. Imagine if the Chinese said: "That's English to me!" then we would have a loop in the logic.

This year we decided to not have our traditional "April Fools" Java newsletter. However, I thought that Sun Microsystems and Microsoft had gotten the date wrong, when they anounced on the 2nd of April that MS had opened up their petty cash box and found some spare $2,000,000,000 that they thought should go to a good cause. Time will tell what the implications are for us Java developers...

Discussion Forum

I would like to thank Matthew Schmidt and Rick Ross from Java Lobby for the nice things they wrote about our newsletter.

Java Lobby have now made a discussion forum available on their website, where you can post comments and questions about each of my newsletters. You are of course also more than welcome to email your comments to me by simply replying to this newsletter, or sending an email to heinz@javaspecialists.co.za.

sun.reflect.Reflection

A few weeks ago, I was chatting to my friend Niko Brummer, who is an expert in speaker verification. Niko loves watersports and frequently applies his theoretical knowledge to his sports, much to my amusement. Niko is always available for a chat about his latest discoveries in Java (and in the ocean).

When we were chatting, Niko mentioned a class called "Reflection" that I had not heard of before. This class comes with Sun's JVM and sits in the sun.reflect.* package. The most useful method in this class is: Class getCallerClass(int i). This method tells you which classes are in our call stack.

Let's look at an example of some classes for making soup. (In case you want to make a nice vegetable soup for a few hundred people, please have a look at the recipe on our website ;-) We want the Potato to have a reference to the Soup in which it swims, and we want the Soup to know which Potatoes it contains. We have a one-to-many relationship and we want this to be maintained correctly. Please remember that this solution is just one of many, and is simply an example of how you could use this class.

import java.util.*;
public class Soup {
  private final List potatos = new ArrayList();

  public void add(Potato p) {
    potatos.add(p);
    p.setSoup(this);
  }

  public String toString() {
    return "Soup {potatos=" + potatos + "}";
  }
}


import sun.reflect.Reflection;

public class Potato {
  private final int id;
  private Soup soup;

  public Potato(int id) {
    this.id = id;
  }

  public void setSoup(Soup soup) {
    this.soup = soup;
    if (Reflection.getCallerClass(2) != Soup.class) {
      soup.add(this);
    }
  }

  public Soup getSoup() {
    return soup;
  }

  public String toString() {
    return "Potato " + id;
  }
}
  

The interesting method is Potato.setSoup(). It checks whether the calling class is Soup. The method getCallerClass() takes the stack depth as a parameter, in this case it would be 2.

We can try this out by making a soup, adding some potatoes, and then associating the soup with a potato that did not have an association.

public class SoupTest {
  public static void main(String[] args) {
    Soup soup = new Soup();
    soup.add(new Potato(1));
    soup.add(new Potato(2));
    soup.add(new Potato(3));
    Potato p4 = new Potato(4);
    soup.add(p4);
    p4.setSoup(soup); // redundant code
    Potato p5 = new Potato(5);
    p5.setSoup(soup);
    System.out.println("soup = " + soup);
  }
}
  

When I run this with java -showversion SoupTest:

java version "1.4.2_04"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_04-b05)
Java HotSpot(TM) Client VM (build 1.4.2_04-b05, mixed mode)

soup = Soup {potatos=[Potato 1, Potato 2, Potato 3, Potato 4, Potato 4, Potato 5]}
  

A handy little class supplied in the Sun JDK.

This

Kind regards

Heinz

This material from The Java(tm) Specialists' Newsletter by Maximum Solutions (South Africa). Please contact Maximum Solutions for more information.


Recommend the tutorial

Ask Questions?    Discuss: sun.reflect.Reflection Package - Java Tutorials  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments