Skip to content

Commit 2efe05f

Browse files
Extract Pattern.compile to static variable
The same pattern can be compiled once.
1 parent 8321211 commit 2efe05f

File tree

2 files changed

+63
-48
lines changed

2 files changed

+63
-48
lines changed

versions-common/src/main/java/org/codehaus/mojo/versions/api/PomHelper.java

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@
7676
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluationException;
7777
import org.codehaus.plexus.component.configurator.expression.ExpressionEvaluator;
7878
import org.codehaus.plexus.util.IOUtil;
79-
import org.codehaus.plexus.util.ReaderFactory;
8079
import org.codehaus.plexus.util.StringUtils;
80+
import org.codehaus.plexus.util.xml.XmlStreamReader;
8181
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
8282
import org.codehaus.stax2.XMLInputFactory2;
8383

@@ -90,9 +90,39 @@
9090
* @author Stephen Connolly
9191
* @since 1.0-alpha-3
9292
*/
93-
public class PomHelper {
93+
public final class PomHelper {
9494
public static final String APACHE_MAVEN_PLUGINS_GROUPID = "org.apache.maven.plugins";
9595

96+
public static final Pattern PATTERN_PROJECT_PROPERTIES = Pattern.compile("/project/properties");
97+
98+
public static final Pattern PATTERN_PROJECT_PROFILE = Pattern.compile("/project/profiles/profile");
99+
100+
public static final Pattern PATTERN_PROJECT_PROFILE_ID = Pattern.compile("/project/profiles/profile/id");
101+
102+
public static final Pattern PATTERN_PROJECT_VERSION = Pattern.compile("/project/version");
103+
104+
public static final Pattern PATTERN_PROJECT_PARENT_VERSION = Pattern.compile("/project/parent/version");
105+
106+
public static final Pattern PATTERN_PROJECT_DEPENDENCY = Pattern.compile("/project" + "(/profiles/profile)?"
107+
+ "((/dependencyManagement)|(/build(/pluginManagement)?/plugins/plugin))?"
108+
+ "/dependencies/dependency");
109+
110+
public static final Pattern PATTERN_PROJECT_DEPENDENCY_VERSION = Pattern.compile("/project" + "(/profiles/profile)?"
111+
+ "((/dependencyManagement)|(/build(/pluginManagement)?/plugins/plugin))?"
112+
+ "/dependencies/dependency"
113+
+ "((/groupId)|(/artifactId)|(/version))");
114+
115+
public static final Pattern PATTERN_PROJECT_PLUGIN = Pattern.compile(
116+
"/project" + "(/profiles/profile)?" + "((/build(/pluginManagement)?)|(/reporting))/plugins/plugin");
117+
118+
public static final Pattern PATTERN_PROJECT_PLUGIN_VERSION = Pattern.compile("/project" + "(/profiles/profile)?"
119+
+ "((/build(/pluginManagement)?)|(/reporting))/plugins/plugin"
120+
+ "((/groupId)|(/artifactId)|(/version))");
121+
122+
private PomHelper() {
123+
// utility class
124+
}
125+
96126
/**
97127
* Gets the raw model before any interpolation what-so-ever.
98128
*
@@ -173,12 +203,12 @@ public static boolean setPropertyVersion(
173203
boolean madeReplacement = false;
174204
if (profileId == null) {
175205
propertyRegex = Pattern.compile("/project/properties/" + RegexUtils.quote(property));
176-
matchScopeRegex = Pattern.compile("/project/properties");
206+
matchScopeRegex = PATTERN_PROJECT_PROPERTIES;
177207
projectProfileId = null;
178208
} else {
179209
propertyRegex = Pattern.compile("/project/profiles/profile/properties/" + RegexUtils.quote(property));
180-
matchScopeRegex = Pattern.compile("/project/profiles/profile");
181-
projectProfileId = Pattern.compile("/project/profiles/profile/id");
210+
matchScopeRegex = PATTERN_PROJECT_PROFILE;
211+
projectProfileId = PATTERN_PROJECT_PROFILE_ID;
182212
}
183213

184214
pom.rewind();
@@ -362,7 +392,6 @@ private void replaceValueInParent() {
362392
public static String getProjectVersion(final ModifiedPomXMLEventReader pom) throws XMLStreamException {
363393
Stack<String> stack = new Stack<>();
364394
String path = "";
365-
final Pattern matchScopeRegex = Pattern.compile("/project/version");
366395

367396
pom.rewind();
368397

@@ -372,12 +401,12 @@ public static String getProjectVersion(final ModifiedPomXMLEventReader pom) thro
372401
stack.push(path);
373402
path = path + "/" + event.asStartElement().getName().getLocalPart();
374403

375-
if (matchScopeRegex.matcher(path).matches()) {
404+
if (PATTERN_PROJECT_VERSION.matcher(path).matches()) {
376405
pom.mark(0);
377406
}
378407
}
379408
if (event.isEndElement()) {
380-
if (matchScopeRegex.matcher(path).matches()) {
409+
if (PATTERN_PROJECT_VERSION.matcher(path).matches()) {
381410
pom.mark(1);
382411
if (pom.hasMark(0) && pom.hasMark(1)) {
383412
return pom.getBetween(0, 1).trim();
@@ -403,9 +432,7 @@ public static boolean setProjectParentVersion(final ModifiedPomXMLEventReader po
403432
throws XMLStreamException {
404433
Stack<String> stack = new Stack<>();
405434
String path = "";
406-
final Pattern matchScopeRegex;
407435
boolean madeReplacement = false;
408-
matchScopeRegex = Pattern.compile("/project/parent/version");
409436

410437
pom.rewind();
411438

@@ -415,12 +442,12 @@ public static boolean setProjectParentVersion(final ModifiedPomXMLEventReader po
415442
stack.push(path);
416443
path = path + "/" + event.asStartElement().getName().getLocalPart();
417444

418-
if (matchScopeRegex.matcher(path).matches()) {
445+
if (PATTERN_PROJECT_PARENT_VERSION.matcher(path).matches()) {
419446
pom.mark(0);
420447
}
421448
}
422449
if (event.isEndElement()) {
423-
if (matchScopeRegex.matcher(path).matches()) {
450+
if (PATTERN_PROJECT_PARENT_VERSION.matcher(path).matches()) {
424451
pom.mark(1);
425452
if (pom.hasMark(0) && pom.hasMark(1)) {
426453
pom.replaceBetween(0, 1, value);
@@ -514,15 +541,6 @@ public static boolean setDependencyVersion(
514541
boolean haveArtifactId = false;
515542
boolean haveOldVersion = false;
516543

517-
final Pattern matchScopeRegex = Pattern.compile("/project" + "(/profiles/profile)?"
518-
+ "((/dependencyManagement)|(/build(/pluginManagement)?/plugins/plugin))?"
519-
+ "/dependencies/dependency");
520-
521-
final Pattern matchTargetRegex = Pattern.compile("/project" + "(/profiles/profile)?"
522-
+ "((/dependencyManagement)|(/build(/pluginManagement)?/plugins/plugin))?"
523-
+ "/dependencies/dependency"
524-
+ "((/groupId)|(/artifactId)|(/version))");
525-
526544
pom.rewind();
527545

528546
while (pom.hasNext()) {
@@ -532,7 +550,7 @@ public static boolean setDependencyVersion(
532550
final String elementName = event.asStartElement().getName().getLocalPart();
533551
path = path + "/" + elementName;
534552

535-
if (matchScopeRegex.matcher(path).matches()) {
553+
if (PATTERN_PROJECT_DEPENDENCY.matcher(path).matches()) {
536554
// we're in a new match scope
537555
// reset any previous partial matches
538556
inMatchScope = true;
@@ -542,7 +560,8 @@ public static boolean setDependencyVersion(
542560
haveGroupId = false;
543561
haveArtifactId = false;
544562
haveOldVersion = false;
545-
} else if (inMatchScope && matchTargetRegex.matcher(path).matches()) {
563+
} else if (inMatchScope
564+
&& PATTERN_PROJECT_DEPENDENCY_VERSION.matcher(path).matches()) {
546565
if ("groupId".equals(elementName)) {
547566
haveGroupId =
548567
groupId.equals(evaluate(pom.getElementText().trim(), implicitProperties));
@@ -557,7 +576,7 @@ public static boolean setDependencyVersion(
557576
}
558577
}
559578
if (event.isEndElement()) {
560-
if (matchTargetRegex.matcher(path).matches()
579+
if (PATTERN_PROJECT_DEPENDENCY_VERSION.matcher(path).matches()
561580
&& "version".equals(event.asEndElement().getName().getLocalPart())) {
562581
pom.mark(1);
563582
String compressedPomVersion =
@@ -570,7 +589,7 @@ public static boolean setDependencyVersion(
570589
// fall back to string comparison
571590
haveOldVersion = compressedOldVersion.equals(compressedPomVersion);
572591
}
573-
} else if (matchScopeRegex.matcher(path).matches()) {
592+
} else if (PATTERN_PROJECT_DEPENDENCY.matcher(path).matches()) {
574593
if (inMatchScope
575594
&& pom.hasMark(0)
576595
&& pom.hasMark(1)
@@ -720,22 +739,13 @@ public static boolean setPluginVersion(
720739
throws XMLStreamException {
721740
Stack<String> stack = new Stack<>();
722741
String path = "";
723-
final Pattern matchScopeRegex;
724-
final Pattern matchTargetRegex;
725742
boolean inMatchScope = false;
726743
boolean madeReplacement = false;
727744
boolean haveGroupId = false;
728745
boolean needGroupId = groupId != null && !APACHE_MAVEN_PLUGINS_GROUPID.equals(groupId);
729746
boolean haveArtifactId = false;
730747
boolean haveOldVersion = false;
731748

732-
matchScopeRegex = Pattern.compile(
733-
"/project" + "(/profiles/profile)?" + "((/build(/pluginManagement)?)|(/reporting))/plugins/plugin");
734-
735-
matchTargetRegex = Pattern.compile("/project" + "(/profiles/profile)?"
736-
+ "((/build(/pluginManagement)?)|(/reporting))/plugins/plugin"
737-
+ "((/groupId)|(/artifactId)|(/version))");
738-
739749
pom.rewind();
740750

741751
while (pom.hasNext()) {
@@ -745,7 +755,7 @@ public static boolean setPluginVersion(
745755
final String elementName = event.asStartElement().getName().getLocalPart();
746756
path = path + "/" + elementName;
747757

748-
if (matchScopeRegex.matcher(path).matches()) {
758+
if (PATTERN_PROJECT_PLUGIN.matcher(path).matches()) {
749759
// we're in a new match scope
750760
// reset any previous partial matches
751761
inMatchScope = true;
@@ -755,7 +765,8 @@ public static boolean setPluginVersion(
755765
haveGroupId = false;
756766
haveArtifactId = false;
757767
haveOldVersion = false;
758-
} else if (inMatchScope && matchTargetRegex.matcher(path).matches()) {
768+
} else if (inMatchScope
769+
&& PATTERN_PROJECT_PLUGIN_VERSION.matcher(path).matches()) {
759770
if ("groupId".equals(elementName)) {
760771
haveGroupId = pom.getElementText().trim().equals(groupId);
761772
path = stack.pop();
@@ -768,7 +779,7 @@ public static boolean setPluginVersion(
768779
}
769780
}
770781
if (event.isEndElement()) {
771-
if (matchTargetRegex.matcher(path).matches()
782+
if (PATTERN_PROJECT_PLUGIN_VERSION.matcher(path).matches()
772783
&& "version".equals(event.asEndElement().getName().getLocalPart())) {
773784
pom.mark(1);
774785

@@ -779,7 +790,7 @@ public static boolean setPluginVersion(
779790
// fall back to string comparison
780791
haveOldVersion = oldVersion.equals(pom.getBetween(0, 1).trim());
781792
}
782-
} else if (matchScopeRegex.matcher(path).matches()) {
793+
} else if (PATTERN_PROJECT_PLUGIN.matcher(path).matches()) {
783794
if (inMatchScope
784795
&& pom.hasMark(0)
785796
&& pom.hasMark(1)
@@ -1524,7 +1535,7 @@ public static int getReactorParentCount(Map<File, Model> reactor, Model model) {
15241535
* @throws java.io.IOException when things go wrong.
15251536
*/
15261537
public static StringBuilder readXmlFile(File outFile) throws IOException {
1527-
try (Reader reader = ReaderFactory.newXmlReader(outFile)) {
1538+
try (Reader reader = new XmlStreamReader(outFile)) {
15281539
return new StringBuilder(IOUtil.toString(reader));
15291540
}
15301541
}

versions-maven-plugin/src/main/java/org/codehaus/mojo/versions/DisplayPluginUpdatesMojo.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ public class DisplayPluginUpdatesMojo extends AbstractVersionsDisplayMojo {
125125
*/
126126
private static final String FROM_SUPER_POM = "(from super-pom) ";
127127

128+
public static final Pattern PATTERN_PROJECT_PLUGIN = Pattern.compile(
129+
"/project(/profiles/profile)?" + "((/build(/pluginManagement)?)|(/reporting))" + "/plugins/plugin");
130+
128131
/**
129132
* @since 1.0-alpha-1
130133
*/
@@ -201,9 +204,6 @@ private Map<String, String> getSuperPomPluginManagement() {
201204
StringBuilder buf = new StringBuilder(IOUtil.toString(reader));
202205
ModifiedPomXMLEventReader pom = newModifiedPomXER(buf, superPom.toString());
203206

204-
Pattern pathRegex = Pattern.compile("/project(/profiles/profile)?"
205-
+ "((/build(/pluginManagement)?)|(/reporting))"
206-
+ "/plugins/plugin");
207207
Stack<StackState> pathStack = new Stack<>();
208208
StackState curState = null;
209209
while (pom.hasNext()) {
@@ -215,7 +215,9 @@ private Map<String, String> getSuperPomPluginManagement() {
215215
if (curState != null) {
216216
String elementName =
217217
event.asStartElement().getName().getLocalPart();
218-
if (pathRegex.matcher(curState.path).matches()) {
218+
if (PATTERN_PROJECT_PLUGIN
219+
.matcher(curState.path)
220+
.matches()) {
219221
if ("groupId".equals(elementName)) {
220222
curState.groupId = pom.getElementText().trim();
221223
continue;
@@ -236,7 +238,9 @@ private Map<String, String> getSuperPomPluginManagement() {
236238
} else if (event.isEndElement()) {
237239
if (curState != null
238240
&& curState.artifactId != null
239-
&& pathRegex.matcher(curState.path).matches()) {
241+
&& PATTERN_PROJECT_PLUGIN
242+
.matcher(curState.path)
243+
.matches()) {
240244
result.putIfAbsent(
241245
Plugin.constructKey(
242246
curState.groupId == null
@@ -755,8 +759,6 @@ private Set<String> findPluginsWithVersionsSpecified(StringBuilder pomContents,
755759
Set<String> result = new HashSet<>();
756760
ModifiedPomXMLEventReader pom = newModifiedPomXER(pomContents, path);
757761

758-
Pattern pathRegex = Pattern.compile(
759-
"/project(/profiles/profile)?" + "((/build(/pluginManagement)?)|(/reporting))" + "/plugins/plugin");
760762
Stack<StackState> pathStack = new Stack<>();
761763
StackState curState = null;
762764
while (pom.hasNext()) {
@@ -766,7 +768,8 @@ private Set<String> findPluginsWithVersionsSpecified(StringBuilder pomContents,
766768
pathStack.clear();
767769
} else if (event.isStartElement()) {
768770
String elementName = event.asStartElement().getName().getLocalPart();
769-
if (curState != null && pathRegex.matcher(curState.path).matches()) {
771+
if (curState != null
772+
&& PATTERN_PROJECT_PLUGIN.matcher(curState.path).matches()) {
770773
if ("groupId".equals(elementName)) {
771774
curState.groupId = pom.getElementText().trim();
772775
continue;
@@ -783,7 +786,8 @@ private Set<String> findPluginsWithVersionsSpecified(StringBuilder pomContents,
783786
pathStack.push(curState);
784787
curState = new StackState(curState.path + "/" + elementName);
785788
} else if (event.isEndElement()) {
786-
if (curState != null && pathRegex.matcher(curState.path).matches()) {
789+
if (curState != null
790+
&& PATTERN_PROJECT_PLUGIN.matcher(curState.path).matches()) {
787791
if (curState.artifactId != null && curState.version != null) {
788792
if (curState.groupId == null) {
789793
curState.groupId = PomHelper.APACHE_MAVEN_PLUGINS_GROUPID;

0 commit comments

Comments
 (0)