Search in sources :

Example 1 with JavaCompiler

use of org.apache.sling.commons.compiler.JavaCompiler in project sling by apache.

the class SightlyJavaCompilerServiceTest method getInstancePojoTest.

private void getInstancePojoTest(String className) throws Exception {
    RenderContextImpl renderContext = Mockito.mock(RenderContextImpl.class);
    JavaCompiler javaCompiler = Mockito.mock(JavaCompiler.class);
    CompilationResult compilationResult = Mockito.mock(CompilationResult.class);
    when(compilationResult.getErrors()).thenReturn(new ArrayList<CompilerMessage>());
    when(javaCompiler.compile(Mockito.any(CompilationUnit[].class), Mockito.any(Options.class))).thenReturn(compilationResult);
    when(classLoaderWriter.getClassLoader().loadClass(className)).thenAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return MockPojo.class;
        }
    });
    Whitebox.setInternalState(compiler, "classLoaderWriter", classLoaderWriter);
    Whitebox.setInternalState(compiler, "javaCompiler", javaCompiler);
    Whitebox.setInternalState(compiler, "scriptingResourceResolverProvider", scriptingResourceResolverProvider);
    Object obj = compiler.getResourceBackedUseObject(renderContext, className);
    assertTrue("Expected to obtain a " + MockPojo.class.getName() + " object.", obj instanceof MockPojo);
}
Also used : Options(org.apache.sling.commons.compiler.Options) CompilerMessage(org.apache.sling.commons.compiler.CompilerMessage) InvocationOnMock(org.mockito.invocation.InvocationOnMock) RenderContextImpl(org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl) JavaCompiler(org.apache.sling.commons.compiler.JavaCompiler) CompilationResult(org.apache.sling.commons.compiler.CompilationResult)

Example 2 with JavaCompiler

use of org.apache.sling.commons.compiler.JavaCompiler in project sling by apache.

the class JspcMojo method initServletContext.

// ---------- Additional Settings ------------------------------------------
private void initServletContext() throws IOException, DependencyResolutionRequiredException {
    if (loader == null) {
        initClassLoader();
    }
    context = new JspCServletContext(getLog(), new URL("file:" + uriSourceRoot.replace('\\', '/') + '/'));
    tldLocationsCache = new JspCTldLocationsCache(context, true, loader);
    JavaCompiler compiler = new EclipseJavaCompiler();
    ClassLoaderWriter writer = new JspCClassLoaderWriter(loader, new File(outputDirectory));
    IOProvider ioProvider = new JspCIOProvider(loader, compiler, writer);
    rctxt = new JspRuntimeContext(context, this, ioProvider);
    jspConfig = new JspConfig(context);
    tagPluginManager = new TagPluginManager(context);
}
Also used : ClassLoaderWriter(org.apache.sling.commons.classloader.ClassLoaderWriter) JspConfig(org.apache.sling.scripting.jsp.jasper.compiler.JspConfig) EclipseJavaCompiler(org.apache.sling.commons.compiler.impl.EclipseJavaCompiler) TagPluginManager(org.apache.sling.scripting.jsp.jasper.compiler.TagPluginManager) JavaCompiler(org.apache.sling.commons.compiler.JavaCompiler) EclipseJavaCompiler(org.apache.sling.commons.compiler.impl.EclipseJavaCompiler) URL(java.net.URL) JspRuntimeContext(org.apache.sling.scripting.jsp.jasper.compiler.JspRuntimeContext) IOProvider(org.apache.sling.scripting.jsp.jasper.IOProvider) JarFile(java.util.jar.JarFile) File(java.io.File)

Aggregations

JavaCompiler (org.apache.sling.commons.compiler.JavaCompiler)2 File (java.io.File)1 URL (java.net.URL)1 JarFile (java.util.jar.JarFile)1 ClassLoaderWriter (org.apache.sling.commons.classloader.ClassLoaderWriter)1 CompilationResult (org.apache.sling.commons.compiler.CompilationResult)1 CompilerMessage (org.apache.sling.commons.compiler.CompilerMessage)1 Options (org.apache.sling.commons.compiler.Options)1 EclipseJavaCompiler (org.apache.sling.commons.compiler.impl.EclipseJavaCompiler)1 IOProvider (org.apache.sling.scripting.jsp.jasper.IOProvider)1 JspConfig (org.apache.sling.scripting.jsp.jasper.compiler.JspConfig)1 JspRuntimeContext (org.apache.sling.scripting.jsp.jasper.compiler.JspRuntimeContext)1 TagPluginManager (org.apache.sling.scripting.jsp.jasper.compiler.TagPluginManager)1 RenderContextImpl (org.apache.sling.scripting.sightly.impl.engine.runtime.RenderContextImpl)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1