langtools/test/tools/sjavac/Wrapper.java
author katleman
Thu, 21 Aug 2014 14:16:14 -0700
changeset 25878 6d561031123e
parent 25603 d5fa4eab2d26
child 32337 c9d3ab9f601c
permissions -rw-r--r--
Added tag jdk9-b27 for changeset 98ce0879ab4c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24067
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
     1
/*
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
     4
 *
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    10
 *
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    15
 * accompanied this code).
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    16
 *
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    20
 *
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    23
 * questions.
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    24
 */
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    25
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    26
import java.io.File;
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    27
import java.lang.reflect.Method;
25603
d5fa4eab2d26 8046824: class SJavacTestUtil and *Wrapper are redundant and should be removed
jjg
parents: 24067
diff changeset
    28
import java.util.Arrays;
24067
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    29
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    30
25603
d5fa4eab2d26 8046824: class SJavacTestUtil and *Wrapper are redundant and should be removed
jjg
parents: 24067
diff changeset
    31
public class Wrapper {
d5fa4eab2d26 8046824: class SJavacTestUtil and *Wrapper are redundant and should be removed
jjg
parents: 24067
diff changeset
    32
    public static void main(String... args) throws Exception {
24067
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    33
        if (!isSJavacOnClassPath()) {
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    34
            System.out.println("sjavac not available: pass by default");
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    35
            return;
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    36
        }
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    37
25603
d5fa4eab2d26 8046824: class SJavacTestUtil and *Wrapper are redundant and should be removed
jjg
parents: 24067
diff changeset
    38
        String testClassName = args[0];
d5fa4eab2d26 8046824: class SJavacTestUtil and *Wrapper are redundant and should be removed
jjg
parents: 24067
diff changeset
    39
        String[] testArgs = Arrays.copyOfRange(args, 1, args.length);
d5fa4eab2d26 8046824: class SJavacTestUtil and *Wrapper are redundant and should be removed
jjg
parents: 24067
diff changeset
    40
24067
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    41
        File srcDir = new File(System.getProperty("test.src"));
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    42
        File clsDir = new File(System.getProperty("test.classes"));
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    43
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    44
        File src = new File(srcDir, testClassName + ".java");
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    45
        File cls = new File(clsDir, testClassName + ".class");
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    46
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    47
        if (cls.lastModified() < src.lastModified()) {
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    48
            System.err.println("Recompiling test class...");
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    49
            String[] javacArgs = { "-d", clsDir.getPath(), src.getPath() };
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    50
            int rc = com.sun.tools.javac.Main.compile(javacArgs);
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    51
            if (rc != 0)
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    52
                throw new Exception("compilation failed");
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    53
        }
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    54
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    55
        Class<?> sjavac = Class.forName(testClassName);
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    56
        Method main = sjavac.getMethod("main", String[].class);
25603
d5fa4eab2d26 8046824: class SJavacTestUtil and *Wrapper are redundant and should be removed
jjg
parents: 24067
diff changeset
    57
        main.invoke(null, new Object[] { testArgs });
24067
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    58
    }
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    59
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    60
    private static boolean isSJavacOnClassPath() {
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    61
        String cls = "com/sun/tools/sjavac/Main.class";
25603
d5fa4eab2d26 8046824: class SJavacTestUtil and *Wrapper are redundant and should be removed
jjg
parents: 24067
diff changeset
    62
        return Wrapper.class.getClassLoader().getResource(cls) != null;
24067
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    63
    }
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    64
}