Skip to content

Commit

Permalink
MCR-3226 activate PMD rule ForLoopVariableCount
Browse files Browse the repository at this point in the history
  • Loading branch information
yagee-de committed Nov 20, 2024
1 parent 0416526 commit a904311
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,16 @@ public List<String> treeLines() {

private void treeLines(List<Indent> prefix, List<String> lines) {
prefix.add(Indent.LEAD);
for (int i = 0, n = entries.size(); i < n; i++) {
int entriesCount = entries.size();
for (int i = 0; i < entriesCount; i++) {
Entry entry = entries.get(i);
if (i == n - 1) {
prefix.remove(prefix.size() - 1);
if (i == entriesCount - 1) {
prefix.removeLast();
prefix.add(Indent.LAST);
}
entry.treeLines(prefix, lines);
}
prefix.remove(prefix.size() - 1);
prefix.removeLast();
}

private enum Indent {
Expand All @@ -101,9 +102,10 @@ private static abstract class Entry {

protected String prefixString(List<Indent> prefix) {
StringBuilder builder = new StringBuilder();
for (int i = 0, n = prefix.size(); i < n; i++) {
int prefixCount = prefix.size();
for (int i = 0; i < prefixCount; i++) {
Indent indent = prefix.get(i);
String symbol = i == n - 1 ? indent.lastSymbol : indent.leadSymbol;
String symbol = i == prefixCount - 1 ? indent.lastSymbol : indent.leadSymbol;
builder.append(symbol);
}
return builder.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@

/**
* Collects parameters used in XSL transformations, by copying them from
* MCRConfiguration, from the HTTP and MyCoRe session, from request attributes etc.
*
* MCRConfiguration, from the HTTP and MyCoRe session, from request attributes etc.
*
* @author Frank Lützenkirchen
*/
public class MCRParameterCollector {
Expand All @@ -63,14 +63,14 @@ public class MCRParameterCollector {
/**
* Collects parameters The collecting of parameters is done in steps,
* each step may overwrite parameters that already have been set.
*
*
* First, all configuration properties from MCRConfiguration are copied.
* Second, those variables stored in the HTTP session, that start with "XSL." are copied.
* Next, variables stored in the MCRSession are copied.
* Next, HTTP request parameters are copied.
*
*
* Only those parameters starting with "XSL." are copied from session and request,
*
*
* @param request the HttpRequest causing the XSL transformation, must NOT be null
*/
public MCRParameterCollector(HttpServletRequest request) {
Expand All @@ -80,13 +80,13 @@ public MCRParameterCollector(HttpServletRequest request) {
/**
* Collects parameters The collecting of parameters is done in steps,
* each step may overwrite parameters that already have been set.
*
*
* First, all configuration properties from MCRConfiguration are copied.
* Second, those variables stored in the HTTP session, that start with "XSL." are copied.
* Next, variables stored in the MCRSession are copied.
* Next, HTTP request parameters are copied.
* Next, HTTP request attributes are copied.
*
*
* @param request the HttpRequest causing the XSL transformation, must NOT be null
* @param onlySetXSLParameters if true, only those parameters starting with "XSL."
* are copied from session and request
Expand Down Expand Up @@ -116,7 +116,7 @@ public MCRParameterCollector(HttpServletRequest request, boolean onlySetXSLParam
/**
* Collects parameters The collecting of parameters is done in steps,
* each step may overwrite parameters that already have been set.
*
*
* First, all configuration properties from MCRConfiguration are copied.
* Next, those variables stored in the MCRSession that start with "XSL." are copied.
*/
Expand All @@ -127,10 +127,10 @@ public MCRParameterCollector() {
/**
* Collects parameters The collecting of parameters is done in steps,
* each step may overwrite parameters that already have been set.
*
*
* First, all configuration properties from MCRConfiguration are copied.
* Next, those variables stored in the MCRSession are copied.
*
*
* @param onlySetXSLParameters if true, only those parameters starting with "XSL." are copied from session
*/
public MCRParameterCollector(boolean onlySetXSLParameters) {
Expand Down Expand Up @@ -181,7 +181,7 @@ public String getParameter(String name, String defaultValue) {
}

/**
* Returns the parameter map.
* Returns the parameter map.
*/
public Map<String, Object> getParameterMap() {
return Collections.unmodifiableMap(parameters);
Expand All @@ -200,10 +200,10 @@ private void setFromConfiguration() {

private String xmlSafe(String key) {
StringBuilder builder = new StringBuilder();
if (key.length() != 0) {
if (!key.isEmpty()) {
char first = key.charAt(0);
builder.append(first == ':' || !Verifier.isXMLNameStartCharacter(first) ? "_" : first);
for (int i = 1, n = key.length(); i < n; i++) {
for (int i = 1; i < key.length(); i++) {
char following = key.charAt(i);
builder.append(following == ':' || !Verifier.isXMLNameCharacter(following) ? "_" : following);
}
Expand Down Expand Up @@ -297,8 +297,8 @@ private void setFromRequestHeader(HttpServletRequest request) {
parameters.put("UserAgent", request.getHeader("User-Agent") != null ? request.getHeader("User-Agent") : "");
}

/**
* Calculates the complete request URL, so that mod_proxy is supported
/**
* Calculates the complete request URL, so that mod_proxy is supported
*/
private String getCompleteURL(HttpServletRequest request) {
StringBuilder buffer = getBaseURLUpToHostName();
Expand Down Expand Up @@ -332,7 +332,7 @@ private StringBuilder getBaseURLUpToHostName() {
/**
* Sets XSL parameters for the given transformer by taking them from the
* properties object provided.
*
*
* @param transformer
* the Transformer object thats parameters should be set
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,13 @@ private byte[] getHash(byte[] salt, int hashSizeBytes, String password) throws E
int digestLength = digest.getDigestLength();

ByteArrayOutputStream buffer = new ByteArrayOutputStream();
for (int i = 0, n = hashSizeBytes / digestLength; i < n; i++) {
int hashIterations = hashSizeBytes / digestLength;
for (int i = 0; i < hashIterations; i++) {
buffer.write(getHash(digest, source, numberOfBytes, i));
digest.reset();
}
buffer.write(getHash(digest, source, numberOfBytes, hashSizeBytes / digestLength),
0, hashSizeBytes - (hashSizeBytes / digestLength) * digestLength);
buffer.write(getHash(digest, source, numberOfBytes, hashIterations), 0,
hashSizeBytes - hashIterations * digestLength);

return buffer.toByteArray();

Expand All @@ -157,10 +158,11 @@ private byte[] getHash(MessageDigest digest, byte[] source, long numberOfBytes,
digest.update((byte) 0);
}

for (long i = 0, n = numberOfBytes / source.length; i < n; i++) {
long iterations = numberOfBytes / source.length;
for (long i = 0; i < iterations; i++) {
digest.update(source);
}
digest.update(source, 0, (int) (numberOfBytes - (numberOfBytes / source.length) * source.length));
digest.update(source, 0, (int) (numberOfBytes - iterations * source.length));

return digest.digest();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ public class MCRBCryptStrategy extends MCRPasswordCheckStrategyBase {
};

BASE_64_TO_RADIX_64_MAP = new char[max(base64Alphabet) + 1];
for (int i = 0, n = base64Alphabet.length; i < n; i++) {
for (int i = 0; i < base64Alphabet.length; i++) {
BASE_64_TO_RADIX_64_MAP[base64Alphabet[i]] = radix64Alphabet[i];
}

RADIX_64_TO_BASE_64_MAP = new char[max(radix64Alphabet) + 1];
for (int i = 0, n = radix64Alphabet.length; i < n; i++) {
for (int i = 0; i < radix64Alphabet.length; i++) {
RADIX_64_TO_BASE_64_MAP[radix64Alphabet[i]] = base64Alphabet[i];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public void addRule(String baseXPath, Node ruleElement) {
NamedNodeMap attributes = ruleElement.getAttributes();
Attr enableReplacementNode = (Attr) attributes.getNamedItem("enable-replacements");
if (enableReplacementNode != null && enableReplacementNode.getValue().equals("true")) {
for (int i = 0, n = attributes.getLength(); i < n; i++) {
int attributesLength = attributes.getLength();
for (int i = 0; i < attributesLength; i++) {
Attr attribute = (Attr) attributes.item(i);
String oldValue = attribute.getNodeValue();
String newValue = session.replaceParameters(oldValue);
Expand Down
3 changes: 0 additions & 3 deletions ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@
<rule ref="category/java/bestpractices.xml/DefaultLabelNotLastInSwitch"/>
<rule ref="category/java/bestpractices.xml/DoubleBraceInitialization"/>
<rule ref="category/java/bestpractices.xml/ForLoopCanBeForeach"/>
<!-- TODO: Create PR to fix this rule -->
<!--
<rule ref="category/java/bestpractices.xml/ForLoopVariableCount"/>
-->
<!-- TODO: Create PR to fix this rule -->
<!--
<rule ref="category/java/bestpractices.xml/GuardLogStatement"/>
Expand Down

0 comments on commit a904311

Please sign in to comment.