List of usage examples for org.eclipse.jdt.internal.compiler.batch Main compile
public boolean compile(String[] argv)
From source file:com.google.devtools.j2objc.AnnotationPreProcessor.java
License:Apache License
/** * Process the given input files, given in the same format as J2ObjC command line args. *//*from www . j a v a2 s. co m*/ void process(Iterable<String> fileArgs) { assert tmpDirectory == null; // Shouldn't run an instance more than once. if (!hasAnnotationProcessors()) { return; } try { tmpDirectory = FileUtil.createTempDir("annotations"); } catch (IOException e) { ErrorUtil.error("failed creating temporary directory: " + e); return; } String tmpDirPath = tmpDirectory.getAbsolutePath(); List<String> compileArgs = Lists.newArrayList(); Joiner pathJoiner = Joiner.on(":"); List<String> sourcePath = Options.getSourcePathEntries(); sourcePath.add(tmpDirPath); compileArgs.add("-sourcepath"); compileArgs.add(pathJoiner.join(sourcePath)); compileArgs.add("-classpath"); List<String> classPath = Options.getClassPathEntries(); compileArgs.add(pathJoiner.join(classPath)); compileArgs.add("-encoding"); compileArgs.add(Options.getCharset().name()); compileArgs.add("-source"); compileArgs.add("1.7"); compileArgs.add("-s"); compileArgs.add(tmpDirPath); compileArgs.add("-d"); compileArgs.add(tmpDirPath); if (Options.isVerbose()) { compileArgs.add("-XprintProcessorInfo"); compileArgs.add("-XprintRounds"); } for (String fileArg : fileArgs) { compileArgs.add(fileArg); } Map<String, String> batchOptions = Maps.newHashMap(); batchOptions.put(CompilerOptions.OPTION_Process_Annotations, CompilerOptions.ENABLED); batchOptions.put(CompilerOptions.OPTION_GenerateClassFiles, CompilerOptions.DISABLED); // Fully qualified name used since "Main" isn't unique. org.eclipse.jdt.internal.compiler.batch.Main batchCompiler = new org.eclipse.jdt.internal.compiler.batch.Main( new PrintWriter(System.out), new PrintWriter(System.err), false, batchOptions, null); if (!batchCompiler.compile(compileArgs.toArray(new String[0]))) { // Any compilation errors will already by displayed. ErrorUtil.error("failed batch processing sources"); } else { addGeneratedSources(tmpDirectory, ""); } }
From source file:com.google.devtools.j2objc.gen.ObjectiveCImplementationGeneratorTest.java
License:Open Source License
public void testPackageInfoOnClasspath() throws IOException { addSourceFile(//w w w .j ava 2s . c o m "@ObjectiveCName(\"FBM\")\n" + "package foo.bar.mumble;\n" + "import com.google.j2objc.annotations.ObjectiveCName;", "src/foo/bar/mumble/package-info.java"); List<String> compileArgs = Lists.newArrayList(); compileArgs.add("-classpath"); compileArgs.add(System.getProperty("java.class.path")); compileArgs.add("-encoding"); compileArgs.add(Options.getCharset().name()); compileArgs.add("-source"); compileArgs.add("1.6"); compileArgs.add(tempDir.getAbsolutePath() + "/src/foo/bar/mumble/package-info.java"); org.eclipse.jdt.internal.compiler.batch.Main batchCompiler = new org.eclipse.jdt.internal.compiler.batch.Main( new PrintWriter(System.out), new PrintWriter(System.err), false, Collections.emptyMap(), null); batchCompiler.compile(compileArgs.toArray(new String[0])); List<String> oldClassPathEntries = new ArrayList<String>(Options.getClassPathEntries()); Options.getClassPathEntries().add(tempDir.getAbsolutePath() + "/src/"); try { String translation = translateSourceFile("package foo.bar.mumble;\n" + "public class Test {}", "foo.bar.mumble.Test", "foo/bar/mumble/Test.h"); assertTranslation(translation, "@interface FBMTest"); assertTranslation(translation, "typedef FBMTest FooBarMumbleTest;"); translation = getTranslatedFile("foo/bar/mumble/Test.m"); assertTranslation(translation, "@implementation FBMTest"); assertNotInTranslation(translation, "FooBarMumbleTest"); } finally { Options.getClassPathEntries().clear(); Options.getClassPathEntries().addAll(oldClassPathEntries); } }
From source file:com.google.devtools.j2objc.pipeline.AnnotationPreProcessor.java
License:Apache License
/** * Process the given input files, given in the same format as J2ObjC command line args. *///from ww w . ja v a 2 s .c o m public void process(Iterable<String> fileArgs) { assert tmpDirectory == null; // Shouldn't run an instance more than once. if (!hasAnnotationProcessors()) { return; } try { tmpDirectory = FileUtil.createTempDir("annotations"); } catch (IOException e) { ErrorUtil.error("failed creating temporary directory: " + e); return; } String tmpDirPath = tmpDirectory.getAbsolutePath(); List<String> compileArgs = Lists.newArrayList(); Joiner pathJoiner = Joiner.on(":"); List<String> sourcePath = Options.getSourcePathEntries(); sourcePath.add(tmpDirPath); compileArgs.add("-sourcepath"); compileArgs.add(pathJoiner.join(sourcePath)); compileArgs.add("-classpath"); List<String> classPath = Options.getClassPathEntries(); compileArgs.add(pathJoiner.join(classPath)); compileArgs.add("-encoding"); compileArgs.add(Options.getCharset().name()); compileArgs.add("-source"); compileArgs.add(Options.getSourceVersion()); compileArgs.add("-s"); compileArgs.add(tmpDirPath); compileArgs.add("-d"); compileArgs.add(tmpDirPath); List<String> processorPath = Options.getProcessorPathEntries(); if (!processorPath.isEmpty()) { compileArgs.add("-processorpath"); compileArgs.add(pathJoiner.join(processorPath)); } String processorClasses = Options.getProcessors(); if (processorClasses != null) { compileArgs.add("-processor"); compileArgs.add(processorClasses); } if (Options.isVerbose()) { compileArgs.add("-XprintProcessorInfo"); compileArgs.add("-XprintRounds"); } for (String fileArg : fileArgs) { compileArgs.add(fileArg); } Map<String, String> batchOptions = Maps.newHashMap(); batchOptions.put(CompilerOptions.OPTION_Process_Annotations, CompilerOptions.ENABLED); batchOptions.put(CompilerOptions.OPTION_GenerateClassFiles, CompilerOptions.DISABLED); // Fully qualified name used since "Main" isn't unique. org.eclipse.jdt.internal.compiler.batch.Main batchCompiler = new org.eclipse.jdt.internal.compiler.batch.Main( new PrintWriter(System.out), new PrintWriter(System.err), false, batchOptions, null); if (!batchCompiler.compile(compileArgs.toArray(new String[0]))) { // Any compilation errors will already by displayed. ErrorUtil.error("failed batch processing sources"); } }
From source file:com.runwaysdk.business.generation.EclipseCompiler.java
License:Open Source License
/** * Calls the ECJ and wraps any errors in a {@link CompilerException} * // w w w . ja v a2 s .c o m * @param args Arguments for the compiler * @throws CompilerException if compilation fails */ private void callECJ(String args[]) { Log log = LogFactory.getLog(COMPILER_LOG); log.trace(Arrays.deepToString(args)); StringWriter output = new StringWriter(); StringWriter errors = new StringWriter(); Main compiler = new Main(new PrintWriter(output), new PrintWriter(errors), false); compiler.compile(args); String message = errors.toString(); if (message.length() > 0) { String error = "Errors found during compilation:\n" + message; throw new CompilerException(error, message); } }
From source file:com.wavemaker.tools.compiler.WaveMakerJavaCompiler.java
License:Open Source License
@Override public int run(InputStream in, OutputStream out, OutputStream err, String... arguments) { Main compiler = new Main(createPrintWriter(out), createPrintWriter(err), true, null, null); boolean succeed = compiler.compile(arguments); return succeed ? 0 : -1; }
From source file:lombok.ast.grammar.EcjCompilerTest.java
License:Open Source License
@Test public boolean testEcjCompiler(File file) throws IOException { if (!EXTENDED) return false; org.eclipse.jdt.internal.compiler.batch.Main main = new org.eclipse.jdt.internal.compiler.batch.Main( new PrintWriter(System.out), new PrintWriter(System.err), false, null, null); File tempDir = getTempDir();//from w ww .j av a2 s . com tempDir.mkdirs(); String[] argv = { "-d", tempDir.getAbsolutePath(), "-encoding", "UTF-8", "-proc:none", "-1.6", "-nowarn", "-enableJavadoc", file.getAbsolutePath() }; main.compile(argv); assertEquals("Errors occurred while compiling this file with ecj", 0, main.globalErrorsCount); return true; }
From source file:org.bonitasoft.engine.business.data.generator.compiler.JDTCompiler.java
License:Open Source License
private void doCompilation(final String[] commandLine, final PrintWriter outWriter, final ByteArrayOutputStream errorStream, final PrintWriter errorWriter) throws CompilationException { final Main mainCompiler = new Main(outWriter, errorWriter, false /* systemExit */, null /* options */, new DummyCompilationProgress()) { @Override//from w w w.ja v a 2 s . co m public FileSystem getLibraryAccess() { final ClassLoader contextClassLoader = classLoader; return new ClassLoaderEnvironment(contextClassLoader); } }; final boolean succeeded = mainCompiler.compile(commandLine); if (!succeeded) { throw new CompilationException(new String(errorStream.toByteArray())); } }
From source file:org.bonitasoft.engine.compiler.JDTCompiler.java
License:Open Source License
private void doCompilation(final String[] commandLine, final PrintWriter outWriter, final ByteArrayOutputStream errorStream, final PrintWriter errorWriter) throws CompilationException { Main mainCompiler = new Main(outWriter, errorWriter, false /* systemExit */, null /* options */, new DummyCompilationProgress()) { @Override/* w w w. ja v a 2 s .co m*/ public FileSystem getLibraryAccess() { ClassLoader contextClassLoader = classLoader; return new ClassLoaderEnvironment(contextClassLoader); } }; final boolean succeeded = mainCompiler.compile(commandLine); if (!succeeded) { throw new CompilationException(new String(errorStream.toByteArray())); } }
From source file:org.codehaus.groovy.eclipse.compiler.GroovyEclipseCompiler.java
License:Open Source License
@SuppressWarnings("rawtypes") public List compile(CompilerConfiguration config) throws CompilerException { String[] args = createCommandLine(config); if (args.length == 0) { getLogger().info("Nothing to compile - all classes are up to date"); return Collections.emptyList(); }/*from w w w.java 2 s . c o m*/ List<CompilerError> messages; if (config.isFork()) { String executable = config.getExecutable(); if (StringUtils.isEmpty(executable)) { try { executable = getJavaExecutable(); } catch (IOException e) { getLogger().warn("Unable to autodetect 'java' path, using 'java' from the environment."); executable = "java"; } } String groovyEclipseLocation = getGroovyEclipseBatchLocation(); messages = compileOutOfProcess(config, executable, groovyEclipseLocation, args); } else { Progress progress = new Progress(); Main main = new Main(new PrintWriter(System.out), new PrintWriter(System.err), false/* systemExit */, null/* options */, progress); boolean result = main.compile(args); messages = formatResult(main, result); } return messages; }
From source file:org.eclipse.objectteams.otdt.tests.ModifyingResourceTests.java
License:Open Source License
/** * E.g. <code>//from w w w.j av a 2s .c o m * org.eclipse.jdt.tests.core.ModifyingResourceTests.generateClassFile( * "A", * "public class A {\n" + * "}") */ public static void generateClassFile(String className, String javaSource) throws IOException { String cu = "d:/temp/" + className + ".java"; FileOutputStream output = new FileOutputStream(cu); try { output.write(javaSource.getBytes()); } finally { output.close(); } Main.compile(cu + " -d d:/temp -classpath " + System.getProperty("java.home") + "/lib/rt.jar"); FileInputStream input = new FileInputStream("d:/temp/" + className + ".class"); try { System.out.println("{"); byte[] buffer = new byte[80]; int read = 0; while (read != -1) { read = input.read(buffer); if (read != -1) System.out.print("\t"); for (int i = 0; i < read; i++) { System.out.print(buffer[i]); System.out.print(", "); } if (read != -1) System.out.println(); } System.out.print("}"); } finally { input.close(); } }