final ProducedType bestSatisfiedType = getBestSatisfiedType(model.getType(), iface);
JCExpression containerInstance = null;
if(!Decl.isToplevel(iface) && !Decl.isLocal(iface)){
// if it's a member type we need to qualify the new instance with its $impl container
ClassOrInterface interfaceContainer = Decl.getClassOrInterfaceContainer(iface, false);
if(interfaceContainer instanceof Interface){
ClassOrInterface modelContainer = model;
while((modelContainer = Decl.getClassOrInterfaceContainer(modelContainer, false)) != null
&& modelContainer.getType().getSupertype(interfaceContainer) == null){
// keep searching
}
if (modelContainer == null) {
throw new BugException("Could not find container that satisfies interface "
+ iface.getQualifiedNameString() + " to find qualifying instance for companion instance for "
+ model.getQualifiedNameString());
}
// if it's an interface we just qualify it properly
if(modelContainer instanceof Interface){
JCExpression containerType = makeJavaType(modelContainer.getType(), JT_COMPANION | JT_SATISFIES | JT_RAW);
containerInstance = makeSelect(containerType, "this");
}else{
// it's a class: find the right field used for the interface container impl
String containerFieldName = getCompanionFieldName((Interface)interfaceContainer);
JCExpression containerType = makeJavaType(modelContainer.getType(), JT_SATISFIES);
containerInstance = makeSelect(makeSelect(containerType, "this"), containerFieldName);
}
}
}