KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > JavaVersion


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2004-2005 University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs;
21
22 import java.util.regex.Matcher JavaDoc;
23 import java.util.regex.Pattern JavaDoc;
24
25 /**
26  * Support for finding out what version of Java we're running on.
27  */

28 public class JavaVersion {
29     private static final Pattern JavaDoc PATTERN = Pattern.compile("^(\\d+)\\.(\\d+)(\\..*)?$");
30
31     private final int major;
32     private final int minor;
33     private final String JavaDoc rest;
34
35     /**
36      * Constant for the Java version we're currently running on.
37      */

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             // Assume Java 1.0.
46
runtimeVersion = new JavaVersion(1, 0);
47             e.printStackTrace();
48         }
49     }
50
51     /**
52      * Constant for Java 1.5 (Tiger).
53      */

54     public static final JavaVersion JAVA_1_5 = new JavaVersion(1, 5);
55
56     /**
57      * Constructor.
58      *
59      * @param versionString a version string, as returned from the
60      * <code>java.version</code> system property:
61      * e.g., "1.4.2_04"
62      */

63     public JavaVersion(String JavaDoc versionString) throws JavaVersionException {
64         Matcher JavaDoc 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 JavaDoc e) {
75             throw new JavaVersionException("Could not parse Java Version string: " + versionString, e);
76         }
77     }
78
79     /**
80      * Constructor.
81      *
82      * @param major major version
83      * @param minor minor version
84      */

85     public JavaVersion(int major, int minor) {
86         this.major = major;
87         this.minor = minor;
88         this.rest = "";
89     }
90
91     /**
92      * Get the major version number.
93      */

94     public int getMajor() {
95         return major;
96     }
97
98     /**
99      * Get the minor version number.
100      */

101     public int getMinor() {
102         return minor;
103     }
104
105     /**
106      * Get the rest of the version string after the major and minor numbers.
107      */

108     public String JavaDoc getRest() {
109         return rest;
110     }
111     
112     @Override JavaDoc
113     public String JavaDoc toString() {
114         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
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     /**
126      * Get the version of Java that we are currently
127      * running under.
128      */

129     public static JavaVersion getRuntimeVersion() {
130         return runtimeVersion;
131     }
132
133     /**
134      * Return whether the Java version represented by this
135      * object is at least as recent as the one given.
136      *
137      * @param other another JavaVersion
138      * @return true if this Java version is at least as recent as
139      * the one given
140      */

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 // vim:ts=3
148
Popular Tags