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

com.athaydes.osgiaas.javac.JavacService Maven / Gradle / Ivy

There is a newer version: 0.9
Show newest version
package com.athaydes.osgiaas.javac;

import com.athaydes.osgiaas.api.env.ClassLoaderContext;
import com.athaydes.osgiaas.javac.internal.DefaultClassLoaderContext;
import com.athaydes.osgiaas.javac.internal.SnippetClass;
import com.athaydes.osgiaas.javac.internal.compiler.OsgiaasJavaCompilerService;

import java.io.PrintStream;
import java.util.Optional;
import java.util.concurrent.Callable;

import static com.athaydes.osgiaas.javac.internal.SnippetClass.asCallableSnippet;

/**
 * Java Compiler Service.
 * 

* This service uses a {@link com.athaydes.osgiaas.javac.internal.compiler.OsgiaasJavaCompiler} to compile Java * source code, managing different class loaders as necessary. *

* Unlike the compiler, this service can compile both Java classes and Java source code snippets * (by first wrapping them into a simple Java class with a main method). */ public interface JavacService { /** * @return the default writer, System.err. */ default PrintStream defaultWriter() { return System.err; } /** * Compiles a Java class with the given name. * * @param classLoaderContext the ClassLoader context * @param qualifiedName qualified name of the Java class * @param code the Java class source code * @param type of the compiled class (usually Object or an interface implemented by the class) * @return the compiled class Object if successful, or empty if a compilation error occurs. * Compilation errors are written to the default writer. */ default Optional> compileJavaClass( ClassLoaderContext classLoaderContext, String qualifiedName, String code ) { return compileJavaClass( classLoaderContext, qualifiedName, code, defaultWriter() ); } /** * Compiles a Java class with the given name. * * @param classLoaderContext the ClassLoader context * @param qualifiedName qualified name of the Java class * @param code the Java class source code * @param writer to capture the compiler output * @param type of the compiled class (usually Object or an interface implemented by the class) * @return the compiled class Object if successful, or empty if a compilation error occurs. * Compilation errors are written to the provided writer. */ Optional> compileJavaClass( ClassLoaderContext classLoaderContext, String qualifiedName, String code, PrintStream writer ); /** * Compiles the given Java source code snippet. *

* Example of a snippet: *

     * 
     * int four = 2 + 2;
     * System.out.println("2 + 2 == " + four );
     * 
     * 
* * @param snippet Java source code snippet (not a class or method) * @return the compiled class Object if successful, or empty if a compilation error occurs. * Compilation errors are written to the default writer. */ default Optional> compileJavaSnippet( String snippet ) { return compileJavaSnippet( JavaSnippet.Builder.withCode( snippet ), DefaultClassLoaderContext.INSTANCE, defaultWriter() ); } /** * Compiles the given Java source code snippet. *

* Example of a snippet: *

     * 
     * int four = 2 + 2;
     * System.out.println("2 + 2 == " + four );
     * 
     * 
* * @param snippet Java source code snippet (not a class or method) * @param classLoaderContext the ClassLoader context * @return the compiled class Object if successful, or empty if a compilation error occurs. * Compilation errors are written to the default writer. */ default Optional> compileJavaSnippet( String snippet, ClassLoaderContext classLoaderContext ) { return compileJavaSnippet( JavaSnippet.Builder.withCode( snippet ), classLoaderContext, defaultWriter() ); } /** * Compiles the given Java source code snippet. *

* Example of a snippet: *

     * 
     * int four = 2 + 2;
     * System.out.println("2 + 2 == " + four );
     * 
     * 
* * @param snippet Java source code snippet (not a class or method) * @param classLoaderContext the ClassLoader context * @param writer to capture the compiler output * @return the compiled class Object if successful, or empty if a compilation error occurs. * Compilation errors are written to the provided writer. */ default Optional> compileJavaSnippet( String snippet, ClassLoaderContext classLoaderContext, PrintStream writer ) { return compileJavaSnippet( JavaSnippet.Builder.withCode( snippet ), classLoaderContext, writer ); } /** * Compiles the given Java source code snippet. *

* Example of a snippet: *

     * 
     * int four = 2 + 2;
     * System.out.println("2 + 2 == " + four );
     * 
     * 
* * @param snippet Java source code snippet (not a class or method) * @return the compiled class Object if successful, or empty if a compilation error occurs. * Compilation errors are written to the default writer. */ default Optional> compileJavaSnippet( JavaSnippet snippet ) { return compileJavaSnippet( snippet, DefaultClassLoaderContext.INSTANCE, defaultWriter() ); } /** * Compiles the given Java source code snippet. *

* Example of a snippet: *

     * 
     * int four = 2 + 2;
     * System.out.println("2 + 2 == " + four );
     * 
     * 
* * @param snippet Java source code snippet (not a class or method) * @param classLoaderContext the ClassLoader context * @return the compiled class Object if successful, or empty if a compilation error occurs. * Compilation errors are written to the default writer. */ default Optional> compileJavaSnippet( JavaSnippet snippet, ClassLoaderContext classLoaderContext ) { return compileJavaSnippet( snippet, classLoaderContext, defaultWriter() ); } /** * Compiles the given Java source code snippet. *

* Example of a snippet: *

     * 
     * int four = 2 + 2;
     * System.out.println("2 + 2 == " + four );
     * 
     * 
* * @param snippet Java source code snippet (not a class or method) * @param classLoaderContext the ClassLoader context * @param writer to capture the compiler output * @return the compiled class Object if successful, or empty if a compilation error occurs. * Compilation errors are written to the provided writer. */ default Optional> compileJavaSnippet( JavaSnippet snippet, ClassLoaderContext classLoaderContext, PrintStream writer ) { SnippetClass snippetClass = asCallableSnippet( snippet ); try { return compileJavaClass( classLoaderContext, snippetClass.getClassName(), snippetClass.getCode(), writer ).map( ( SnippetClass::uncheckedInstantiator ) ); } catch ( Exception e ) { throw new RuntimeException( e ); } } default String getJavaSnippetClass( JavaSnippet snippet ) { return asCallableSnippet( snippet ).getCode(); } /** * @param classLoaderContext to be augmented * @return the provided classLoader context augmented with the Java compiler's * own loader, which allows the augmented context to see classes compiled by the * JavaC compiler as well as the ones from the provided context. */ ClassLoaderContext getAugmentedClassLoaderContext( ClassLoaderContext classLoaderContext ); /** * @return a default implementation of the JavacService. */ static JavacService createDefault() { return new OsgiaasJavaCompilerService(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy