Program: 17098
Project: 17098
Buggy source:
/** 
 * Configure a JavaBean based on a set of command line arguments.   For a command line construct such as "-foo 42", this method will use available <code>BeanInfo</code> (usually obtained by introspection) to find a property named "foo".  The argument will be interpreted according to the type of the "foo" property, then the appropriate mutator method (generally named setFoo) will be called to configure the property on the bean. <p> Currently supported property types are <code>int, double, boolean, String, File, Reader, Writer, InputStream, OutputStream</code>, plus arrays of all the above types.  In the case of arrays, the option may appear multiple times on the command line, otherwise recurrance of the same option is an error. </p> <p> For stream types, the parameter is interpreted as a filename unless it is equal to "-" in which case standard input or standard output are used as appropriate.  Each of the standard streams may only be used one. </p> <p> In the future, this method will probably be extended to handle multiple parameter occurances, and use Annotations to generate more useful help messages when something goes wrong. </p>
 * @param bean 
 * @param args
 * @return A string array which contains any 'anonymous' arguments (may be empty)
 * @throws ConfigurationException
 */
public static String[] configureBean(Object bean,String[] args) throws ConfigurationException {
  BeanInfo bi;
  try {
    bi=Introspector.getBeanInfo(bean.getClass());
  }
 catch (  Exception ex) {
    throw new ConfigurationException("String_Node_Str" + ex.getMessage());
  }
  Map<String,PropertyDescriptor> propertiesByName=new HashMap<String,PropertyDescriptor>();
  for (Iterator<PropertyDescriptor> pi=Arrays.asList(bi.getPropertyDescriptors()).iterator(); pi.hasNext(); ) {
    PropertyDescriptor pd=pi.next();
    propertiesByName.put(pd.getName(),pd);
  }
  List<String> anonArgs=new ArrayList<String>();
  Map<PropertyDescriptor,List<String>> arrayProps=new HashMap<PropertyDescriptor,List<String>>();
  Set<PropertyDescriptor> usedProps=new HashSet<PropertyDescriptor>();
  boolean stdInUsed=false;
  boolean stdOutUsed=false;
  for (int i=0; i < args.length; ++i) {
    String arg=args[i];
    if (arg == null)     continue;
    if ((arg.length() > 0) && arg.charAt(0) == '-') {
      PropertyDescriptor pd=(PropertyDescriptor)propertiesByName.get(arg.substring(1));
      boolean arrayMode=false;
      Object propVal=null;
      Class propType=null;
      if (pd == null) {
        if (arg.startsWith("String_Node_Str")) {
          String altPropName=Introspector.decapitalize(arg.substring(3));
          pd=(PropertyDescriptor)propertiesByName.get(altPropName);
          if (pd == null) {
            throw new ConfigurationException("String_Node_Str" + arg.substring(1) + "String_Node_Str"+ altPropName);
          }
          propType=pd.getPropertyType();
          if (propType == Boolean.TYPE) {
            propVal=Boolean.FALSE;
          }
 else {
            throw new ConfigurationException("String_Node_Str" + arg + "String_Node_Str");
          }
        }
 else {
          throw new ConfigurationException("String_Node_Str" + arg.substring(1));
        }
      }
 else {
        propType=pd.getPropertyType();
        if (propType.isArray()) {
          arrayMode=true;
          propType=propType.getComponentType();
        }
        if (propType == Integer.TYPE) {
          try {
            propVal=new Integer(args[++i]);
          }
 catch (          Exception ex) {
            throw new ConfigurationException("String_Node_Str" + arg + "String_Node_Str");
          }
        }
 else         if (propType == Double.TYPE) {
          try {
            propVal=new Double(args[++i]);
          }
 catch (          Exception ex) {
            throw new ConfigurationException("String_Node_Str" + arg + "String_Node_Str");
          }
        }
 else         if (propType == String.class) {
          propVal=args[++i];
        }
 else         if (propType == Boolean.TYPE) {
          String val=args[++i];
          if (val == null)           propVal=Boolean.TRUE;
 else {
            if (val.equalsIgnoreCase("String_Node_Str"))             propVal=Boolean.TRUE;
 else             propVal=Boolean.FALSE;
          }
        }
 else         if (File.class.isAssignableFrom(propType)) {
          propVal=new File(args[++i]);
        }
 else         if (Reader.class.isAssignableFrom(propType)) {
          String name=args[++i];
          if ("String_Node_Str".equals(name)) {
            if (stdInUsed) {
              throw new ConfigurationException("String_Node_Str");
            }
            propVal=new InputStreamReader(System.in);
            stdInUsed=true;
          }
 else {
            try {
              propVal=new FileReader(new File(name));
            }
 catch (            Exception ex) {
              throw new ConfigurationException("String_Node_Str" + name + "String_Node_Str");
            }
          }
        }
 else         if (InputStream.class.isAssignableFrom(propType)) {
          String name=args[++i];
          if ("String_Node_Str".equals(name)) {
            if (stdInUsed) {
              throw new ConfigurationException("String_Node_Str");
            }
            propVal=System.in;
            stdInUsed=true;
          }
 else {
            try {
              propVal=new FileInputStream(new File(name));
            }
 catch (            Exception ex) {
              throw new ConfigurationException("String_Node_Str" + name + "String_Node_Str");
            }
          }
        }
 else         if (Writer.class.isAssignableFrom(propType)) {
          String name=args[++i];
          if ("String_Node_Str".equals(name)) {
            if (stdOutUsed) {
              throw new ConfigurationException("String_Node_Str");
            }
            propVal=new OutputStreamWriter(System.out);
            stdOutUsed=true;
          }
 else {
            try {
              propVal=new FileWriter(new File(name));
            }
 catch (            Exception ex) {
              throw new ConfigurationException("String_Node_Str" + name + "String_Node_Str");
            }
          }
        }
 else         if (OutputStream.class.isAssignableFrom(propType)) {
          String name=args[++i];
          if ("String_Node_Str".equals(name)) {
            if (stdOutUsed) {
              throw new ConfigurationException("String_Node_Str");
            }
            propVal=System.out;
            stdOutUsed=true;
          }
 else {
            try {
              propVal=new FileOutputStream(new File(name));
            }
 catch (            Exception ex) {
              throw new ConfigurationException("String_Node_Str" + name + "String_Node_Str");
            }
          }
        }
 else {
          System.err.println("String_Node_Str" + arg + "String_Node_Str"+ propType);
          System.exit(1);
        }
      }
      if (arrayMode) {
        List valList=(List)arrayProps.get(pd);
        if (valList == null) {
          valList=new ArrayList();
          arrayProps.put(pd,valList);
        }
        valList.add(propVal);
      }
 else {
        if (usedProps.contains(pd)) {
          throw new ConfigurationException("String_Node_Str" + pd.getName());
        }
        try {
          pd.getWriteMethod().invoke(bean,new Object[]{propVal});
        }
 catch (        InvocationTargetException ex) {
          throw new ConfigurationException("String_Node_Str" + pd.getName() + "String_Node_Str");
        }
catch (        Exception ex) {
          throw new ConfigurationException("String_Node_Str" + pd.getName() + "String_Node_Str");
        }
        usedProps.add(pd);
      }
    }
 else {
      anonArgs.add(arg);
    }
  }
  for (Iterator api=arrayProps.entrySet().iterator(); api.hasNext(); ) {
    Map.Entry me=(Map.Entry)api.next();
    PropertyDescriptor pd=(PropertyDescriptor)me.getKey();
    List vals=(List)me.getValue();
    Class compType=pd.getPropertyType().getComponentType();
    Object valArray;
    if (compType.isPrimitive()) {
      if (compType == Integer.TYPE) {
        valArray=CollectionTools.toIntArray(vals);
      }
 else       if (compType == Double.TYPE) {
        valArray=CollectionTools.toDoubleArray(vals);
      }
 else {
        throw new ConfigurationException("String_Node_Str" + compType.getName() + "String_Node_Str");
      }
    }
 else {
      valArray=vals.toArray((Object[])Array.newInstance(compType,vals.size()));
    }
    try {
      pd.getWriteMethod().invoke(bean,new Object[]{valArray});
    }
 catch (    InvocationTargetException ex) {
      throw new ConfigurationException("String_Node_Str" + pd.getName() + "String_Node_Str");
    }
catch (    Exception ex) {
      throw new ConfigurationException("String_Node_Str" + pd.getName() + "String_Node_Str");
    }
  }
  return (String[])anonArgs.toArray(new String[anonArgs.size()]);
}

Fixed version (ground truth):
/** 
 * Configure a JavaBean based on a set of command line arguments.   For a command line construct such as "-foo 42", this method will use available <code>BeanInfo</code> (usually obtained by introspection) to find a property named "foo".  The argument will be interpreted according to the type of the "foo" property, then the appropriate mutator method (generally named setFoo) will be called to configure the property on the bean. <p> Currently supported property types are <code>int, double, boolean, String, File, Reader, Writer, InputStream, OutputStream</code>, plus arrays of all the above types.  In the case of arrays, the option may appear multiple times on the command line, otherwise recurrance of the same option is an error. </p> <p> For stream types, the parameter is interpreted as a filename unless it is equal to "-" in which case standard input or standard output are used as appropriate.  Each of the standard streams may only be used one. </p> <p> In the future, this method will probably be extended to handle multiple parameter occurances, and use Annotations to generate more useful help messages when something goes wrong. </p>
 * @param bean 
 * @param args
 * @return A string array which contains any 'anonymous' arguments (may be empty)
 * @throws ConfigurationException
 */
public static String[] configureBean(Object bean,String[] args) throws ConfigurationException {
  BeanInfo bi;
  try {
    bi=Introspector.getBeanInfo(bean.getClass());
  }
 catch (  Exception ex) {
    throw new ConfigurationException("String_Node_Str" + ex.getMessage());
  }
  Map<String,PropertyDescriptor> propertiesByName=new HashMap<String,PropertyDescriptor>();
  for (Iterator<PropertyDescriptor> pi=Arrays.asList(bi.getPropertyDescriptors()).iterator(); pi.hasNext(); ) {
    PropertyDescriptor pd=pi.next();
    propertiesByName.put(pd.getName(),pd);
  }
  List<String> anonArgs=new ArrayList<String>();
  Map<PropertyDescriptor,List<String>> arrayProps=new HashMap<PropertyDescriptor,List<String>>();
  Set<PropertyDescriptor> usedProps=new HashSet<PropertyDescriptor>();
  boolean stdInUsed=false;
  boolean stdOutUsed=false;
  for (int i=0; i < args.length; ++i) {
    String arg=args[i];
    if (arg == null)     continue;
    if ((arg.length() > 0) && arg.charAt(0) == '-') {
      PropertyDescriptor pd=(PropertyDescriptor)propertiesByName.get(arg.substring(1));
      boolean arrayMode=false;
      Object propVal=null;
      Class propType=null;
      if (pd == null) {
        if (arg.startsWith("String_Node_Str")) {
          String altPropName=Introspector.decapitalize(arg.substring(3));
          pd=(PropertyDescriptor)propertiesByName.get(altPropName);
          if (pd == null) {
            throw new ConfigurationException("String_Node_Str" + arg.substring(1) + "String_Node_Str"+ altPropName);
          }
          propType=pd.getPropertyType();
          if (propType == Boolean.TYPE) {
            propVal=Boolean.FALSE;
          }
 else {
            throw new ConfigurationException("String_Node_Str" + arg + "String_Node_Str");
          }
        }
 else {
          throw new ConfigurationException("String_Node_Str" + arg.substring(1));
        }
      }
 else {
        propType=pd.getPropertyType();
        if (propType.isArray()) {
          arrayMode=true;
          propType=propType.getComponentType();
        }
        if (propType == Integer.TYPE) {
          try {
            propVal=new Integer(args[++i]);
          }
 catch (          Exception ex) {
            throw new ConfigurationException("String_Node_Str" + arg + "String_Node_Str");
          }
        }
 else         if (propType == Double.TYPE || propType == Double.class) {
          try {
            propVal=new Double(args[++i]);
          }
 catch (          Exception ex) {
            throw new ConfigurationException("String_Node_Str" + arg + "String_Node_Str");
          }
        }
 else         if (propType == String.class) {
          propVal=args[++i];
        }
 else         if (propType == Boolean.TYPE) {
          String val=args[++i];
          if (val == null)           propVal=Boolean.TRUE;
 else {
            if (val.equalsIgnoreCase("String_Node_Str"))             propVal=Boolean.TRUE;
 else             propVal=Boolean.FALSE;
          }
        }
 else         if (File.class.isAssignableFrom(propType)) {
          propVal=new File(args[++i]);
        }
 else         if (Reader.class.isAssignableFrom(propType)) {
          String name=args[++i];
          if ("String_Node_Str".equals(name)) {
            if (stdInUsed) {
              throw new ConfigurationException("String_Node_Str");
            }
            propVal=new InputStreamReader(System.in);
            stdInUsed=true;
          }
 else {
            try {
              propVal=new FileReader(new File(name));
            }
 catch (            Exception ex) {
              throw new ConfigurationException("String_Node_Str" + name + "String_Node_Str");
            }
          }
        }
 else         if (InputStream.class.isAssignableFrom(propType)) {
          String name=args[++i];
          if ("String_Node_Str".equals(name)) {
            if (stdInUsed) {
              throw new ConfigurationException("String_Node_Str");
            }
            propVal=System.in;
            stdInUsed=true;
          }
 else {
            try {
              propVal=new FileInputStream(new File(name));
            }
 catch (            Exception ex) {
              throw new ConfigurationException("String_Node_Str" + name + "String_Node_Str");
            }
          }
        }
 else         if (Writer.class.isAssignableFrom(propType)) {
          String name=args[++i];
          if ("String_Node_Str".equals(name)) {
            if (stdOutUsed) {
              throw new ConfigurationException("String_Node_Str");
            }
            propVal=new OutputStreamWriter(System.out);
            stdOutUsed=true;
          }
 else {
            try {
              propVal=new FileWriter(new File(name));
            }
 catch (            Exception ex) {
              throw new ConfigurationException("String_Node_Str" + name + "String_Node_Str");
            }
          }
        }
 else         if (OutputStream.class.isAssignableFrom(propType)) {
          String name=args[++i];
          if ("String_Node_Str".equals(name)) {
            if (stdOutUsed) {
              throw new ConfigurationException("String_Node_Str");
            }
            propVal=System.out;
            stdOutUsed=true;
          }
 else {
            try {
              propVal=new FileOutputStream(new File(name));
            }
 catch (            Exception ex) {
              throw new ConfigurationException("String_Node_Str" + name + "String_Node_Str");
            }
          }
        }
 else {
          System.err.println("String_Node_Str" + arg + "String_Node_Str"+ propType);
          System.exit(1);
        }
      }
      if (arrayMode) {
        List valList=(List)arrayProps.get(pd);
        if (valList == null) {
          valList=new ArrayList();
          arrayProps.put(pd,valList);
        }
        valList.add(propVal);
      }
 else {
        if (usedProps.contains(pd)) {
          throw new ConfigurationException("String_Node_Str" + pd.getName());
        }
        try {
          pd.getWriteMethod().invoke(bean,new Object[]{propVal});
        }
 catch (        InvocationTargetException ex) {
          throw new ConfigurationException("String_Node_Str" + pd.getName() + "String_Node_Str");
        }
catch (        Exception ex) {
          throw new ConfigurationException("String_Node_Str" + pd.getName() + "String_Node_Str");
        }
        usedProps.add(pd);
      }
    }
 else {
      anonArgs.add(arg);
    }
  }
  for (Iterator api=arrayProps.entrySet().iterator(); api.hasNext(); ) {
    Map.Entry me=(Map.Entry)api.next();
    PropertyDescriptor pd=(PropertyDescriptor)me.getKey();
    List vals=(List)me.getValue();
    Class compType=pd.getPropertyType().getComponentType();
    Object valArray;
    if (compType.isPrimitive()) {
      if (compType == Integer.TYPE) {
        valArray=CollectionTools.toIntArray(vals);
      }
 else       if (compType == Double.TYPE) {
        valArray=CollectionTools.toDoubleArray(vals);
      }
 else {
        throw new ConfigurationException("String_Node_Str" + compType.getName() + "String_Node_Str");
      }
    }
 else {
      valArray=vals.toArray((Object[])Array.newInstance(compType,vals.size()));
    }
    try {
      pd.getWriteMethod().invoke(bean,new Object[]{valArray});
    }
 catch (    InvocationTargetException ex) {
      throw new ConfigurationException("String_Node_Str" + pd.getName() + "String_Node_Str");
    }
catch (    Exception ex) {
      throw new ConfigurationException("String_Node_Str" + pd.getName() + "String_Node_Str");
    }
  }
  return (String[])anonArgs.toArray(new String[anonArgs.size()]);
}