Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * @(#)
    * Copyright (c) 2011 Werner Randelshofer, Goldau, Switzerland.
   * All rights reserved.
   * You may not use, copy or modify this file, except in compliance with the
   * license agreement you entered into with Werner Randelshofer.
   * For details see accompanying license terms.
  * This class has been derived from,
  * v 1.2 2005-06-20 18:26:16 by Paul Speed.
  * Copyright (c) 2004, Paul Speed
  * All rights reserved.
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  * 1) Redistributions of source code must retain the above copyright notice,
  *    this list of conditions and the following disclaimer.
  * 2) Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
  * 3) Neither the names "Progeeks", "Meta-JB", nor the names of its contributors
  *    may be used to endorse or promote products derived from this software
  *    without specific prior written permission.
 import java.beans.*;
Property change listener that holds weak reference to a target property change listener. If the weak reference becomes null (meaning the delegate has been GC'ed) then this listener will remove itself from any beans that it receives events from. It isn't perfect, but it's a lot better than nothing... and presumably beans that no longer send out events probably don't care if their listeners weren't properly cleaned up. Design pattern: Proxy.

Paul Speed
$Id: 299 2013-01-03 07:40:18Z werner $
 public class WeakPropertyChangeListener implements PropertyChangeListener {
         this. = new WeakReference<PropertyChangeListener>(target);

Method that can be subclassed to provide additional remove support. Default implementation only supports StandardBeans.
     protected void removeFromSource(PropertyChangeEvent event) {
         // Remove ourselves from the source
         Object src = event.getSource();
         try {
             src.getClass().getMethod("removePropertyChangeListener"new Class[] {PropertyChangeListener.class}).invoke(srcthis);
         } catch (Exception ex) {
             InternalError ie = new InternalError("Could not remove WeakPropertyChangeListener from "+src+".");
             throw ie;
     public void propertyChange(PropertyChangeEvent event) {
         PropertyChangeListener listener = (PropertyChangeListener.get();
         if (listener == null) {

Returns the target of this proxy. Returns null if the target has been garbage collected.

The target or null.
    public PropertyChangeListener getTarget() {
        return .get();
    public String toString() {
        return super.toString()+"["+.get()+"]";
New to GrepCode? Check out our FAQ X