-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
some refactoring in preparation of monitorBean on tree and list
- Loading branch information
Showing
3 changed files
with
71 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.tbee.sway.support; | ||
|
||
import java.beans.PropertyChangeListener; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
|
||
public class BeanUtil { | ||
|
||
public record PropertyChangeConnector(Method addPropertyChangeListenerMethod, Method removePropertyChangeListenerMethod) { | ||
public boolean isComplete() { | ||
return (addPropertyChangeListenerMethod != null && removePropertyChangeListenerMethod != null); | ||
} | ||
|
||
public void register(Object bean, PropertyChangeListener propertyChangeListener) { | ||
try { | ||
addPropertyChangeListenerMethod.invoke(bean, propertyChangeListener); | ||
} | ||
catch (IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} | ||
catch (InvocationTargetException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public void unregister(Object bean, PropertyChangeListener propertyChangeListener) { | ||
try { | ||
removePropertyChangeListenerMethod.invoke(bean, propertyChangeListener); | ||
} | ||
catch (IllegalAccessException e) { | ||
throw new RuntimeException(e); | ||
} | ||
catch (InvocationTargetException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} | ||
|
||
static public <T> PropertyChangeConnector getPropertyChangeConnector(Class<T> bean) { | ||
Method addPropertyChangeListenerMethod = null; | ||
Method removePropertyChangeListenerMethod = null; | ||
try { | ||
addPropertyChangeListenerMethod = bean.getMethod("addPropertyChangeListener", new Class<?>[]{PropertyChangeListener.class}); | ||
} | ||
catch (NoSuchMethodException e) { | ||
// ignore silently | ||
} | ||
try { | ||
removePropertyChangeListenerMethod = bean.getMethod("removePropertyChangeListener", new Class<?>[]{PropertyChangeListener.class}); | ||
} | ||
catch (NoSuchMethodException e) { | ||
// ignore silently | ||
} | ||
return new PropertyChangeConnector(addPropertyChangeListenerMethod, removePropertyChangeListenerMethod); | ||
} | ||
} |