Skip to content

Commit

Permalink
[DROOLS-677] Resolve nested classes when parent is imported
Browse files Browse the repository at this point in the history
(cherry picked from commit d88a83e)
  • Loading branch information
sotty authored and mariofusco committed Jan 2, 2015
1 parent 56155b2 commit c625aec
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 14 deletions.
Expand Up @@ -169,18 +169,21 @@ private String resolveName( String type,
TypeResolver typeResolver,
List<TypeDefinition> unresolvedTypes,
boolean forceResolution ) {
boolean qualified = TypeDeclarationUtils.isQualified( type );

if ( ! TypeDeclarationUtils.isQualified( type ) ) {
if ( ! qualified ) {
type = TypeDeclarationUtils.lookupSimpleNameByImports( type, typeDescr, packageDescr, kbuilder.getRootClassLoader() );
}

if ( ! TypeDeclarationUtils.isQualified( type ) ) {
type = TypeDeclarationUtils.resolveType( type,
packageDescr,
kbuilder.getPackageRegistry( packageDescr.getNamespace() ) );
}
// if not qualified yet, it has to be resolved
// DROOLS-677 : if qualified, it may be a partial name (e.g. an inner class)
type = TypeDeclarationUtils.resolveType( type,
packageDescr,
kbuilder.getPackageRegistry( packageDescr.getNamespace() ) );
qualified = TypeDeclarationUtils.isQualified( type );


if ( ! TypeDeclarationUtils.isQualified( type ) ) {
if ( ! qualified ) {
try {
Class klass = typeResolver.resolveType( type, TypeResolver.EXCLUDE_ANNOTATION_CLASS_FILTER );
type = klass.getCanonicalName();
Expand All @@ -190,14 +193,15 @@ private String resolveName( String type,
}
} else {
type = TypeDeclarationUtils.typeName2ClassName( type, kbuilder.getRootClassLoader() );
qualified = TypeDeclarationUtils.isQualified( type );
}

if ( forceResolution && ! TypeDeclarationUtils.isQualified( type ) ) {
if ( forceResolution && ! qualified ) {
TypeDeclaration temp = new TypeDeclaration( type );
temp.setTypeClassName( type );
unresolvedTypes.add( new TypeDefinition( temp, null ) );
}
return TypeDeclarationUtils.isQualified( type ) ? type : null;
return qualified ? type : null;
}


Expand Down
Expand Up @@ -93,12 +93,17 @@ public static String resolveType( String klass,
}

//look among imports
for (ImportDescr id : packageDescr.getImports()) {
String fqKlass = id.getTarget();
if ( fqKlass.endsWith( "." + klass ) ) {
//logger.info("Replace supertype " + sup + " with full name " + id.getTarget());
return arrayIndex < 0 ? fqKlass : fqKlass + arraySuffix;
String temp = klass;
while ( temp.length() > 0 ) {
for ( ImportDescr id : packageDescr.getImports() ) {
String fqKlass = id.getTarget();
if ( fqKlass.endsWith( "." + temp ) ) {
//logger.info("Replace supertype " + sup + " with full name " + id.getTarget());
fqKlass = fqKlass.substring( 0, fqKlass.lastIndexOf( temp ) ) + klass;
return arrayIndex < 0 ? fqKlass : fqKlass + arraySuffix;
}
}
temp = temp.substring( 0, Math.max( 0, temp.lastIndexOf( '.' ) ) );
}

//look among local declarations
Expand Down
Expand Up @@ -7002,4 +7002,22 @@ public int getValue() {
return wrapped != null ? wrapped.length() : 0;
}
}

@Test
public void testImportInner() throws Exception {
// DROOLS-677
String drl =
"package org.drools.test; " +
"import " + Misc2Test.class.getName() + "; " +

"declare Foo " +
" bar : Misc2Test.AA " +
"end " +
"" ;

KieHelper helper = new KieHelper();
helper.addContent( drl, ResourceType.DRL );
assertTrue( helper.verify().getMessages( org.kie.api.builder.Message.Level.ERROR ).isEmpty() );
}

}

0 comments on commit c625aec

Please sign in to comment.