Skip to content

Commit 8985a0f

Browse files
authored
Improve Version implementation, Use rewrite's like version compare (#1918)
* Improve `Version` implementation, Use rewrite's like version compare Signed-off-by: BoykoAlex <alex.boyko@broadcom.com> * Version compare tests Signed-off-by: BoykoAlex <alex.boyko@broadcom.com> * Cleanup Version implementation Signed-off-by: BoykoAlex <alex.boyko@broadcom.com> --------- Signed-off-by: BoykoAlex <alex.boyko@broadcom.com>
1 parent 13cf698 commit 8985a0f

22 files changed

Lines changed: 744 additions & 299 deletions

File tree

headless-services/commons/commons-java/src/main/java/org/springframework/ide/vscode/commons/java/SpringProjectUtil.java

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ public class SpringProjectUtil {
3232

3333
public static final String SPRING_BOOT = "spring-boot";
3434
public static final String SPRING_WEB = "spring-web";
35-
35+
3636
private static final String GENERATION_VERSION_STR = "([0-9]+)";
3737

3838
public static final Logger log = LoggerFactory.getLogger(SpringProjectUtil.class);
39-
39+
4040
private static final Pattern GENERATION_VERSION = Pattern.compile(GENERATION_VERSION_STR);
41-
41+
4242
public static boolean isSpringProject(IJavaProject jp) {
4343
return jp.getClasspath().findBinaryLibraryByPrefix("spring-core").isPresent();
4444
}
@@ -50,50 +50,43 @@ public static boolean isBootProject(IJavaProject jp) {
5050
public static boolean hasBootActuators(IJavaProject jp) {
5151
return jp.getClasspath().findBinaryLibraryByPrefix("spring-boot-actuator-").isPresent();
5252
}
53-
53+
5454
/**
55-
* Parses version from the given generation name (e.g. "2.1.x"
56-
* @param name
57-
* @return Version if valid generation name with major and minor components
58-
* @throws Exception if invalid generation name
55+
* Parses a {@link Version} from the given generation name (e.g. {@code "2.1.x"} → {@code 2.1.0}).
56+
*
57+
* @param name generation name containing at least major and minor numeric components
58+
* @return parsed {@link Version}
59+
* @throws IllegalArgumentException if the name does not contain major and minor components
5960
*/
6061
public static Version getVersionFromGeneration(String name) throws Exception {
6162
Matcher matcher = GENERATION_VERSION.matcher(name);
6263
String major = null;
6364
String minor = null;
64-
65+
6566
if (matcher.find()) {
66-
int start = matcher.start();
67-
int end = matcher.end();
68-
major = name.substring(start, end);
67+
major = name.substring(matcher.start(), matcher.end());
6968
}
70-
7169
if (matcher.find()) {
72-
int start = matcher.start();
73-
int end = matcher.end();
74-
minor = name.substring(start, end);
70+
minor = name.substring(matcher.start(), matcher.end());
7571
}
76-
72+
7773
if (major != null && minor != null) {
78-
return new Version(
79-
Integer.parseInt(major),
80-
Integer.parseInt(minor),
81-
0,
82-
null
83-
);
74+
return Version.parse("%s.%s.0".formatted(major, minor));
8475
}
8576

8677
throw new IllegalArgumentException("Invalid semver. Unable to parse major and minor version from: " + name);
8778
}
8879

8980
public static Version getDependencyVersionByPrefix(IJavaProject jp, String dependencyPrefix) {
90-
return jp.getClasspath().findBinaryLibraryByPrefix(dependencyPrefix).map(cpe -> cpe.getVersion()).orElse(null);
81+
return jp.getClasspath().findBinaryLibraryByPrefix(dependencyPrefix)
82+
.map(cpe -> Version.parse(cpe.getVersion())).orElse(null);
9183
}
92-
93-
public static Version getDependencyVersionByName(IJavaProject jp, String name) {
94-
return jp.getClasspath().findBinaryLibraryByName(name).map(cpe -> cpe.getVersion()).orElse(null);
84+
85+
public static Version getDependencyVersionByName(IJavaProject jp, String name) {
86+
return jp.getClasspath().findBinaryLibraryByName(name)
87+
.map(cpe -> Version.parse(cpe.getVersion())).orElse(null);
9588
}
96-
89+
9790
public static boolean hasDependencyStartingWith(IJavaProject jp, String dependency, Predicate<CPE> filter) {
9891
IClasspath classpath = jp.getClasspath();
9992
return classpath.findBinaryLibraryByPrefix(dependency).or(() -> {
@@ -123,37 +116,23 @@ public static boolean hasDependencyStartingWith(IJavaProject jp, String dependen
123116
public static Version getSpringBootVersion(IJavaProject jp) {
124117
return getDependencyVersionByName(jp, SPRING_BOOT);
125118
}
126-
119+
127120
public static Predicate<IJavaProject> springBootVersionGreaterOrEqual(int major, int minor, int patch) {
128121
return libraryVersionGreaterOrEqual(SPRING_BOOT, major, minor, patch);
129122
}
130123

131124
public static Predicate<IJavaProject> libraryVersionGreaterOrEqual(String libraryName, int major, int minor, int patch) {
132125
return project -> {
133126
Version version = getDependencyVersionByName(project, libraryName);
134-
if (version == null) {
135-
return false;
136-
}
137-
if (major > version.getMajor()) {
138-
return false;
139-
}
140-
if (major == version.getMajor()) {
141-
if (minor > version.getMinor()) {
142-
return false;
143-
}
144-
if (minor == version.getMinor()) {
145-
return patch <= version.getPatch();
146-
}
147-
}
148-
return true;
127+
return version != null && version.compareTo(Version.parse("%d.%d.%d".formatted(major, minor, patch))) >= 0;
149128
};
150129
}
151-
130+
152131
/**
153132
* Finds Spring Boot application.properties and application.yml files in the project's resource folders.
154133
* Prioritizes main application.properties/yml files over profile-specific files (application-*.properties/yml).
155134
* Excludes test resources.
156-
*
135+
*
157136
* @param project the Java project
158137
* @return list of Boot properties/YAML file paths, with main files prioritized over profile-specific files
159138
*/
@@ -177,8 +156,6 @@ public static List<Path> findBootPropertiesFiles(IJavaProject project) {
177156
// ignore
178157
return Collections.emptyList();
179158
}
180-
181159
}
182-
183160

184161
}

0 commit comments

Comments
 (0)