test/tools/javac/versions/CheckClassFileVersion.java

Tue, 25 May 2010 15:54:51 -0700

author
ohair
date
Tue, 25 May 2010 15:54:51 -0700
changeset 554
9d9f26857129
parent 1
9a66ca7c79fa
child 2525
2eb010b6cb22
permissions
-rw-r--r--

6943119: Rebrand source copyright notices
Reviewed-by: darcy

     1 /*
     2  * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  */
    24 import java.io.*;
    25 import java.nio.*;
    26 import java.nio.channels.*;
    29 public class CheckClassFileVersion {
    31     static String get(String fn) throws IOException {
    32         ByteBuffer bb = ByteBuffer.allocate(1024);
    33         FileChannel fc = new FileInputStream(fn).getChannel();
    34         try {
    35             bb.clear();
    36             if (fc.read(bb) < 0)
    37                 throw new IOException("Could not read any bytes");
    38             bb.flip();
    39             int minor = bb.getShort(4);
    40             int major = bb.getShort(6);
    41             return major + "." + minor;
    42         } finally {
    43             fc.close();
    44         }
    45     }
    47     public static void main(String[] args) throws IOException {
    48         String cfv = get(args[0]);
    49         if (args.length == 1) {
    50             System.out.println(cfv);
    51             return;
    52         }
    53         if (!cfv.equals(args[1])) {
    54             System.err.println("Class-file version mismatch: Expected "
    55                                + args[1] + ", got " + cfv);
    56             System.exit(1);
    57         }
    58     }
    60 }

mercurial