|
Groovy example source code file (JavacJavaCompiler.java)
The Groovy JavacJavaCompiler.java source code/* * Copyright 2003-2009 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.codehaus.groovy.tools.javac; import groovy.lang.GroovyClassLoader; import java.io.File; import java.io.PrintWriter; import java.io.StringWriter; import java.lang.reflect.Method; import java.lang.reflect.InvocationTargetException; import java.util.*; import java.net.URLClassLoader; import java.net.URL; import java.net.URISyntaxException; import org.codehaus.groovy.control.CompilationUnit; import org.codehaus.groovy.control.CompilerConfiguration; import org.codehaus.groovy.control.messages.ExceptionMessage; import org.codehaus.groovy.control.messages.SimpleMessage; import org.codehaus.groovy.runtime.DefaultGroovyMethods; public class JavacJavaCompiler implements JavaCompiler { private CompilerConfiguration config; public JavacJavaCompiler(CompilerConfiguration config) { this.config = config; } public void compile(List<String> files, CompilationUnit cu) { String[] javacParameters = makeParameters(files, cu.getClassLoader()); StringWriter javacOutput = null; int javacReturnValue = 0; try { Class javac = findJavac(cu); Method method = null; try { method = javac.getMethod("compile", new Class[]{String[].class, PrintWriter.class}); javacOutput = new StringWriter(); PrintWriter writer = new PrintWriter(javacOutput); Object ret = method.invoke(null, javacParameters, writer); javacReturnValue = (Integer) ret; } catch (NoSuchMethodException e) { } if (method == null) { method = javac.getMethod("compile", new Class[]{String[].class}); Object ret = method.invoke(null, new Object[]{javacParameters}); javacReturnValue = (Integer) ret; } cu.getConfiguration().getOutput(); } catch (InvocationTargetException ite) { cu.getErrorCollector().addFatalError(new ExceptionMessage((Exception) ite.getCause(), true, cu)); } catch (Exception e) { cu.getErrorCollector().addFatalError(new ExceptionMessage(e, true, cu)); } if (javacReturnValue != 0) { switch (javacReturnValue) { case 1: addJavacError("Compile error during compilation with javac.", cu, javacOutput); break; case 2: addJavacError("Invalid commandline usage for javac.", cu, javacOutput); break; case 3: addJavacError("System error during compilation with javac.", cu, javacOutput); break; case 4: addJavacError("Abnormal termination of javac.", cu, javacOutput); break; default: addJavacError("unexpected return value by javac.", cu, javacOutput); break; } } else { // print warnings if any System.out.print(javacOutput); } } private void addJavacError(String header, CompilationUnit cu, StringWriter msg) { if (msg != null) { header = header + "\n" + msg.getBuffer().toString(); } else { header = header + "\nThis javac version does not support compile(String[],PrintWriter), " + "so no further details of the error are available. The message error text " + "should be found on System.err.\n"; } cu.getErrorCollector().addFatalError(new SimpleMessage(header, cu)); } private String[] makeParameters(List<String> files, GroovyClassLoader parentClassLoader) { Map options = config.getJointCompilationOptions(); LinkedList<String> paras = new LinkedList Other Groovy examples (source code examples)Here is a short list of links related to this Groovy JavacJavaCompiler.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.