[core] Consolidate internal api in PMDConfiguration

PMDConfiguration#setAnalysisCache
PMDConfiguration#getAnalysisCache
This commit is contained in:
Andreas Dangel
2024-02-10 10:38:26 +01:00
parent 2cc9ae9d7e
commit 3a635da9c2
4 changed files with 39 additions and 9 deletions

View File

@ -316,6 +316,9 @@ package or made (package) private and are _not accessible_ anymore.
* All constructors are package private now.
* {%jdoc !!core::lang.ast.LexException %} - the constructor `LexException(boolean, String, int, int, String, char)` is now package private.
It is only used by JavaCC-generated token managers.
* {%jdoc !!core::PMDConfiguration %}
* Method `setAnalysisCache(AnalysisCache)` is now package private. Use {%jdoc core::PMDConfiguration#setAnalysisCacheLocation(java.lang.String) %} instead.
* Method `getAnalysisCache()` is now package private.
* pmd-ant
* {%jdoc !!ant::ant.Formatter %}
* Method `getRenderer()` has been removed.

View File

@ -21,13 +21,13 @@ import org.checkerframework.checker.nullness.qual.Nullable;
import org.slf4j.LoggerFactory;
import net.sourceforge.pmd.annotation.DeprecatedUntil700;
import net.sourceforge.pmd.annotation.InternalApi;
import net.sourceforge.pmd.cache.internal.AnalysisCache;
import net.sourceforge.pmd.cache.internal.FileAnalysisCache;
import net.sourceforge.pmd.cache.internal.NoopAnalysisCache;
import net.sourceforge.pmd.internal.util.ClasspathClassLoader;
import net.sourceforge.pmd.lang.Language;
import net.sourceforge.pmd.lang.LanguageRegistry;
import net.sourceforge.pmd.lang.LanguageVersion;
import net.sourceforge.pmd.lang.rule.RulePriority;
import net.sourceforge.pmd.lang.rule.RuleSetLoader;
import net.sourceforge.pmd.renderers.Renderer;
@ -455,8 +455,7 @@ public class PMDConfiguration extends AbstractConfiguration {
*
* @apiNote This is internal API.
*/
@InternalApi
public AnalysisCache getAnalysisCache() {
AnalysisCache getAnalysisCache() {
// Make sure we are not null
if (analysisCache == null || isIgnoreIncrementalAnalysis() && !(analysisCache instanceof NoopAnalysisCache)) {
// sets a noop cache
@ -476,8 +475,7 @@ public class PMDConfiguration extends AbstractConfiguration {
*
* @apiNote This is internal API. Use {@link #setAnalysisCacheLocation(String)} to configure a cache.
*/
@InternalApi
public void setAnalysisCache(final AnalysisCache cache) {
void setAnalysisCache(final AnalysisCache cache) {
// the doc says it's a noop if incremental analysis was disabled,
// but it's actually the getter that enforces that
this.analysisCache = cache == null ? new NoopAnalysisCache() : cache;

View File

@ -0,0 +1,28 @@
/*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/
package net.sourceforge.pmd;
import net.sourceforge.pmd.annotation.InternalApi;
import net.sourceforge.pmd.cache.internal.AnalysisCache;
/**
* Internal API.
*
* <p>Acts as a bridge between outer parts of PMD and the restricted access
* internal API of this package.
*
* <p><b>None of this is published API, and compatibility can be broken anytime!</b>
* Use this only at your own risk.
*
* @apiNote Internal API
*/
@InternalApi
public final class InternalApiBridgeForTestsOnly {
private InternalApiBridgeForTestsOnly() {}
public static void setAnalysisCache(PMDConfiguration pmdConfiguration, AnalysisCache cache) {
pmdConfiguration.setAnalysisCache(cache);
}
}

View File

@ -19,6 +19,7 @@ import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import net.sourceforge.pmd.FooRule;
import net.sourceforge.pmd.InternalApiBridgeForTestsOnly;
import net.sourceforge.pmd.PMDConfiguration;
import net.sourceforge.pmd.PmdAnalysis;
import net.sourceforge.pmd.cache.internal.AnalysisCache;
@ -71,7 +72,7 @@ class GlobalAnalysisListenerTest {
PMDConfiguration config = newConfig();
AnalysisCache mockCache = spy(NoopAnalysisCache.class);
config.setAnalysisCache(mockCache);
InternalApiBridgeForTestsOnly.setAnalysisCache(config, mockCache);
MyFooRule rule = new MyFooRule();
runPmd(config, GlobalAnalysisListener.noop(), rule);
@ -86,7 +87,7 @@ class GlobalAnalysisListenerTest {
PMDConfiguration config = newConfig();
AnalysisCache mockCache = spy(NoopAnalysisCache.class);
config.setAnalysisCache(mockCache);
InternalApiBridgeForTestsOnly.setAnalysisCache(config, mockCache);
BrokenRule rule = new BrokenRule(); // the broken rule throws
runPmd(config, GlobalAnalysisListener.noop(), rule);
@ -102,7 +103,7 @@ class GlobalAnalysisListenerTest {
PMDConfiguration config = newConfig();
AnalysisCache mockCache = spy(NoopAnalysisCache.class);
config.setAnalysisCache(mockCache);
InternalApiBridgeForTestsOnly.setAnalysisCache(config, mockCache);
BrokenRule rule = new BrokenRule(); // the broken rule throws
// now the exception should be propagated
@ -122,7 +123,7 @@ class GlobalAnalysisListenerTest {
@NonNull
private PMDConfiguration newConfig() {
PMDConfiguration config = new PMDConfiguration();
config.setAnalysisCache(new NoopAnalysisCache());
config.setAnalysisCacheLocation(null);
config.setIgnoreIncrementalAnalysis(true);
config.setThreads(0); // no multithreading for this test
return config;