1 19 20 package edu.umd.cs.findbugs; 21 22 import java.util.regex.Matcher ; 23 import java.util.regex.Pattern ; 24 25 28 public class JavaVersion { 29 private static final Pattern PATTERN = Pattern.compile("^(\\d+)\\.(\\d+)(\\..*)?$"); 30 31 private final int major; 32 private final int minor; 33 private final String rest; 34 35 38 private static JavaVersion runtimeVersion; 39 40 static { 41 try { 42 runtimeVersion = new JavaVersion(SystemProperties.getProperty("java.version")); 43 } catch (JavaVersionException e) { 44 System.err.println("Warning: Unknown version of Java"); 45 runtimeVersion = new JavaVersion(1, 0); 47 e.printStackTrace(); 48 } 49 } 50 51 54 public static final JavaVersion JAVA_1_5 = new JavaVersion(1, 5); 55 56 63 public JavaVersion(String versionString) throws JavaVersionException { 64 Matcher matcher = PATTERN.matcher(versionString); 65 if (!matcher.matches()) 66 throw new JavaVersionException("Could not parse Java version string: " + versionString); 67 try { 68 major = Integer.parseInt(matcher.group(1)); 69 minor = Integer.parseInt(matcher.group(2)); 70 if (matcher.group(3) != null) 71 rest = matcher.group(3); 72 else 73 rest = ""; 74 } catch (NumberFormatException e) { 75 throw new JavaVersionException("Could not parse Java Version string: " + versionString, e); 76 } 77 } 78 79 85 public JavaVersion(int major, int minor) { 86 this.major = major; 87 this.minor = minor; 88 this.rest = ""; 89 } 90 91 94 public int getMajor() { 95 return major; 96 } 97 98 101 public int getMinor() { 102 return minor; 103 } 104 105 108 public String getRest() { 109 return rest; 110 } 111 112 @Override 113 public String toString() { 114 StringBuffer buf = new StringBuffer (); 115 buf.append(major); 116 buf.append('.'); 117 buf.append(minor); 118 if (rest != null) { 119 buf.append(rest); 120 } 121 122 return buf.toString(); 123 } 124 125 129 public static JavaVersion getRuntimeVersion() { 130 return runtimeVersion; 131 } 132 133 141 public boolean isSameOrNewerThan(JavaVersion other) { 142 return this.major > other.major || 143 (this.major == other.major && this.minor >= other.minor); 144 } 145 } 146 147 | Popular Tags |