Program: 11843
Project: 11843
Buggy source:
/** 
 * Generates a Java source file using JET, with   {@link org.eclipse.emf.codegen.util.ImportManager import management}and, when running under Eclipse,   {@link org.eclipse.emf.codegen.merge.java.JMerger merging} and{@link org.eclipse.jdt.core.formatter.CodeFormatter code formatting} capabilities.<p>When running under Eclipse, the encoding for the file is determined from the workspace. Failing that, or in standalone, the platform default is used.
 * @param targetPath the workspace path of the directory in or under which the file will be created, depending on thespecified package name. When running standalone, this path will be converted to a platform resource URI that should be mapped to a physical file URI by the  {@link #getURIConverter() URIConverter}.
 * @param packageName the package name for the generated compilation unit.
 * @param className the name of the public class in the generated compilation unit.
 * @param jetEmitter the <code>JETEmitter</code> to use for generating the code.
 * @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 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#mergerFacadeHelperClass mergerFacadeHelperClass}<li>  {@link Generator.Options#mergeRulesURI mergeRulesURI}<li>  {@link Generator.Options#codeFormatting codeFormatting} <li> {@link Generator.Options#codeFormatterOptions codeFormatterOptions} <li> {@link Generator.Options#resourceSet resourceSet}</ul>
 */
protected void generateJava(String targetPath,String packageName,String className,JETEmitter jetEmitter,Object[] arguments,Monitor monitor){
  try {
    monitor.beginTask("String_Node_Str",4);
    URI targetDirectory=toURI(targetPath).appendSegments(packageName.split("String_Node_Str"));
    URI targetFile=targetDirectory.appendSegment(className + "String_Node_Str");
    monitor.subTask(CodeGenEcorePlugin.INSTANCE.getString("String_Node_Str",new Object[]{targetFile}));
    ensureContainerExists(targetDirectory,createMonitor(monitor,1));
    if (arguments == null) {
      arguments=new Object[]{generatingObject};
    }
    createImportManager(packageName,className);
    setLineDelimiter(getLineDelimiter(targetFile,getEncoding(targetFile)));
    String emitterResult=jetEmitter.generate(createMonitor(monitor,1),arguments,getLineDelimiter());
    boolean changed=true;
    String newContents=emitterResult;
    boolean targetExists=exists(targetFile);
    JControlModel jControlModel=getGenerator().getJControlModel();
    boolean mergeSuccessful=jControlModel.canMerge();
    if (mergeSuccessful) {
      JMerger jMerger=new JMerger(jControlModel);
      jMerger.setFixInterfaceBrace(jControlModel.getFacadeHelper().fixInterfaceBrace());
      try {
        jMerger.setSourceCompilationUnit(jMerger.createCompilationUnitForContents(emitterResult));
      }
 catch (      RuntimeException runtimeException) {
        if (targetExists) {
          throw runtimeException;
        }
 else {
          mergeSuccessful=false;
        }
      }
      if (mergeSuccessful) {
        Object codeFormatter=getGenerator().getOptions().codeFormatting ? createCodeFormatter(getGenerator().getOptions().codeFormatterOptions,targetFile) : null;
        if (targetExists) {
          monitor.subTask(CodeGenEcorePlugin.INSTANCE.getString("String_Node_Str",new Object[]{targetFile}));
          jMerger.setTargetCompilationUnit(jMerger.createCompilationUnitForInputStream(createInputStream(targetFile),getEncoding(targetFile)));
          String oldContents=jMerger.getTargetCompilationUnitContents();
          monitor.subTask(CodeGenEcorePlugin.INSTANCE.getString("String_Node_Str",new Object[]{targetFile}));
          jMerger.merge();
          newContents=formatCode(jMerger.getTargetCompilationUnitContents(),codeFormatter);
          changed=!oldContents.equals(newContents);
          if (changed && isReadOnly(targetFile) && validateEdit(targetFile,createMonitor(monitor,1))) {
            jMerger.setTargetCompilationUnit(jMerger.createCompilationUnitForInputStream(createInputStream(targetFile),getEncoding(targetFile)));
            jMerger.remerge();
            newContents=formatCode(jMerger.getTargetCompilationUnitContents(),codeFormatter);
          }
        }
 else {
          changed=true;
          monitor.subTask(CodeGenEcorePlugin.INSTANCE.getString("String_Node_Str",new Object[]{targetFile}));
          jMerger.merge();
          newContents=formatCode(jMerger.getTargetCompilationUnitContents(),codeFormatter);
        }
        if (jControlModel.getFacadeHelper() != null) {
          jControlModel.getFacadeHelper().reset();
        }
      }
    }
    if (!mergeSuccessful) {
      newContents=CodeGenUtil.convertFormat(jControlModel.getLeadingTabReplacement(),jControlModel.convertToStandardBraceStyle(),emitterResult);
      if (targetExists) {
        monitor.subTask(CodeGenEcorePlugin.INSTANCE.getString("String_Node_Str",new Object[]{targetFile}));
        String oldContents=getContents(targetFile,null);
        changed=!oldContents.equals(newContents);
      }
 else {
        changed=true;
      }
    }
    monitor.worked(1);
    if (changed) {
      String encoding=getEncoding(targetFile);
      byte[] bytes=encoding == null ? newContents.getBytes() : newContents.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[]{className + "String_Node_Str"});
        targetFile=targetDirectory.appendSegment(baseName);
        monitor.subTask(CodeGenEcorePlugin.INSTANCE.getString("String_Node_Str",new Object[]{targetFile}));
      }
      if (isReadOnly(targetFile)) {
        if (getGenerator().getOptions().forceOverwrite) {
          setWriteable(targetFile);
        }
 else {
          targetFile=targetDirectory.appendSegment("String_Node_Str" + className + "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 e) {
    throw e instanceof RuntimeException ? (RuntimeException)e : new WrappedException(e);
  }
 finally {
    clearImportManager();
    setLineDelimiter(null);
    monitor.done();
  }
}

Fixed version (ground truth):
/** 
 * Generates a Java source file using JET, with   {@link org.eclipse.emf.codegen.util.ImportManager import management}and, when running under Eclipse,   {@link org.eclipse.emf.codegen.merge.java.JMerger merging} and{@link org.eclipse.jdt.core.formatter.CodeFormatter code formatting} capabilities.<p>When running under Eclipse, the encoding for the file is determined from the workspace. Failing that, or in stand-alone, the platform default is used.
 * @param targetPath the workspace path of the directory in or under which the file will be created, depending on thespecified package name. When running stand-alone, this path will be converted to a platform resource URI that should be mapped to a physical file URI by the  {@link #getURIConverter() URIConverter}.
 * @param packageName the package name for the generated compilation unit.
 * @param className the name of the public class in the generated compilation unit.
 * @param jetEmitter the <code>JETEmitter</code> to use for generating the code.
 * @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 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#mergerFacadeHelperClass mergerFacadeHelperClass}<li>  {@link Generator.Options#mergeRulesURI mergeRulesURI}<li>  {@link Generator.Options#codeFormatting codeFormatting} <li> {@link Generator.Options#codeFormatterOptions codeFormatterOptions} <li> {@link Generator.Options#resourceSet resourceSet}</ul>
 */
protected void generateJava(String targetPath,String packageName,String className,JETEmitter jetEmitter,Object[] arguments,Monitor monitor){
  try {
    monitor.beginTask("String_Node_Str",4);
    URI targetDirectory=toURI(targetPath).appendSegments(packageName.split("String_Node_Str"));
    URI targetFile=targetDirectory.appendSegment(className + "String_Node_Str");
    monitor.subTask(CodeGenEcorePlugin.INSTANCE.getString("String_Node_Str",new Object[]{targetFile}));
    ensureContainerExists(targetDirectory,createMonitor(monitor,1));
    if (arguments == null) {
      arguments=new Object[]{generatingObject};
    }
    createImportManager(packageName,className);
    setLineDelimiter(getLineDelimiter(targetFile,getEncoding(targetFile)));
    String emitterResult=jetEmitter.generate(createMonitor(monitor,1),arguments,getLineDelimiter());
    boolean changed=true;
    String newContents=emitterResult;
    boolean targetExists=exists(targetFile);
    JControlModel jControlModel=getGenerator().getJControlModel();
    boolean mergeSuccessful=jControlModel.canMerge();
    if (mergeSuccessful) {
      JMerger jMerger=new JMerger(jControlModel);
      jMerger.setFixInterfaceBrace(jControlModel.getFacadeHelper().fixInterfaceBrace());
      try {
        jMerger.setSourceCompilationUnit(jMerger.createCompilationUnitForContents(emitterResult));
      }
 catch (      RuntimeException runtimeException) {
        if (targetExists) {
          throw runtimeException;
        }
 else {
          mergeSuccessful=false;
        }
      }
      if (mergeSuccessful) {
        Object codeFormatter=getGenerator().getOptions().codeFormatting ? createCodeFormatter(getGenerator().getOptions().codeFormatterOptions,targetFile) : null;
        if (targetExists) {
          monitor.subTask(CodeGenEcorePlugin.INSTANCE.getString("String_Node_Str",new Object[]{targetFile}));
          jMerger.setTargetCompilationUnit(jMerger.createCompilationUnitForInputStream(createInputStream(targetFile),getEncoding(targetFile)));
          String oldContents=jMerger.getTargetCompilationUnitContents();
          monitor.subTask(CodeGenEcorePlugin.INSTANCE.getString("String_Node_Str",new Object[]{targetFile}));
          jMerger.merge();
          newContents=formatCode(jMerger.getTargetCompilationUnitContents(),codeFormatter);
          changed=!oldContents.equals(newContents);
          if (changed && isReadOnly(targetFile) && validateEdit(targetFile,createMonitor(monitor,1))) {
            jMerger.setTargetCompilationUnit(jMerger.createCompilationUnitForInputStream(createInputStream(targetFile),getEncoding(targetFile)));
            jMerger.remerge();
            newContents=formatCode(jMerger.getTargetCompilationUnitContents(),codeFormatter);
          }
        }
 else {
          changed=true;
          monitor.subTask(CodeGenEcorePlugin.INSTANCE.getString("String_Node_Str",new Object[]{targetFile}));
          jMerger.merge();
          newContents=formatCode(jMerger.getTargetCompilationUnitContents(),codeFormatter);
        }
        if (jControlModel.getFacadeHelper() != null) {
          jControlModel.getFacadeHelper().reset();
        }
      }
    }
    if (!mergeSuccessful) {
      newContents=CodeGenUtil.convertFormat(jControlModel.getLeadingTabReplacement(),jControlModel.convertToStandardBraceStyle(),emitterResult);
      if (targetExists) {
        monitor.subTask(CodeGenEcorePlugin.INSTANCE.getString("String_Node_Str",new Object[]{targetFile}));
        String oldContents=getContents(targetFile,null);
        changed=!oldContents.equals(newContents);
      }
 else {
        changed=true;
      }
    }
    monitor.worked(1);
    if (changed) {
      String encoding=getEncoding(targetFile);
      byte[] bytes=encoding == null ? newContents.getBytes() : newContents.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[]{className + "String_Node_Str"});
        targetFile=targetDirectory.appendSegment(baseName);
        monitor.subTask(CodeGenEcorePlugin.INSTANCE.getString("String_Node_Str",new Object[]{targetFile}));
      }
      if (isReadOnly(targetFile)) {
        if (getGenerator().getOptions().forceOverwrite) {
          setWriteable(targetFile);
        }
 else {
          targetFile=targetDirectory.appendSegment("String_Node_Str" + className + "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 e) {
    throw e instanceof RuntimeException ? (RuntimeException)e : new WrappedException(e);
  }
 finally {
    clearImportManager();
    setLineDelimiter(null);
    monitor.done();
  }
}