[lang-test] Use processing stages only in BaseParsingHelper
This commit is contained in:
@ -23,7 +23,7 @@ public class ParserCornersTest {
|
|||||||
|
|
||||||
private static final String MULTICATCH = "public class Foo { public void bar() { "
|
private static final String MULTICATCH = "public class Foo { public void bar() { "
|
||||||
+ "try { System.out.println(); } catch (RuntimeException | IOException e) {} } }";
|
+ "try { System.out.println(); } catch (RuntimeException | IOException e) {} } }";
|
||||||
private final JavaParsingHelper java = JavaParsingHelper.WITH_PROCESSING.withResourceContext(ParserCornersTest.class);
|
private final JavaParsingHelper java = JavaParsingHelper.JUST_PARSE.withResourceContext(ParserCornersTest.class);
|
||||||
private final JavaParsingHelper java8 = java.withDefaultVersion("1.8");
|
private final JavaParsingHelper java8 = java.withDefaultVersion("1.8");
|
||||||
private final JavaParsingHelper java4 = java.withDefaultVersion("1.4");
|
private final JavaParsingHelper java4 = java.withDefaultVersion("1.4");
|
||||||
private final JavaParsingHelper java5 = java.withDefaultVersion("1.5");
|
private final JavaParsingHelper java5 = java.withDefaultVersion("1.5");
|
||||||
|
@ -8,6 +8,7 @@ import net.sourceforge.pmd.lang.LanguageVersion
|
|||||||
import net.sourceforge.pmd.lang.LanguageVersionHandler
|
import net.sourceforge.pmd.lang.LanguageVersionHandler
|
||||||
import net.sourceforge.pmd.lang.ParserOptions
|
import net.sourceforge.pmd.lang.ParserOptions
|
||||||
import net.sourceforge.pmd.lang.ast.AstAnalysisContext;
|
import net.sourceforge.pmd.lang.ast.AstAnalysisContext;
|
||||||
|
import net.sourceforge.pmd.lang.ast.AstProcessingStage
|
||||||
import net.sourceforge.pmd.lang.ast.Node
|
import net.sourceforge.pmd.lang.ast.Node
|
||||||
import net.sourceforge.pmd.lang.ast.RootNode
|
import net.sourceforge.pmd.lang.ast.RootNode
|
||||||
import org.apache.commons.io.IOUtils
|
import org.apache.commons.io.IOUtils
|
||||||
@ -105,36 +106,42 @@ abstract class BaseParsingHelper<Self : BaseParsingHelper<Self, T>, T : RootNode
|
|||||||
* so.
|
* so.
|
||||||
*/
|
*/
|
||||||
@JvmOverloads
|
@JvmOverloads
|
||||||
open fun parse(sourceCode: String, version: String? = null): T {
|
fun parse(sourceCode: String, version: String? = null): T {
|
||||||
val lversion = if (version == null) defaultVersion else getVersion(version)
|
val lversion = if (version == null) defaultVersion else getVersion(version)
|
||||||
val handler = lversion.languageVersionHandler
|
val handler = lversion.languageVersionHandler
|
||||||
val options = params.parserOptions ?: handler.defaultParserOptions
|
val options = params.parserOptions ?: handler.defaultParserOptions
|
||||||
val parser = handler.getParser(options)
|
val parser = handler.getParser(options)
|
||||||
val rootNode = rootClass.cast(parser.parse(null, StringReader(sourceCode)))
|
val rootNode = rootClass.cast(parser.parse(null, StringReader(sourceCode)))
|
||||||
if (params.doProcess) {
|
if (params.doProcess) {
|
||||||
if (!handler.processingStages.isEmpty()) {
|
postProcessing(handler, lversion, rootNode)
|
||||||
var astAnalysisContext = object : AstAnalysisContext {
|
|
||||||
override fun getTypeResolutionClassLoader() : ClassLoader {
|
|
||||||
return javaClass.classLoader
|
|
||||||
}
|
|
||||||
override fun getLanguageVersion() : LanguageVersion {
|
|
||||||
return lversion
|
|
||||||
}
|
|
||||||
}
|
|
||||||
handler.processingStages.forEach {
|
|
||||||
it.processAST(rootNode, astAnalysisContext)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
handler.getQualifiedNameResolutionFacade(javaClass.classLoader).start(rootNode)
|
|
||||||
handler.getSymbolFacade(javaClass.classLoader).start(rootNode)
|
|
||||||
handler.dataFlowFacade.start(rootNode)
|
|
||||||
handler.getTypeResolutionFacade(javaClass.classLoader).start(rootNode)
|
|
||||||
handler.multifileFacade.start(rootNode)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return rootNode
|
return rootNode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Select the processing stages that this should run in [postProcessing],
|
||||||
|
* by default runs everything.
|
||||||
|
*/
|
||||||
|
protected open fun selectProcessingStages(handler: LanguageVersionHandler): List<AstProcessingStage<*>> =
|
||||||
|
handler.processingStages
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called only if [Params.doProcess] is true.
|
||||||
|
*/
|
||||||
|
protected open fun postProcessing(handler: LanguageVersionHandler, lversion: LanguageVersion, rootNode: T?) {
|
||||||
|
val astAnalysisContext = object : AstAnalysisContext {
|
||||||
|
override fun getTypeResolutionClassLoader(): ClassLoader = javaClass.classLoader
|
||||||
|
|
||||||
|
override fun getLanguageVersion(): LanguageVersion = lversion
|
||||||
|
}
|
||||||
|
|
||||||
|
val stages = selectProcessingStages(handler).sortedWith(Comparator { o1, o2 -> o1.compare(o2) })
|
||||||
|
|
||||||
|
stages.forEach {
|
||||||
|
it.processAST(rootNode, astAnalysisContext)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetches and [parse]s the [resource] using the context defined for this
|
* Fetches and [parse]s the [resource] using the context defined for this
|
||||||
* instance (by default uses this class' classloader, but can be configured
|
* instance (by default uses this class' classloader, but can be configured
|
||||||
|
Reference in New Issue
Block a user