All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.greenpepper.shaded.org.codehaus.janino.Compiler Maven / Gradle / Ivy

There is a newer version: 4.2.4
Show newest version

/*
 * Janino - An embedded Java[TM] compiler
 *
 * Copyright (c) 2001-2010, Arno Unkrig
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
 * following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
 *       following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
 *       following disclaimer in the documentation and/or other materials provided with the distribution.
 *    3. The name of the author may not be used to endorse or promote products derived from this software without
 *       specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

package com.greenpepper.shaded.org.codehaus.janino;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import com.greenpepper.shaded.org.codehaus.commons.compiler.CompileException;
import com.greenpepper.shaded.org.codehaus.commons.compiler.ErrorHandler;
import com.greenpepper.shaded.org.codehaus.commons.compiler.Location;
import com.greenpepper.shaded.org.codehaus.commons.compiler.WarningHandler;
import com.greenpepper.shaded.org.codehaus.janino.Java.CompilationUnit;
import com.greenpepper.shaded.org.codehaus.janino.util.Benchmark;
import com.greenpepper.shaded.org.codehaus.janino.util.ClassFile;
import com.greenpepper.shaded.org.codehaus.janino.util.StringPattern;
import com.greenpepper.shaded.org.codehaus.janino.util.resource.DirectoryResourceCreator;
import com.greenpepper.shaded.org.codehaus.janino.util.resource.DirectoryResourceFinder;
import com.greenpepper.shaded.org.codehaus.janino.util.resource.FileResource;
import com.greenpepper.shaded.org.codehaus.janino.util.resource.FileResourceCreator;
import com.greenpepper.shaded.org.codehaus.janino.util.resource.PathResourceFinder;
import com.greenpepper.shaded.org.codehaus.janino.util.resource.Resource;
import com.greenpepper.shaded.org.codehaus.janino.util.resource.ResourceCreator;
import com.greenpepper.shaded.org.codehaus.janino.util.resource.ResourceFinder;


/**
 * A simplified substitute for the javac tool.
 *
 * Usage:
 * 
 * java com.greenpepper.shaded.org.codehaus.janino.Compiler \
 *           [ -d destination-dir ] \
 *           [ -sourcepath dirlist ] \
 *           [ -classpath dirlist ] \
 *           [ -extdirs dirlist ] \
 *           [ -bootclasspath dirlist ] \
 *           [ -encoding encoding ] \
 *           [ -verbose ] \
 *           [ -g:none ] \
 *           [ -g:{source,lines,vars} ] \
 *           [ -warn:pattern-list ] \
 *           source-file ...
 * java com.greenpepper.shaded.org.codehaus.janino.Compiler -help
 * 
*/ @SuppressWarnings({ "rawtypes", "unchecked" }) public class Compiler { private static final boolean DEBUG = false; /** Command line interface. */ public static void main(String[] args) { File destinationDirectory = Compiler.NO_DESTINATION_DIRECTORY; File[] optionalSourcePath = null; File[] classPath = { new File(".") }; File[] optionalExtDirs = null; File[] optionalBootClassPath = null; String optionalCharacterEncoding = null; boolean verbose = false; boolean debugSource = true; boolean debugLines = true; boolean debugVars = false; StringPattern[] warningHandlePatterns = Compiler.DEFAULT_WARNING_HANDLE_PATTERNS; boolean rebuild = false; // Process command line options. int i; for (i = 0; i < args.length; ++i) { String arg = args[i]; if (arg.charAt(0) != '-') break; if ("-d".equals(arg)) { destinationDirectory = new File(args[++i]); } else if ("-sourcepath".equals(arg)) { optionalSourcePath = PathResourceFinder.parsePath(args[++i]); } else if ("-classpath".equals(arg)) { classPath = PathResourceFinder.parsePath(args[++i]); } else if ("-extdirs".equals(arg)) { optionalExtDirs = PathResourceFinder.parsePath(args[++i]); } else if ("-bootclasspath".equals(arg)) { optionalBootClassPath = PathResourceFinder.parsePath(args[++i]); } else if ("-encoding".equals(arg)) { optionalCharacterEncoding = args[++i]; } else if ("-verbose".equals(arg)) { verbose = true; } else if ("-g".equals(arg)) { debugSource = true; debugLines = true; debugVars = true; } else if (arg.startsWith("-g:")) { if (arg.indexOf("none") != -1) debugSource = (debugLines = (debugVars = false)); if (arg.indexOf("source") != -1) debugSource = true; if (arg.indexOf("lines") != -1) debugLines = true; if (arg.indexOf("vars") != -1) debugVars = true; } else if (arg.startsWith("-warn:")) { warningHandlePatterns = StringPattern.parseCombinedPattern(arg.substring(6)); } else if ("-rebuild".equals(arg)) { rebuild = true; } else if ("-help".equals(arg)) { System.out.printf(Compiler.USAGE, (Object[]) null); System.exit(1); } else { System.err.println("Unrecognized command line option \"" + arg + "\"; try \"-help\"."); System.exit(1); } } // Get source file names. if (i == args.length) { System.err.println("No source files given on command line; try \"-help\"."); System.exit(1); } File[] sourceFiles = new File[args.length - i]; for (int j = i; j < args.length; ++j) sourceFiles[j - i] = new File(args[j]); // Create the compiler object. final Compiler compiler = new Compiler( optionalSourcePath, classPath, optionalExtDirs, optionalBootClassPath, destinationDirectory, optionalCharacterEncoding, verbose, debugSource, debugLines, debugVars, warningHandlePatterns, rebuild ); // Compile source files. try { compiler.compile(sourceFiles); } catch (Exception e) { System.err.println(e.toString()); System.exit(1); } } private static final String USAGE = ( "" + "Usage:%n" + "%n" + " java " + Compiler.class.getName() + " [

* Notice that the CU is not compiled here! */ private IClass defineIClassFromSourceResource(Resource sourceResource, String className) throws ClassNotFoundException { // Parse the source file. UnitCompiler uc; try { Java.CompilationUnit cu = Compiler.this.parseCompilationUnit( sourceResource.getFileName(), // fileName new BufferedInputStream(sourceResource.open()), // inputStream Compiler.this.optionalCharacterEncoding // optionalCharacterEncoding ); uc = new UnitCompiler(cu, Compiler.this.iClassLoader); } catch (IOException ex) { throw new ClassNotFoundException("Parsing compilation unit \"" + sourceResource + "\"", ex); } catch (CompileException ex) { throw new ClassNotFoundException("Parsing compilation unit \"" + sourceResource + "\"", ex); } // Remember compilation unit for later compilation. Compiler.this.parsedCompilationUnits.add(uc); // Define the class. IClass res = uc.findClass(className); if (res == null) { // This is a really complicated case: We may find a source file on the source // path that seemingly contains the declaration of the class we are looking // for, but doesn't. This is possible if the underlying file system has // case-insensitive file names and/or file names that are limited in length // (e.g. DOS 8.3). return null; } this.defineIClass(res); return res; } /** * Open the given {@code classFileResource}, read its contents, define it in the * {@link IClassLoader}, and resolve it (this step may involve loading more classes). */ private IClass defineIClassFromClassFileResource(Resource classFileResource) throws ClassNotFoundException { Compiler.this.benchmark.beginReporting("Loading class file \"" + classFileResource.getFileName() + "\""); try { InputStream is = null; ClassFile cf; try { is = classFileResource.open(); cf = new ClassFile(new BufferedInputStream(is)); } catch (IOException ex) { throw new ClassNotFoundException("Opening class file resource \"" + classFileResource + "\"", ex); } finally { if (is != null) try { is.close(); } catch (IOException e) {} } ClassFileIClass result = new ClassFileIClass( cf, // classFile CompilerIClassLoader.this // iClassLoader ); // Important: We must FIRST call "defineIClass()" so that the // new IClass is known to the IClassLoader, and THEN // "resolveAllClasses()", because otherwise endless recursion could // occur. this.defineIClass(result); result.resolveAllClasses(); return result; } finally { Compiler.this.benchmark.endReporting(); } } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy