Thoughts on adding getElementClass() method to StackTraceElement? (original) (raw)

Peter Levart peter.levart at gmail.com
Sat Jun 15 22:05:13 UTC 2013


On 06/15/2013 11:06 PM, Nick Williams wrote:

B) A Class loaded by a child class loader obtains a StackTraceElement containing Classes loaded by a sibling class loader. This would obviously be the more dangerous scenario (think: one web application accessing another web application's classes), but I don't think it's actually possible. I don't think a stack trace generated in one class loader can possibly make its way into code in a sibling class loader, and I know that an execution stack can't contain classes from two sibling class loaders (only from class loaders with parent/child relationships).

Why not? Consider this hypothetical code:

Parent class loader:

 public class A {
     public static final A INSTANCE = new A();
     final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
     public void addPropertyChangeListener(PropertyChangeListener 

pcl) { pcs.addPropertyChangeListener(pcl); } String property; public void setProperty(String property) { pcs.firePropertyChange("property", this.property, this.property = property); } }

Child class loader 1:

 static class B implements PropertyChangeListener {
     B() {
         A a = A.INSTANCE;
         a.addPropertyChangeListener(this);
     }

     @Override
     public void propertyChange(PropertyChangeEvent evt) {
         StackTraceElement[] st = new Throwable().getStackTrace();
         // can we see C.class in 'st' array?
     }
 }

Child class loader 2:

 static class C {
     void m() {
         A a = A.INSTANCE;
         a.setProperty("XYZ");
     }
 }

Regards, Peter



More information about the core-libs-dev mailing list