Skip to content

Commit 2473095

Browse files
committed
[Misc] Simplify code flagged by SonarCloud (inline returns, factor duplicated literals)
* java:S1488 - immediately return the expression instead of assigning it to a temporary variable (12 issues). * java:S1192 - define/reuse a constant instead of duplicating a string literal (8 issues). Co-Authored-By: Vincent Massol <vincent@massol.net>
1 parent 4e2ed56 commit 2473095

19 files changed

Lines changed: 70 additions & 53 deletions

File tree

xwiki-platform-core/xwiki-platform-captcha/xwiki-platform-captcha-default/src/main/java/org/xwiki/captcha/AbstractCaptcha.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ protected Map<String, Object> getConfiguredParameters()
203203
*/
204204
protected XWikiContext getContext()
205205
{
206-
XWikiContext context = contextProvider.get();
207-
return context;
206+
return contextProvider.get();
208207
}
209208
}

xwiki-platform-core/xwiki-platform-captcha/xwiki-platform-captcha-jcaptcha/xwiki-platform-captcha-jcaptcha-api/src/main/java/org/xwiki/captcha/internal/JCaptchaCaptcha.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ protected boolean validate(Map<String, Object> captchaParameters) throws Captcha
140140

141141
String answer = request.getParameter("captchaAnswer");
142142

143-
boolean result = captchaService.validateResponseForID(id, answer);
144-
return result;
143+
return captchaService.validateResponseForID(id, answer);
145144
}
146145
}

xwiki-platform-core/xwiki-platform-feed/xwiki-platform-feed-api/src/main/java/com/xpn/xwiki/plugin/feed/FeedPlugin.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,12 +300,10 @@ public SyndFeed getFeedForce(String sfeed, boolean ignoreInvalidFeeds, XWikiCont
300300
XWikiFeedFetcher feedFetcher = new XWikiFeedFetcher();
301301
feedFetcher.setUserAgent(context.getWiki().Param("xwiki.plugins.feed.useragent",
302302
context.getWiki().getHttpUserAgent(context)));
303-
SyndFeed feed =
304-
feedFetcher.retrieveFeed(
305-
feedURL,
306-
(int) context.getWiki().ParamAsLong("xwiki.plugins.feed.timeout",
307-
context.getWiki().getHttpTimeout(context)));
308-
return feed;
303+
return feedFetcher.retrieveFeed(
304+
feedURL,
305+
(int) context.getWiki().ParamAsLong("xwiki.plugins.feed.timeout",
306+
context.getWiki().getHttpTimeout(context)));
309307
} catch (Exception ex) {
310308
if (ignoreInvalidFeeds) {
311309
@SuppressWarnings("unchecked")

xwiki-platform-core/xwiki-platform-feed/xwiki-platform-feed-api/src/main/java/com/xpn/xwiki/plugin/feed/SyndEntryDocumentSource.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ public String getPropertyName()
140140

141141
public static final String CONTENT_LENGTH = "ContentLength";
142142

143+
/**
144+
* The JTidy configuration property name holding the input encoding.
145+
*/
146+
private static final String INPUT_ENCODING = "input-encoding";
147+
143148
public static final Properties TIDY_FEED_CONFIG;
144149

145150
public static final Properties TIDY_XML_CONFIG;
@@ -180,7 +185,7 @@ public String getPropertyName()
180185
TIDY_XML_CONFIG.setProperty("input-xml", "yes");
181186
TIDY_XML_CONFIG.setProperty("output-xml", "yes");
182187
TIDY_XML_CONFIG.setProperty("add-xml-pi", "no");
183-
TIDY_XML_CONFIG.setProperty("input-encoding", "UTF8");
188+
TIDY_XML_CONFIG.setProperty(INPUT_ENCODING, "UTF8");
184189

185190
// HTML specific configuration
186191
TIDY_HTML_CONFIG = new Properties(TIDY_FEED_CONFIG);
@@ -189,7 +194,7 @@ public String getPropertyName()
189194
TIDY_HTML_CONFIG.setProperty("drop-empty-paras", "yes");
190195
TIDY_HTML_CONFIG.setProperty("enclose-text", "yes");
191196
TIDY_HTML_CONFIG.setProperty("logical-emphasis", "yes");
192-
TIDY_HTML_CONFIG.setProperty("input-encoding", "UTF8");
197+
TIDY_HTML_CONFIG.setProperty(INPUT_ENCODING, "UTF8");
193198

194199
// default parameters for all instances of this class
195200
DEFAULT_PARAMS = new HashMap<>();
@@ -636,7 +641,7 @@ public static org.w3c.dom.Document tidy(String xmlFragment, Properties config)
636641
// Even if we add a message listener we still have to redirect the output. Otherwise all the messages will be
637642
// written to the standard output (besides being logged by TIDY_LOGGER).
638643
ByteArrayOutputStream out = new ByteArrayOutputStream();
639-
ByteArrayInputStream in = new ByteArrayInputStream(xmlFragment.getBytes(Charset.forName(config.getProperty("input-encoding"))));
644+
ByteArrayInputStream in = new ByteArrayInputStream(xmlFragment.getBytes(Charset.forName(config.getProperty(INPUT_ENCODING))));
640645
return tidy.parseDOM(in, out);
641646
}
642647

xwiki-platform-core/xwiki-platform-legacy/xwiki-platform-legacy-mail/xwiki-platform-legacy-mail-send/xwiki-platform-legacy-mail-send-default/src/main/java/org/xwiki/mail/internal/factory/users/UsersMimeMessageFactory.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,8 @@ public Iterator<MimeMessage> createMessage(Object userReferencesObject, Map<Stri
6565

6666
MimeMessageFactory factory = getInternalMimeMessageFactory(factoryHint);
6767

68-
UsersMimeMessageIterator iterator = new UsersMimeMessageIterator(userReferences, factory, parameters,
68+
return new UsersMimeMessageIterator(userReferences, factory, parameters,
6969
this.componentManagerProvider.get());
70-
return iterator;
7170
}
7271

7372
}

xwiki-platform-core/xwiki-platform-legacy/xwiki-platform-legacy-mail/xwiki-platform-legacy-mail-send/xwiki-platform-legacy-mail-send-storage/src/main/java/org/xwiki/mail/script/DeprecatedMailStorageScriptService.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,7 @@ public ScriptMailResult resendAsynchronously(String batchId, String uniqueMessag
117117
{
118118
try {
119119
MailStatusResult statusResult = this.mailResender.resendAsynchronously(batchId, uniqueMessageId);
120-
ScriptMailResult scriptMailResult = new ScriptMailResult(new DefaultMailResult(batchId), statusResult);
121-
return scriptMailResult;
120+
return new ScriptMailResult(new DefaultMailResult(batchId), statusResult);
122121
} catch (MailStoreException e) {
123122
// Save the exception for reporting through the script services's getLastError() API
124123
setError(e);
@@ -311,8 +310,7 @@ private Map<String, Object> normalizeFilterMap(Map<String, Object> filterMap)
311310

312311
private MimeMessage loadMessage(Session session, String batchId, String mailId) throws MailStoreException
313312
{
314-
MimeMessage message = this.mailContentStore.load(session, batchId, mailId);
315-
return message;
313+
return this.mailContentStore.load(session, batchId, mailId);
316314
}
317315

318316
@Override

xwiki-platform-core/xwiki-platform-mailsender/src/main/java/com/xpn/xwiki/plugin/mailsender/MailSenderPlugin.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ public class MailSenderPlugin extends XWikiDefaultPlugin
100100
*/
101101
private static final String EMAIL_ENCODING = "UTF-8";
102102

103+
/**
104+
* Error message logged when a {@link MessagingException} occurs while sending emails.
105+
*/
106+
private static final String MESSAGING_EXCEPTION_ERROR = "MessagingException has occured.";
107+
103108
/**
104109
* Error code signaling that the mail template requested for
105110
* {@link #sendMailFromTemplate(String, String, String, String, String, String, VelocityContext, XWikiContext)} was
@@ -673,7 +678,7 @@ public boolean sendMails(Collection<Mail> emails, MailConfiguration mailConfigur
673678
transport.close();
674679
}
675680
} catch (MessagingException ex) {
676-
LOGGER.error("MessagingException has occured.", ex);
681+
LOGGER.error(MESSAGING_EXCEPTION_ERROR, ex);
677682
}
678683
transport = null;
679684
session = null;
@@ -689,7 +694,7 @@ public boolean sendMails(Collection<Mail> emails, MailConfiguration mailConfigur
689694
throw ex;
690695
}
691696
} catch (MessagingException mex) {
692-
LOGGER.error("MessagingException has occured.", mex);
697+
LOGGER.error(MESSAGING_EXCEPTION_ERROR, mex);
693698
LOGGER.error("Detailed email information" + mail.toString());
694699
if (emailCount == 1) {
695700
throw mex;
@@ -706,7 +711,7 @@ public boolean sendMails(Collection<Mail> emails, MailConfiguration mailConfigur
706711
transport.close();
707712
}
708713
} catch (MessagingException ex) {
709-
LOGGER.error("MessagingException has occured.", ex);
714+
LOGGER.error(MESSAGING_EXCEPTION_ERROR, ex);
710715
}
711716

712717
LOGGER.info("sendEmails: Email count = " + emailCount + " sent count = " + count);

xwiki-platform-core/xwiki-platform-mailsender/src/main/java/com/xpn/xwiki/plugin/mailsender/MailSenderPluginApi.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public class MailSenderPluginApi extends PluginApi<MailSenderPlugin> implements
4444
*/
4545
private static final Logger LOGGER = LoggerFactory.getLogger(MailSenderPluginApi.class);
4646

47+
/**
48+
* The key under which the error message is stored in the XWiki context.
49+
*/
50+
private static final String ERROR_KEY = "error";
51+
4752
/**
4853
* API constructor.
4954
*
@@ -119,7 +124,7 @@ public int sendMessageFromTemplate(String from, String to, String cc, String bcc
119124
} catch (Exception e) {
120125
// If the exception is a null pointer exception there is no message and e.getMessage() is null.
121126
if (e.getMessage() != null) {
122-
this.context.put("error", e.getMessage());
127+
this.context.put(ERROR_KEY, e.getMessage());
123128
}
124129
LOGGER.error("sendMessageFromTemplate", e);
125130
return -1;
@@ -150,7 +155,7 @@ public int sendMessageFromTemplate(String from, String to, String cc, String bcc
150155
} catch (Exception e) {
151156
// If the exception is a null pointer exception there is no message and e.getMessage() is null.
152157
if (e.getMessage() != null) {
153-
this.context.put("error", e.getMessage());
158+
this.context.put(ERROR_KEY, e.getMessage());
154159
}
155160
LOGGER.error("sendMessageFromTemplate", e);
156161
return -1;
@@ -172,7 +177,7 @@ public int sendMail(Mail mail)
172177
} catch (Exception e) {
173178
// If the exception is a null pointer exception there is no message and e.getMessage() is null.
174179
if (e.getMessage() != null) {
175-
this.context.put("error", e.getMessage());
180+
this.context.put(ERROR_KEY, e.getMessage());
176181
}
177182
LOGGER.error("Failed to send email [" + mail.toString() + "]", e);
178183
result = -1;
@@ -196,7 +201,7 @@ public int sendMail(Mail mail, MailConfiguration mailConfiguration)
196201
} catch (Exception e) {
197202
// If the exception is a null pointer exception there is no message and e.getMessage() is null.
198203
if (e.getMessage() != null) {
199-
this.context.put("error", e.getMessage());
204+
this.context.put(ERROR_KEY, e.getMessage());
200205
}
201206
LOGGER.error("Failed to send email [" + mail.toString() + "] using mail configuration ["
202207
+ mailConfiguration.toString() + "]", e);

xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/XWiki.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4227,7 +4227,8 @@ public int createUser(String userName, Map<String, ?> map, EntityReference paren
42274227

42284228
try {
42294229
// TODO: Verify existing user
4230-
XWikiDocument doc = getDocument(new DocumentReference(context.getWikiId(), "XWiki", userName), context);
4230+
XWikiDocument doc =
4231+
getDocument(new DocumentReference(context.getWikiId(), SYSTEM_SPACE, userName), context);
42314232

42324233
if (!doc.isNew()) {
42334234
// TODO: throws Exception

xwiki-platform-core/xwiki-platform-oldcore/src/main/java/com/xpn/xwiki/api/XWiki.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ public class XWiki extends Api
7171
/** Logging helper object. */
7272
protected static final Logger LOGGER = LoggerFactory.getLogger(XWiki.class);
7373

74+
/** Value returned when Groovy parsing is denied because of missing programming rights. */
75+
private static final String GROOVY_MISSINGRIGHTS = "groovy_missingrights";
76+
7477
/** The internal object wrapped by this API. */
7578
private com.xpn.xwiki.XWiki xwiki;
7679

@@ -2686,7 +2689,7 @@ public java.lang.Object parseGroovyFromString(String script) throws XWikiExcepti
26862689
if (hasProgrammingRights()) {
26872690
return this.xwiki.parseGroovyFromString(script, getXWikiContext());
26882691
}
2689-
return "groovy_missingrights";
2692+
return GROOVY_MISSINGRIGHTS;
26902693
}
26912694

26922695
/**
@@ -2703,7 +2706,7 @@ public java.lang.Object parseGroovyFromPage(String script, String jarWikiPage) t
27032706
if (this.xwiki.getRightService().hasProgrammingRights(doc, getXWikiContext())) {
27042707
return this.xwiki.parseGroovyFromString(doc.getContent(), jarWikiPage, getXWikiContext());
27052708
}
2706-
return "groovy_missingrights";
2709+
return GROOVY_MISSINGRIGHTS;
27072710
}
27082711

27092712
/**
@@ -2720,7 +2723,7 @@ public java.lang.Object parseGroovyFromPage(String fullname) throws XWikiExcepti
27202723
if (this.xwiki.getRightService().hasProgrammingRights(doc, getXWikiContext())) {
27212724
return this.xwiki.parseGroovyFromString(doc.getContent(), getXWikiContext());
27222725
}
2723-
return "groovy_missingrights";
2726+
return GROOVY_MISSINGRIGHTS;
27242727
}
27252728

27262729
/**

0 commit comments

Comments
 (0)