Program: 102044
Project: 102044
Buggy source:
/** 
 * Resolve function such that all functions are resolved and type-safe.
 */
void resolveFunction(Function function,FunctionLibrary library) throws Exception {
  if (function.getFunctionDescriptor() != null) {
    return;
  }
  boolean hasArgWithoutType=false;
  Expression[] args=function.getArgs();
  Class<?>[] types=new Class[args.length];
  for (int i=0; i < args.length; i++) {
    types[i]=args[i].getType();
    if (types[i] == null) {
      if (!(args[i] instanceof Reference)) {
        throw new QueryResolverException(Messages.gs(Messages.TEIID.TEIID30067,new Object[]{args[i],function}));
      }
      hasArgWithoutType=true;
    }
  }
  if (FunctionLibrary.isConvert(function) && hasArgWithoutType) {
    Constant constant=(Constant)function.getArg(1);
    Class<?> type=getDataTypeManager().getDataTypeClass((String)constant.getValue());
    setDesiredType(function.getArg(0),type,function);
    types[0]=type;
    hasArgWithoutType=false;
  }
  List<FunctionDescriptor> fds;
  try {
    fds=findWithImplicitConversions(library,function,args,types,hasArgWithoutType);
    if (fds.isEmpty()) {
      if (!library.hasFunctionMethod(function.getName(),args.length)) {
        throw new QueryResolverException(Messages.gs(Messages.TEIID.TEIID30068,function));
      }
      if (hasArgWithoutType) {
        throw new QueryResolverException(Messages.gs(Messages.TEIID.TEIID30069,function));
      }
      throw new QueryResolverException(Messages.gs(Messages.TEIID.TEIID30070,function));
    }
    if (fds.size() > 1) {
      throw new QueryResolverException(Messages.gs(Messages.TEIID.TEIID31150,function));
    }
  }
 catch (  Exception e) {
    if (hasArgWithoutType) {
      throw new QueryResolverException(Messages.gs(Messages.TEIID.TEIID30069,function));
    }
    throw new QueryResolverException(Messages.gs(Messages.TEIID.TEIID31150,function));
  }
  FunctionDescriptor fd=fds.get(0);
  if (fd.getMethod().isVarArgs() && fd.getTypes().length == types.length && library.isVarArgArrayParam(fd.getMethod(),types,types.length - 1,fd.getTypes()[types.length - 1])) {
    fd=fd.clone();
    fd.setCalledWithVarArgArrayParam(true);
  }
  if (fd.isSystemFunction(IFunctionLibrary.FunctionName.CONVERT) || fd.isSystemFunction(IFunctionLibrary.FunctionName.CAST)) {
    String dataType=(String)((Constant)args[1]).getValue();
    Class<?> dataTypeClass=getDataTypeManager().getDataTypeClass(dataType);
    fd=library.findTypedConversionFunction(args[0].getType(),dataTypeClass);
    Class<?> srcTypeClass=args[0].getType();
    if (srcTypeClass != null && dataTypeClass != null && !srcTypeClass.equals(dataTypeClass) && !getDataTypeManager().isTransformable(srcTypeClass,dataTypeClass)) {
      throw new QueryResolverException(Messages.gs(Messages.TEIID.TEIID30071,new Object[]{getDataTypeManager().getDataTypeName(srcTypeClass),dataType}));
    }
  }
 else   if (fd.isSystemFunction(IFunctionLibrary.FunctionName.LOOKUP)) {
    ResolverUtil.ResolvedLookup lookup=ResolverUtil.resolveLookup(function,metadata);
    fd=library.copyFunctionChangeReturnType(fd,lookup.getReturnElement().getType());
  }
 else   if (fd.isSystemFunction(IFunctionLibrary.FunctionName.ARRAY_GET) && args[0].getType().isArray()) {
    if (args[0].getType().isArray()) {
      fd=library.copyFunctionChangeReturnType(fd,args[0].getType().getComponentType());
    }
 else {
      if (function.getType() != null) {
        setDesiredType(args[0],function.getType(),function);
      }
      if (args[0].getType() != DataTypeManagerService.DefaultDataTypes.OBJECT.getTypeClass()) {
        throw new QueryResolverException(Messages.gs(Messages.TEIID.TEIID31145,getDataTypeManager().getDataTypeName(args[0].getType()),function));
      }
    }
  }
 else   if (Boolean.valueOf(fd.getMethod().getProperty(TEIID_PASS_THROUGH_TYPE,false))) {
    fd=library.copyFunctionChangeReturnType(fd,args[0].getType());
  }
  function.setFunctionDescriptor(fd);
  function.setType(fd.getReturnType());
  if (CoreConstants.SYSTEM_MODEL.equals(fd.getSchema()) && StringUtil.startsWithIgnoreCase(function.getName(),SYS_PREFIX)) {
    function.setName(function.getName().substring(SYS_PREFIX.length()));
  }
}

Fixed version (ground truth):
/** 
 * Resolve function such that all functions are resolved and type-safe.
 */
void resolveFunction(Function function,FunctionLibrary library) throws Exception {
  if (function.getFunctionDescriptor() != null) {
    return;
  }
  boolean hasArgWithoutType=false;
  Expression[] args=function.getArgs();
  Class<?>[] types=new Class[args.length];
  for (int i=0; i < args.length; i++) {
    types[i]=args[i].getType();
    if (types[i] == null) {
      if (!(args[i] instanceof Reference)) {
        throw new QueryResolverException(Messages.gs(Messages.TEIID.TEIID30067,new Object[]{args[i],function}));
      }
      hasArgWithoutType=true;
    }
  }
  if (FunctionLibrary.isConvert(function) && hasArgWithoutType) {
    Constant constant=(Constant)function.getArg(1);
    Class<?> type=getDataTypeManager().getDataTypeClass((String)constant.getValue());
    setDesiredType(function.getArg(0),type,function);
    types[0]=type;
    hasArgWithoutType=false;
  }
  List<FunctionDescriptor> fds;
  try {
    fds=findWithImplicitConversions(library,function,args,types,hasArgWithoutType);
    if (fds.isEmpty()) {
      if (!library.hasFunctionMethod(function.getName(),args.length)) {
        throw new QueryResolverException(Messages.gs(Messages.TEIID.TEIID30068,function));
      }
      if (hasArgWithoutType) {
        throw new QueryResolverException(Messages.gs(Messages.TEIID.TEIID30069,function));
      }
      throw new QueryResolverException(Messages.gs(Messages.TEIID.TEIID30070,function));
    }
    if (fds.size() > 1) {
      throw new QueryResolverException(Messages.gs(Messages.TEIID.TEIID31150,function));
    }
  }
 catch (  Exception e) {
    if (e instanceof QueryResolverException)     throw e;
    if (hasArgWithoutType) {
      throw new QueryResolverException(Messages.gs(Messages.TEIID.TEIID30069,function));
    }
    throw new QueryResolverException(Messages.gs(Messages.TEIID.TEIID31150,function));
  }
  FunctionDescriptor fd=fds.get(0);
  if (fd.getMethod().isVarArgs() && fd.getTypes().length == types.length && library.isVarArgArrayParam(fd.getMethod(),types,types.length - 1,fd.getTypes()[types.length - 1])) {
    fd=fd.clone();
    fd.setCalledWithVarArgArrayParam(true);
  }
  if (fd.isSystemFunction(IFunctionLibrary.FunctionName.CONVERT) || fd.isSystemFunction(IFunctionLibrary.FunctionName.CAST)) {
    String dataType=(String)((Constant)args[1]).getValue();
    Class<?> dataTypeClass=getDataTypeManager().getDataTypeClass(dataType);
    fd=library.findTypedConversionFunction(args[0].getType(),dataTypeClass);
    Class<?> srcTypeClass=args[0].getType();
    if (srcTypeClass != null && dataTypeClass != null && !srcTypeClass.equals(dataTypeClass) && !getDataTypeManager().isTransformable(srcTypeClass,dataTypeClass)) {
      throw new QueryResolverException(Messages.gs(Messages.TEIID.TEIID30071,new Object[]{getDataTypeManager().getDataTypeName(srcTypeClass),dataType}));
    }
  }
 else   if (fd.isSystemFunction(IFunctionLibrary.FunctionName.LOOKUP)) {
    ResolverUtil.ResolvedLookup lookup=ResolverUtil.resolveLookup(function,metadata);
    fd=library.copyFunctionChangeReturnType(fd,lookup.getReturnElement().getType());
  }
 else   if (fd.isSystemFunction(IFunctionLibrary.FunctionName.ARRAY_GET) && args[0].getType().isArray()) {
    if (args[0].getType().isArray()) {
      fd=library.copyFunctionChangeReturnType(fd,args[0].getType().getComponentType());
    }
 else {
      if (function.getType() != null) {
        setDesiredType(args[0],function.getType(),function);
      }
      if (args[0].getType() != DataTypeManagerService.DefaultDataTypes.OBJECT.getTypeClass()) {
        throw new QueryResolverException(Messages.gs(Messages.TEIID.TEIID31145,getDataTypeManager().getDataTypeName(args[0].getType()),function));
      }
    }
  }
 else   if (Boolean.valueOf(fd.getMethod().getProperty(TEIID_PASS_THROUGH_TYPE,false))) {
    fd=library.copyFunctionChangeReturnType(fd,args[0].getType());
  }
  function.setFunctionDescriptor(fd);
  function.setType(fd.getReturnType());
  if (CoreConstants.SYSTEM_MODEL.equals(fd.getSchema()) && StringUtil.startsWithIgnoreCase(function.getName(),SYS_PREFIX)) {
    function.setName(function.getName().substring(SYS_PREFIX.length()));
  }
}