"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "pmd-lang-test/src/main/kotlin/net/sourceforge/pmd/lang/ast/test/BaseParsingHelper.kt" between
pmd-src-6.37.0.zip and pmd-src-6.38.0.zip

About: PMD is a source code analyzer for Java, JavaScript, XML and others. It finds unused variables, empty catch blocks, unnecessary object creation, and so forth. Sources (Java).

BaseParsingHelper.kt  (pmd-src-6.37.0):BaseParsingHelper.kt  (pmd-src-6.38.0)
/* /*
* BSD-style license; for more info see http://pmd.sourceforge.net/license.html * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
*/ */
package net.sourceforge.pmd.lang.ast.test package net.sourceforge.pmd.lang.ast.test
import net.sourceforge.pmd.lang.LanguageRegistry import net.sourceforge.pmd.*
import net.sourceforge.pmd.lang.LanguageVersion import net.sourceforge.pmd.lang.*
import net.sourceforge.pmd.lang.LanguageVersionHandler
import net.sourceforge.pmd.lang.ParserOptions
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
import java.io.File
import java.io.InputStream import java.io.InputStream
import java.io.StringReader import java.io.StringReader
import java.nio.charset.StandardCharsets import java.nio.charset.StandardCharsets
/** /**
* Language-independent base for a parser utils class. * Language-independent base for a parser utils class.
* Implementations are language-specific. * Implementations are language-specific.
*/ */
abstract class BaseParsingHelper<Self : BaseParsingHelper<Self, T>, T : RootNode >( abstract class BaseParsingHelper<Self : BaseParsingHelper<Self, T>, T : RootNode >(
protected val langName: String, protected val langName: String,
skipping to change at line 57 skipping to change at line 56
internal val resourcePrefix: String get() = params.resourcePrefix internal val resourcePrefix: String get() = params.resourcePrefix
/** /**
* Returns the language version with the given version string. * Returns the language version with the given version string.
* If null, this defaults to the default language version for * If null, this defaults to the default language version for
* this instance (not necessarily the default language version * this instance (not necessarily the default language version
* defined by the language module). * defined by the language module).
*/ */
fun getVersion(version: String?): LanguageVersion { fun getVersion(version: String?): LanguageVersion {
val language = LanguageRegistry.getLanguage(langName) val language = language
return if (version == null) language.defaultVersion return if (version == null) language.defaultVersion
else language.getVersion(version) ?: throw AssertionError("Unsupp else language.getVersion(version)
orted version $version for language $language") ?: throw AssertionError("Unsupported version $version for language $
language")
} }
val defaultVersion: LanguageVersion val language: Language
get() = LanguageRegistry.getLanguage(langName)
?: throw AssertionError("'$langName' is not a supported language (av
ailable ${LanguageRegistry.getLanguages()})")
val defaultVersion: LanguageVersion
get() = getVersion(params.defaultVerString) get() = getVersion(params.defaultVerString)
protected abstract fun clone(params: Params): Self protected abstract fun clone(params: Params): Self
/** /**
* Returns an instance of [Self] for which all parsing methods * Returns an instance of [Self] for which all parsing methods
* default their language version to the provided [version] * default their language version to the provided [version]
* If the [version] is null, then the default language version * If the [version] is null, then the default language version
* defined by the language module is used instead. * defined by the language module is used instead.
*/ */
skipping to change at line 110 skipping to change at line 114
ArrayList<R>().also { ArrayList<R>().also {
parse(source, version).findDescendantsOfType(target, it, true) parse(source, version).findDescendantsOfType(target, it, true)
} }
/** /**
* Parses the [sourceCode] with the given [version]. This may execute * Parses the [sourceCode] with the given [version]. This may execute
* additional processing passes if this instance is configured to do * additional processing passes if this instance is configured to do
* so. * so.
*/ */
@JvmOverloads @JvmOverloads
open fun parse(sourceCode: String, version: String? = null): T { open fun parse(
sourceCode: String,
version: String? = null,
fileName: String = "src/a/test-file-name.${language.extensions[0]}"
): T {
val lversion = if (version == null) defaultVersion else getVersion(versi on) val lversion = if (version == null) defaultVersion else getVersion(versi on)
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(AbstractParser.doParse(parser, fileName, S tringReader(sourceCode)))
if (params.doProcess) { if (params.doProcess) {
handler.getQualifiedNameResolutionFacade(javaClass.classLoader).star t(rootNode) handler.getQualifiedNameResolutionFacade(javaClass.classLoader).star t(rootNode)
handler.getSymbolFacade(javaClass.classLoader).start(rootNode) handler.getSymbolFacade(javaClass.classLoader).start(rootNode)
handler.dataFlowFacade.start(rootNode) handler.dataFlowFacade.start(rootNode)
handler.getTypeResolutionFacade(javaClass.classLoader).start(rootNod e) handler.getTypeResolutionFacade(javaClass.classLoader).start(rootNod e)
handler.multifileFacade.start(rootNode) handler.multifileFacade.start(rootNode)
} }
return rootNode return rootNode
} }
/** /**
* 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
* with [withResourceContext]). * with [withResourceContext]).
*/ */
@JvmOverloads @JvmOverloads
open fun parseResource(resource: String, version: String? = null): T = open fun parseResource(resource: String, version: String? = null): T =
parse(readResource(resource), version) parse(readResource(resource), version, fileName = resource)
/** /**
* Fetches the source of the given [clazz]. * Fetches the source of the given [clazz].
*/ */
@JvmOverloads @JvmOverloads
open fun parseClass(clazz: Class<*>, version: String? = null): T = open fun parseClass(clazz: Class<*>, version: String? = null): T =
parse(readClassSource(clazz), version) parse(readClassSource(clazz), version)
protected fun readResource(resourceName: String): String { protected fun readResource(resourceName: String): String {
skipping to change at line 171 skipping to change at line 179
* *
* @throws IllegalArgumentException if the source file wasn't found * @throws IllegalArgumentException if the source file wasn't found
*/ */
private fun readClassSource(clazz: Class<*>): String { private fun readClassSource(clazz: Class<*>): String {
var sourceFile = clazz.name.replace('.', '/') + ".java" var sourceFile = clazz.name.replace('.', '/') + ".java"
// Consider nested classes // Consider nested classes
if (clazz.name.contains("$")) { if (clazz.name.contains("$")) {
sourceFile = sourceFile.substring(0, clazz.name.indexOf('$')) + ".ja va" sourceFile = sourceFile.substring(0, clazz.name.indexOf('$')) + ".ja va"
} }
val input = javaClass.classLoader.getResourceAsStream(sourceFile) val input = javaClass.classLoader.getResourceAsStream(sourceFile)
?: throw IllegalArgumentException("Unable to find source file $s ourceFile for $clazz") ?: throw IllegalArgumentException("Unable to find source file $sourc eFile for $clazz")
return consume(input) return consume(input)
} }
/**
* Execute the given [rule] on the [code]. Produce a report with the violati
ons
* found by the rule. The language version of the piece of code is determine
d by the [params].
*/
@JvmOverloads
fun executeRule(
rule: Rule,
code: String,
filename: String = "testfile.${language.extensions[0]}"
): Report {
val config = PMDConfiguration().apply {
val marker = params.parserOptions?.suppressMarker
if (marker != null)
suppressMarker = marker
}
val processor = SourceCodeProcessor(config)
val ctx = RuleContext()
val report = Report()
ctx.report = report
ctx.sourceCodeFile = File(filename)
ctx.isIgnoreExceptions = false
val rules = RuleSet.forSingleRule(rule)
try {
processor.processSourceCode(StringReader(code), RuleSets(rules), ctx
)
} catch (e: PMDException) {
throw e.cause!!
}
return report
}
fun executeRuleOnResource(rule: Rule, resourcePath: String): Report =
executeRule(rule, readResource(resourcePath))
} }
 End of changes. 10 change blocks. 
12 lines changed or deleted 58 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)