Skip to content
This repository has been archived by the owner on May 3, 2020. It is now read-only.

Commit

Permalink
Fix mutable beans where subclass has no properties
Browse files Browse the repository at this point in the history
  • Loading branch information
jodastephen committed Jun 17, 2019
1 parent 72bb261 commit 5f44ff7
Show file tree
Hide file tree
Showing 7 changed files with 667 additions and 53 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ sudo: false
language: java
jdk:
- openjdk11
- oraclejdk9
- oraclejdk8
- openjdk8
cache:
directories:
- "$HOME/.m2/repository"
Expand Down
4 changes: 4 additions & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
Provide ability to copy a bean to a builder of a different type.
See JodaBeanUtils.copy(Bean, Class).
</action>
<action dev="jodastephen" type="add">
Fix mutable beans where the subclass has no properties.
Fixes #210.
</action>
</release>
<release version="2.7.1" date="2019-06-04" description="Version 2.7.1">
<action dev="jodastephen" type="fix">
Expand Down
18 changes: 14 additions & 4 deletions src/main/java/org/joda/beans/gen/BeanGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -1330,8 +1330,13 @@ private void generateBuilderGet() {
}
addLine(3, "}");
} else {
data.ensureImport(NoSuchElementException.class);
addLine(3, "throw new NoSuchElementException(\"Unknown property: \" + propertyName);");
if (!data.isRootClass()) {
addLine(3, "super.get(propertyName);");
addLine(3, "return this;");
} else {
data.ensureImport(NoSuchElementException.class);
addLine(3, "throw new NoSuchElementException(\"Unknown property: \" + propertyName);");
}
}
addLine(2, "}");
addBlankLine();
Expand Down Expand Up @@ -1364,8 +1369,13 @@ private void generateBuilderSet() {
addLine(3, "}");
addLine(3, "return this;");
} else {
data.ensureImport(NoSuchElementException.class);
addLine(3, "throw new NoSuchElementException(\"Unknown property: \" + propertyName);");
if (!data.isRootClass()) {
addLine(3, "super.set(propertyName, newValue);");
addLine(3, "return this;");
} else {
data.ensureImport(NoSuchElementException.class);
addLine(3, "throw new NoSuchElementException(\"Unknown property: \" + propertyName);");
}
}
addLine(2, "}");
addBlankLine();
Expand Down
58 changes: 58 additions & 0 deletions src/test/java/org/joda/beans/TestMutableDerived.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2001-present Stephen Colebourne
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.joda.beans;

import static org.junit.Assert.assertEquals;

import org.joda.beans.sample.MutableDerivedBean;
import org.junit.Test;

/**
* Test mutable derived beans.
*/
public class TestMutableDerived {

@Test
public void test_mutableDerivedBean() {
MutableDerivedBean test = (MutableDerivedBean) MutableDerivedBean.builder()
.baseBeanString("HopeNotHate")
.build();
assertEquals(test.getBaseBeanString(), "HopeNotHate");
assertEquals(test.metaBean().metaPropertyCount(), 1);
assertEquals(test.metaBean().metaPropertyMap().keySet().iterator().next(), "baseBeanString");
assertEquals(test.metaBean().baseBeanString().get(test), "HopeNotHate");

test.metaBean().baseBeanString().set(test, "Now");
assertEquals(test.getBaseBeanString(), "Now");
assertEquals(test.metaBean().baseBeanString().get(test), "Now");

test.metaBean().baseBeanString().setString(test, "Please");
assertEquals(test.getBaseBeanString(), "Please");
assertEquals(test.metaBean().baseBeanString().get(test), "Please");
}

@Test
public void test_mutableDerivedBean_builder() {
MutableDerivedBean test = (MutableDerivedBean) MutableDerivedBean.builder()
.set("baseBeanString", "HopeNotHate")
.build();
assertEquals(test.getBaseBeanString(), "HopeNotHate");
assertEquals(test.metaBean().metaPropertyCount(), 1);
assertEquals(test.metaBean().metaPropertyMap().keySet().iterator().next(), "baseBeanString");
assertEquals(test.metaBean().baseBeanString().get(test), "HopeNotHate");
}

}
Loading

0 comments on commit 5f44ff7

Please sign in to comment.