Skip to content

Commit 4cf1795

Browse files
committed
Consistent to/cc/bcc array handling and revised hashCode without text
Issue: SPR-16671 (cherry picked from commit 1cc513d)
1 parent 5629fa2 commit 4cf1795

File tree

1 file changed

+39
-45
lines changed

1 file changed

+39
-45
lines changed

spring-context-support/src/main/java/org/springframework/mail/SimpleMailMessage.java

+39-45
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -74,15 +74,9 @@ public SimpleMailMessage(SimpleMailMessage original) {
7474
Assert.notNull(original, "'original' message argument must not be null");
7575
this.from = original.getFrom();
7676
this.replyTo = original.getReplyTo();
77-
if (original.getTo() != null) {
78-
this.to = copy(original.getTo());
79-
}
80-
if (original.getCc() != null) {
81-
this.cc = copy(original.getCc());
82-
}
83-
if (original.getBcc() != null) {
84-
this.bcc = copy(original.getBcc());
85-
}
77+
this.to = copyOrNull(original.getTo());
78+
this.cc = copyOrNull(original.getCc());
79+
this.bcc = copyOrNull(original.getBcc());
8680
this.sentDate = original.getSentDate();
8781
this.subject = original.getSubject();
8882
this.text = original.getText();
@@ -182,21 +176,21 @@ public String getText() {
182176
* @param target the {@code MailMessage} to copy to
183177
*/
184178
public void copyTo(MailMessage target) {
185-
Assert.notNull(target, "'target' message argument must not be null");
179+
Assert.notNull(target, "'target' MailMessage must not be null");
186180
if (getFrom() != null) {
187181
target.setFrom(getFrom());
188182
}
189183
if (getReplyTo() != null) {
190184
target.setReplyTo(getReplyTo());
191185
}
192186
if (getTo() != null) {
193-
target.setTo(getTo());
187+
target.setTo(copy(getTo()));
194188
}
195189
if (getCc() != null) {
196-
target.setCc(getCc());
190+
target.setCc(copy(getCc()));
197191
}
198192
if (getBcc() != null) {
199-
target.setBcc(getBcc());
193+
target.setBcc(copy(getBcc()));
200194
}
201195
if (getSentDate() != null) {
202196
target.setSentDate(getSentDate());
@@ -210,20 +204,6 @@ public void copyTo(MailMessage target) {
210204
}
211205

212206

213-
@Override
214-
public String toString() {
215-
StringBuilder sb = new StringBuilder("SimpleMailMessage: ");
216-
sb.append("from=").append(this.from).append("; ");
217-
sb.append("replyTo=").append(this.replyTo).append("; ");
218-
sb.append("to=").append(StringUtils.arrayToCommaDelimitedString(this.to)).append("; ");
219-
sb.append("cc=").append(StringUtils.arrayToCommaDelimitedString(this.cc)).append("; ");
220-
sb.append("bcc=").append(StringUtils.arrayToCommaDelimitedString(this.bcc)).append("; ");
221-
sb.append("sentDate=").append(this.sentDate).append("; ");
222-
sb.append("subject=").append(this.subject).append("; ");
223-
sb.append("text=").append(this.text);
224-
return sb.toString();
225-
}
226-
227207
@Override
228208
public boolean equals(Object other) {
229209
if (this == other) {
@@ -235,33 +215,47 @@ public boolean equals(Object other) {
235215
SimpleMailMessage otherMessage = (SimpleMailMessage) other;
236216
return (ObjectUtils.nullSafeEquals(this.from, otherMessage.from) &&
237217
ObjectUtils.nullSafeEquals(this.replyTo, otherMessage.replyTo) &&
238-
java.util.Arrays.equals(this.to, otherMessage.to) &&
239-
java.util.Arrays.equals(this.cc, otherMessage.cc) &&
240-
java.util.Arrays.equals(this.bcc, otherMessage.bcc) &&
218+
ObjectUtils.nullSafeEquals(this.to, otherMessage.to) &&
219+
ObjectUtils.nullSafeEquals(this.cc, otherMessage.cc) &&
220+
ObjectUtils.nullSafeEquals(this.bcc, otherMessage.bcc) &&
241221
ObjectUtils.nullSafeEquals(this.sentDate, otherMessage.sentDate) &&
242222
ObjectUtils.nullSafeEquals(this.subject, otherMessage.subject) &&
243223
ObjectUtils.nullSafeEquals(this.text, otherMessage.text));
244224
}
245225

246226
@Override
247227
public int hashCode() {
248-
int hashCode = (this.from == null ? 0 : this.from.hashCode());
249-
hashCode = 29 * hashCode + (this.replyTo == null ? 0 : this.replyTo.hashCode());
250-
for (int i = 0; this.to != null && i < this.to.length; i++) {
251-
hashCode = 29 * hashCode + (this.to == null ? 0 : this.to[i].hashCode());
252-
}
253-
for (int i = 0; this.cc != null && i < this.cc.length; i++) {
254-
hashCode = 29 * hashCode + (this.cc == null ? 0 : this.cc[i].hashCode());
255-
}
256-
for (int i = 0; this.bcc != null && i < this.bcc.length; i++) {
257-
hashCode = 29 * hashCode + (this.bcc == null ? 0 : this.bcc[i].hashCode());
258-
}
259-
hashCode = 29 * hashCode + (this.sentDate == null ? 0 : this.sentDate.hashCode());
260-
hashCode = 29 * hashCode + (this.subject == null ? 0 : this.subject.hashCode());
261-
hashCode = 29 * hashCode + (this.text == null ? 0 : this.text.hashCode());
228+
int hashCode = ObjectUtils.nullSafeHashCode(this.from);
229+
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.replyTo);
230+
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.to);
231+
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.cc);
232+
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.bcc);
233+
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.sentDate);
234+
hashCode = 29 * hashCode + ObjectUtils.nullSafeHashCode(this.subject);
262235
return hashCode;
263236
}
264237

238+
@Override
239+
public String toString() {
240+
StringBuilder sb = new StringBuilder("SimpleMailMessage: ");
241+
sb.append("from=").append(this.from).append("; ");
242+
sb.append("replyTo=").append(this.replyTo).append("; ");
243+
sb.append("to=").append(StringUtils.arrayToCommaDelimitedString(this.to)).append("; ");
244+
sb.append("cc=").append(StringUtils.arrayToCommaDelimitedString(this.cc)).append("; ");
245+
sb.append("bcc=").append(StringUtils.arrayToCommaDelimitedString(this.bcc)).append("; ");
246+
sb.append("sentDate=").append(this.sentDate).append("; ");
247+
sb.append("subject=").append(this.subject).append("; ");
248+
sb.append("text=").append(this.text);
249+
return sb.toString();
250+
}
251+
252+
253+
private static String[] copyOrNull(String[] state) {
254+
if (state == null) {
255+
return null;
256+
}
257+
return copy(state);
258+
}
265259

266260
private static String[] copy(String[] state) {
267261
String[] copy = new String[state.length];

0 commit comments

Comments
 (0)