Skip to content

Commit

Permalink
[core] Fixups from pull request review
Browse files Browse the repository at this point in the history
  • Loading branch information
adangel committed Jul 31, 2021
1 parent fea395c commit 88547fc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
Expand Up @@ -226,7 +226,7 @@ public LanguageVersion getForceLanguageVersion() {
*
* @return true if ${@link #getForceLanguageVersion()} is not null
*/
public Boolean isForceLanguageVersion() {
public boolean isForceLanguageVersion() {
return forceLanguageVersion != null;
}

Expand Down
Expand Up @@ -10,6 +10,8 @@
import java.io.Reader;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import net.sourceforge.pmd.annotation.InternalApi;
import net.sourceforge.pmd.benchmark.TimeTracker;
Expand All @@ -31,6 +33,8 @@
@InternalApi
public class SourceCodeProcessor {

private static final Logger LOG = Logger.getLogger(SourceCodeProcessor.class.getName());

private final PMDConfiguration configuration;

public SourceCodeProcessor(PMDConfiguration configuration) {
Expand Down Expand Up @@ -113,7 +117,9 @@ private void processSourceCodeWithoutCache(final Reader sourceCode, final RuleSe
processSource(sourceCode, ruleSets, ctx);
} catch (ParseException pe) {
configuration.getAnalysisCache().analysisFailed(ctx.getSourceCodeFile());
if (!configuration.isForceLanguageVersion()) {
if (configuration.isForceLanguageVersion()) {
LOG.log(Level.FINE, "Error while parsing " + ctx.getSourceCodeFile(), pe);
} else {
throw new PMDException("Error while parsing " + ctx.getSourceCodeFile(), pe);
}
} catch (Exception e) {
Expand Down Expand Up @@ -203,12 +209,19 @@ private void processSource(Reader sourceCode, RuleSets ruleSets, RuleContext ctx
}

private void determineLanguage(RuleContext ctx) {
// If LanguageVersion of the source file is not known, make a
// determination
LanguageVersion languageVersion = ctx.getLanguageVersion();
if (languageVersion == null) {
languageVersion = configuration.getForceLanguageVersion();
languageVersion = languageVersion != null ? languageVersion : configuration.getLanguageVersionOfFile(ctx.getSourceCodeFilename());
if (ctx.getLanguageVersion() != null) {
// we already have a language
return;
}

// If LanguageVersion of the source file is not known, make a determination
LanguageVersion forceLanguage = configuration.getForceLanguageVersion();
if (forceLanguage != null) {
// use force language if given
ctx.setLanguageVersion(forceLanguage);
} else {
// otherwise determine by file extension
LanguageVersion languageVersion = configuration.getLanguageVersionOfFile(ctx.getSourceCodeFilename());
ctx.setLanguageVersion(languageVersion);
}
}
Expand Down
16 changes: 12 additions & 4 deletions pmd-core/src/main/java/net/sourceforge/pmd/cli/PMDParameters.java
Expand Up @@ -217,14 +217,18 @@ public PMDConfiguration toConfiguration() {
configuration.setAnalysisCacheLocation(this.cacheLocation);
configuration.setIgnoreIncrementalAnalysis(this.isIgnoreIncrementalAnalysis());

LanguageVersion forceLangVersion = LanguageRegistry
.findLanguageVersionByTerseName(this.getForceLanguage());
if (forceLangVersion != null) {
configuration.setForceLanguageVersion(forceLangVersion);
}

LanguageVersion languageVersion = LanguageRegistry
.findLanguageVersionByTerseName(forceLanguage != null ? forceLanguage : (this.getLanguage()) + ' ' + this.getVersion());
.findLanguageVersionByTerseName(this.getLanguage() + ' ' + this.getVersion());
if (languageVersion != null) {
if (forceLanguage != null) {
configuration.setForceLanguageVersion(languageVersion);
}
configuration.getLanguageVersionDiscoverer().setDefaultLanguageVersion(languageVersion);
}

try {
configuration.prependClasspath(this.getAuxclasspath());
} catch (IOException e) {
Expand Down Expand Up @@ -311,6 +315,10 @@ public String getLanguage() {
return language != null ? language : LanguageRegistry.getDefaultLanguage().getTerseName();
}

public String getForceLanguage() {
return forceLanguage != null ? forceLanguage : "";
}

public String getAuxclasspath() {
return auxclasspath;
}
Expand Down

0 comments on commit 88547fc

Please sign in to comment.