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 497eb1674f juneau-marshall improvements
497eb1674f is described below
commit 497eb1674ff302c30adf84e141cabf9d8bc54a69
Author: James Bognar <[email protected]>
AuthorDate: Mon Dec 15 10:39:30 2025 -0500
juneau-marshall improvements
---
.../juneau/commons/utils/AssertionUtils.java | 8 +++
.../juneau/commons/utils/ThrowableUtils.java | 1 -
.../java/org/apache/juneau/AnnotationApplier.java | 40 +++-----------
.../java/org/apache/juneau/AnnotationWorkList.java | 18 +++----
.../java/org/apache/juneau/ConfigException.java | 1 -
.../SerializerConfigAnnotation_Test.java | 61 ++++++++++++++++++++++
6 files changed, 84 insertions(+), 45 deletions(-)
diff --git
a/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/AssertionUtils.java
b/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/AssertionUtils.java
index 30f2cf3612..7f9b31ce2a 100644
---
a/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/AssertionUtils.java
+++
b/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/AssertionUtils.java
@@ -19,6 +19,8 @@ package org.apache.juneau.commons.utils;
import static org.apache.juneau.commons.utils.ThrowableUtils.*;
import static org.apache.juneau.commons.utils.Utils.*;
+import java.util.function.*;
+
/**
* Utility methods for argument validation and assertion.
*
@@ -120,6 +122,12 @@ public class AssertionUtils {
return o;
}
+ public static final <T> T assertNotNull(T o, String msg, Object...args)
throws IllegalStateException {
+ if (o == null)
+ throw illegalState(msg, args);
+ return o;
+ }
+
/**
* Throws an {@link IllegalArgumentException} if the specified string
is <jk>null</jk> or blank.
*
diff --git
a/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/ThrowableUtils.java
b/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/ThrowableUtils.java
index cec04b04de..7621517243 100644
---
a/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/ThrowableUtils.java
+++
b/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/ThrowableUtils.java
@@ -31,7 +31,6 @@ import org.apache.juneau.commons.settings.*;
public class ThrowableUtils {
static AtomicBoolean VERBOSE = new
AtomicBoolean(Settings.get().getBoolean("juneau.enableVerboseExceptions").orElse(false));
-// static AtomicBoolean VERBOSE = new AtomicBoolean(true);
/**
* Interface used with {@link
Utils#safeSupplier(SupplierWithThrowable)}.
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/AnnotationApplier.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/AnnotationApplier.java
index 55442d48f2..338009c10a 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/AnnotationApplier.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/AnnotationApplier.java
@@ -16,6 +16,7 @@
*/
package org.apache.juneau;
+import static org.apache.juneau.commons.utils.AssertionUtils.*;
import static org.apache.juneau.commons.utils.ClassUtils.*;
import static org.apache.juneau.commons.utils.StringUtils.*;
import static org.apache.juneau.commons.utils.Utils.*;
@@ -81,46 +82,26 @@ import org.apache.juneau.svl.*;
* }
* </p>
*
- *
* @param <A> The annotation that this applier reads from.
* @param <B> The builder class to apply the annotation to.
*/
public abstract class AnnotationApplier<A extends Annotation,B> {
- /**
- * Represents a no-op configuration apply.
- */
- public static class NoOp extends AnnotationApplier<Annotation,Object> {
-
- /**
- * Constructor.
- *
- * @param r The string resolver to use for resolving strings.
- */
- public NoOp(VarResolverSession r) {
- super(Annotation.class, Object.class, r);
- }
-
- @Override /* Overridden from ConfigApply */
- public void apply(AnnotationInfo<Annotation> ai, Object b) { /*
no-op */ }
- }
-
private final VarResolverSession vr;
private final Class<A> ca;
-
private final Class<B> cb;
/**
* Constructor.
*
* @param annotationClass The annotation class.
- * @param builderClass The annotation class.
- * @param vr The string resolver to use for resolving strings.
+ * @param builderClass The builder class.
+ * @param varResolverSession The string resolver to use for resolving
strings.
*/
- protected AnnotationApplier(Class<A> annotationClass, Class<B>
builderClass, VarResolverSession vr) {
- this.vr = vr == null ? VarResolver.DEFAULT.createSession() : vr;
- this.ca = annotationClass;
- this.cb = builderClass;
+ protected AnnotationApplier(Class<A> annotationClass, Class<B>
builderClass, VarResolverSession varResolverSession) {
+ ca = assertArgNotNull("annotationClass", annotationClass);
+ cb = assertArgNotNull("builderClass", builderClass);
+ vr = assertArgNotNull("vr", varResolverSession);
}
/**
@@ -151,13 +132,6 @@ public abstract class AnnotationApplier<A extends
Annotation,B> {
return cb.isInstance(builder);
}
- /**
- * Returns the builder class that this applier applies to.
- *
- * @return The builder class that this applier applies to.
- */
- public Class<?> getBuilderClass() { return cb; }
-
private Character toCharacter(String in, String loc) {
if (in.length() != 1)
throw new ConfigException("Invalid syntax for character
on annotation @{0}({1}): {2}", ca.getSimpleName(), loc, in);
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/AnnotationWorkList.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/AnnotationWorkList.java
index 24eadde3b9..579a57916b 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/AnnotationWorkList.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/AnnotationWorkList.java
@@ -16,7 +16,8 @@
*/
package org.apache.juneau;
-import static org.apache.juneau.commons.utils.CollectionUtils.*;
+import static org.apache.juneau.commons.utils.AssertionUtils.*;
+import static org.apache.juneau.commons.utils.Utils.*;
import java.lang.annotation.*;
import java.lang.reflect.*;
@@ -79,6 +80,7 @@ public class AnnotationWorkList extends
ArrayList<AnnotationWork> {
private final VarResolverSession vrs;
private AnnotationWorkList(VarResolverSession vrs) {
+ assertArgNotNull("vrs", vrs);
this.vrs = vrs;
}
@@ -113,17 +115,13 @@ public class AnnotationWorkList extends
ArrayList<AnnotationWork> {
@SuppressWarnings("unchecked")
private void applyAnnotation(AnnotationInfo<?> ai) {
try {
- Annotation a = ai.inner();
- ContextApply cpa =
a.annotationType().getAnnotation(ContextApply.class);
+ var a = ai.inner();
+ var cpa =
assertNotNull(a.annotationType().getAnnotation(ContextApply.class), "Annotation
found without @ContextApply: %s", cn(ai.annotationType()));
Constructor<? extends AnnotationApplier<?,?>>[]
applyConstructors;
- if (cpa == null) {
- applyConstructors =
a(AnnotationApplier.NoOp.class.getConstructor(VarResolverSession.class));
- } else {
- applyConstructors = new
Constructor[cpa.value().length];
- for (var i = 0; i < cpa.value().length; i++)
- applyConstructors[i] = (Constructor<?
extends
AnnotationApplier<?,?>>)cpa.value()[i].getConstructor(VarResolverSession.class);
- }
+ applyConstructors = new Constructor[cpa.value().length];
+ for (var i = 0; i < cpa.value().length; i++)
+ applyConstructors[i] = (Constructor<? extends
AnnotationApplier<?,?>>)cpa.value()[i].getConstructor(VarResolverSession.class);
for (var applyConstructor : applyConstructors) {
AnnotationApplier<Annotation,Object> applier =
(AnnotationApplier<Annotation,Object>)applyConstructor.newInstance(vrs);
diff --git
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ConfigException.java
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ConfigException.java
index 809900a0e7..3d750d7de2 100644
---
a/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ConfigException.java
+++
b/juneau-core/juneau-marshall/src/main/java/org/apache/juneau/ConfigException.java
@@ -23,7 +23,6 @@ import java.text.*;
/**
* An exception that typically occurs when trying to perform an invalid
operation on a configuration property.
*
- *
* @serial exclude
*/
public class ConfigException extends BasicRuntimeException {
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/serializer/SerializerConfigAnnotation_Test.java
b/juneau-utest/src/test/java/org/apache/juneau/serializer/SerializerConfigAnnotation_Test.java
index 8dc652b81b..48ff479db8 100644
---
a/juneau-utest/src/test/java/org/apache/juneau/serializer/SerializerConfigAnnotation_Test.java
+++
b/juneau-utest/src/test/java/org/apache/juneau/serializer/SerializerConfigAnnotation_Test.java
@@ -19,6 +19,7 @@ package org.apache.juneau.serializer;
import static org.apache.juneau.commons.utils.CollectionUtils.*;
import static org.junit.jupiter.api.Assertions.*;
+import java.nio.charset.Charset;
import java.util.function.*;
import org.apache.juneau.*;
@@ -215,4 +216,64 @@ class SerializerConfigAnnotation_Test extends TestBase {
check("RESOURCE", x.getUriRelativity());
check("NONE", x.getUriResolution());
}
+
+
//-----------------------------------------------------------------------------------------------------------------
+ // Error cases - invalid values
+
//-----------------------------------------------------------------------------------------------------------------
+
+ @SerializerConfig(
+ quoteChar="$X{ab}"
+ )
+ static class D {}
+ static ClassInfo d = ClassInfo.of(D.class);
+
+ @Test void d01_invalidQuoteChar_throwsException() {
+ var al = AnnotationWorkList.of(sr, rstream(d.getAnnotations()));
+ assertThrows(ConfigException.class, () -> {
+ JsonSerializer.create().apply(al).build();
+ });
+ }
+
+ @SerializerConfig(
+ fileCharset="$X{UTF-8}",
+ streamCharset="$X{ISO-8859-1}"
+ )
+ static class E {}
+ static ClassInfo e = ClassInfo.of(E.class);
+
+ @Test void d02_charsetWithNonDefaultValue() {
+ var al = AnnotationWorkList.of(sr, rstream(e.getAnnotations()));
+ var x = JsonSerializer.create().apply(al).build().getSession();
+ check("UTF-8", x.getFileCharset().name());
+ check("ISO-8859-1", x.getStreamCharset().name());
+ }
+
+ @SerializerConfig(
+ fileCharset="$X{default}",
+ streamCharset="$X{DEFAULT}"
+ )
+ static class E2 {}
+ static ClassInfo e2 = ClassInfo.of(E2.class);
+
+ @Test void d02b_charsetWithDefaultValue() {
+ var al = AnnotationWorkList.of(sr,
rstream(e2.getAnnotations()));
+ var x = JsonSerializer.create().apply(al).build().getSession();
+ check(Charset.defaultCharset().name(),
x.getFileCharset().name());
+ check(Charset.defaultCharset().name(),
x.getStreamCharset().name());
+ }
+
+ @SerializerConfig(
+ initialDepth="$X{abc}",
+ maxDepth="$X{xyz}",
+ maxIndent="$X{invalid}"
+ )
+ static class F {}
+ static ClassInfo f = ClassInfo.of(F.class);
+
+ @Test void d03_invalidInteger_throwsException() {
+ var al = AnnotationWorkList.of(sr, rstream(f.getAnnotations()));
+ assertThrows(ConfigException.class, () -> {
+ JsonSerializer.create().apply(al).build();
+ });
+ }
}
\ No newline at end of file