OsgiaasJavaCompilerService implements JavacService { @Override public <T> Optional<Class<T>> compileJavaClass( ClassLoaderContext classLoaderContext, String qualifiedName, String code, PrintStream writer ) { OsgiaasJavaCompiler compiler = getOsgiaasJavaCompiler( classLoaderContext ); return compiler.compile( qualifiedName, code, writer ); }  @Override Optional<Class<T>> compileJavaClass( ClassLoaderContext classLoaderContext,
                                                    String qualifiedName,
                                                    String code,
                                                    PrintStream writer ); @Override ClassLoaderContext getAugmentedClassLoaderContext( ClassLoaderContext classLoaderContext );  }
@Test public void canDefineClass() throws Exception { String classDef = "public class Hello{ public static String get() {return \"hello\";}}"; Class<?> cls = javacService.compileJavaClass( DefaultClassLoaderContext.INSTANCE, "Hello", classDef ) .orElseThrow( () -> new AssertionError( "Failed to compile class" ) ); Object instance = cls.newInstance(); Method getMethod = cls.getMethod( "get" ); Object result = getMethod.invoke( instance ); assertEquals( "hello", result ); classDef = "public class Hey{ public static String hey() {return Hello.get() + \"hey\";}}"; cls = javacService.compileJavaClass( DefaultClassLoaderContext.INSTANCE, "Hey", classDef ) .orElseThrow( () -> new AssertionError( "Failed to compile class" ) ); instance = cls.newInstance(); getMethod = cls.getMethod( "hey" ); result = getMethod.invoke( instance ); assertEquals( "hellohey", result ); }
@Test public void canCastClassToKnownInterfaceAndUsePackageName() throws Exception { String classDef = "package com.acme.util;" + "import java.util.function.IntSupplier;" + "public class ZeroSupplier implements IntSupplier {" + "public int getAsInt() { return 0; }" + "}"; Class<?> cls = javacService.compileJavaClass( DefaultClassLoaderContext.INSTANCE, "com.acme.util.ZeroSupplier", classDef ) .orElseThrow( () -> new AssertionError( "Failed to compile class" ) ); IntSupplier instance = ( IntSupplier ) cls.newInstance(); int zero = instance.getAsInt(); assertEquals( 0, zero ); }
@Test public void canDefineClassAfterCompilerError() throws Exception { String errorDef = "clazz Bad{ public String get() {return \"bad\";}}"; String classDef = "class Good{ public String get() {return \"good\";}}"; ByteArrayOutputStream out = new ByteArrayOutputStream(); PrintStream writer = new PrintStream( out ); try { javacService.compileJavaClass( DefaultClassLoaderContext.INSTANCE, "Bad", errorDef, writer ); fail( "Should not have compiled class successfully" ); } catch ( Throwable t ) { } javacService.compileJavaClass( DefaultClassLoaderContext.INSTANCE, "Good", classDef, writer ); Callable script = javacService.compileJavaSnippet( JavaSnippet.Builder.withCode( "return new Good().get();" ), DefaultClassLoaderContext.INSTANCE, writer ) .orElseThrow( () -> new AssertionError( "Failed to compile class\n" + out ) ); Object result = script.call(); assertEquals( "good", result ); }