Skip to content

Commit fa5bdba

Browse files
[highsource#616] fix PropertyListenerInjectorPlugin setters not updated
1 parent 14e39e8 commit fa5bdba

File tree

2 files changed

+61
-10
lines changed

2 files changed

+61
-10
lines changed

basics/basic/src/main/java/org/jvnet/jaxb2_commons/plugin/propertylistenerinjector/PropertyListenerInjectorPlugin.java

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
import com.sun.codemodel.JInvocation;
77
import com.sun.codemodel.JMethod;
88
import com.sun.codemodel.JMod;
9+
import com.sun.codemodel.JType;
10+
import com.sun.codemodel.JVar;
911
import com.sun.tools.xjc.Options;
1012
import com.sun.tools.xjc.Plugin;
1113
import com.sun.tools.xjc.model.CPluginCustomization;
1214
import com.sun.tools.xjc.outline.ClassOutline;
15+
import com.sun.tools.xjc.outline.FieldOutline;
1316
import com.sun.tools.xjc.outline.Outline;
1417
import com.sun.tools.xjc.util.DOMUtils;
1518
import javax.xml.bind.annotation.XmlTransient;
@@ -73,29 +76,30 @@ public boolean run(Outline model, Options opt, ErrorHandler errorHandler) {
7376
interfaceName = globalInterfaceSetting;
7477
}
7578
if (VetoableChangeListener.class.getName().equals(interfaceName)) {
76-
addSupport(VetoableChangeListener.class, VetoableChangeSupport.class, co.implClass);
79+
addSupport(VetoableChangeListener.class, VetoableChangeSupport.class, co);
7780
}
7881
if (PropertyChangeListener.class.getName().equals(interfaceName)) {
79-
addSupport(PropertyChangeListener.class, PropertyChangeSupport.class, co.implClass);
82+
addSupport(PropertyChangeListener.class, PropertyChangeSupport.class, co);
8083
}
8184

8285
}
8386

8487
return true;
8588
}
8689

87-
private void addSupport(Class listener, Class support, JDefinedClass target) {
90+
private void addSupport(Class listener, Class support, ClassOutline classOutline) {
8891

92+
JDefinedClass target = classOutline.implClass;
8993
// add the support field.
9094
// JFieldVar field = target.field(JMod.PRIVATE|JMod.TRANSIENT, support, "support");
91-
JFieldVar field = target.field(JMod.PRIVATE, support, "support");
95+
JFieldVar fieldSupport = target.field(JMod.PRIVATE, support, "support");
9296

93-
field.annotate(XmlTransient.class);
97+
fieldSupport.annotate(XmlTransient.class);
9498

9599
// and initialize it....
96-
field.init(JExpr.direct("new " + support.getSimpleName() + "(this)"));
100+
fieldSupport.init(JExpr.direct("new " + support.getSimpleName() + "(this)"));
97101

98-
// we need to hadd
102+
// we need to add
99103
for (Method method : support.getMethods()) {
100104
if (Modifier.isPublic(method.getModifiers())) {
101105
if (method.getName().startsWith("add")) {
@@ -118,6 +122,38 @@ private void addSupport(Class listener, Class support, JDefinedClass target) {
118122
}
119123
}
120124
}
125+
126+
final FieldOutline[] declaredFields = classOutline.getDeclaredFields();
127+
for (final FieldOutline fieldOutline : declaredFields) {
128+
129+
final String publicName = fieldOutline.getPropertyInfo().getName(true);
130+
131+
final String setterName = "set" + publicName;
132+
133+
final JType rawType = fieldOutline.getRawType();
134+
final JMethod boxifiedSetter = target.getMethod(setterName, new JType[] { rawType.boxify() });
135+
final JMethod unboxifiedSetter = target.getMethod(setterName, new JType[] { rawType.unboxify() });
136+
final JMethod setter = boxifiedSetter != null ? boxifiedSetter : unboxifiedSetter;
137+
138+
if (setter != null) {
139+
final String privateName = fieldOutline.getPropertyInfo().getName(false);
140+
final JFieldVar field = target.fields().get(privateName);
141+
if (field != null) {
142+
/*
143+
* String oldValue = this.value;
144+
* this.value = newValue;
145+
* this.pcs.firePropertyChange("value", oldValue, newValue);
146+
*/
147+
setter.body().pos(0);
148+
149+
final JVar oldPropertyValue = setter.body().decl(JMod.FINAL,
150+
rawType, "old" + publicName,
151+
field);
152+
setter.body().pos(setter.body().getContents().size());
153+
setter.body().add(fieldSupport.invoke("firePropertyChange").arg(privateName).arg(oldPropertyValue).arg(field));
154+
}
155+
}
156+
}
121157
}
122158

123159
private void addMethod(Method method, JDefinedClass target) {
Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
11
package org.jvnet.jaxb.tests.propertylistenerinjector;
22

3+
import org.junit.Assert;
34
import org.junit.Test;
45

56
import generated.Address;
67

8+
import java.beans.PropertyChangeEvent;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
712
public class AddressTest {
813

914
@Test
1015
public void testAddress() {
16+
List<PropertyChangeEvent> events = new ArrayList<>();
17+
1118
Address a = new Address();
12-
// just checking new methods have been added
13-
a.addPropertyChangeListener(null);
14-
a.removePropertyChangeListener(null);
19+
a.setStreet("Dollar Lane");
20+
a.setCity("Los Angeles");
21+
22+
a.addPropertyChangeListener("street", events::add);
23+
a.setStreet("Penny Lane");
24+
a.setCity("New York");
25+
Assert.assertEquals(1, events.size());
26+
PropertyChangeEvent e = events.get(0);
27+
Assert.assertEquals("Dollar Lane", e.getOldValue());
28+
Assert.assertEquals("Penny Lane", e.getNewValue());
29+
Assert.assertEquals("street", e.getPropertyName());
1530
}
1631
}

0 commit comments

Comments
 (0)