Merge branch 'pr-3385'

This commit is contained in:
Juan Martín Sotuyo Dodero
2021-07-06 17:42:01 -03:00
4 changed files with 28 additions and 14 deletions

View File

@@ -18,12 +18,15 @@ This is a {{ site.pmd.release_type }} release.
* apex
* [#3329](https://github.com/pmd/pmd/issues/3329): \[apex] ApexCRUDViolation doesn't report SOQL for loops
* core
* [#3387](https://github.com/pmd/pmd/issues/3387): \[core] CPD should avoid unnecessary copies when running with --skip-lexical-errors
### API Changes
### External Contributions
* [#3367](https://github.com/pmd/pmd/pull/3367): \[apex] Check SOQL CRUD on for loops - [Jonathan Wiesel](https://github.com/jonathanwiesel)
* [#3385](https://github.com/pmd/pmd/pull/3385): \[core] CPD: Optimize --skip-lexical-errors option - [Woongsik Choi](https://github.com/woongsikchoi)
{% endtocmaker %}

View File

@@ -147,13 +147,12 @@ public class CPD {
}
private void addAndSkipLexicalErrors(SourceCode sourceCode) throws IOException {
TokenEntry.State savedTokenEntry = new TokenEntry.State(tokens.getTokens());
final TokenEntry.State savedState = tokens.snapshot();
try {
addAndThrowLexicalError(sourceCode);
} catch (TokenMgrError e) {
System.err.println("Skipping " + sourceCode.getFileName() + ". Reason: " + e.getMessage());
tokens.getTokens().clear();
tokens.getTokens().addAll(savedTokenEntry.restore());
tokens.restore(savedState);
}
}

View File

@@ -4,8 +4,8 @@
package net.sourceforge.pmd.cpd;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
@@ -94,21 +94,23 @@ public class TokenEntry implements Comparable<TokenEntry> {
* entries.
*/
public static class State {
private int tokenCount;
private Map<String, Integer> tokens;
private List<TokenEntry> entries;
private final int tokenCount;
private final int tokensMapSize;
public State(List<TokenEntry> entries) {
public State() {
this.tokenCount = TokenEntry.TOKEN_COUNT.get().intValue();
this.tokens = new HashMap<>(TokenEntry.TOKENS.get());
this.entries = new ArrayList<>(entries);
this.tokensMapSize = TokenEntry.TOKENS.get().size();
}
public List<TokenEntry> restore() {
public void restore(final List<TokenEntry> entries) {
TokenEntry.TOKEN_COUNT.get().set(tokenCount);
TOKENS.get().clear();
TOKENS.get().putAll(tokens);
return entries;
final Iterator<Map.Entry<String, Integer>> it = TOKENS.get().entrySet().iterator();
while (it.hasNext()) {
if (it.next().getValue() > tokensMapSize) {
it.remove();
}
}
entries.subList(tokenCount, entries.size()).clear();
}
}

View File

@@ -8,6 +8,8 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import net.sourceforge.pmd.cpd.TokenEntry.State;
public class Tokens {
private List<TokenEntry> tokens = new ArrayList<>();
@@ -44,4 +46,12 @@ public class Tokens {
return tokens;
}
public State snapshot() {
return new State();
}
public void restore(final State savedState) {
savedState.restore(tokens);
}
}