src/share/jaxws_classes/com/sun/tools/internal/ws/wscompile/JavaCompilerHelper.java

changeset 0
373ffda63c9a
child 637
9c07ef4934dd
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/ws/wscompile/JavaCompilerHelper.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,94 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.  Oracle designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Oracle in the LICENSE file that accompanied this code.
    1.13 + *
    1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.16 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.17 + * version 2 for more details (a copy is included in the LICENSE file that
    1.18 + * accompanied this code).
    1.19 + *
    1.20 + * You should have received a copy of the GNU General Public License version
    1.21 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.23 + *
    1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.25 + * or visit www.oracle.com if you need additional information or have any
    1.26 + * questions.
    1.27 + */
    1.28 +
    1.29 +package com.sun.tools.internal.ws.wscompile;
    1.30 +
    1.31 +import com.sun.istack.internal.tools.ParallelWorldClassLoader;
    1.32 +import com.sun.tools.internal.ws.resources.JavacompilerMessages;
    1.33 +
    1.34 +import java.io.File;
    1.35 +import java.io.OutputStream;
    1.36 +import java.io.PrintWriter;
    1.37 +import java.lang.reflect.InvocationTargetException;
    1.38 +import java.lang.reflect.Method;
    1.39 +import java.net.MalformedURLException;
    1.40 +import java.net.URL;
    1.41 +import java.net.URISyntaxException;
    1.42 +
    1.43 +/**
    1.44 + * A helper class to invoke javac.
    1.45 + *
    1.46 + * @author WS Development Team
    1.47 + */
    1.48 +class JavaCompilerHelper{
    1.49 +    static File getJarFile(Class clazz) {
    1.50 +        URL url = null;
    1.51 +        try {
    1.52 +            url = ParallelWorldClassLoader.toJarUrl(clazz.getResource('/'+clazz.getName().replace('.','/')+".class"));
    1.53 +            return new File(url.toURI());
    1.54 +        } catch (ClassNotFoundException e) {
    1.55 +            // if we can't figure out where JAXB/JAX-WS API are, we couldn't have been executing this code.
    1.56 +            throw new Error(e);
    1.57 +        } catch (MalformedURLException e) {
    1.58 +            // if we can't figure out where JAXB/JAX-WS API are, we couldn't have been executing this code.
    1.59 +            throw new Error(e);
    1.60 +        } catch (URISyntaxException e) {
    1.61 +            // url.toURI() is picky and doesn't like ' ' in URL, so this is the fallback
    1.62 +            return new File(url.getPath());
    1.63 +        }
    1.64 +    }
    1.65 +
    1.66 +    static boolean compile(String[] args, OutputStream out, ErrorReceiver receiver){
    1.67 +        ClassLoader cl = Thread.currentThread().getContextClassLoader();
    1.68 +        try {
    1.69 +            /* try to use the new compiler */
    1.70 +            Class comSunToolsJavacMainClass =
    1.71 +                    cl.loadClass("com.sun.tools.javac.Main");
    1.72 +            try {
    1.73 +                Method compileMethod =
    1.74 +                        comSunToolsJavacMainClass.getMethod(
    1.75 +                                "compile",
    1.76 +                                compileMethodSignature);
    1.77 +                    Object result =
    1.78 +                            compileMethod.invoke(
    1.79 +                                    null, args, new PrintWriter(out));
    1.80 +                    return result instanceof Integer && (Integer) result == 0;
    1.81 +            } catch (NoSuchMethodException e2) {
    1.82 +                receiver.error(JavacompilerMessages.JAVACOMPILER_NOSUCHMETHOD_ERROR("getMethod(\"compile\", Class[])"), e2);
    1.83 +            } catch (IllegalAccessException e) {
    1.84 +                receiver.error(e);
    1.85 +            } catch (InvocationTargetException e) {
    1.86 +                receiver.error(e);
    1.87 +            }
    1.88 +        } catch (ClassNotFoundException e) {
    1.89 +            receiver.error(JavacompilerMessages.JAVACOMPILER_CLASSPATH_ERROR("com.sun.tools.javac.Main"), e);
    1.90 +        } catch (SecurityException e) {
    1.91 +            receiver.error(e);
    1.92 +        }
    1.93 +        return false;
    1.94 +    }
    1.95 +
    1.96 +    private static final Class[] compileMethodSignature = {String[].class, PrintWriter.class};
    1.97 +}

mercurial