Author: mar...@jb... Date: 2006-07-08 09:02:09 -0400 (Sat, 08 Jul 2006) New Revision: 4950 Added: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/compiler/Java5Test.java labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/compiler/java5_rule.drl Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilder.java labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilderConfiguration.java Log: JBRULES-363 Java 5 language support Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilder.java =================================================================== --- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilder.java 2006-07-08 00:31:45 UTC (rev 4949) +++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilder.java 2006-07-08 13:02:09 UTC (rev 4950) @@ -28,12 +28,14 @@ import java.util.Set; import org.apache.commons.jci.compilers.CompilationResult; +import org.apache.commons.jci.compilers.EclipseJavaCompiler; +import org.apache.commons.jci.compilers.EclipseJavaCompilerSettings; import org.apache.commons.jci.compilers.JavaCompiler; import org.apache.commons.jci.compilers.JavaCompilerFactory; import org.apache.commons.jci.problems.CompilationProblem; import org.apache.commons.jci.readers.MemoryResourceReader; import org.apache.commons.jci.readers.ResourceReader; -import org.apache.commons.jci.stores.ResourceStore; +import org.drools.RuntimeDroolsException; import org.drools.base.ClassFieldExtractorCache; import org.drools.lang.descr.FunctionDescr; import org.drools.lang.descr.PackageDescr; @@ -99,8 +101,8 @@ configuration = new PackageBuilderConfiguration(); } - this.compiler = getCompiler( configuration.getCompiler() ); this.configuration = configuration; + loadCompiler(); this.src = new MemoryResourceReader(); this.results = new ArrayList(); this.errorHandlers = new HashMap(); @@ -298,9 +300,10 @@ "java", this.src ); ruleDescr.SetClassName( ucFirst( ruleClassName ) ); - - final RuleBuilder builder = new RuleBuilder( getTypeResolver(), classFieldExtractorCache ); + final RuleBuilder builder = new RuleBuilder( getTypeResolver(), + classFieldExtractorCache ); + builder.build( this.pkg, ruleDescr ); @@ -351,8 +354,8 @@ * It will not actually call the compiler */ private void addRuleSemantics(final RuleBuilder builder, - final Rule rule, - final RuleDescr ruleDescr) { + final Rule rule, + final RuleDescr ruleDescr) { // The compilation result is for th entire rule, so difficult to associate with any descr addClassCompileTask( this.pkg.getName() + "." + ruleDescr.getClassName(), builder.getRuleClass(), @@ -425,13 +428,13 @@ Collection errors = this.errorHandlers.values(); for ( Iterator iter = errors.iterator(); iter.hasNext(); ) { ErrorHandler handler = (ErrorHandler) iter.next(); - if ( !(handler instanceof RuleInvokerErrorHandler) ) { + if ( !(handler instanceof RuleInvokerErrorHandler) ) { this.results.add( handler.getError() ); } else { //we don't really want to report invoker errors. //mostly as they can happen when there is a syntax error in the RHS //and otherwise, it is a programmatic error in drools itself. - System.err.println( "!!!! An error occurred compiling the invoker: " + handler.getError() ); + System.err.println( "!!!! An error occurred compiling the invoker: " + handler.getError() ); } } } @@ -498,13 +501,21 @@ return newName; } - private JavaCompiler getCompiler(final int compiler) { - switch ( compiler ) { - case PackageBuilderConfiguration.JANINO : - return JavaCompilerFactory.getInstance().createCompiler( "janino" ); + private void loadCompiler() { + switch ( configuration.getCompiler() ) { + case PackageBuilderConfiguration.JANINO : { + if ( !"1.4".intern().equals( configuration.getJavaLanguageLevel() ) ) throw new RuntimeDroolsException( "Incompatible Java language level with selected compiler" ); + compiler = JavaCompilerFactory.getInstance().createCompiler( "janino" ); + } case PackageBuilderConfiguration.ECLIPSE : - default : - return JavaCompilerFactory.getInstance().createCompiler( "eclipse" ); + default : { + EclipseJavaCompilerSettings eclipseSettings = new EclipseJavaCompilerSettings(); + eclipseSettings.getMap().put( "org.eclipse.jdt.core.compiler.codegen.targetPlatform", + configuration.getJavaLanguageLevel() ); + eclipseSettings.getMap().put( "org.eclipse.jdt.core.compiler.source", + configuration.getJavaLanguageLevel() ); + compiler = new EclipseJavaCompiler( eclipseSettings ); + } } } @@ -620,4 +631,6 @@ } + private static JavaCompiler cachedJavaCompiler = null; + } \ No newline at end of file Modified: labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilderConfiguration.java =================================================================== --- labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilderConfiguration.java 2006-07-08 00:31:45 UTC (rev 4949) +++ labs/jbossrules/trunk/drools-compiler/src/main/java/org/drools/compiler/PackageBuilderConfiguration.java 2006-07-08 13:02:09 UTC (rev 4950) @@ -16,6 +16,8 @@ * limitations under the License. */ +import java.util.Arrays; + import org.drools.RuntimeDroolsException; /** @@ -27,6 +29,9 @@ * * You can also use the system property "drools.compiler" to set the desired compiler. * The valid values are "ECLIPSE" and "JANINO" only. + * + * The default Java language level is 1.4 but it can be configured using the + * system property "drools.compiler.lnglevel". Valid values are 1.4, 1.5 and 1.6. */ public class PackageBuilderConfiguration { public static final int ECLIPSE = 0; @@ -38,6 +43,12 @@ private int compiler = PackageBuilderConfiguration.CONFIGURED_COMPILER; private ClassLoader classLoader; + + public static final String DEFAULT_LANGUAGE_LEVEL = "1.4"; + + public static final String[] LANGUAGE_LEVELS = new String[] {"1.4","1.5","1.6"}; + + private String languageLevel = null; public PackageBuilderConfiguration() { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); @@ -50,6 +61,19 @@ public int getCompiler() { return this.compiler; } + + public String getJavaLanguageLevel() { + if (languageLevel != null) + return languageLevel; + languageLevel = System.getProperty( "drools.compiler.lnglevel", DEFAULT_LANGUAGE_LEVEL ); + if (Arrays.binarySearch( LANGUAGE_LEVELS, languageLevel ) < 0) + throw new RuntimeDroolsException( "value '" + languageLevel + "' is not a valid language level" ); + return languageLevel; + } + + public void setJavaLanguageLevel(String level) { + languageLevel = level; + } /** * Set the compiler to be used when building the rules semantic code blocks. Added: labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/compiler/Java5Test.java =================================================================== --- labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/compiler/Java5Test.java 2006-07-08 00:31:45 UTC (rev 4949) +++ labs/jbossrules/trunk/drools-compiler/src/test/java/org/drools/compiler/Java5Test.java 2006-07-08 13:02:09 UTC (rev 4950) @@ -0,0 +1,33 @@ +package org.drools.compiler; + +import java.io.InputStreamReader; + +import org.drools.DroolsTestCase; + +public class Java5Test extends DroolsTestCase { + + public void testJava5Rule() throws Exception { + String javaVersion = System.getProperty( "java.specification.version" ); + //do not execute tests under JDK 1.4 + //otherwise the compiled version cannot be interpreted + if (javaVersion.equals( "1.4" )) { + System.out.println("Skipping Java 1.5 tests - current JDK not compatible"); + return; + } + PackageBuilderConfiguration conf = new PackageBuilderConfiguration(); + conf.setCompiler( PackageBuilderConfiguration.ECLIPSE ); + conf.setJavaLanguageLevel( "1.5" ); + final PackageBuilder builder = new PackageBuilder(conf); + builder.addPackageFromDrl( new InputStreamReader( this.getClass().getResourceAsStream( "java5_rule.drl" ) ) ); + assertFalse( builder.hasErrors() ); + } + + public void testJava14Defaults() throws Exception { + PackageBuilderConfiguration conf = new PackageBuilderConfiguration(); + conf.setCompiler( PackageBuilderConfiguration.JANINO ); + final PackageBuilder builder = new PackageBuilder(conf); + builder.addPackageFromDrl( new InputStreamReader( this.getClass().getResourceAsStream( "java5_rule.drl" ) ) ); + assertTrue( builder.hasErrors() ); + } + +} \ No newline at end of file Added: labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/compiler/java5_rule.drl =================================================================== --- labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/compiler/java5_rule.drl 2006-07-08 00:31:45 UTC (rev 4949) +++ labs/jbossrules/trunk/drools-compiler/src/test/resources/org/drools/compiler/java5_rule.drl 2006-07-08 13:02:09 UTC (rev 4950) @@ -0,0 +1,13 @@ +package org.drools.test; + +import org.drools.Cheese; + +rule "like cheddar" + when + Cheese( $type:type ) + then + System.out.println("I like " + $type); + for (byte bt:$type.getBytes()) { + System.out.println("byte=" + bt); + } +end \ No newline at end of file |