Program: 11840
Project: 11840
Buggy source:
/** 
 * Generates an arbitrary text artifact using JET.
 * @param targetPathName the path name of the target file. This should be a workspace path; when running standalone,it will be converted to a platform resource URI that should be mapped to a physical file URI by the {@link #getURIConverter() URIConverter}.
 * @param jetEmitter the <code>JETEmitter</code> to use for generating the text.
 * @param arguments the argument array to pass to the <code>JETEmitter</code>'s{@link JETEmitter#generate(Monitor,Object[]) generate(Monitor, Object[])} method. If null, an array willbe constructed containing only the  {@link #generatingObject object} for which code is being generated. 
 * @param overwrite whether an existing file should be overritten.
 * @param encoding an override of the default encoding. If "ISO-8859-1" is specified,{@link org.eclipse.emf.codegen.util.CodeGenUtil#unicodeEscapeEncode(String) Unicode escape encoding}  is performed to represent non-Latin characters. The default encoding, when running under Eclipse, is determined from the workspace. Failing that, or in standalone, the platform default is used.
 * @param monitor the <code>Monitor</code> through which to report progress.<p>This method also consults the following  {@link Generator#getOptions() generator options}: <ul> <li>  {@link Generator.Options#redirectionPattern redirectionPattern}<li>  {@link Generator.Options#forceOverwrite forceOverwrite}<li>  {@link Generator.Options#dynamicTemplates dynamicTemplates}<li>  {@link Generator.Options#resourceSet resourceSet}</ul>
 */
protected void generateText(String targetPathName,JETEmitter jetEmitter,Object[] arguments,boolean overwrite,String encoding,Monitor monitor){
  try {
    monitor.beginTask("String_Node_Str",3);
    URI targetFile=toURI(targetPathName);
    monitor.subTask(CodeGenEcorePlugin.INSTANCE.getString("String_Node_Str",new Object[]{targetFile}));
    URI targetDirectory=targetFile.trimSegments(1);
    ensureContainerExists(targetDirectory,createMonitor(monitor,1));
    boolean exists=exists(targetFile);
    if (!exists || overwrite) {
      if (arguments == null) {
        arguments=new Object[]{generatingObject};
      }
      setLineDelimiter(getLineDelimiter(targetFile,encoding));
      String emitterResult=jetEmitter.generate(createMonitor(monitor,1),arguments,getLineDelimiter());
      if (PROPERTIES_ENCODING.equals(encoding)) {
        emitterResult=CodeGenUtil.unicodeEscapeEncode(emitterResult);
      }
      if (encoding == null) {
        encoding=getEncoding(targetFile);
      }
      boolean changed=true;
      if (exists) {
        monitor.subTask(CodeGenEcorePlugin.INSTANCE.getString("String_Node_Str",new Object[]{targetFile}));
        String oldContents=getContents(targetFile,encoding);
        changed=!emitterResult.equals(oldContents);
      }
      if (changed) {
        byte[] bytes=encoding == null ? emitterResult.toString().getBytes() : emitterResult.toString().getBytes(encoding);
        String redirection=getGenerator().getOptions().redirectionPattern;
        boolean redirect=redirection != null && redirection.indexOf("String_Node_Str") != -1;
        if (redirect) {
          String baseName=MessageFormat.format(redirection,new Object[]{targetFile.lastSegment()});
          targetFile=targetDirectory.appendSegment(baseName);
          monitor.subTask(CodeGenEcorePlugin.INSTANCE.getString("String_Node_Str",new Object[]{targetFile}));
        }
        if (isReadOnly(targetFile)) {
          if (getGenerator().getOptions().forceOverwrite) {
            validateEdit(targetFile,createMonitor(monitor,1));
            setWriteable(targetFile);
          }
 else {
            targetFile=targetDirectory.appendSegment("String_Node_Str" + targetFile.lastSegment() + "String_Node_Str");
            monitor.subTask(CodeGenEcorePlugin.INSTANCE.getString("String_Node_Str",new Object[]{targetFile}));
          }
        }
        OutputStream outputStream=createOutputStream(targetFile);
        outputStream.write(bytes);
        outputStream.close();
      }
    }
  }
 catch (  Exception exception) {
    CodeGenEcorePlugin.INSTANCE.log(exception);
  }
 finally {
    setLineDelimiter(null);
    monitor.done();
  }
}

Fixed version (ground truth):
/** 
 * Generates an arbitrary text artifact using JET.
 * @param targetPathName the path name of the target file. This should be a workspace path; when running stand-alone,it will be converted to a platform resource URI that should be mapped to a physical file URI by the {@link #getURIConverter() URIConverter}.
 * @param jetEmitter the <code>JETEmitter</code> to use for generating the text.
 * @param arguments the argument array to pass to the <code>JETEmitter</code>'s{@link JETEmitter#generate(Monitor,Object[]) generate(Monitor, Object[])} method. If null, an array willbe constructed containing only the  {@link #generatingObject object} for which code is being generated. 
 * @param overwrite whether an existing file should be overwritten.
 * @param encoding an override of the default encoding. If "ISO-8859-1" is specified,{@link org.eclipse.emf.codegen.util.CodeGenUtil#unicodeEscapeEncode(String) Unicode escape encoding}  is performed to represent non-Latin characters. The default encoding, when running under Eclipse, is determined from the workspace. Failing that, or in stand-alone, the platform default is used.
 * @param monitor the <code>Monitor</code> through which to report progress.<p>This method also consults the following  {@link Generator#getOptions() generator options}: <ul> <li>  {@link Generator.Options#redirectionPattern redirectionPattern}<li>  {@link Generator.Options#forceOverwrite forceOverwrite}<li>  {@link Generator.Options#dynamicTemplates dynamicTemplates}<li>  {@link Generator.Options#resourceSet resourceSet}</ul>
 */
protected void generateText(String targetPathName,JETEmitter jetEmitter,Object[] arguments,boolean overwrite,String encoding,Monitor monitor){
  try {
    monitor.beginTask("String_Node_Str",3);
    URI targetFile=toURI(targetPathName);
    monitor.subTask(CodeGenEcorePlugin.INSTANCE.getString("String_Node_Str",new Object[]{targetFile}));
    URI targetDirectory=targetFile.trimSegments(1);
    ensureContainerExists(targetDirectory,createMonitor(monitor,1));
    boolean exists=exists(targetFile);
    if (!exists || overwrite) {
      if (arguments == null) {
        arguments=new Object[]{generatingObject};
      }
      setLineDelimiter(getLineDelimiter(targetFile,encoding));
      String emitterResult=jetEmitter.generate(createMonitor(monitor,1),arguments,getLineDelimiter());
      if (PROPERTIES_ENCODING.equals(encoding)) {
        emitterResult=CodeGenUtil.unicodeEscapeEncode(emitterResult);
      }
      if (encoding == null) {
        encoding=getEncoding(targetFile);
      }
      boolean changed=true;
      if (exists) {
        monitor.subTask(CodeGenEcorePlugin.INSTANCE.getString("String_Node_Str",new Object[]{targetFile}));
        String oldContents=getContents(targetFile,encoding);
        changed=!emitterResult.equals(oldContents);
      }
      if (changed) {
        byte[] bytes=encoding == null ? emitterResult.toString().getBytes() : emitterResult.toString().getBytes(encoding);
        String redirection=getGenerator().getOptions().redirectionPattern;
        boolean redirect=redirection != null && redirection.indexOf("String_Node_Str") != -1;
        if (redirect) {
          String baseName=MessageFormat.format(redirection,new Object[]{targetFile.lastSegment()});
          targetFile=targetDirectory.appendSegment(baseName);
          monitor.subTask(CodeGenEcorePlugin.INSTANCE.getString("String_Node_Str",new Object[]{targetFile}));
        }
        if (isReadOnly(targetFile)) {
          if (getGenerator().getOptions().forceOverwrite) {
            validateEdit(targetFile,createMonitor(monitor,1));
            setWriteable(targetFile);
          }
 else {
            targetFile=targetDirectory.appendSegment("String_Node_Str" + targetFile.lastSegment() + "String_Node_Str");
            monitor.subTask(CodeGenEcorePlugin.INSTANCE.getString("String_Node_Str",new Object[]{targetFile}));
          }
        }
        OutputStream outputStream=createOutputStream(targetFile);
        outputStream.write(bytes);
        outputStream.close();
      }
    }
  }
 catch (  Exception exception) {
    CodeGenEcorePlugin.INSTANCE.log(exception);
  }
 finally {
    setLineDelimiter(null);
    monitor.done();
  }
}