langtools/test/tools/sjavac/SJavacTestUtil.java
author alundblad
Tue, 22 Apr 2014 16:51:10 +0200
changeset 24067 76e7b6bbbd85
permissions -rw-r--r--
8035063: Option handling in sjavac needs to be rewritten Summary: Option handling code rewritten. Exclusion / inclusion patterns changed from package to directories. Reviewed-by: jjg, jfranck
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;
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    28
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
public class SJavacTestUtil {
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    31
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    32
    public static void runSjavacTest(String testClassName, String[] args)
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    33
            throws Exception {
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    34
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    35
        if (!isSJavacOnClassPath()) {
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    36
            System.out.println("sjavac not available: pass by default");
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    37
            return;
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    38
        }
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    39
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    40
        File srcDir = new File(System.getProperty("test.src"));
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    41
        File clsDir = new File(System.getProperty("test.classes"));
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    42
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    43
        File src = new File(srcDir, testClassName + ".java");
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    44
        File cls = new File(clsDir, testClassName + ".class");
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    45
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    46
        if (cls.lastModified() < src.lastModified()) {
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    47
            System.err.println("Recompiling test class...");
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    48
            String[] javacArgs = { "-d", clsDir.getPath(), src.getPath() };
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    49
            int rc = com.sun.tools.javac.Main.compile(javacArgs);
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    50
            if (rc != 0)
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    51
                throw new Exception("compilation failed");
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    52
        }
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
        Class<?> sjavac = Class.forName(testClassName);
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    55
        Method main = sjavac.getMethod("main", String[].class);
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    56
        main.invoke(null, new Object[] { args });
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    57
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";
76e7b6bbbd85 8035063: Option handling in sjavac needs to be rewritten
alundblad
parents:
diff changeset
    62
        return SJavacTestUtil.class.getClassLoader().getResource(cls) != null;
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
}