Search in sources :

Example 1 with MemoryResourceStore

use of org.kie.memorycompiler.resources.MemoryResourceStore in project drools by kiegroup.

the class JavaCompilerI18NTest method testi18NFile.

@Test
public void testi18NFile() throws Exception {
    String fileStr = "com/myspace/test/あ.java";
    List<String> classes = new ArrayList<>();
    classes.add(fileStr);
    MemoryResourceReader reader = new MemoryResourceReader();
    MemoryResourceStore store = new MemoryResourceStore();
    String fileContents = "package com.myspace.test; public class あ { }";
    reader.add(fileStr, fileContents.getBytes());
    EclipseJavaCompilerSettings settings = new EclipseJavaCompilerSettings();
    settings.setSourceVersion("1.5");
    settings.setTargetVersion("1.5");
    EclipseJavaCompiler compiler = new EclipseJavaCompiler(settings, "");
    CompilationResult res = compiler.compile(classes.toArray(new String[classes.size()]), reader, store);
    assertEquals(res.getErrors().length, 0);
}
Also used : MemoryResourceStore(org.kie.memorycompiler.resources.MemoryResourceStore) MemoryResourceReader(org.kie.memorycompiler.resources.MemoryResourceReader) ArrayList(java.util.ArrayList) CompilationResult(org.kie.memorycompiler.CompilationResult) Test(org.junit.Test)

Example 2 with MemoryResourceStore

use of org.kie.memorycompiler.resources.MemoryResourceStore in project drools by kiegroup.

the class KieMemoryCompiler method compileNoLoad.

/**
 * Compile the given sources and returns the generated byte codes.
 * Additional compiler settings can be provided using JavaCompilerSettings
 *
 * @param classNameSourceMap
 * @param classLoader
 * @param compilerSettings
 * @return
 */
public static Map<String, byte[]> compileNoLoad(Map<String, String> classNameSourceMap, ClassLoader classLoader, JavaCompilerSettings compilerSettings, JavaConfiguration.CompilerType compilerType) {
    MemoryResourceReader reader = new MemoryResourceReader();
    MemoryResourceStore store = new MemoryResourceStore();
    String[] classNames = new String[classNameSourceMap.size()];
    int i = 0;
    for (Map.Entry<String, String> entry : classNameSourceMap.entrySet()) {
        classNames[i] = toJavaSource(entry.getKey());
        reader.add(classNames[i], entry.getValue().getBytes());
        i++;
    }
    JavaConfiguration javaConfiguration = new JavaConfiguration();
    javaConfiguration.setCompiler(compilerType);
    javaConfiguration.setJavaLanguageLevel(findJavaVersion());
    JavaCompiler compiler = JavaCompilerFactory.loadCompiler(javaConfiguration);
    CompilationResult res = compilerSettings == null ? compiler.compile(classNames, reader, store, classLoader) : compiler.compile(classNames, reader, store, classLoader, compilerSettings);
    if (res.getErrors().length > 0) {
        throw new KieMemoryCompilerException(Arrays.toString(res.getErrors()));
    }
    Map<String, byte[]> toReturn = new HashMap<>();
    for (Map.Entry<KiePath, byte[]> entry : store.getResources().entrySet()) {
        toReturn.put(toClassName(entry.getKey().asString()), entry.getValue());
    }
    return toReturn;
}
Also used : HashMap(java.util.HashMap) MemoryResourceStore(org.kie.memorycompiler.resources.MemoryResourceStore) MemoryResourceReader(org.kie.memorycompiler.resources.MemoryResourceReader) KiePath(org.kie.memorycompiler.resources.KiePath) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

MemoryResourceReader (org.kie.memorycompiler.resources.MemoryResourceReader)2 MemoryResourceStore (org.kie.memorycompiler.resources.MemoryResourceStore)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.junit.Test)1 CompilationResult (org.kie.memorycompiler.CompilationResult)1 KiePath (org.kie.memorycompiler.resources.KiePath)1