Skip to content

Commit af684ea

Browse files
authored
Merge pull request #895 from eclipse-mylyn/894-coreutilgetruntimeversion-fails-to-parse-java-version-properly
CoreUtil.getRuntimeVersion() fails to parse Java version properly #894
2 parents 2cd52f2 + 1764d67 commit af684ea

File tree

2 files changed

+31
-49
lines changed
  • mylyn.commons
    • org.eclipse.mylyn.commons.core/src/org/eclipse/mylyn/commons/core
    • org.eclipse.mylyn.commons.tests/src/org/eclipse/mylyn/commons/tests/core

2 files changed

+31
-49
lines changed

mylyn.commons/org.eclipse.mylyn.commons.core/src/org/eclipse/mylyn/commons/core/CoreUtil.java

Lines changed: 16 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -170,61 +170,33 @@ private static void defaultSystemProperty(String key, String defaultValue) {
170170
* Returns the version of the Java runtime.
171171
*
172172
* @since 3.7
173-
* @return {@link Version#emptyVersion} if the version can not be determined
173+
* @return Version
174174
*/
175175
public static Version getRuntimeVersion() {
176-
Version result = parseRuntimeVersion(Runtime.version().toString());
177-
return result;
176+
return convertToOsgiVersion(Runtime.version());
178177
}
179178

180179
/**
181180
* @since 4.3
181+
* @return {@link Version#emptyVersion} if the version can not be determined
182182
*/
183-
public static Version parseRuntimeVersion(String versionString) {
184-
if (versionString != null) {
185-
int firstSeparator = versionString.indexOf('.');
186-
if (firstSeparator != -1) {
187-
try {
188-
int secondSeparator = versionString.indexOf('.', firstSeparator + 1);
189-
if (secondSeparator != -1) {
190-
int index = findLastNumberIndex(versionString, secondSeparator);
191-
String qualifier = versionString.substring(index + 1);
192-
if ((qualifier.startsWith("_") || qualifier.startsWith("+")) && qualifier.length() > 1) { //$NON-NLS-1$
193-
versionString = versionString.substring(0, index + 1) + "." + qualifier.substring(1); //$NON-NLS-1$
194-
} else {
195-
versionString = versionString.substring(0, index + 1);
196-
}
197-
return new Version(versionString);
198-
}
199-
return new Version(
200-
versionString.substring(0, findLastNumberIndex(versionString, firstSeparator) + 1));
201-
} catch (IllegalArgumentException e) {
202-
// ignore
203-
}
204-
} else { // Java 22 seems to have changed the format for some reason to "nn+nn"
205-
int plusSeparator = versionString.indexOf('+');
206-
if (plusSeparator != -1) {
207-
return new Version(
208-
versionString.substring(0, plusSeparator));
209-
}
210-
}
183+
public static Version parseRuntimeVersion(String version) {
184+
try {
185+
return convertToOsgiVersion(java.lang.Runtime.Version.parse(version));
186+
} catch (IllegalArgumentException e) {
187+
return Version.emptyVersion;
211188
}
212-
return Version.emptyVersion;
213189
}
214190

215-
private static int findLastNumberIndex(String versionString, int secondSeparator) {
216-
int lastDigit = secondSeparator;
217-
for (int i = secondSeparator + 1; i < versionString.length(); i++) {
218-
if (Character.isDigit(versionString.charAt(i))) {
219-
lastDigit++;
220-
} else {
221-
break;
222-
}
223-
}
224-
if (lastDigit == secondSeparator) {
225-
return secondSeparator - 1;
191+
private static Version convertToOsgiVersion(java.lang.Runtime.Version version) {
192+
if (version != null) {
193+
int feature = version.feature();
194+
int interim = version.interim();
195+
int update = version.update();
196+
String pre = version.pre().orElse(""); //$NON-NLS-1$
197+
return new Version(feature, interim, update, pre);
226198
}
227-
return lastDigit;
199+
return Version.emptyVersion;
228200
}
229201

230202
/**

mylyn.commons/org.eclipse.mylyn.commons.tests/src/org/eclipse/mylyn/commons/tests/core/CoreUtilTest.java

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,10 @@ public void testAreEqualUnequalStrings() {
135135
assertFalse(CoreUtil.areEqual("a", "b"));
136136
}
137137

138+
138139
@Test
139140
public void testGetRuntimeVersion() {
140-
assertEquals(new Version(1, 5, 0, "2"), CoreUtil.parseRuntimeVersion("1.5.0_2"));
141+
assertEquals(new Version(1, 5, 0), CoreUtil.parseRuntimeVersion("1.5.0.2"));
141142
}
142143

143144
@Test
@@ -147,24 +148,33 @@ public void testGetRuntimeVersionShort() {
147148

148149
@Test
149150
public void testGetRuntimeVersionLetters() {
150-
assertEquals(new Version(1, 7, 0), CoreUtil.parseRuntimeVersion("1.7-CUSTOM"));
151+
assertEquals(new Version(1, 7, 0, "CUSTOM"), CoreUtil.parseRuntimeVersion("1.7-CUSTOM"));
151152
}
152153

153154
@Test
154155
public void testGetRuntimeVersionTrailingUnderscore() {
155-
assertEquals(new Version(1, 5, 0), CoreUtil.parseRuntimeVersion("1.5.0_"));
156+
assertEquals(new Version(1, 5, 0, "A"), CoreUtil.parseRuntimeVersion("1.5.0.1-A"));
157+
}
158+
159+
@Test
160+
public void testGetRuntimeVersionQualifier() {
161+
assertEquals(new Version(1, 2, 1), CoreUtil.parseRuntimeVersion("1.2.1"));
156162
}
157163

158164
@Test
159-
public void testGetRuntimeVersionNoQualifier() {
160-
assertEquals(new Version(1, 2, 0), CoreUtil.parseRuntimeVersion("1.2.0"));
165+
public void testGetRuntimeVersionJava21Ubuntu() {
166+
assertEquals(new Version(21, 0, 8), CoreUtil.parseRuntimeVersion("21.0.8+9-Ubuntu-0ubuntu124.04.1"));
161167
}
162168

163169
@Test
164170
public void testGetRuntimeVersionJava22() {
165171
assertEquals(new Version(22, 0, 0), CoreUtil.parseRuntimeVersion("22+38"));
166172
}
167173

174+
@Test
175+
public void testGetBadRuntimeVersion() {
176+
assertEquals(new Version(0, 0, 0), CoreUtil.parseRuntimeVersion("21.0.A"));
177+
}
168178

169179
@Test
170180
public void testAsFileName() {

0 commit comments

Comments
 (0)