jaxws/src/jdk.xml.ws/share/classes/com/sun/tools/internal/ws/wscompile/JavaCompilerHelper.java
changeset 26658 e04929038abf
parent 25871 b80b84e87032
equal deleted inserted replaced
26542:9d0e6639a4d7 26658:e04929038abf
     1 /*
     1 /*
     2  * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    26 package com.sun.tools.internal.ws.wscompile;
    26 package com.sun.tools.internal.ws.wscompile;
    27 
    27 
    28 import com.sun.istack.internal.tools.ParallelWorldClassLoader;
    28 import com.sun.istack.internal.tools.ParallelWorldClassLoader;
    29 import com.sun.tools.internal.ws.resources.JavacompilerMessages;
    29 import com.sun.tools.internal.ws.resources.JavacompilerMessages;
    30 
    30 
       
    31 import javax.tools.JavaCompiler;
       
    32 import javax.tools.ToolProvider;
    31 import java.io.File;
    33 import java.io.File;
    32 import java.io.OutputStream;
    34 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;
    35 import java.net.MalformedURLException;
       
    36 import java.net.URISyntaxException;
    37 import java.net.URL;
    37 import java.net.URL;
    38 import java.net.URISyntaxException;
       
    39 
    38 
    40 /**
    39 /**
    41  * A helper class to invoke javac.
    40  * A helper class to invoke javac.
    42  *
    41  *
    43  * @author WS Development Team
    42  * @author WS Development Team
    59             return new File(url.getPath());
    58             return new File(url.getPath());
    60         }
    59         }
    61     }
    60     }
    62 
    61 
    63     static boolean compile(String[] args, OutputStream out, ErrorReceiver receiver){
    62     static boolean compile(String[] args, OutputStream out, ErrorReceiver receiver){
    64         ClassLoader cl = Thread.currentThread().getContextClassLoader();
       
    65         try {
    63         try {
    66             /* try to use the new compiler */
    64             JavaCompiler comp = ToolProvider.getSystemJavaCompiler();
    67             Class comSunToolsJavacMainClass =
    65             if (comp == null) {
    68                     cl.loadClass("com.sun.tools.javac.Main");
    66                 receiver.error(JavacompilerMessages.NO_JAVACOMPILER_ERROR(), null);
    69             try {
    67                 return false;
    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             }
    68             }
    85         } catch (ClassNotFoundException e) {
    69             return 0 == comp.run(null, out, out, args);
    86             receiver.error(JavacompilerMessages.JAVACOMPILER_CLASSPATH_ERROR("com.sun.tools.javac.Main"), e);
       
    87         } catch (SecurityException e) {
    70         } catch (SecurityException e) {
    88             receiver.error(e);
    71             receiver.error(e);
    89         }
    72         }
    90         return false;
    73         return false;
    91     }
    74     }
    92 
    75 
    93     private static final Class[] compileMethodSignature = {String[].class, PrintWriter.class};
       
    94 }
    76 }