Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.beans.PropertyVetoException;
import java.beans.VetoableChangeListener;
import java.beans.VetoableChangeSupport;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -80,18 +81,18 @@ public boolean run(Outline model, Options opt, ErrorHandler errorHandler) {
interfaceName = globalInterfaceSetting;
}
if (VetoableChangeListener.class.getName().equals(interfaceName)) {
addSupport(VetoableChangeListener.class, VetoableChangeSupport.class, co);
addSupport(VetoableChangeListener.class, VetoableChangeSupport.class, co, "fireVetoableChange", PropertyVetoException.class);
}
if (PropertyChangeListener.class.getName().equals(interfaceName)) {
addSupport(PropertyChangeListener.class, PropertyChangeSupport.class, co);
addSupport(PropertyChangeListener.class, PropertyChangeSupport.class, co, "firePropertyChange", null);
}

}

return true;
}

private void addSupport(Class listener, Class support, ClassOutline classOutline) {
private void addSupport(Class listener, Class support, ClassOutline classOutline, String methodName, Class exceptionSetter) {

JDefinedClass target = classOutline.implClass;
// add the support field.
Expand Down Expand Up @@ -145,16 +146,22 @@ private void addSupport(Class listener, Class support, ClassOutline classOutline
if (field != null) {
/*
* String oldValue = this.value;
* this.value = newValue;
* // if VetoableChangeSupport
* this.pcs.fireVetoableChange("value", oldValue, newValue);
* // else if PropertyChangeSupport
* this.pcs.firePropertyChange("value", oldValue, newValue);
* // end of setter
* this.value = newValue;
*/
setter.body().pos(0);
if (exceptionSetter != null) {
setter._throws(exceptionSetter);
}

final JVar oldPropertyValue = setter.body().decl(JMod.FINAL,
rawType, "old" + publicName,
field);
setter.body().pos(setter.body().getContents().size());
setter.body().add(fieldSupport.invoke("firePropertyChange").arg(privateName).arg(oldPropertyValue).arg(field));
setter.body().add(fieldSupport.invoke(methodName).arg(privateName).arg(oldPropertyValue).arg(setter.params().get(0)));
}
}
}
Expand Down
1 change: 1 addition & 0 deletions jaxb-plugins-parent/tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
<module>po</module>
<module>po-2.3</module>
<module>propertylistenerinjector</module>
<module>propertylistenerinjector-vetoable</module>
<module>qa-simple</module>
<module>qa-strategic</module>
<module>simple-hashCode-equals-01</module>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb-plugins-tests</artifactId>
<version>4.0.11-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<artifactId>jaxb-plugins-test-propertylistenerinjector2</artifactId>
<packaging>jar</packaging>
<name>JAXB Tools :: JAXB Plugins :: Test [propertylistenerinjector-vetoable]</name>
<dependencies>
<dependency>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb-maven-plugin-testing</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<defaultGoal>test</defaultGoal>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb-maven-plugin</artifactId>
<configuration>
<extension>true</extension>
<args>
<arg>-Xinject-listener-code</arg>
</args>
<plugins>
<plugin>
<groupId>org.jvnet.jaxb</groupId>
<artifactId>jaxb-plugins</artifactId>
</plugin>
</plugins>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xs:element name="Person">
<xs:complexType>
<xs:sequence>
<xs:element name="firstName" type="xs:string"/>
<xs:element name="middleName" type="xs:string"/>
<xs:element name="lastName" type="xs:string"/>

<xs:element name="residentialAddress" type="Address" minOccurs="1" maxOccurs="1"/>
<xs:element name="mailingAddress" type="Address" minOccurs="0" maxOccurs="1"/>
<xs:element name="mailingAddressIdentical" type="xs:boolean" default="true"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:complexType name="Address">
<xs:sequence>
<xs:element name="careOf" type="xs:string" default="none" />
<xs:element name="street" type="xs:string"/>
<xs:element name="number" type="xs:int" default="42"/>
<xs:element name="apt" type="xs:string"/>
<xs:element name="city" type="xs:string"/>
<xs:element name="state" type="xs:string"/>
<xs:element name="ZIPCode" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<jaxb:bindings
version="3.0"
xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
xmlns:li="urn:jaxb.jvnet.org:plugin:propertyListenerInjector"
jaxb:extensionBindingPrefixes="xjc li"
xmlns:a="a">

<jaxb:bindings schemaLocation="Person.xsd">
<jaxb:bindings node="/xsd:schema">
<li:listener>java.beans.VetoableChangeListener</li:listener>
</jaxb:bindings>
</jaxb:bindings>
</jaxb:bindings>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.jvnet.jaxb.tests.propertylistenerinjector;

import generated.Address;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class AddressTest {

@Test
public void testAddress() throws PropertyVetoException {
List<PropertyChangeEvent> events = new ArrayList<>();

Address a = new Address();
a.setStreet("Dollar Lane");
a.setCity("Los Angeles");

a.addVetoableChangeListener("street", events::add);
a.setStreet("Penny Lane");
a.setCity("New York");
Assertions.assertEquals(1, events.size());
PropertyChangeEvent e = events.get(0);
Assertions.assertEquals("Dollar Lane", e.getOldValue());
Assertions.assertEquals("Penny Lane", e.getNewValue());
Assertions.assertEquals("street", e.getPropertyName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
log4j.rootCategory=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.target=system.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n