KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > platform > Java


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util.platform;
23
24
25 /**
26  * Provides common access to specifics about the version of <em>Java</em>
27  * that a virtual machine supports.
28  *
29  * <p>Determines the version of the <em>Java Virtual Machine</em> by checking
30  * for the availablity of version specific classes.<p>
31  *
32  * <p>Classes are loaded in the following order:
33  * <ol>
34  * <li><tt>java.lang.Enum</tt> was introduced in JDK 1.5</li>
35  * <li><tt>java.lang.StackTraceElement</tt> was introduced in JDK 1.4</li>
36  * <li><tt>java.lang.StrictMath</tt> was introduced in JDK 1.3</li>
37  * <li><tt>java.lang.ThreadLocal</tt> was introduced in JDK 1.2</li>
38  * <li><tt>java.lang.Void</tt> was introduced in JDK 1.1</li>
39  * </ol>
40  * </p>
41  *
42  * @version <tt>$Revision: 1958 $</tt>
43  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
44  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
45  */

46 public final class Java
47 {
48    /** Prevent instantiation */
49    private Java() {}
50
51    /** Java version 1.0 token */
52    public static final int VERSION_1_0 = 0x01;
53
54    /** Java version 1.1 token */
55    public static final int VERSION_1_1 = 0x02;
56
57    /** Java version 1.2 token */
58    public static final int VERSION_1_2 = 0x03;
59
60    /** Java version 1.3 token */
61    public static final int VERSION_1_3 = 0x04;
62
63    /** Java version 1.4 token */
64    public static final int VERSION_1_4 = 0x05;
65    
66    /** Java version 1.5 token */
67    public static final int VERSION_1_5 = 0x06;
68    
69    /**
70     * Private to avoid over optimization by the compiler.
71     *
72     * @see #getVersion() Use this method to access this final value.
73     */

74    private static final int VERSION;
75
76    /** Initialize VERSION. */
77    static
78    {
79       // default to 1.0
80
int version = VERSION_1_0;
81
82       try
83       {
84          // check for 1.1
85
Class.forName("java.lang.Void");
86          version = VERSION_1_1;
87
88          // check for 1.2
89
Class.forName("java.lang.ThreadLocal");
90          version = VERSION_1_2;
91
92          // check for 1.3
93
Class.forName("java.lang.StrictMath");
94          version = VERSION_1_3;
95
96          // check for 1.4
97
Class.forName("java.lang.StackTraceElement");
98          version = VERSION_1_4;
99          
100          // check for 1.5
101
Class.forName("java.lang.Enum");
102          version = VERSION_1_5;
103       }
104       catch (ClassNotFoundException JavaDoc ignore)
105       {
106       }
107       VERSION = version;
108    }
109
110    /**
111     * Return the version of <em>Java</em> supported by the VM.
112     *
113     * @return The version of <em>Java</em> supported by the VM.
114     */

115    public static int getVersion()
116    {
117       return VERSION;
118    }
119
120    /**
121     * Returns true if the given version identifer is equal to the
122     * version identifier of the current virtuial machine.
123     *
124     * @param version The version identifier to check for.
125     * @return True if the current virtual machine is the same version.
126     */

127    public static boolean isVersion(final int version)
128    {
129       return VERSION == version;
130    }
131
132    /**
133     * Returns true if the current virtual machine is compatible with
134     * the given version identifer.
135     *
136     * @param version The version identifier to check compatibility of.
137     * @return True if the current virtual machine is compatible.
138     */

139    public static boolean isCompatible(final int version)
140    {
141       // if our vm is the same or newer then we are compatible
142
return VERSION >= version;
143    }
144 }
145
Popular Tags