Skip to content

Commit d9f9dc9

Browse files
committed
Fixes InaccessibleObjectException when printing errormessage on modulepath
1 parent d011999 commit d9f9dc9

File tree

3 files changed

+78
-12
lines changed

3 files changed

+78
-12
lines changed

equalsverifier-core/src/main/java/nl/jqno/equalsverifier/internal/util/Formatter.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static Formatter of(String message, Object... objects) {
4747
public String format() {
4848
String result = message;
4949
for (Object object : objects) {
50-
String s = result.replaceFirst("%%", Matcher.quoteReplacement(stringify(object)));
50+
String s = result.replaceFirst("%%", Matcher.quoteReplacement(stringify(object, true)));
5151
if (result.equals(s)) {
5252
throw new IllegalStateException("Too many parameters");
5353
}
@@ -59,18 +59,19 @@ public String format() {
5959
return result;
6060
}
6161

62-
private String stringify(Object obj) {
62+
private String stringify(Object obj, boolean recurse) {
6363
if (obj == null) {
6464
return "null";
6565
}
6666
try {
6767
return obj.toString();
6868
}
6969
catch (AbstractMethodError e) {
70-
return stringifyByReflection(obj);
70+
return recurse ? stringifyByReflection(obj) : objectToString(obj);
7171
}
7272
catch (Throwable e) {
73-
return stringifyByReflection(obj) + "-throws " + e.getClass().getSimpleName() + "(" + e.getMessage() + ")";
73+
var throwsClause = "-throws " + e.getClass().getSimpleName() + "(" + e.getMessage() + ")";
74+
return (recurse ? stringifyByReflection(obj) : objectToString(obj)) + throwsClause;
7475
}
7576
}
7677

@@ -91,7 +92,7 @@ private String stringifyByReflection(Object obj) {
9192
result.append("=");
9293

9394
Object value = probe.getValue(obj);
94-
result.append(stringify(value));
95+
result.append(stringify(value, false));
9596
}
9697

9798
if (!foundFields) {
@@ -101,4 +102,8 @@ private String stringifyByReflection(Object obj) {
101102
result.append("]");
102103
return result.toString();
103104
}
105+
106+
private String objectToString(Object obj) {
107+
return obj.getClass().getSimpleName() + "@" + Integer.toHexString(System.identityHashCode(obj));
108+
}
104109
}

equalsverifier-core/src/test/java/nl/jqno/equalsverifier/internal/util/FormatterTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ void oneThrowingContainerParameter() {
8888
ThrowingContainer tc = new ThrowingContainer(i.instantiate());
8989
Formatter f = Formatter.of("TC: %%", tc);
9090
String expected =
91-
"TC: [ThrowingContainer t=[Throwing i=0 s=null]-throws IllegalStateException(msg)]-throws IllegalStateException(msg)";
92-
assertThat(f.format()).contains(expected);
91+
"TC: \\[ThrowingContainer t=Throwing@.*-throws IllegalStateException\\(msg\\)\\]-throws IllegalStateException\\(msg\\)";
92+
assertThat(f.format()).matches(expected);
9393
}
9494

9595
@Test
@@ -98,7 +98,7 @@ void oneAbstractContainerParameter() {
9898
AbstractContainer ac = new AbstractContainer(i.instantiate());
9999

100100
Formatter f = Formatter.of("AC: %%", ac);
101-
assertThat(f.format()).contains("AC: [AbstractContainer ad=[AbstractDelegation y=0]]");
101+
assertThat(f.format()).matches("AC: \\[AbstractContainer ad=AbstractDelegation.*\\]");
102102
}
103103

104104
@Test
@@ -107,10 +107,9 @@ void parameterWithMixOfVariousFields() {
107107
mix.throwing = new Throwing(42, "empty");
108108

109109
Formatter f = Formatter.of("%%", mix);
110-
String expected =
111-
"[Mix i=42 s=null t=not null throwing=[Throwing i=42 s=empty]-throws IllegalStateException(msg)]"
112-
+ "-throws UnsupportedOperationException(null)";
113-
assertThat(f.format()).contains(expected);
110+
String expected = "\\[Mix i=42 s=null t=not null throwing=Throwing@.*-throws IllegalStateException\\(msg\\)\\]"
111+
+ "-throws UnsupportedOperationException\\(null\\)";
112+
assertThat(f.format()).matches(expected);
114113
}
115114

116115
@Test

equalsverifier-test-jpms/src/test/java/it/MockitoWorksInTheModularWorldTest.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package it;
22

3+
import java.util.List;
4+
import java.util.Objects;
5+
36
import nl.jqno.equalsverifier.EqualsVerifier;
47
import nl.jqno.equalsverifier.ScanOption;
58
import nl.jqno.equalsverifier.jpms.model.*;
69
import nl.jqno.equalsverifier.jpms.model.Records.RecordPoint;
710
import nl.jqno.equalsverifier.jpms.model.Records.RecordPointContainer;
11+
import nl.jqno.equalsverifier_testhelpers.ExpectedException;
812
import nl.jqno.equalsverifier_testhelpers.types.RecursiveTypeHelper.RecursiveTypeContainer;
913
import org.junit.jupiter.api.Test;
1014

@@ -51,4 +55,62 @@ void forPackageWorks() {
5155
.forPackage("nl.jqno.equalsverifier.jpms.model", ScanOption.except(Records.class::equals))
5256
.verify();
5357
}
58+
59+
@Test
60+
void showingMessagesWorks() {
61+
ExpectedException
62+
.when(() -> EqualsVerifier.forClass(IncorrectEquals.class).verify())
63+
.assertFailure()
64+
.assertMessageContains("Significant fields");
65+
}
66+
67+
@SuppressWarnings("InconsistentHashCode")
68+
static final class IncorrectEquals {
69+
final List<Foo> l;
70+
final int i;
71+
72+
public IncorrectEquals(List<Foo> l, int i) {
73+
this.l = l;
74+
this.i = i;
75+
}
76+
77+
@Override
78+
public boolean equals(Object obj) {
79+
return obj instanceof IncorrectEquals other && Objects.equals(l, other.l);
80+
}
81+
82+
@Override
83+
public int hashCode() {
84+
return Objects.hash(l, i);
85+
}
86+
87+
@Override
88+
public String toString() {
89+
return "IncorrectEquals: [" + l + ", " + i + "]";
90+
}
91+
}
92+
93+
static final class Foo {
94+
final String s;
95+
96+
public Foo(String s) {
97+
this.s = s;
98+
}
99+
100+
@Override
101+
public boolean equals(Object obj) {
102+
return obj instanceof Foo other && Objects.equals(s, other.s);
103+
}
104+
105+
@Override
106+
public int hashCode() {
107+
return Objects.hash(s);
108+
}
109+
110+
@Override
111+
public String toString() {
112+
return "Foo: [" + s + "]";
113+
}
114+
}
115+
54116
}

0 commit comments

Comments
 (0)