001/** 002 * Copyright (C) 2006-2025 Talend Inc. - www.talend.com 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.talend.sdk.component.runtime.reflect; 017 018public class JavaVersion { 019 020 /** 021 * Retrieve major version 022 * 023 * @param version 024 * @return major version 025 */ 026 public static String getMajor(final String version) { 027 final String[] versionElements = version.split("\\."); 028 try { 029 final String unsureVersion = strip(versionElements[0]); 030 return isOldNotation(strip(unsureVersion)) ? strip(versionElements[1]) : unsureVersion; 031 } catch (ArrayIndexOutOfBoundsException e) { 032 return "-1"; 033 } 034 } 035 036 public static int getMajorNumber(final String version) { 037 try { 038 return Integer.parseInt(getMajor(version)); 039 } catch (NumberFormatException e) { 040 return -1; 041 } 042 } 043 044 public static int major() { 045 return getMajorNumber(System.getProperty("java.version")); 046 } 047 048 /** 049 * Retrieve minor version 050 * <p> 051 * Minor may contain chars like _+- 052 * 053 * @param version 054 * @return minor version 055 */ 056 public static String getMinor(final String version) { 057 final String[] versionElements = version.split("\\."); 058 try { 059 // case for early access previews 060 if (versionElements.length == 1) { 061 return versionElements[0].indexOf('-') > 0 062 ? versionElements[0].substring(versionElements[0].indexOf('-') + 1) 063 : "-1"; 064 } else { 065 final String unsureVersion = strip(versionElements[0]); 066 return isOldNotation(unsureVersion) ? extractFromOldNotation(versionElements[2], true) 067 : versionElements[1]; 068 } 069 } catch (ArrayIndexOutOfBoundsException e) { 070 return "-1"; 071 } 072 } 073 074 public static int getMinorNumber(final String version) { 075 try { 076 return Integer.parseInt(strip(getMinor(version))); 077 } catch (NumberFormatException e) { 078 return -1; 079 } 080 } 081 082 public static int minor() { 083 return getMinorNumber(System.getProperty("java.version")); 084 } 085 086 /** 087 * Retrieve revision 088 * <p> 089 * Revision may contain chars like _+- 090 * 091 * @param version 092 * @return revision 093 */ 094 public static String getRevision(final String version) { 095 final String[] versionElements = version.split("\\."); 096 try { 097 final String unsureVersion = strip(versionElements[0]); 098 return isOldNotation(unsureVersion) ? extractFromOldNotation(versionElements[2], false) 099 : versionElements[2]; 100 } catch (ArrayIndexOutOfBoundsException e) { 101 return "-1"; 102 } 103 } 104 105 public static int getRevisionNumber(final String version) { 106 try { 107 return Integer.parseInt(strip(getRevision(version))); 108 } catch (NumberFormatException e) { 109 return -1; 110 } 111 } 112 113 public static int revision() { 114 return getRevisionNumber(System.getProperty("java.version")); 115 } 116 117 private static String strip(final String segment) { 118 return segment.replaceAll("[-+_].*", ""); 119 } 120 121 private static boolean isOldNotation(final String segment) { 122 return "1".equals(segment); 123 } 124 125 private static String extractFromOldNotation(final String segment, final boolean isMinor) { 126 return isMinor ? segment.substring(0, segment.indexOf('_')) : segment.substring(segment.indexOf('_') + 1); 127 } 128 129}