Search in sources :

Example 1 with JavaConfiguration

use of org.kie.memorycompiler.JavaConfiguration in project drools by kiegroup.

the class ParserTest method testErrorLineNumbers.

// not for exec-model
@Test
public void testErrorLineNumbers() throws Exception {
    // this test aims to test semantic errors
    // parser errors are another test case
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("errors_in_rule.drl", getClass()), ResourceType.DRL);
    final KnowledgeBuilderError[] errors = kbuilder.getErrors().toArray(new KnowledgeBuilderError[0]);
    assertEquals(3, errors.length);
    final DescrBuildError stiltonError = (DescrBuildError) errors[0];
    assertTrue(stiltonError.getMessage().contains("Stilton"));
    assertNotNull(stiltonError.getDescr());
    assertTrue(stiltonError.getLine() != -1);
    // check that its getting it from the ruleDescr
    assertEquals(stiltonError.getLine(), stiltonError.getDescr().getLine());
    // check the absolute error line number (there are more).
    assertEquals(26, stiltonError.getLine());
    final DescrBuildError poisonError = (DescrBuildError) errors[1];
    assertTrue(poisonError.getMessage().contains("Poison"));
    assertEquals(28, poisonError.getLine());
    final KnowledgeBuilderConfigurationImpl cfg = new KnowledgeBuilderConfigurationImpl();
    final JavaConfiguration javaConf = (JavaConfiguration) cfg.getDialectConfiguration("java");
    switch(javaConf.getCompiler()) {
        case NATIVE:
            assertTrue(errors[2].getMessage().contains("illegal"));
            break;
        case ECLIPSE:
            assertTrue(errors[2].getMessage().contains("add"));
            break;
        default:
            fail("Unknown compiler used");
    }
    // now check the RHS, not being too specific yet, as long as it has the
    // rules line number, not zero
    final DescrBuildError rhsError = (DescrBuildError) errors[2];
    assertTrue(rhsError.getLine() >= 23 && rhsError.getLine() <= 32);
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) JavaConfiguration(org.kie.memorycompiler.JavaConfiguration) Test(org.junit.Test)

Example 2 with JavaConfiguration

use of org.kie.memorycompiler.JavaConfiguration in project drools by kiegroup.

the class KieBuilderImpl method compileJavaClasses.

private void compileJavaClasses(ClassLoader classLoader, Predicate<String> classFilter) {
    List<String> classFiles = new ArrayList<>();
    for (KiePath filePath : srcMfs.getFilePaths()) {
        if (filePath.endsWith(".class")) {
            trgMfs.write(filePath, getResource(srcMfs, filePath), true);
            classFiles.add(filePath.substring(0, filePath.asString().length() - ".class".length()).asString());
        }
    }
    List<String> javaFiles = new ArrayList<>();
    List<String> javaTestFiles = new ArrayList<>();
    for (KiePath filePath : srcMfs.getFilePaths()) {
        String fileName = filePath.asString();
        if (isJavaSourceFile(fileName) && noClassFileForGivenSourceFile(classFiles, fileName) && notVetoedByFilter(classFilter, fileName)) {
            if (!fileName.startsWith(JAVA_ROOT) && !fileName.startsWith(JAVA_TEST_ROOT)) {
                results.addMessage(Level.WARNING, fileName, "Found Java file out of the Java source folder: \"" + fileName + "\"");
            } else if (fileName.substring(JAVA_ROOT.length()).indexOf('/') < 0) {
                results.addMessage(Level.ERROR, fileName, "A Java class must have a package: " + fileName.substring(JAVA_ROOT.length()) + " is not allowed");
            } else {
                if (fileName.startsWith(JAVA_ROOT)) {
                    javaFiles.add(fileName);
                } else {
                    javaTestFiles.add(fileName);
                }
            }
        }
    }
    if (!javaFiles.isEmpty() || !javaTestFiles.isEmpty()) {
        KnowledgeBuilderConfigurationImpl kconf = new KnowledgeBuilderConfigurationImpl(classLoader);
        JavaConfiguration javaConf = (JavaConfiguration) kconf.getDialectConfiguration("java");
        compileJavaClasses(javaConf, classLoader, javaFiles, JAVA_ROOT);
        compileJavaClasses(javaConf, classLoader, javaTestFiles, JAVA_TEST_ROOT);
    }
}
Also used : KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) ArrayList(java.util.ArrayList) KiePath(org.kie.memorycompiler.resources.KiePath) JavaConfiguration(org.kie.memorycompiler.JavaConfiguration)

Aggregations

KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)2 JavaConfiguration (org.kie.memorycompiler.JavaConfiguration)2 ArrayList (java.util.ArrayList)1 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)1 Test (org.junit.Test)1 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)1 KnowledgeBuilderError (org.kie.internal.builder.KnowledgeBuilderError)1 KiePath (org.kie.memorycompiler.resources.KiePath)1