Skip to content

Commit

Permalink
[DROOLS-270] Primitve globals will result in a runtime exception
Browse files Browse the repository at this point in the history
  • Loading branch information
sotty committed Oct 2, 2013
1 parent a87a45c commit 0198b14
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
Expand Up @@ -20,10 +20,12 @@

public class GlobalError extends DroolsError {
private final GlobalDescr globalDescr;
private String message;

public GlobalError(final GlobalDescr globalDescr) {
public GlobalError(final GlobalDescr globalDescr, String message) {
super(globalDescr.getResource());
this.globalDescr = globalDescr;
this.message = message;
}

@Override
Expand All @@ -40,11 +42,11 @@ public int[] getLines() {
}

public String getMessage() {
return getGlobal();
return message;
}

public String toString() {
return "GlobalError: " + getGlobal();
return "GlobalError: " + getGlobal() + " : " + message;
}

}
Expand Up @@ -1437,12 +1437,16 @@ private void processGlobals(PackageRegistry pkgRegistry, PackageDescr packageDes

try {
Class<?> clazz = pkgRegistry.getTypeResolver().resolveType(className);
if ( clazz.isPrimitive() ) {
this.results.add( new GlobalError( global, " Primitive types are not allowed in globals : " + className ) );
return;
}
pkgRegistry.getPackage().addGlobal( identifier,
clazz );
this.globals.put( identifier,
clazz );
} catch (final ClassNotFoundException e) {
this.results.add( new GlobalError( global ) );
this.results.add( new GlobalError( global, e.getMessage() ) );
e.printStackTrace();
}
}
Expand Down
Expand Up @@ -2522,4 +2522,18 @@ public void testBindingComplexExpressionErrors() {



@Test
public void testPrimitiveGlobals() {
String drl = "package org.drools.compiler.integrationtests\n" +
"\n" +
"global int foo;\n" +
"\n" +
"";
KnowledgeBuilder kb = KnowledgeBuilderFactory.newKnowledgeBuilder();
kb.add( new ByteArrayResource( drl.getBytes() ), ResourceType.DRL );
System.out.println( kb.getErrors() );
assertTrue( kb.hasErrors() );
}


}

0 comments on commit 0198b14

Please sign in to comment.