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

Wed, 27 Apr 2016 01:27:09 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:27:09 +0800
changeset 0
373ffda63c9a
child 637
9c07ef4934dd
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/jaxws/
changeset: 657:d47a47f961ee
tag: jdk8u25-b17

     1 /*
     2  * Copyright (c) 1997, 2012, 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.  Oracle designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Oracle in the LICENSE file that accompanied this code.
    10  *
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    14  * version 2 for more details (a copy is included in the LICENSE file that
    15  * accompanied this code).
    16  *
    17  * You should have received a copy of the GNU General Public License version
    18  * 2 along with this work; if not, write to the Free Software Foundation,
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    20  *
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    22  * or visit www.oracle.com if you need additional information or have any
    23  * questions.
    24  */
    26 package com.sun.tools.internal.ws.wscompile;
    28 import com.sun.istack.internal.tools.ParallelWorldClassLoader;
    29 import com.sun.tools.internal.ws.resources.JavacompilerMessages;
    31 import java.io.File;
    32 import java.io.OutputStream;
    33 import java.io.PrintWriter;
    34 import java.lang.reflect.InvocationTargetException;
    35 import java.lang.reflect.Method;
    36 import java.net.MalformedURLException;
    37 import java.net.URL;
    38 import java.net.URISyntaxException;
    40 /**
    41  * A helper class to invoke javac.
    42  *
    43  * @author WS Development Team
    44  */
    45 class JavaCompilerHelper{
    46     static File getJarFile(Class clazz) {
    47         URL url = null;
    48         try {
    49             url = ParallelWorldClassLoader.toJarUrl(clazz.getResource('/'+clazz.getName().replace('.','/')+".class"));
    50             return new File(url.toURI());
    51         } catch (ClassNotFoundException e) {
    52             // if we can't figure out where JAXB/JAX-WS API are, we couldn't have been executing this code.
    53             throw new Error(e);
    54         } catch (MalformedURLException e) {
    55             // if we can't figure out where JAXB/JAX-WS API are, we couldn't have been executing this code.
    56             throw new Error(e);
    57         } catch (URISyntaxException e) {
    58             // url.toURI() is picky and doesn't like ' ' in URL, so this is the fallback
    59             return new File(url.getPath());
    60         }
    61     }
    63     static boolean compile(String[] args, OutputStream out, ErrorReceiver receiver){
    64         ClassLoader cl = Thread.currentThread().getContextClassLoader();
    65         try {
    66             /* try to use the new compiler */
    67             Class comSunToolsJavacMainClass =
    68                     cl.loadClass("com.sun.tools.javac.Main");
    69             try {
    70                 Method compileMethod =
    71                         comSunToolsJavacMainClass.getMethod(
    72                                 "compile",
    73                                 compileMethodSignature);
    74                     Object result =
    75                             compileMethod.invoke(
    76                                     null, args, new PrintWriter(out));
    77                     return result instanceof Integer && (Integer) result == 0;
    78             } catch (NoSuchMethodException e2) {
    79                 receiver.error(JavacompilerMessages.JAVACOMPILER_NOSUCHMETHOD_ERROR("getMethod(\"compile\", Class[])"), e2);
    80             } catch (IllegalAccessException e) {
    81                 receiver.error(e);
    82             } catch (InvocationTargetException e) {
    83                 receiver.error(e);
    84             }
    85         } catch (ClassNotFoundException e) {
    86             receiver.error(JavacompilerMessages.JAVACOMPILER_CLASSPATH_ERROR("com.sun.tools.javac.Main"), e);
    87         } catch (SecurityException e) {
    88             receiver.error(e);
    89         }
    90         return false;
    91     }
    93     private static final Class[] compileMethodSignature = {String[].class, PrintWriter.class};
    94 }

mercurial