Ignore property methods with incorrect num args
Previously we assumed that if a method started with "get" or "is" that
it had 0 arguments. Likewise we assumed that if a method started with
"set" it had exactly 1 argument. These unchecked assumptions cause
IllegalStateExceptions later on in execution, and impose unnecessary
constraints on method signatures of JSON-B model classes.
Signed-off-by: Andrew Guibert <andy.guibert@gmail.com>
diff --git a/src/main/java/org/eclipse/yasson/internal/ClassParser.java b/src/main/java/org/eclipse/yasson/internal/ClassParser.java
index 82deefb..5ea3ac0 100644
--- a/src/main/java/org/eclipse/yasson/internal/ClassParser.java
+++ b/src/main/java/org/eclipse/yasson/internal/ClassParser.java
@@ -92,7 +92,7 @@
Method[] declaredMethods = AccessController.doPrivileged((PrivilegedAction<Method[]>) ifc::getDeclaredMethods);
for(Method method : declaredMethods) {
final String methodName = method.getName();
- if (!isPropertyMethod(methodName)) {
+ if (!isPropertyMethod(method)) {
continue;
}
String propertyName = toPropertyMethod(methodName);
@@ -101,7 +101,7 @@
//May happen for classes which both extend a class with some method and implement interface with same method.
continue;
}
- JsonbAnnotatedElement<Method> methodElement = isGetter(methodName) ?
+ JsonbAnnotatedElement<Method> methodElement = isGetter(method) ?
property.getGetterElement() : property.getSetterElement();
//Only push iface annotations if not overridden on impl classes
for (Annotation ann : method.getDeclaredAnnotations()) {
@@ -116,14 +116,14 @@
Method[] declaredMethods = AccessController.doPrivileged((PrivilegedAction<Method[]>) clazz::getDeclaredMethods);
for (Method method : declaredMethods) {
String name = method.getName();
- if (!isPropertyMethod(name)) {
+ if (!isPropertyMethod(method)) {
continue;
}
final String propertyName = toPropertyMethod(name);
Property property = classProperties.computeIfAbsent(propertyName, n -> new Property(n, classElement));
- if (isSetter(name)) {
+ if (isSetter(method)) {
property.setSetter(method);
} else {
property.setGetter(method);
@@ -131,20 +131,20 @@
}
}
- private boolean isGetter(String methodName) {
- return methodName.startsWith(GET_PREFIX) || methodName.startsWith(IS_PREFIX);
+ private boolean isGetter(Method m) {
+ return (m.getName().startsWith(GET_PREFIX) || m.getName().startsWith(IS_PREFIX)) && m.getParameterCount() == 0;
}
- private boolean isSetter(String methodName) {
- return methodName.startsWith(SET_PREFIX);
+ private boolean isSetter(Method m) {
+ return m.getName().startsWith(SET_PREFIX) && m.getParameterCount() == 1;
}
private String toPropertyMethod(String name) {
return Introspector.decapitalize(name.substring(name.startsWith(IS_PREFIX) ? 2 : 3, name.length()));
}
- private boolean isPropertyMethod(String name) {
- return name.startsWith(GET_PREFIX) || name.startsWith(SET_PREFIX) || name.startsWith(IS_PREFIX);
+ private boolean isPropertyMethod(Method m) {
+ return isGetter(m) || isSetter(m);
}
private void parseFields(JsonbAnnotatedElement<Class<?>> classElement, Map<String, Property> classProperties) {
diff --git a/src/test/java/org/eclipse/yasson/defaultmapping/basic/UnqualifiedPropertiesTest.java b/src/test/java/org/eclipse/yasson/defaultmapping/basic/UnqualifiedPropertiesTest.java
new file mode 100644
index 0000000..f1d8862
--- /dev/null
+++ b/src/test/java/org/eclipse/yasson/defaultmapping/basic/UnqualifiedPropertiesTest.java
@@ -0,0 +1,55 @@
+/*******************************************************************************
+ * Copyright (c) 2017 Oracle and/or its affiliates. All rights reserved.
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
+ * which accompanies this distribution.
+ * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
+ * and the Eclipse Distribution License is available at
+ * http://www.eclipse.org/org/documents/edl-v10.php.
+ *
+ * Contributors:
+ * Andrew Guibert
+ ******************************************************************************/
+package org.eclipse.yasson.defaultmapping.basic;
+
+import static org.junit.Assert.assertEquals;
+
+import javax.json.bind.JsonbBuilder;
+
+import org.junit.Test;
+
+public class UnqualifiedPropertiesTest {
+
+ public static class Widget {
+
+ public long now;
+
+ public String getFoo() {
+ return "foo";
+ }
+
+ public String getBar(final int baz) {
+ return "bar" + baz;
+ }
+
+ public boolean isPositive(int num) {
+ return num > 0;
+ }
+
+ public Widget setNow() {
+ now = 1511576115722L;
+ return this;
+ }
+ }
+
+ @Test
+ public void testGetWithArgs() {
+ assertEquals("{\"foo\":\"foo\",\"now\":0}", JsonbBuilder.create().toJson(new Widget()));
+ }
+
+ @Test
+ public void testSetWithNoArgs() {
+ assertEquals("{\"foo\":\"foo\",\"now\":1511576115722}", JsonbBuilder.create().toJson(new Widget().setNow()));
+ }
+
+}