SjavacBase.java revision 3573:c4a18ee691c4
117680Spst/*
217680Spst * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
317680Spst * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
417680Spst *
517680Spst * This code is free software; you can redistribute it and/or modify it
617680Spst * under the terms of the GNU General Public License version 2 only, as
717680Spst * published by the Free Software Foundation.
817680Spst *
917680Spst * This code is distributed in the hope that it will be useful, but WITHOUT
1017680Spst * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1117680Spst * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1217680Spst * version 2 for more details (a copy is included in the LICENSE file that
1317680Spst * accompanied this code).
1417680Spst *
1517680Spst * You should have received a copy of the GNU General Public License version
1617680Spst * 2 along with this work; if not, write to the Free Software Foundation,
1717680Spst * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1817680Spst *
1917680Spst * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2057278Sfenner * or visit www.oracle.com if you need additional information or have any
2157278Sfenner * questions.
2217680Spst */
2317680Spst
2417680Spstimport java.lang.reflect.Method;
25127675Sbmsimport java.util.Arrays;
26190207Srpaulo
2717680Spstimport toolbox.ToolBox;
2817680Spst
2956893Sfennerpublic class SjavacBase {
3056893Sfenner
3156893Sfenner    protected final static ToolBox toolbox = new ToolBox();
3256893Sfenner
33127675Sbms    /**
34235530Sdelphij     * Utility method for invoking sjavac. Method accepts Objects as arguments
35235530Sdelphij     * (which are turned into Strings through Object::toString) to allow clients
36235530Sdelphij     * to pass for instance Path objects.
37235530Sdelphij     */
38146778Ssam    public static int compile(Object... args) throws ReflectiveOperationException {
39235530Sdelphij        // Use reflection to avoid a compile-time dependency on sjavac Main
40235530Sdelphij        System.out.println("compile: " + Arrays.toString(args));
4117680Spst        Class<?> c = Class.forName("com.sun.tools.sjavac.Main");
42235530Sdelphij        Method m = c.getDeclaredMethod("go", String[].class);
43235530Sdelphij        String[] strArgs = new String[args.length];
44146778Ssam        for (int i = 0; i < args.length; i++)
45146778Ssam            strArgs[i] = args[i].toString();
46146778Ssam        int rc = (Integer) m.invoke(null, (Object) strArgs);
47146778Ssam        return rc;
48146778Ssam    }
49146778Ssam}
50146778Ssam