This is an automated email from the ASF dual-hosted git repository.
jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git
The following commit(s) were added to refs/heads/master by this push:
new 6d3ef16 OpenApiSerializer tests.
6d3ef16 is described below
commit 6d3ef16bb80471df84776a9ac24bce50c2b000c6
Author: JamesBognar <[email protected]>
AuthorDate: Thu Jul 5 19:18:20 2018 -0400
OpenApiSerializer tests.
---
.../juneau/httppart/OpenApiPartSerializerTest.java | 138 ++++++-----
.../main/java/org/apache/juneau/BeanSession.java | 11 +-
.../src/main/java/org/apache/juneau/ClassMeta.java | 99 ++++++--
.../apache/juneau/httppart/OpenApiPartParser.java | 10 +-
.../juneau/httppart/OpenApiPartSerializer.java | 2 +
.../org/apache/juneau/internal/ClassUtils.java | 252 ++++++++++++++++-----
.../org/apache/juneau/internal/TransformCache.java | 19 ++
7 files changed, 386 insertions(+), 145 deletions(-)
diff --git
a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/httppart/OpenApiPartSerializerTest.java
b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/httppart/OpenApiPartSerializerTest.java
index 7289117..cbf791c 100644
---
a/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/httppart/OpenApiPartSerializerTest.java
+++
b/juneau-core/juneau-core-test/src/test/java/org/apache/juneau/httppart/OpenApiPartSerializerTest.java
@@ -13,6 +13,7 @@
package org.apache.juneau.httppart;
import static org.junit.Assert.*;
+import static org.apache.juneau.internal.StringUtils.*;
import java.util.*;
@@ -159,67 +160,64 @@ public class OpenApiPartSerializerTest {
}
}
-//
//-----------------------------------------------------------------------------------------------------------------
-// // type = string
-//
//-----------------------------------------------------------------------------------------------------------------
-//
-// public static class C1 {
-// private String f;
-// public C1(byte[] b) {
-// f = "C1-" + new String(b);
-// }
-// @Override
-// public String toString() {
-// return f;
-// }
-// }
-//
-// public static class C2 {
-// private String f;
-// public C2(String s) {
-// f = "C2-" + s;
-// }
-// @Override
-// public String toString() {
-// return f;
-// }
-// }
-//
-// public static class C3 {
-// private String f;
-// public C3(String[] in) {
-// f = "C3-" + JsonSerializer.DEFAULT_LAX.toString(in);
-// }
-// @Override
-// public String toString() {
-// return f;
-// }
-// }
-//
-//
-// @Test
-// public void c01_stringType_simple() throws Exception {
-// HttpPartSchema ps = schema("string").build();
-// assertEquals("foo", s.serialize(ps, "foo", String.class));
-// }
-//
-// @Test
-// public void c02_stringType_default() throws Exception {
-// HttpPartSchema ps = schema("string")._default("x").build();
-// assertEquals("foo", s.serialize(ps, "foo", String.class));
-// assertEquals("x", s.serialize(ps, null, String.class));
-// }
-//
-// @Test
-// public void c03_stringType_byteFormat() throws Exception {
-// HttpPartSchema ps = schema("string", "byte").build();
-// String in = base64Encode("foo".getBytes());
-// assertEquals("foo", s.serialize(ps, in, String.class));
-// assertEquals("foo", IOUtils.read(s.serialize(ps, in,
InputStream.class)));
-// assertEquals("foo", IOUtils.read(s.serialize(ps, in,
Reader.class)));
-// assertEquals("C1-foo", s.serialize(ps, in,
C1.class).toString());
-// }
-//
+
//-----------------------------------------------------------------------------------------------------------------
+ // type = string
+
//-----------------------------------------------------------------------------------------------------------------
+
+ public static class C1 {
+ private byte[] f;
+ public C1(byte[] f) {
+ this.f = f;
+ }
+ public byte[] toByteArray() {
+ return f;
+ }
+ }
+
+ public static class C2 {
+ private String f;
+ public C2(String s) {
+ f = "C2-" + s;
+ }
+ @Override
+ public String toString() {
+ return f;
+ }
+ }
+
+ public static class C3 {
+ private String[] f;
+ public C3(String...in) {
+ f = in;
+ }
+ public String[] toStringArray() {
+ return f;
+ }
+ }
+
+
+ @Test
+ public void c01_stringType_simple() throws Exception {
+ HttpPartSchema ps = schema("string").build();
+ assertEquals("foo", s.serialize(ps, "foo"));
+ }
+
+ @Test
+ public void c02_stringType_default() throws Exception {
+ HttpPartSchema ps = schema("string")._default("x").build();
+ assertEquals("foo", s.serialize(ps, "foo"));
+ assertEquals("x", s.serialize(ps, null));
+ }
+
+ @Test
+ public void c03_stringType_byteFormat() throws Exception {
+ HttpPartSchema ps = schema("string", "byte").build();
+ byte[] foob = "foo".getBytes();
+ String expected = base64Encode(foob);
+ assertEquals(expected, s.serialize(ps, foob));
+ assertEquals(expected, s.serialize(ps, new
C1(foob)).toString());
+ }
+
// @Test
// public void c04_stringType_binaryFormat() throws Exception {
// HttpPartSchema ps = schema("string", "binary").build();
@@ -311,7 +309,7 @@ public class OpenApiPartSerializerTest {
//
assertObjectEquals("['C3-[\\'foo\\',\\'bar\\']','C3-[\\'baz\\']']",
s.serialize(ps, "foo,bar|baz", C3[].class));
//
assertObjectEquals("['C3-[\\'foo\\',\\'bar\\']','C3-[\\'baz\\']']",
s.serialize(ps, "foo,bar|baz", List.class, C3.class));
// }
-//
+
//
//-----------------------------------------------------------------------------------------------------------------
// // type = array
//
//-----------------------------------------------------------------------------------------------------------------
@@ -1068,12 +1066,12 @@ public class OpenApiPartSerializerTest {
private static HttpPartSchemaBuilder schema() {
return HttpPartSchema.create();
}
-//
-// private static HttpPartSchemaBuilder schema(String type) {
-// return HttpPartSchema.create(type);
-// }
-//
-// private static HttpPartSchemaBuilder schema(String type, String format)
{
-// return HttpPartSchema.create(type, format);
-// }
+
+ private static HttpPartSchemaBuilder schema(String type) {
+ return HttpPartSchema.create(type);
+ }
+
+ private static HttpPartSchemaBuilder schema(String type, String format)
{
+ return HttpPartSchema.create(type, format);
+ }
}
\ No newline at end of file
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanSession.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanSession.java
index 7feb819..fc0e814 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanSession.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/BeanSession.java
@@ -511,6 +511,10 @@ public class BeanSession extends Session {
return (T)toArray(type,
Arrays.asList((Object[])value));
else if (startsWith(value.toString(), '['))
return (T)toArray(type, new
ObjectList(value.toString()).setBeanSession(this));
+ else if (type.hasTransformFrom(vt))
+ return type.transformFrom(value);
+ else if (vt.hasTransformTo(type))
+ return vt.transformTo(value, type);
else
return (T)toArray(type, new
ObjectList((Object[])StringUtils.split(value.toString())).setBeanSession(this));
}
@@ -681,8 +685,11 @@ public class BeanSession extends Session {
return (T)((Calendar)value).getTime();
}
- if (type.hasTransformForObject(value))
- return type.transform(value);
+ if (type.hasTransformFrom(vt))
+ return type.transformFrom(value);
+
+ if (vt.hasTransformTo(type))
+ return vt.transformTo(value, type);
if (type.isBean())
return
newBeanMap(type.getInnerClass()).load(value.toString()).getBean();
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java
index d420d6f..c5cf4a9 100644
--- a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java
+++ b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ClassMeta.java
@@ -119,7 +119,8 @@ public final class ClassMeta<T> implements Type {
private final BeanRegistry beanRegistry; // The bean
registry of this class meta (if it has one).
private final ClassMeta<?>[] args; // Arg types if
this is an array of args.
private final Object example; // Example
object.
- private final Map<Class<?>,Transform<?,T>> transforms = new
ConcurrentHashMap<>();
+ private final Map<Class<?>,Transform<?,T>> fromTransforms = new
ConcurrentHashMap<>();
+ private final Map<Class<?>,Transform<T,?>> toTransforms = new
ConcurrentHashMap<>();
private final Transform<Reader,T> readerTransform;
private final Transform<InputStream,T> inputStreamTransform;
private final Transform<String,T> stringTransform;
@@ -2081,7 +2082,7 @@ public final class ClassMeta<T> implements Type {
* @return <jk>true</jk> if this class has a transform associated with
it that allows it to be created from a Reader.
*/
public boolean hasReaderTransform() {
- return hasTransform(Reader.class);
+ return hasTransformFrom(Reader.class);
}
/**
@@ -2090,7 +2091,7 @@ public final class ClassMeta<T> implements Type {
* @return The transform, or <jk>null</jk> if no such transform exists.
*/
public Transform<Reader,T> getReaderTransform() {
- return getTransform(Reader.class);
+ return getFromTransform(Reader.class);
}
/**
@@ -2099,7 +2100,7 @@ public final class ClassMeta<T> implements Type {
* @return <jk>true</jk> if this class has a transform associated with
it that allows it to be created from an InputStream.
*/
public boolean hasInputStreamTransform() {
- return hasTransform(InputStream.class);
+ return hasTransformFrom(InputStream.class);
}
/**
@@ -2108,7 +2109,7 @@ public final class ClassMeta<T> implements Type {
* @return The transform, or <jk>null</jk> if no such transform exists.
*/
public Transform<InputStream,T> getInputStreamTransform() {
- return getTransform(InputStream.class);
+ return getFromTransform(InputStream.class);
}
/**
@@ -2135,18 +2136,38 @@ public final class ClassMeta<T> implements Type {
* @param c The class type to convert from.
* @return <jk>true</jk> if this class can be instantiated from the
specified type.
*/
- public boolean hasTransform(Class<?> c) {
- return getTransform(c) != null;
+ public boolean hasTransformFrom(Class<?> c) {
+ return getFromTransform(c) != null;
}
/**
- * Returns <jk>true</jk> if this class can be instantiated from the
specified object.
+ * Returns <jk>true</jk> if this class can be instantiated from the
specified type.
+ *
+ * @param c The class type to convert from.
+ * @return <jk>true</jk> if this class can be instantiated from the
specified type.
+ */
+ public boolean hasTransformFrom(ClassMeta<?> c) {
+ return getFromTransform(c.getInnerClass()) != null;
+ }
+
+ /**
+ * Returns <jk>true</jk> if this class can be transformed to the
specified type.
+ *
+ * @param c The class type to convert from.
+ * @return <jk>true</jk> if this class can be transformed to the
specified type.
+ */
+ public boolean hasTransformTo(Class<?> c) {
+ return getToTransform(c) != null;
+ }
+
+ /**
+ * Returns <jk>true</jk> if this class can be transformed to the
specified type.
*
- * @param o The object to convert from.
- * @return <jk>true</jk> if this class can be instantiated from the
specified object.
+ * @param c The class type to convert from.
+ * @return <jk>true</jk> if this class can be transformed to the
specified type.
*/
- public boolean hasTransformForObject(Object o) {
- return getTransform(o.getClass()) != null;
+ public boolean hasTransformTo(ClassMeta<?> c) {
+ return getToTransform(c.getInnerClass()) != null;
}
/**
@@ -2156,27 +2177,71 @@ public final class ClassMeta<T> implements Type {
* @return The transformed object.
*/
@SuppressWarnings({"unchecked","rawtypes"})
- public T transform(Object o) {
- Transform t = getTransform(o.getClass());
+ public T transformFrom(Object o) {
+ Transform t = getFromTransform(o.getClass());
return (T)(t == null ? null : t.transform(o));
}
/**
+ * Transforms the specified object into an instance of this class.
+ *
+ * @param o The object to transform.
+ * @param c The class
+ * @return The transformed object.
+ */
+ @SuppressWarnings({"unchecked","rawtypes"})
+ public <O> O transformTo(Object o, Class<O> c) {
+ Transform t = getToTransform(c);
+ return (O)(t == null ? null : t.transform(o));
+ }
+
+ /**
+ * Transforms the specified object into an instance of this class.
+ *
+ * @param o The object to transform.
+ * @param c The class
+ * @return The transformed object.
+ */
+ public <O> O transformTo(Object o, ClassMeta<O> c) {
+ return transformTo(o, c.getInnerClass());
+ }
+
+ /**
* Returns the transform for this class for creating instances from
other object types.
*
* @param c The transform-from class.
* @return The transform, or <jk>null</jk> if no such transform exists.
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
- public <I> Transform<I,T> getTransform(Class<I> c) {
- Transform t = transforms.get(c);
+ public <I> Transform<I,T> getFromTransform(Class<I> c) {
+ Transform t = fromTransforms.get(c);
if (t == TransformCache.NULL)
return null;
if (t == null) {
t = TransformCache.get(c, innerClass);
if (t == null)
t = TransformCache.NULL;
- transforms.put(c, t);
+ fromTransforms.put(c, t);
+ }
+ return t == TransformCache.NULL ? null : t;
+ }
+
+ /**
+ * Returns the transform for this class for creating instances from
other object types.
+ *
+ * @param c The transform-from class.
+ * @return The transform, or <jk>null</jk> if no such transform exists.
+ */
+ @SuppressWarnings({ "rawtypes", "unchecked" })
+ public <O> Transform<T,O> getToTransform(Class<O> c) {
+ Transform t = toTransforms.get(c);
+ if (t == TransformCache.NULL)
+ return null;
+ if (t == null) {
+ t = TransformCache.get(innerClass, c);
+ if (t == null)
+ t = TransformCache.NULL;
+ toTransforms.put(c, t);
}
return t == TransformCache.NULL ? null : t;
}
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/OpenApiPartParser.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/OpenApiPartParser.java
index d482732..e6507ce 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/OpenApiPartParser.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/OpenApiPartParser.java
@@ -43,7 +43,7 @@ import org.apache.juneau.parser.*;
* <li>{@link Reader} - Returns a {@link
InputStreamReader} wrapped around a {@link ByteArrayInputStream}.
* <li>{@link String} - Constructed using {@link
String#String(byte[])}.
* <li>{@link Object} - Returns the default
<code><jk>byte</jk>[]</code>.
- * <li>Any POJO transformable from a
<code><jk>byte</jk>[]</code> (via constructors or static create methods).
+ * <li>Any POJO transformable from a
<code><jk>byte</jk>[]</code> (via constructors, static create methods, or toX()
instance methods).
* </ul>
* </td>
* </tr>
@@ -60,7 +60,7 @@ import org.apache.juneau.parser.*;
* <li>{@link GregorianCalendar}
* <li>{@link String} - Converted using {@link
Calendar#toString()}.
* <li>{@link Object} - Returns the default {@link
Calendar}.
- * <li>Any POJO transformable from a {@link
Calendar} (via constructors or static create methods).
+ * <li>Any POJO transformable from a {@link
Calendar} (via constructors, static create methods, or toX() instance methods).
* </ul>
* </td>
* </tr>
@@ -80,7 +80,7 @@ import org.apache.juneau.parser.*;
* <ul>
* <li>{@link String} (default)
* <li>{@link Object} - Returns the default {@link
String}.
- * <li>Any POJO transformable from a {@link
String} (via constructors or static create methods).
+ * <li>Any POJO transformable from a {@link
String} (via constructors, static create methods, or toX() instance methods).
* </ul>
* </td>
* </tr>
@@ -95,7 +95,7 @@ import org.apache.juneau.parser.*;
* <li><jk>boolean</code>
* <li>{@link String}
* <li>{@link Object} - Returns the default {@link
Boolean}.
- * <li>Any POJO transformable from a {@link
Boolean} (via constructors or static create methods).
+ * <li>Any POJO transformable from a {@link
Boolean} (via constructors, static create methods, or toX() instance methods).
* </ul>
* </td>
* </tr>
@@ -333,7 +333,7 @@ public class OpenApiPartParser extends UonPartParser {
} else {
o = ss;
}
- if
(type.hasTransform(schema.getParsedType().getInnerClass()))
+ if
(type.hasTransformFrom(schema.getParsedType()) ||
schema.getParsedType().hasTransformTo(type))
return toType(toType(o,
schema.getParsedType()), type);
return toType(o, type);
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/OpenApiPartSerializer.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/OpenApiPartSerializer.java
index 6aac247..74f134e 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/OpenApiPartSerializer.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/httppart/OpenApiPartSerializer.java
@@ -220,6 +220,8 @@ public class OpenApiPartSerializer extends
UonPartSerializer {
}
schema.validateInput(out);
+ if (out == null)
+ out = schema.getDefault();
return out;
}
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/ClassUtils.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/ClassUtils.java
index 70145f2..175c5d1 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/ClassUtils.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/ClassUtils.java
@@ -258,7 +258,7 @@ public final class ClassUtils {
for (ClassFlags f : flags) {
switch (f) {
case DEPRECATED:
- if (! isDeprecated(x))
+ if (isNotDeprecated(x))
return false;
break;
case NOT_DEPRECATED:
@@ -266,7 +266,7 @@ public final class ClassUtils {
return false;
break;
case PUBLIC:
- if (! isPublic(x))
+ if (isNotPublic(x))
return false;
break;
case NOT_PUBLIC:
@@ -274,7 +274,7 @@ public final class ClassUtils {
return false;
break;
case STATIC:
- if (! isStatic(x))
+ if (isNotStatic(x))
return false;
break;
case NOT_STATIC:
@@ -282,7 +282,7 @@ public final class ClassUtils {
return false;
break;
case ABSTRACT:
- if (! isAbstract(x))
+ if (isNotAbstract(x))
return false;
break;
case NOT_ABSTRACT:
@@ -312,7 +312,7 @@ public final class ClassUtils {
for (ClassFlags f : flags) {
switch (f) {
case DEPRECATED:
- if (! isDeprecated(x))
+ if (isNotDeprecated(x))
return false;
break;
case NOT_DEPRECATED:
@@ -320,15 +320,15 @@ public final class ClassUtils {
return false;
break;
case HAS_ARGS:
- if (x.getParameterTypes().length == 0)
+ if (hasNoArgs(x))
return false;
break;
case HAS_NO_ARGS:
- if (x.getParameterTypes().length != 0)
+ if (hasArgs(x))
return false;
break;
case PUBLIC:
- if (! isPublic(x))
+ if (isNotPublic(x))
return false;
break;
case NOT_PUBLIC:
@@ -336,7 +336,7 @@ public final class ClassUtils {
return false;
break;
case STATIC:
- if (! isStatic(x))
+ if (isNotStatic(x))
return false;
break;
case NOT_STATIC:
@@ -344,7 +344,7 @@ public final class ClassUtils {
return false;
break;
case ABSTRACT:
- if (! isAbstract(x))
+ if (isNotAbstract(x))
return false;
break;
case NOT_ABSTRACT:
@@ -372,7 +372,7 @@ public final class ClassUtils {
for (ClassFlags f : flags) {
switch (f) {
case DEPRECATED:
- if (! isDeprecated(x))
+ if (isNotDeprecated(x))
return false;
break;
case NOT_DEPRECATED:
@@ -380,15 +380,15 @@ public final class ClassUtils {
return false;
break;
case HAS_ARGS:
- if (x.getParameterTypes().length == 0)
+ if (hasNoArgs(x))
return false;
break;
case HAS_NO_ARGS:
- if (x.getParameterTypes().length != 0)
+ if (hasArgs(x))
return false;
break;
case PUBLIC:
- if (! isPublic(x))
+ if (isNotPublic(x))
return false;
break;
case NOT_PUBLIC:
@@ -420,7 +420,7 @@ public final class ClassUtils {
for (ClassFlags f : flags) {
switch (f) {
case DEPRECATED:
- if (! isDeprecated(x))
+ if (isNotDeprecated(x))
return false;
break;
case NOT_DEPRECATED:
@@ -432,7 +432,7 @@ public final class ClassUtils {
case HAS_NO_ARGS:
break;
case PUBLIC:
- if (! isPublic(x))
+ if (isNotPublic(x))
return false;
break;
case NOT_PUBLIC:
@@ -440,7 +440,7 @@ public final class ClassUtils {
return false;
break;
case STATIC:
- if (! isStatic(x))
+ if (isNotStatic(x))
return false;
break;
case NOT_STATIC:
@@ -448,7 +448,7 @@ public final class ClassUtils {
return false;
break;
case TRANSIENT:
- if (! isTransient(x))
+ if (isNotTransient(x))
return false;
break;
case NOT_TRANSIENT:
@@ -480,7 +480,7 @@ public final class ClassUtils {
return true;
break;
case NOT_DEPRECATED:
- if (! isDeprecated(x))
+ if (isNotDeprecated(x))
return true;
break;
case PUBLIC:
@@ -488,7 +488,7 @@ public final class ClassUtils {
return true;
break;
case NOT_PUBLIC:
- if (! isPublic(x))
+ if (isNotPublic(x))
return true;
break;
case STATIC:
@@ -496,7 +496,7 @@ public final class ClassUtils {
return true;
break;
case NOT_STATIC:
- if (! isStatic(x))
+ if (isNotStatic(x))
return true;
break;
case ABSTRACT:
@@ -504,7 +504,7 @@ public final class ClassUtils {
return true;
break;
case NOT_ABSTRACT:
- if (! isAbstract(x))
+ if (isNotAbstract(x))
return true;
break;
case TRANSIENT:
@@ -534,15 +534,15 @@ public final class ClassUtils {
return true;
break;
case NOT_DEPRECATED:
- if (! isDeprecated(x))
+ if (isNotDeprecated(x))
return true;
break;
case HAS_ARGS:
- if (x.getParameterTypes().length != 0)
+ if (hasArgs(x))
return true;
break;
case HAS_NO_ARGS:
- if (x.getParameterTypes().length == 0)
+ if (hasNoArgs(x))
return true;
break;
case PUBLIC:
@@ -550,7 +550,7 @@ public final class ClassUtils {
return true;
break;
case NOT_PUBLIC:
- if (! isPublic(x))
+ if (isNotPublic(x))
return true;
break;
case STATIC:
@@ -558,7 +558,7 @@ public final class ClassUtils {
return true;
break;
case NOT_STATIC:
- if (! isStatic(x))
+ if (isNotStatic(x))
return true;
break;
case ABSTRACT:
@@ -566,7 +566,7 @@ public final class ClassUtils {
return true;
break;
case NOT_ABSTRACT:
- if (! isAbstract(x))
+ if (isNotAbstract(x))
return true;
break;
case TRANSIENT:
@@ -594,15 +594,15 @@ public final class ClassUtils {
return true;
break;
case NOT_DEPRECATED:
- if (! isDeprecated(x))
+ if (isNotDeprecated(x))
return true;
break;
case HAS_ARGS:
- if (x.getParameterTypes().length != 0)
+ if (hasArgs(x))
return true;
break;
case HAS_NO_ARGS:
- if (x.getParameterTypes().length == 0)
+ if (hasNoArgs(x))
return true;
break;
case PUBLIC:
@@ -610,7 +610,7 @@ public final class ClassUtils {
return true;
break;
case NOT_PUBLIC:
- if (! isPublic(x))
+ if (isNotPublic(x))
return true;
break;
case STATIC:
@@ -642,7 +642,7 @@ public final class ClassUtils {
return true;
break;
case NOT_DEPRECATED:
- if (! isDeprecated(x))
+ if (isNotDeprecated(x))
return true;
break;
case PUBLIC:
@@ -650,7 +650,7 @@ public final class ClassUtils {
return true;
break;
case NOT_PUBLIC:
- if (! isPublic(x))
+ if (isNotPublic(x))
return true;
break;
case STATIC:
@@ -658,7 +658,7 @@ public final class ClassUtils {
return true;
break;
case NOT_STATIC:
- if (! isStatic(x))
+ if (isNotStatic(x))
return true;
break;
case TRANSIENT:
@@ -666,7 +666,7 @@ public final class ClassUtils {
return true;
break;
case NOT_TRANSIENT:
- if (! isTransient(x))
+ if (isNotTransient(x))
return true;
break;
case HAS_ARGS:
@@ -718,7 +718,27 @@ public final class ClassUtils {
}
/**
- * Returns <jk>true</jk> if the specified method has the specified
number of arguments.
+ * Returns <jk>true</jk> if the specified constructor has one or more
arguments.
+ *
+ * @param x The method to test.
+ * @return <jk>true</jk> if the specified constructor has one or more
arguments.
+ */
+ public static boolean hasArgs(Constructor<?> x) {
+ return x.getParameterTypes().length > 0;
+ }
+
+ /**
+ * Returns <jk>true</jk> if the specified constructor has zero
arguments.
+ *
+ * @param x The method to test.
+ * @return <jk>true</jk> if the specified constructor has zero
arguments.
+ */
+ public static boolean hasNoArgs(Constructor<?> x) {
+ return x.getParameterTypes().length == 0;
+ }
+
+ /**
+ * Returns <jk>true</jk> if the specified constructor has the specified
number of arguments.
*
* @param x The method to test.
* @param number The number of expected arguments.
@@ -762,20 +782,20 @@ public final class ClassUtils {
}
/**
- * Returns <jk>true</jk> if the specified class doesn't have the {@link
Deprecated @Deprecated} annotation on it.
+ * Returns <jk>true</jk> if the specified class has the {@link
Deprecated @Deprecated} annotation on it.
*
* @param c The class.
- * @return <jk>true</jk> if the specified class doesn't have the {@link
Deprecated @Deprecated} annotation on it.
+ * @return <jk>true</jk> if the specified class has the {@link
Deprecated @Deprecated} annotation on it.
*/
public static boolean isDeprecated(Class<?> c) {
return c.isAnnotationPresent(Deprecated.class);
}
/**
- * Returns <jk>true</jk> if the specified method doesn't have the
{@link Deprecated @Deprecated} annotation on it.
+ * Returns <jk>true</jk> if the specified method has the {@link
Deprecated @Deprecated} annotation on it.
*
* @param m The method.
- * @return <jk>true</jk> if the specified method doesn't have the
{@link Deprecated @Deprecated} annotation on it.
+ * @return <jk>true</jk> if the specified method has the {@link
Deprecated @Deprecated} annotation on it.
*/
public static boolean isDeprecated(Method m) {
return m.isAnnotationPresent(Deprecated.class);
@@ -783,40 +803,40 @@ public final class ClassUtils {
}
/**
- * Returns <jk>true</jk> if the specified constructor doesn't have the
{@link Deprecated @Deprecated} annotation on it.
+ * Returns <jk>true</jk> if the specified constructor has the {@link
Deprecated @Deprecated} annotation on it.
*
* @param c The constructor.
- * @return <jk>true</jk> if the specified constructor doesn't have the
{@link Deprecated @Deprecated} annotation on it.
+ * @return <jk>true</jk> if the specified constructor has the {@link
Deprecated @Deprecated} annotation on it.
*/
public static boolean isDeprecated(Constructor<?> c) {
return c.isAnnotationPresent(Deprecated.class);
}
/**
- * Returns <jk>true</jk> if the specified field doesn't have the {@link
Deprecated @Deprecated} annotation on it.
+ * Returns <jk>true</jk> if the specified field has the {@link
Deprecated @Deprecated} annotation on it.
*
* @param f The field.
- * @return <jk>true</jk> if the specified field doesn't have the {@link
Deprecated @Deprecated} annotation on it.
+ * @return <jk>true</jk> if the specified field has the {@link
Deprecated @Deprecated} annotation on it.
*/
public static boolean isDeprecated(Field f) {
return f.isAnnotationPresent(Deprecated.class);
}
/**
- * Returns <jk>true</jk> if the specified class has the {@link
Deprecated @Deprecated} annotation on it.
+ * Returns <jk>true</jk> if the specified class doesn't have the {@link
Deprecated @Deprecated} annotation on it.
*
* @param c The class.
- * @return <jk>true</jk> if the specified class has the {@link
Deprecated @Deprecated} annotation on it.
+ * @return <jk>true</jk> if the specified class doesn't have the {@link
Deprecated @Deprecated} annotation on it.
*/
public static boolean isNotDeprecated(Class<?> c) {
return ! c.isAnnotationPresent(Deprecated.class);
}
/**
- * Returns <jk>true</jk> if the specified method has the {@link
Deprecated @Deprecated} annotation on it.
+ * Returns <jk>true</jk> if the specified method doesn't have the
{@link Deprecated @Deprecated} annotation on it.
*
* @param m The method.
- * @return <jk>true</jk> if the specified method has the {@link
Deprecated @Deprecated} annotation on it.
+ * @return <jk>true</jk> if the specified method doesn't have the
{@link Deprecated @Deprecated} annotation on it.
*/
public static boolean isNotDeprecated(Method m) {
return ! m.isAnnotationPresent(Deprecated.class);
@@ -824,16 +844,26 @@ public final class ClassUtils {
}
/**
- * Returns <jk>true</jk> if the specified constructor has the {@link
Deprecated @Deprecated} annotation on it.
+ * Returns <jk>true</jk> if the specified constructor doesn't have the
{@link Deprecated @Deprecated} annotation on it.
*
* @param c The constructor.
- * @return <jk>true</jk> if the specified constructor has the {@link
Deprecated @Deprecated} annotation on it.
+ * @return <jk>true</jk> if the specified constructor doesn't have the
{@link Deprecated @Deprecated} annotation on it.
*/
public static boolean isNotDeprecated(Constructor<?> c) {
return ! c.isAnnotationPresent(Deprecated.class);
}
/**
+ * Returns <jk>true</jk> if the specified field doesn't have the {@link
Deprecated @Deprecated} annotation on it.
+ *
+ * @param f The field.
+ * @return <jk>true</jk> if the specified field doesn't have the {@link
Deprecated @Deprecated} annotation on it.
+ */
+ public static boolean isNotDeprecated(Field f) {
+ return ! f.isAnnotationPresent(Deprecated.class);
+ }
+
+ /**
* Returns <jk>true</jk> if the specified class is public.
*
* @param c The class.
@@ -844,6 +874,16 @@ public final class ClassUtils {
}
/**
+ * Returns <jk>true</jk> if the specified class is not public.
+ *
+ * @param c The class.
+ * @return <jk>true</jk> if the specified class is not public.
+ */
+ public static boolean isNotPublic(Class<?> c) {
+ return ! Modifier.isPublic(c.getModifiers());
+ }
+
+ /**
* Returns <jk>true</jk> if the specified class is public.
*
* @param c The class.
@@ -854,6 +894,16 @@ public final class ClassUtils {
}
/**
+ * Returns <jk>true</jk> if the specified class is not static.
+ *
+ * @param c The class.
+ * @return <jk>true</jk> if the specified class is not static.
+ */
+ public static boolean isNotStatic(Class<?> c) {
+ return ! Modifier.isStatic(c.getModifiers());
+ }
+
+ /**
* Returns <jk>true</jk> if the specified class is abstract.
*
* @param c The class.
@@ -864,6 +914,16 @@ public final class ClassUtils {
}
/**
+ * Returns <jk>true</jk> if the specified class is not abstract.
+ *
+ * @param c The class.
+ * @return <jk>true</jk> if the specified class is not abstract.
+ */
+ public static boolean isNotAbstract(Class<?> c) {
+ return ! Modifier.isAbstract(c.getModifiers());
+ }
+
+ /**
* Returns <jk>true</jk> if the specified method is abstract.
*
* @param m The method.
@@ -874,6 +934,16 @@ public final class ClassUtils {
}
/**
+ * Returns <jk>true</jk> if the specified method is not abstract.
+ *
+ * @param m The method.
+ * @return <jk>true</jk> if the specified method is not abstract.
+ */
+ public static boolean isNotAbstract(Method m) {
+ return ! Modifier.isAbstract(m.getModifiers());
+ }
+
+ /**
* Returns <jk>true</jk> if the specified method is public.
*
* @param m The method.
@@ -884,6 +954,16 @@ public final class ClassUtils {
}
/**
+ * Returns <jk>true</jk> if the specified method is not public.
+ *
+ * @param m The method.
+ * @return <jk>true</jk> if the specified method is not public.
+ */
+ public static boolean isNotPublic(Method m) {
+ return ! Modifier.isPublic(m.getModifiers());
+ }
+
+ /**
* Returns <jk>true</jk> if the specified field is public.
*
* @param f The field.
@@ -894,6 +974,16 @@ public final class ClassUtils {
}
/**
+ * Returns <jk>true</jk> if the specified field is not public.
+ *
+ * @param f The field.
+ * @return <jk>true</jk> if the specified field is not public.
+ */
+ public static boolean isNotPublic(Field f) {
+ return ! Modifier.isPublic(f.getModifiers());
+ }
+
+ /**
* Returns <jk>true</jk> if the specified method is static.
*
* @param m The method.
@@ -904,6 +994,16 @@ public final class ClassUtils {
}
/**
+ * Returns <jk>true</jk> if the specified method is not static.
+ *
+ * @param m The method.
+ * @return <jk>true</jk> if the specified method is not static.
+ */
+ public static boolean isNotStatic(Method m) {
+ return ! Modifier.isStatic(m.getModifiers());
+ }
+
+ /**
* Returns <jk>true</jk> if the specified field is static.
*
* @param f The field.
@@ -914,6 +1014,16 @@ public final class ClassUtils {
}
/**
+ * Returns <jk>true</jk> if the specified field is not static.
+ *
+ * @param f The field.
+ * @return <jk>true</jk> if the specified field is not static.
+ */
+ public static boolean isNotStatic(Field f) {
+ return ! Modifier.isStatic(f.getModifiers());
+ }
+
+ /**
* Returns <jk>true</jk> if the specified constructor is public.
*
* @param c The constructor.
@@ -924,6 +1034,16 @@ public final class ClassUtils {
}
/**
+ * Returns <jk>true</jk> if the specified constructor is not public.
+ *
+ * @param c The constructor.
+ * @return <jk>true</jk> if the specified constructor is not public.
+ */
+ public static boolean isNotPublic(Constructor<?> c) {
+ return ! Modifier.isPublic(c.getModifiers());
+ }
+
+ /**
* Returns <jk>true</jk> if the specified field is transient.
*
* @param f The field.
@@ -934,6 +1054,36 @@ public final class ClassUtils {
}
/**
+ * Returns <jk>true</jk> if the specified field is not transient.
+ *
+ * @param f The field.
+ * @return <jk>true</jk> if the specified field is not transient.
+ */
+ public static boolean isNotTransient(Field f) {
+ return ! Modifier.isTransient(f.getModifiers());
+ }
+
+ /**
+ * Returns <jk>true</jk> if the specified method has one or more
arguments.
+ *
+ * @param x The method to test.
+ * @return <jk>true</jk> if the specified method has one or more
arguments.
+ */
+ public static boolean hasArgs(Method x) {
+ return x.getParameterTypes().length > 0;
+ }
+
+ /**
+ * Returns <jk>true</jk> if the specified method has zero arguments.
+ *
+ * @param x The method to test.
+ * @return <jk>true</jk> if the specified method has zero arguments.
+ */
+ public static boolean hasNoArgs(Method x) {
+ return x.getParameterTypes().length == 0;
+ }
+
+ /**
* Returns <jk>true</jk> if the specified method has the specified name.
*
* @param m The method to test.
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/TransformCache.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/TransformCache.java
index c0736e8..72902e1 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/TransformCache.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/internal/TransformCache.java
@@ -15,6 +15,7 @@ package org.apache.juneau.internal;
import java.util.concurrent.*;
import static org.apache.juneau.internal.ClassUtils.*;
+import static org.apache.juneau.internal.ClassFlags.*;
import java.lang.reflect.*;
import java.util.*;
@@ -182,6 +183,24 @@ public class TransformCache {
}
}
+ if (t == null) {
+ for (Method m2 : getAllMethods(ic, false)) {
+ if (isAll(m2, PUBLIC, NOT_STATIC, HAS_NO_ARGS,
NOT_DEPRECATED) && m2.getName().startsWith("to") && m2.getReturnType() == oc) {
+ final Method m3 = m2;
+ t = new Transform<I,O>() {
+ @Override
+ public O transform(Object
outer, I in) {
+ try {
+ return
(O)m3.invoke(in);
+ } catch (Exception e) {
+ throw new
RuntimeException(e);
+ }
+ }
+ };
+ break;
+ }
+ }
+ }
if (t == null)
t = NULL;