Example usage for org.apache.commons.jci.compilers JavaCompiler compile

List of usage examples for org.apache.commons.jci.compilers JavaCompiler compile

Introduction

In this page you can find the example usage for org.apache.commons.jci.compilers JavaCompiler compile.

Prototype

CompilationResult compile(final String[] pResourcePaths, final ResourceReader pReader,
        final ResourceStore pStore, final ClassLoader pClassLoader, final JavaCompilerSettings pSettings);

Source Link

Document

Compiles the java resources "some/path/to/MyJava.java" read through the ResourceReader and then stores the resulting classes in the ResourceStore under "some/path/to/MyJava.class".

Usage

From source file:org.raml.jaxrs.codegen.core.GeneratorTestCase.java

private void run(final JaxrsVersion jaxrsVersion, final boolean useJsr303Annotations) throws Exception {
    final Set<String> generatedSources = new HashSet<String>();

    final Configuration configuration = new Configuration();
    configuration.setJaxrsVersion(jaxrsVersion);
    configuration.setUseJsr303Annotations(useJsr303Annotations);
    configuration.setOutputDirectory(codegenOutputFolder.getRoot());

    configuration.setBasePackageName(TEST_BASE_PACKAGE);
    generatedSources.addAll(new Generator().run(
            new InputStreamReader(getClass().getResourceAsStream("/org/raml/full-config-with-patch.yaml")),
            configuration));//from   w ww .j ava  2 s .  com

    configuration.setBasePackageName(TEST_BASE_PACKAGE + ".params");
    generatedSources.addAll(new Generator().run(
            new InputStreamReader(
                    getClass().getResourceAsStream("/org/raml/params/param-types-with-repeat.yaml")),
            configuration));

    configuration.setBasePackageName(TEST_BASE_PACKAGE + ".integration");
    generatedSources.addAll(new Generator().run(
            new InputStreamReader(getClass()
                    .getResourceAsStream("/org/raml/integration/sales-enablement-api-with-collections.yaml")),
            configuration));

    configuration.setBasePackageName(TEST_BASE_PACKAGE + ".rules");
    generatedSources.addAll(new Generator().run(
            new InputStreamReader(getClass().getResourceAsStream("/org/raml/rules/resource-full-ok.yaml")),
            configuration));
    generatedSources.addAll(new Generator().run(
            new InputStreamReader(
                    getClass().getResourceAsStream("/org/raml/rules/resource-with-description-ok.yaml")),
            configuration));
    generatedSources.addAll(new Generator().run(
            new InputStreamReader(getClass().getResourceAsStream("/org/raml/rules/resource-with-uri.yaml")),
            configuration));

    configuration.setBasePackageName(TEST_BASE_PACKAGE + ".schema");
    generatedSources.addAll(new Generator().run(
            new InputStreamReader(getClass().getResourceAsStream("/org/raml/schema/valid-xml-global.yaml")),
            configuration));
    generatedSources.addAll(new Generator().run(
            new InputStreamReader(getClass().getResourceAsStream("/org/raml/schema/valid-xml.yaml")),
            configuration));

    // test compile the classes
    final JavaCompiler compiler = new JavaCompilerFactory().createCompiler("eclipse");

    final JavaCompilerSettings settings = compiler.createDefaultSettings();
    settings.setSourceVersion("1.5");
    settings.setTargetVersion("1.5");
    settings.setDebug(true);

    final String[] sources = generatedSources.toArray(EMPTY_STRING_ARRAY);
    System.out.println("Test compiling: " + Arrays.toString(sources));

    final FileResourceReader sourceReader = new FileResourceReader(codegenOutputFolder.getRoot());
    final FileResourceStore classWriter = new FileResourceStore(compilationOutputFolder.getRoot());
    final CompilationResult result = compiler.compile(sources, sourceReader, classWriter,
            Thread.currentThread().getContextClassLoader(), settings);

    assertThat(ToStringBuilder.reflectionToString(result.getErrors(), ToStringStyle.SHORT_PREFIX_STYLE),
            result.getErrors(), is(emptyArray()));

    assertThat(ToStringBuilder.reflectionToString(result.getWarnings(), ToStringStyle.SHORT_PREFIX_STYLE),
            result.getWarnings(), is(emptyArray()));

    // test load the classes with Jersey
    final URLClassLoader resourceClassLoader = new URLClassLoader(
            new URL[] { compilationOutputFolder.getRoot().toURI().toURL() });

    final ClassLoader initialClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(resourceClassLoader);
        final ResourceConfig config = new PackagesResourceConfig(TEST_BASE_PACKAGE);

        assertThat("Found: " + config.getRootResourceClasses(), config.getRootResourceClasses(), hasSize(13));

        // TODO testing: actually send HTTP requests at the resources
    } finally {
        Thread.currentThread().setContextClassLoader(initialClassLoader);
    }
}

From source file:org.raml.jaxrs.codegen.core.MediaTypeGeneratorTestCase.java

private void run(final JaxrsVersion jaxrsVersion, final boolean useJsr303Annotations) throws Exception {
    final Set<String> generatedSources = new HashSet<String>();

    final Configuration configuration = new Configuration();
    configuration.setJaxrsVersion(jaxrsVersion);
    configuration.setUseJsr303Annotations(useJsr303Annotations);
    configuration.setOutputDirectory(codegenOutputFolder.getRoot());

    configuration.setBasePackageName(TEST_BASE_PACKAGE);
    String dirPath = getClass().getResource("/org/raml").getPath();

    configuration.setSourceDirectory(new File(dirPath));
    generatedSources.addAll(new Generator().run(
            new InputStreamReader(
                    getClass().getResourceAsStream("/org/raml/mediatype/application_octet-stream.yaml")),
            configuration));/*w  w w . jav a  2 s .c o m*/

    // test compile the classes
    final JavaCompiler compiler = new JavaCompilerFactory().createCompiler("eclipse");

    final JavaCompilerSettings settings = compiler.createDefaultSettings();
    settings.setSourceVersion("1.5");
    settings.setTargetVersion("1.5");
    settings.setDebug(true);

    final String[] sources = generatedSources.toArray(EMPTY_STRING_ARRAY);

    final FileResourceReader sourceReader = new FileResourceReader(codegenOutputFolder.getRoot());
    final FileResourceStore classWriter = new FileResourceStore(compilationOutputFolder.getRoot());
    final CompilationResult result = compiler.compile(sources, sourceReader, classWriter,
            Thread.currentThread().getContextClassLoader(), settings);

    assertThat(ToStringBuilder.reflectionToString(result.getErrors(), ToStringStyle.SHORT_PREFIX_STYLE),
            result.getErrors(), is(emptyArray()));

    assertThat(ToStringBuilder.reflectionToString(result.getWarnings(), ToStringStyle.SHORT_PREFIX_STYLE),
            result.getWarnings(), is(emptyArray()));

    // test load the classes with Jersey
    final URLClassLoader resourceClassLoader = new URLClassLoader(
            new URL[] { compilationOutputFolder.getRoot().toURI().toURL() });

    final ClassLoader initialClassLoader = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(resourceClassLoader);
        final ResourceConfig config = new PackagesResourceConfig(TEST_BASE_PACKAGE);

        Set<Class<?>> classes = config.getClasses();

        Iterator<Class<?>> it = classes.iterator();
        Class<?> something = it.next();
        Method[] methods = something.getDeclaredMethods();

        Method methodWithInputStreamParam = methods[0];

        assertEquals(InputStream.class.getName(), methodWithInputStreamParam.getParameterTypes()[0].getName());
    } finally {
        Thread.currentThread().setContextClassLoader(initialClassLoader);
    }
}

From source file:org.raml.jaxrs.codegen.core.ResponseWrapperGeneratorTestCase.java

private void run(final JaxrsVersion jaxrsVersion, final boolean useJsr303Annotations) throws Exception {
    final Set<String> generatedSources = new HashSet<String>();

    final Configuration configuration = new Configuration();
    configuration.setJaxrsVersion(jaxrsVersion);
    configuration.setUseJsr303Annotations(useJsr303Annotations);
    configuration.setOutputDirectory(codegenOutputFolder.getRoot());

    configuration.setBasePackageName(TEST_BASE_PACKAGE);
    String dirPath = getClass().getResource("/org/raml").getPath();

    configuration.setSourceDirectory(new File(dirPath));
    generatedSources.addAll(new Generator().run(
            new InputStreamReader(getClass().getResourceAsStream("/org/raml/responses/wrapper.yaml")),
            configuration));//from  w w w  .  j  a  v a2 s . co m

    // test compile the classes
    final JavaCompiler compiler = new JavaCompilerFactory().createCompiler("eclipse");

    final JavaCompilerSettings settings = compiler.createDefaultSettings();
    settings.setSourceVersion("1.5");
    settings.setTargetVersion("1.5");
    settings.setDebug(true);

    final String[] sources = generatedSources.toArray(EMPTY_STRING_ARRAY);

    final FileResourceReader sourceReader = new FileResourceReader(codegenOutputFolder.getRoot());
    final FileResourceStore classWriter = new FileResourceStore(compilationOutputFolder.getRoot());
    final CompilationResult result = compiler.compile(sources, sourceReader, classWriter,
            Thread.currentThread().getContextClassLoader(), settings);
    CompilationProblem[] errors = result.getErrors();
    assertThat(ToStringBuilder.reflectionToString(errors, ToStringStyle.SHORT_PREFIX_STYLE), errors,
            is(emptyArray()));
    assertThat(ToStringBuilder.reflectionToString(result.getWarnings(), ToStringStyle.SHORT_PREFIX_STYLE),
            result.getWarnings(), is(emptyArray()));
}