Skip to content

Commit 942ae7e

Browse files
authored
Merge pull request github#5142 from Marcono1234/marcono1234/maven-pom-improvements
Java: Improve MavenPom documentation, rename inconsistent predicates
2 parents 02578cf + e89891f commit 942ae7e

File tree

1 file changed

+52
-40
lines changed

1 file changed

+52
-40
lines changed

java/ql/src/semmle/code/xml/MavenPom.qll

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class ProtoPom extends XMLElement {
2828
Version getVersion() { result = this.getAChild() }
2929

3030
/**
31-
* Gets a string representing the version, or an empty string if no version
31+
* Gets a string representing the version, or an empty string if no `version`
3232
* tag was provided.
3333
*/
3434
string getVersionString() {
@@ -53,7 +53,7 @@ class Pom extends ProtoPom {
5353
Pom() {
5454
this.getName() = "project" and
5555
// Ignore "dependency-reduced-pom" files - these are generated by the
56-
// shading plugin, and duplicate existing pom files.
56+
// Maven Shade Plugin, and duplicate existing POM files.
5757
this.getFile().getStem() != "dependency-reduced-pom"
5858
}
5959

@@ -77,7 +77,7 @@ class Pom extends ProtoPom {
7777
/** Gets a child XML element named "dependencies". */
7878
Dependencies getDependencies() { result = this.getAChild() }
7979

80-
/** Gets a child XML element named `dependencyManagement`. */
80+
/** Gets a child XML element named "dependencyManagement". */
8181
DependencyManagement getDependencyManagement() { result = getAChild() }
8282

8383
/** Gets a Dependency element for this POM. */
@@ -100,7 +100,8 @@ class Pom extends ProtoPom {
100100
}
101101

102102
/**
103-
* Gets a property value defined for this project with the given name.
103+
* Gets a property value defined for this project with the given name, either in a local
104+
* `<properties>` section, or in the `<properties>` section of an ancestor POM.
104105
*/
105106
PomProperty getProperty(string name) {
106107
result.getName() = name and
@@ -112,7 +113,7 @@ class Pom extends ProtoPom {
112113
*/
113114
PomElement getProjectProperty() {
114115
(
115-
// It must either be a child of the pom, or a child of the parent node of the pom
116+
// It must either be a child of the POM, or a child of the parent node of the POM
116117
result = getAChild()
117118
or
118119
result = getParentPom().getAChild() and
@@ -124,8 +125,8 @@ class Pom extends ProtoPom {
124125
}
125126

126127
/**
127-
* Resolve the given placeholder (if possible) in the static context of this pom. Resolution
128-
* occurs by considering the properties defined by this project.
128+
* Resolve the given placeholder (if possible) in the static context of this POM. Resolution
129+
* occurs by considering the properties defined by this project or an ancestor project.
129130
*/
130131
string resolvePlaceholder(string name) {
131132
if name.prefix(8) = "project."
@@ -142,32 +143,33 @@ class Pom extends ProtoPom {
142143
}
143144

144145
/**
145-
* Gets all the dependencies that are exported by this pom. An exported dependency is one that
146-
* is transitively available, i.e. one with scope compile.
146+
* Gets all the dependencies that are exported by this POM. An exported dependency is one that
147+
* is transitively available, i.e. one with scope "compile".
147148
*/
148149
Dependency getAnExportedDependency() {
149150
result = getADependency() and result.getScope() = "compile"
150151
}
151152

152153
/**
153-
* Gets a pom dependency that is exported by this pom. An exported dependency is one that
154-
* is transitively available, i.e. one with scope compile.
154+
* Gets a POM dependency that is exported by this POM. An exported dependency is one that
155+
* is transitively available, i.e. one with scope "compile".
155156
*/
156157
Pom getAnExportedPom() { result = getAnExportedDependency().getPom() }
157158

158159
/**
159-
* Gets the `<parent>` element of this pom, if any.
160+
* Gets the `<parent>` element of this POM, if any.
160161
*/
161162
Parent getParentElement() { result = getAChild() }
162163

163164
/**
164-
* Gets the pom referred to by the `<parent>` element of this pom, if any.
165+
* Gets the POM referred to by the `<parent>` element of this POM, if any.
165166
*/
166167
Pom getParentPom() { result = getParentElement().getPom() }
167168

168169
/**
169170
* Gets the version specified for dependency `dep` in a `dependencyManagement`
170-
* section if this pom or one of its ancestors.
171+
* section in this POM or one of its ancestors, or an empty string if no version
172+
* is specified.
171173
*/
172174
string getVersionStringForDependency(Dependency dep) {
173175
if exists(getDependencyManagement().getDependency(dep))
@@ -223,12 +225,13 @@ class Dependency extends ProtoPom {
223225
Pom getPom() { result.getShortCoordinate() = this.getShortCoordinate() }
224226

225227
/**
226-
* Gets the jar file that we think maven resolved this dependency to (if any).
228+
* Gets the jar file that Maven likely resolved this dependency to (if any).
229+
* See `MavenRepo.getAnArtifact(ProtoPom)` for how this match is determined.
227230
*/
228231
File getJar() { exists(MavenRepo mr | result = mr.getAnArtifact(this)) }
229232

230233
/**
231-
* Gets the scope of this dependency. If the scope tag is present, this will
234+
* Gets the scope of this dependency. If the `scope` tag is present, this will
232235
* be the string contents of that tag, otherwise it defaults to "compile".
233236
*/
234237
string getScope() {
@@ -249,14 +252,14 @@ class Dependency extends ProtoPom {
249252
}
250253

251254
/**
252-
* A Maven dependency element that represents an actual dependency from a given pom project.
255+
* A Maven dependency element that represents an actual dependency from a given POM project.
253256
*/
254257
class PomDependency extends Dependency {
255258
PomDependency() {
256259
exists(Pom source |
257-
// This dependency must be a dependency of a pom - dependency tags can also appear in the dependency
258-
// management section, where they do not directly contribute to the dependencies of the containing
259-
// pom.
260+
// This dependency must be a dependency of a POM - dependency tags can also appear in the
261+
// dependencyManagement section, where they do not directly contribute to the dependencies of
262+
// the containing POM.
260263
source.getADependency() = this and
261264
// Consider dependencies that can be used at compile time.
262265
(
@@ -284,7 +287,7 @@ class PomElement extends XMLElement {
284287
s = allCharactersString() and
285288
if s.matches("${%")
286289
then
287-
// Resolve the placeholder in the parent pom
290+
// Resolve the placeholder in the parent POM
288291
result = getParent*().(Pom).resolvePlaceholder(s.substring(2, s.length() - 1))
289292
else result = s
290293
)
@@ -330,7 +333,7 @@ class Dependencies extends PomElement {
330333
Dependency getADependency() { result = this.getAChild() }
331334
}
332335

333-
/** An XML element named `dependencyManagement`, as found in Maven POM XML files. */
336+
/** An XML element named "dependencyManagement", as found in Maven POM XML files. */
334337
class DependencyManagement extends PomElement {
335338
DependencyManagement() { getName() = "dependencyManagement" }
336339

@@ -349,7 +352,7 @@ class DependencyManagement extends PomElement {
349352
}
350353

351354
/**
352-
* An XML element name "properties", as found in Maven POM XML files.
355+
* An XML element named "properties", as found in Maven POM XML files.
353356
*/
354357
class PomProperties extends PomElement {
355358
PomProperties() { this.getName() = "properties" }
@@ -366,8 +369,8 @@ class PomProperty extends PomElement {
366369
}
367370

368371
/**
369-
* A folder that represents a maven local repository using the standard layout. Any folder called
370-
* "repository" with a parent name ".m2" is considered to be a maven repository.
372+
* A folder that represents a local Maven repository using the standard layout. Any folder called
373+
* "repository" with a parent name ".m2" is considered to be a Maven repository.
371374
*/
372375
class MavenRepo extends Folder {
373376
MavenRepo() { getBaseName() = "repository" and getParentContainer().getBaseName() = ".m2" }
@@ -378,18 +381,18 @@ class MavenRepo extends Folder {
378381
File getAJarFile() { result = getAChildContainer*().(File) and result.getExtension() = "jar" }
379382

380383
/**
381-
* Gets any jar artifacts in this repository that match the pom project definition. This is an
382-
* over approximation. For soft qualifiers (e.g. 1.0) we return precise matches in preference to
383-
* artefact only matches. For hard qualifiers (e.g. [1.0]) we return only precise matches. For
384-
* all other qualifiers, we return all matches regardless of version.
384+
* Gets any jar artifacts in this repository that match the POM project definition. This is an
385+
* over approximation. For soft qualifiers (e.g. 1.0) precise matches are returned in preference
386+
* to artifact-only matches. For hard qualifiers (e.g. [1.0]) only precise matches are returned.
387+
* For all other qualifiers, all matches are returned regardless of version.
385388
*/
386389
MavenRepoJar getAnArtifact(ProtoPom pom) {
387390
result = getAJarFile() and
388391
if exists(MavenRepoJar mrj | mrj.preciseMatch(pom)) or versionHardMatch(pom)
389392
then
390393
// Either a hard match qualifier, or soft and there is at least one precise match
391394
result.preciseMatch(pom)
392-
else result.artefactMatches(pom)
395+
else result.artifactMatches(pom)
393396
}
394397
}
395398

@@ -401,16 +404,19 @@ private predicate versionHardMatch(ProtoPom pom) {
401404
}
402405

403406
/**
404-
* A jar file inside a maven repository.
407+
* A jar file inside a Maven repository.
405408
*
406409
* See: https://cwiki.apache.org/confluence/display/MAVENOLD/Repository+Layout+-+Final
407410
*/
408411
class MavenRepoJar extends File {
409412
MavenRepoJar() { exists(MavenRepo mr | mr.getAJarFile() = this) }
410413

411-
string getGroupID() {
414+
/**
415+
* Gets the `groupId` of this jar.
416+
*/
417+
string getGroupId() {
412418
exists(MavenRepo mr | mr.getAJarFile() = this |
413-
// Assuming the standard layout, the first part of the directory structure from the maven
419+
// Assuming the standard layout, the first part of the directory structure from the Maven
414420
// repository will be the groupId converted to a path by replacing "." with "/".
415421
result =
416422
getParentContainer()
@@ -422,24 +428,30 @@ class MavenRepoJar extends File {
422428
)
423429
}
424430

425-
string getArtefactID() { result = getParentContainer().getParentContainer().getBaseName() }
431+
/**
432+
* Gets the `artifactId` of this jar.
433+
*/
434+
string getArtifactId() { result = getParentContainer().getParentContainer().getBaseName() }
426435

436+
/**
437+
* Gets the artifact version string of this jar.
438+
*/
427439
string getVersion() { result = getParentContainer().getBaseName() }
428440

429441
/**
430-
* Holds if this jar is an artefact for the given pom or dependency, regardless of which version it is.
442+
* Holds if this jar is an artifact for the given POM or dependency, regardless of which version it is.
431443
*/
432-
predicate artefactMatches(ProtoPom pom) {
433-
pom.getGroup().getValue() = getGroupID() and
434-
pom.getArtifact().getValue() = getArtefactID()
444+
predicate artifactMatches(ProtoPom pom) {
445+
pom.getGroup().getValue() = getGroupId() and
446+
pom.getArtifact().getValue() = getArtifactId()
435447
}
436448

437449
/**
438-
* Holds if this jar is both an artefact for the pom, and has a version string that matches the pom
450+
* Holds if this jar is both an artifact for the POM, and has a version string that matches the POM
439451
* version string. Only soft and hard version matches are supported.
440452
*/
441453
predicate preciseMatch(ProtoPom pom) {
442-
artefactMatches(pom) and
454+
artifactMatches(pom) and
443455
if versionHardMatch(pom)
444456
then ("[" + getVersion() + "]").matches(pom.getVersionString() + "%")
445457
else getVersion().matches(pom.getVersionString() + "%")

0 commit comments

Comments
 (0)