Permalink
Browse files

[DROOLS-83] allow to use imported classes in declared types fields in…

…itialization
  • Loading branch information...
1 parent e41093a commit 0b6117875bc076e9d70c3701aa5d53bd3312e495 @mariofusco mariofusco committed Mar 19, 2013
@@ -2333,8 +2333,7 @@ private Class resolveAnnotation( String annotation,
}
try {
- return resolver.resolveType( annotation.substring( 0,
- 1 ).toUpperCase() + annotation.substring( 1 ) );
+ return resolver.resolveType(annotation.substring(0, 1).toUpperCase() + annotation.substring(1));
} catch (ClassNotFoundException e) {
// internal annotation, or annotation which can't be resolved.
return null;
@@ -2552,7 +2551,67 @@ private void generateDeclaredBean( AbstractClassTypeDeclarationDescr typeDescr,
generateDeclaredBean( typeDescr,
type,
pkgRegistry,
- def );
+ expandImportsInFieldInitExpr( def, pkgRegistry ) );
+ }
+
+ private ClassDefinition expandImportsInFieldInitExpr(ClassDefinition def, PackageRegistry pkgRegistry) {
+ TypeResolver typeResolver = pkgRegistry.getPackage().getTypeResolver();
+ for (FieldDefinition field : def.getFieldsDefinitions()) {
+ field.setInitExpr(rewriteInitExprWithImports(field.getInitExpr(), typeResolver));
+ }
+ return def;
+ }
+
+ private String rewriteInitExprWithImports(String expr, TypeResolver typeResolver) {
+ if (expr == null) {
+ return null;
+ }
+ StringBuilder sb = new StringBuilder();
+ boolean inQuotes = false;
+ boolean inTypeName = false;
+ boolean afterDot = false;
+ int typeStart = 0;
+ for (int i = 0; i < expr.length(); i++) {
+ char ch = expr.charAt(i);
+ if (Character.isJavaIdentifierStart(ch)) {
+ if (!inTypeName && !inQuotes && !afterDot) {
+ typeStart = i;
+ inTypeName = true;
+ }
+ } else if (!Character.isJavaIdentifierPart(ch)) {
+ if (ch == '"') {
+ inQuotes = !inQuotes;
+ } else if (ch == '.' && !inQuotes) {
+ afterDot = true;
+ } else if (!Character.isSpaceChar(ch)) {
+ afterDot = false;
+ }
+ if (inTypeName) {
+ inTypeName = false;
+ String type = expr.substring(typeStart, i);
+ sb.append(getFullTypeName(type, typeResolver));
+ }
+ }
+ if (!inTypeName) {
+ sb.append(ch);
+ }
+ }
+ if (inTypeName) {
+ String type = expr.substring(typeStart);
+ sb.append(getFullTypeName(type, typeResolver));
+ }
+ return sb.toString();
+ }
+
+ private String getFullTypeName(String type, TypeResolver typeResolver) {
+ if (type.equals("new")) {
+ return type;
+ }
+ try {
+ return typeResolver.getFullTypeName(type);
+ } catch (ClassNotFoundException e) {
+ return type;
+ }
}
private void generateDeclaredBean( AbstractClassTypeDeclarationDescr typeDescr,
@@ -8971,7 +8971,7 @@ public void testDeclaresWithArrayFields() throws Exception {
" zint\t: Integer[] " + " = new Integer[] {2,3} @key \n" +
" aaaa\t: String[][] \n" +
" bbbb\t: int[][] \n" +
- " aprs\t: Person[] " + " = new org.drools.compiler.test.Person[] { new org.drools.compiler.test.Man() } \n" +
+ " aprs\t: Person[] " + " = new Person[] { new Man() } \n" +
"end\n" +
"\n" +
"rule \"Init\"\n" +

0 comments on commit 0b61178

Please sign in to comment.