4040import jakarta .mail .Store ;
4141import jakarta .mail .URLName ;
4242import jakarta .mail .internet .MimeMessage ;
43+ import org .jspecify .annotations .Nullable ;
4344
4445import org .springframework .beans .factory .DisposableBean ;
4546import org .springframework .expression .Expression ;
@@ -79,33 +80,34 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
7980 */
8081 public static final String DEFAULT_SI_USER_FLAG = "spring-integration-mail-adapter" ;
8182
82- private final URLName url ;
83+ private final @ Nullable URLName url ;
8384
8485 private final ReentrantReadWriteLock folderLock = new ReentrantReadWriteLock ();
8586
8687 private final Lock folderReadLock = this .folderLock .readLock ();
8788
8889 private final Lock folderWriteLock = this .folderLock .writeLock ();
8990
90- private String protocol ;
91+ private @ Nullable String protocol ;
9192
9293 private int maxFetchSize = -1 ;
9394
94- private Session session ;
95+ private @ Nullable Session session ;
9596
9697 private boolean shouldDeleteMessages ;
9798
9899 private int folderOpenMode = Folder .READ_ONLY ;
99100
100101 private Properties javaMailProperties = new Properties ();
101102
102- private Authenticator javaMailAuthenticator ;
103+ private @ Nullable Authenticator javaMailAuthenticator ;
103104
105+ @ SuppressWarnings ("NullAway.Init" )
104106 private StandardEvaluationContext evaluationContext ;
105107
106- private Expression selectorExpression ;
108+ private @ Nullable Expression selectorExpression ;
107109
108- private HeaderMapper <MimeMessage > headerMapper ;
110+ private @ Nullable HeaderMapper <MimeMessage > headerMapper ;
109111
110112 private String userFlag = DEFAULT_SI_USER_FLAG ;
111113
@@ -117,9 +119,9 @@ public abstract class AbstractMailReceiver extends IntegrationObjectSupport impl
117119
118120 private boolean flaggedAsFallback = true ;
119121
120- private volatile Store store ;
122+ private volatile @ Nullable Store store ;
121123
122- private volatile Folder folder ;
124+ private volatile @ Nullable Folder folder ;
123125
124126 public AbstractMailReceiver () {
125127 this .url = null ;
@@ -130,7 +132,7 @@ public AbstractMailReceiver(URLName urlName) {
130132 this .url = urlName ;
131133 }
132134
133- public AbstractMailReceiver (String url ) {
135+ public AbstractMailReceiver (@ Nullable String url ) {
134136 if (url != null ) {
135137 this .url = new URLName (url );
136138 }
@@ -139,7 +141,7 @@ public AbstractMailReceiver(String url) {
139141 }
140142 }
141143
142- public void setSelectorExpression (Expression selectorExpression ) {
144+ public void setSelectorExpression (@ Nullable Expression selectorExpression ) {
143145 this .selectorExpression = selectorExpression ;
144146 }
145147
@@ -306,7 +308,7 @@ public void setFlaggedAsFallback(boolean flaggedAsFallback) {
306308 this .flaggedAsFallback = flaggedAsFallback ;
307309 }
308310
309- protected Folder getFolder () {
311+ protected @ Nullable Folder getFolder () {
310312 return this .folder ;
311313 }
312314
@@ -334,6 +336,7 @@ private void openSession() {
334336
335337 private void connectStoreIfNecessary () throws MessagingException {
336338 if (this .store == null ) {
339+ Assert .state (this .session != null , "'session' should not be null" );
337340 if (this .url != null ) {
338341 this .store = this .session .getStore (this .url );
339342 }
@@ -345,7 +348,8 @@ else if (this.protocol != null) {
345348 }
346349 }
347350 if (!this .store .isConnected ()) {
348- this .logger .debug (() -> "connecting to store [" + this .store .getURLName () + "]" );
351+ URLName urlName = this .store .getURLName ();
352+ this .logger .debug (() -> "connecting to store [" + urlName + "]" );
349353 this .store .connect ();
350354 }
351355 }
@@ -360,7 +364,8 @@ protected void openFolder() throws MessagingException {
360364 connectStoreIfNecessary ();
361365 }
362366 if (this .folder == null || !this .folder .exists ()) {
363- throw new IllegalStateException ("no such folder [" + this .url .getFile () + "]" );
367+ String file = this .url != null ? this .url .getFile () : "" ;
368+ throw new IllegalStateException ("no such folder [" + file + "]" );
364369 }
365370 if (this .folder .isOpen ()) {
366371 return ;
@@ -371,6 +376,7 @@ protected void openFolder() throws MessagingException {
371376 }
372377
373378 private Folder obtainFolderInstance () throws MessagingException {
379+ Assert .state (this .store != null , "'store' should not be null" );
374380 if (this .url == null ) {
375381 return this .store .getDefaultFolder ();
376382 }
@@ -422,7 +428,9 @@ protected void closeFolder() {
422428 }
423429
424430 private MimeMessage [] searchAndFilterMessages () throws MessagingException {
425- this .logger .debug (() -> "attempting to receive mail from folder [" + this .folder .getFullName () + "]" );
431+ Assert .state (this .folder != null , "'folder' should not be null" );
432+ String fullName = this .folder .getFullName ();
433+ this .logger .debug (() -> "attempting to receive mail from folder [" + fullName + "]" );
426434 Message [] messagesToProcess ;
427435 Message [] messages = searchForNewMessages ();
428436 if (this .maxFetchSize > 0 && messages .length > this .maxFetchSize ) {
@@ -549,7 +557,9 @@ private void setMessageFlagsAndMaybeDeleteMessages(Message[] messages) throws Me
549557 private void setMessageFlags (Message [] filteredMessages ) throws MessagingException {
550558 boolean recentFlagSupported = false ;
551559
552- Flags flags = getFolder ().getPermanentFlags ();
560+ Folder folder = getFolder ();
561+ Assert .state (folder != null , "'folder' should not be null" );
562+ Flags flags = folder .getPermanentFlags ();
553563
554564 if (flags != null ) {
555565 recentFlagSupported = flags .contains (Flags .Flag .RECENT );
@@ -612,6 +622,7 @@ protected void fetchMessages(Message[] messages) throws MessagingException {
612622 contentsProfile .add (FetchProfile .Item .ENVELOPE );
613623 contentsProfile .add (FetchProfile .Item .CONTENT_INFO );
614624 contentsProfile .add (FetchProfile .Item .FLAGS );
625+ Assert .state (this .folder != null , "'folder' should not be null" );
615626 this .folder .fetch (messages , contentsProfile );
616627 }
617628
@@ -658,10 +669,12 @@ protected void onInit() {
658669
659670 @ Override
660671 public String toString () {
661- return this .url .toString ();
672+ String urlName = this .store != null && this .store .getURLName () != null
673+ ? this .store .getURLName ().toString () : "" ;
674+ return this .url != null ? this .url .toString () : urlName ;
662675 }
663676
664- Store getStore () {
677+ @ Nullable Store getStore () {
665678 return this .store ;
666679 }
667680
@@ -678,7 +691,7 @@ private final class IntegrationMimeMessage extends MimeMessage {
678691
679692 private final MimeMessage source ;
680693
681- private final Object content ;
694+ private final @ Nullable Object content ;
682695
683696 IntegrationMimeMessage (MimeMessage source ) throws MessagingException {
684697 super (source );
@@ -700,7 +713,7 @@ private final class IntegrationMimeMessage extends MimeMessage {
700713 }
701714
702715 @ Override
703- public Folder getFolder () {
716+ public @ Nullable Folder getFolder () {
704717 if (!AbstractMailReceiver .this .autoCloseFolder ) {
705718 return AbstractMailReceiver .this .folder ;
706719 }
@@ -731,7 +744,7 @@ public int getLineCount() throws MessagingException {
731744 }
732745
733746 @ Override
734- public Object getContent () throws IOException , MessagingException {
747+ public @ Nullable Object getContent () throws IOException , MessagingException {
735748 if (AbstractMailReceiver .this .simpleContent ) {
736749 return super .getContent ();
737750 }
0 commit comments