[core] Fixups from pull request review

This commit is contained in:
Andreas Dangel
2021-07-31 15:56:14 +02:00
parent fea395cfa9
commit 88547fc14c
3 changed files with 33 additions and 12 deletions

View File

@@ -226,7 +226,7 @@ public class PMDConfiguration extends AbstractConfiguration {
*
* @return true if ${@link #getForceLanguageVersion()} is not null
*/
public Boolean isForceLanguageVersion() {
public boolean isForceLanguageVersion() {
return forceLanguageVersion != null;
}

View File

@@ -10,6 +10,8 @@ import java.io.InputStreamReader;
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;
@@ -31,6 +33,8 @@ import net.sourceforge.pmd.lang.xpath.Initializer;
@InternalApi
public class SourceCodeProcessor {
private static final Logger LOG = Logger.getLogger(SourceCodeProcessor.class.getName());
private final PMDConfiguration configuration;
public SourceCodeProcessor(PMDConfiguration configuration) {
@@ -113,7 +117,9 @@ public class SourceCodeProcessor {
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) {
@@ -203,12 +209,19 @@ public class SourceCodeProcessor {
}
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);
}
}

View File

@@ -217,14 +217,18 @@ public class PMDParameters {
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) {
@@ -311,6 +315,10 @@ public class PMDParameters {
return language != null ? language : LanguageRegistry.getDefaultLanguage().getTerseName();
}
public String getForceLanguage() {
return forceLanguage != null ? forceLanguage : "";
}
public String getAuxclasspath() {
return auxclasspath;
}