Skip to content

Commit ca5105a

Browse files
[#1803] better message in IllegalAnnotationsException (detail cause) + test case
1 parent 78a4691 commit ca5105a

File tree

3 files changed

+72
-9
lines changed

3 files changed

+72
-9
lines changed

jaxb-ri/runtime/impl/src/main/java/org/glassfish/jaxb/runtime/v2/runtime/IllegalAnnotationsException.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved.
3+
* Copyright (c) 2025 Contributors to the Eclipse Foundation. All rights reserved.
34
*
45
* This program and the accompanying materials are made available under the
56
* terms of the Eclipse Distribution License v. 1.0, which is available at
@@ -35,19 +36,19 @@ public class IllegalAnnotationsException extends JAXBException {
3536
private static final long serialVersionUID = 1L;
3637

3738
public IllegalAnnotationsException(List<IllegalAnnotationException> errors) {
38-
super(errors.size()+" counts of IllegalAnnotationExceptions");
39-
assert !errors.isEmpty() : "there must be at least one error";
39+
super(buildMessage(errors));
4040
this.errors = Collections.unmodifiableList(new ArrayList<>(errors));
4141
}
4242

43-
@Override
44-
public String toString() {
45-
StringBuilder sb = new StringBuilder(super.toString());
46-
sb.append('\n');
47-
48-
for( IllegalAnnotationException error : errors )
49-
sb.append(error.toString()).append('\n');
43+
private static String buildMessage(List<IllegalAnnotationException> errors) {
44+
assert !errors.isEmpty() : "there must be at least one error";
45+
46+
StringBuilder sb = new StringBuilder(errors.size() * 200);
47+
sb.append(errors.size()).append(" counts of IllegalAnnotationExceptions").append('\n');
5048

49+
for (IllegalAnnotationException error : errors) {
50+
sb.append("- ").append(error.toString()).append('\n');
51+
}
5152
return sb.toString();
5253
}
5354

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Distribution License v. 1.0, which is available at
6+
* http://www.eclipse.org/org/documents/edl-v10.php.
7+
*
8+
* SPDX-License-Identifier: BSD-3-Clause
9+
*/
10+
package org.glassfish.jaxb.runtime.v2.runtime;
11+
12+
import jakarta.xml.bind.annotation.XmlAttribute;
13+
import jakarta.xml.bind.annotation.XmlRootElement;
14+
15+
import java.util.Date;
16+
17+
@XmlRootElement(name = "illegalAnnotationsExceptionDto")
18+
public class IllegalAnnotationsExceptionDTO {
19+
20+
@XmlAttribute(name = "field")
21+
protected Date field;
22+
23+
public Date getField() {
24+
return field;
25+
}
26+
27+
public void setField(Date field) {
28+
this.field = field;
29+
}
30+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (c) 2025 Contributors to the Eclipse Foundation. All rights reserved.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Eclipse Distribution License v. 1.0, which is available at
6+
* http://www.eclipse.org/org/documents/edl-v10.php.
7+
*
8+
* SPDX-License-Identifier: BSD-3-Clause
9+
*/
10+
package org.glassfish.jaxb.runtime.v2.runtime;
11+
12+
import jakarta.xml.bind.JAXBContext;
13+
import jakarta.xml.bind.JAXBException;
14+
import org.junit.Assert;
15+
import org.junit.Test;
16+
17+
public class IllegalAnnotationsExceptionTest {
18+
@Test
19+
public void test() {
20+
try {
21+
JAXBContext.newInstance(IllegalAnnotationsExceptionDTO.class);
22+
Assert.fail("no exception thrown");
23+
} catch (IllegalAnnotationsException e) {
24+
// actual message (x counts of IllegalAnnotationExceptions)
25+
Assert.assertTrue(e.getMessage().contains("1 counts of IllegalAnnotationExceptions"));
26+
// new detailed message containing location of exception if present (here it is)
27+
Assert.assertTrue(e.getMessage().contains("this problem is related to the following location:"));
28+
} catch (JAXBException e) {
29+
Assert.fail("unexpected JAXBException");
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)