Program: 45265
Project: 45265
Buggy source:
public I18nRefactorAction(){
  super(new EditorActionHandler(){
    public void execute(    final Editor editor,    DataContext dataContext){
      if (editor == null)       return;
      SelectionModel selectionModel=editor.getSelectionModel();
      final Project project=editor.getProject();
      if (project == null)       return;
      PsiDocumentManager psiManager=PsiDocumentManager.getInstance(project);
      PsiFile psiFile=psiManager.getPsiFile(editor.getDocument());
      if (!(psiFile instanceof PsiJavaFile)) {
        return;
      }
      PsiJavaFile javaFile=(PsiJavaFile)psiFile;
      PsiFile props=findMessagesDotProperties(project,javaFile);
      if (props == null) {
        Messages.showErrorDialog("String_Node_Str","String_Node_Str");
        return;
      }
      if (!(props instanceof PropertiesFile)) {
        Messages.showErrorDialog("String_Node_Str","String_Node_Str");
        return;
      }
      final PropertiesFile propsFile=(PropertiesFile)props;
      PsiElement e=findSelectedPsiElement(selectionModel,javaFile);
      while (e != null) {
        if (e instanceof PsiExpression)         break;
        e=e.getParent();
      }
      if (e == null) {
        Messages.showErrorDialog("String_Node_Str","String_Node_Str");
        return;
      }
      final PsiExpression exp=(PsiExpression)e;
      final PsiClassType stringType=PsiType.getJavaLangString(PsiManager.getInstance(project),e.getResolveScope());
      if (exp.getType() == null || !exp.getType().equals(stringType)) {
        Messages.showErrorDialog("String_Node_Str","String_Node_Str");
        return;
      }
      final String key=getResourceName(javaFile);
      if (key == null)       return;
      final StringBuilder propertyValue=new StringBuilder();
      final StringBuilder expression=new StringBuilder();
      new Runnable(){
        int numArgs=0;
        public void run(){
          process(exp);
        }
        private void process(        PsiElement exp){
          if (exp instanceof PsiLiteralExpression) {
            PsiLiteralExpression lit=(PsiLiteralExpression)exp;
            if (lit.getType().equals(stringType)) {
              escapeAndAppend(lit.getValue().toString());
              return;
            }
          }
          if (exp instanceof PsiBinaryExpression) {
            PsiBinaryExpression binExp=(PsiBinaryExpression)exp;
            if (binExp.getOperationTokenType() == JavaTokenType.PLUS) {
              process(binExp.getLOperand());
              process(binExp.getROperand());
              return;
            }
          }
          propertyValue.append('{').append(numArgs++).append('}');
          if (expression.length() > 0)           expression.append(',');
          expression.append(exp.getText());
        }
        /** 
 * Takes the literal string value and appends that to <tt>propertyValue</tt> with proper escaping.
 */
        private void escapeAndAppend(        String value){
          for (          char ch : value.toCharArray()) {
switch (ch) {
case '\'':
              propertyValue.append("String_Node_Str");
            break;
case '{':
case '}':
          propertyValue.append('\'').append(ch).append('\'');
        break;
default :
      propertyValue.append(ch);
  }
}
}
}
.run();
ApplicationManager.getApplication().runWriteAction(new Runnable(){
public void run(){
try {
  propsFile.addPropertyAfter(PropertiesElementFactory.createProperty(project,key,propertyValue.toString()),findAnchor(propsFile,key));
}
 catch (IncorrectOperationException x) {
  Messages.showErrorDialog(x.getMessage(),"String_Node_Str");
  return;
}
expression.insert(0,"String_Node_Str" + toJavaIdentifier(key) + "String_Node_Str");
expression.append("String_Node_Str");
TextRange tr=exp.getTextRange();
editor.getDocument().deleteString(tr.getStartOffset(),tr.getEndOffset());
EditorModificationUtil.insertStringAtCaret(editor,expression.toString());
}
private Property findAnchor(PropertiesFile propsFile,String key){
List<Property> list=propsFile.getProperties();
for (int i=0; i < list.size() - 1; i++) {
  Property prev=list.get(i);
  Property next=list.get(i + 1);
  if (prev.getKey().compareTo(key) < 0 && key.compareTo(next.getKey()) < 0)   return prev;
}
if (list.isEmpty()) return null;
return list.get(list.size() - 1);
}
}
);
}
private String getResourceName(PsiJavaFile javaFile){
String key=Messages.showInputDialog("String_Node_Str","String_Node_Str",null);
if (key == null || key.length() == 0) return null;
return getMainClassName(javaFile.getName()) + '.' + key;
}
private String getMainClassName(String name){
if (name.endsWith("String_Node_Str")) return name.substring(0,name.length() - 5);
return name;
}
/** 
 * Locates <tt>Messages.properties</tt> in the same package
 */
private PsiFile findMessagesDotProperties(Project project,PsiJavaFile javaFile){
PsiPackage pkg=PsiManager.getInstance(project).findPackage(javaFile.getPackageName());
for (PsiDirectory dir : pkg.getDirectories()) {
PsiFile props=dir.findFile("String_Node_Str");
if (props != null) return props;
}
return null;
}
/** 
 * Finds the smallest   {@link PsiElement} that encompasses the current selection.
 */
@Nullable private PsiElement findSelectedPsiElement(SelectionModel selectionModel,PsiJavaFile javaFile){
PsiElement e=javaFile.findElementAt(selectionModel.getSelectionStart());
if (e == null) return null;
while (true) {
PsiElement p=e.getParent();
TextRange tr=p.getTextRange();
if (selectionModel.getSelectionEnd() < tr.getEndOffset()) return e;
e=p;
}
}
/** 
 * Copied from the localizer code. Converts a property name to a method name.
 */
protected String toJavaIdentifier(String key){
return key.replace('.','_');
}
}
);
}

Fixed version (ground truth):
public I18nRefactorAction(){
  super(new EditorActionHandler(){
    public void execute(    final Editor editor,    DataContext dataContext){
      if (editor == null)       return;
      SelectionModel selectionModel=editor.getSelectionModel();
      final Project project=editor.getProject();
      if (project == null)       return;
      PsiDocumentManager psiManager=PsiDocumentManager.getInstance(project);
      PsiFile psiFile=psiManager.getPsiFile(editor.getDocument());
      if (!(psiFile instanceof PsiJavaFile)) {
        return;
      }
      PsiJavaFile javaFile=(PsiJavaFile)psiFile;
      PsiFile props=findMessagesDotProperties(project,javaFile);
      if (props == null) {
        Messages.showErrorDialog("String_Node_Str","String_Node_Str");
        return;
      }
      if (!(props instanceof PropertiesFile)) {
        Messages.showErrorDialog("String_Node_Str","String_Node_Str");
        return;
      }
      final PropertiesFile propsFile=(PropertiesFile)props;
      PsiElement e=findSelectedPsiElement(selectionModel,javaFile);
      while (e != null) {
        if (e instanceof PsiExpression)         break;
        e=e.getParent();
      }
      if (e == null) {
        Messages.showErrorDialog("String_Node_Str","String_Node_Str");
        return;
      }
      final PsiExpression exp=(PsiExpression)e;
      final PsiClassType stringType=PsiType.getJavaLangString(PsiManager.getInstance(project),e.getResolveScope());
      if (exp.getType() == null || !exp.getType().equals(stringType)) {
        Messages.showErrorDialog("String_Node_Str","String_Node_Str");
        return;
      }
      final String key=getResourceName(javaFile);
      if (key == null)       return;
      final StringBuilder propertyValue=new StringBuilder();
      final StringBuilder expression=new StringBuilder();
      new Runnable(){
        int numArgs=0;
        public void run(){
          process(exp);
        }
        private void process(        PsiElement exp){
          if (exp instanceof PsiLiteralExpression) {
            PsiLiteralExpression lit=(PsiLiteralExpression)exp;
            if (lit.getType().equals(stringType)) {
              escapeAndAppend(lit.getValue().toString());
              return;
            }
          }
          if (exp instanceof PsiBinaryExpression) {
            PsiBinaryExpression binExp=(PsiBinaryExpression)exp;
            if (binExp.getOperationTokenType() == JavaTokenType.PLUS) {
              process(binExp.getLOperand());
              process(binExp.getROperand());
              return;
            }
          }
          propertyValue.append('{').append(numArgs++).append('}');
          if (expression.length() > 0)           expression.append(',');
          expression.append(exp.getText());
        }
        /** 
 * Takes the literal string value and appends that to <tt>propertyValue</tt> with proper escaping.
 */
        private void escapeAndAppend(        String value){
          for (          char ch : value.toCharArray()) {
switch (ch) {
case '\'':
              propertyValue.append("String_Node_Str");
            break;
case '{':
case '}':
          propertyValue.append('\'').append(ch).append('\'');
        break;
default :
      propertyValue.append(ch);
  }
}
}
}
.run();
ApplicationManager.getApplication().runWriteAction(new Runnable(){
public void run(){
try {
  propsFile.addPropertyAfter(PropertiesElementFactory.createProperty(project,key,propertyValue.toString()),findAnchor(propsFile,key));
}
 catch (IncorrectOperationException x) {
  Messages.showErrorDialog(x.getMessage(),"String_Node_Str");
  return;
}
expression.insert(0,"String_Node_Str" + toJavaIdentifier(key) + "String_Node_Str");
expression.append("String_Node_Str");
TextRange tr=exp.getTextRange();
editor.getDocument().deleteString(tr.getStartOffset(),tr.getEndOffset());
EditorModificationUtil.insertStringAtCaret(editor,expression.toString());
}
private Property findAnchor(PropertiesFile propsFile,String key){
List<Property> list=propsFile.getProperties();
for (int i=0; i < list.size() - 1; i++) {
  Property prev=list.get(i);
  Property next=list.get(i + 1);
  if (prev.getKey().compareTo(key) < 0 && key.compareTo(next.getKey()) < 0)   return prev;
}
if (list.isEmpty()) return null;
return list.get(list.size() - 1);
}
}
);
}
private String getResourceName(PsiJavaFile javaFile){
String key=Messages.showInputDialog("String_Node_Str","String_Node_Str",null);
if (key == null || key.length() == 0) return null;
return getMainClassName(javaFile.getName()) + '.' + key;
}
private String getMainClassName(String name){
if (name.endsWith("String_Node_Str")) return name.substring(0,name.length() - 5);
return name;
}
/** 
 * Locates <tt>Messages.properties</tt> in the same package
 */
private PsiFile findMessagesDotProperties(Project project,PsiJavaFile javaFile){
PsiPackage pkg=PsiManager.getInstance(project).findPackage(javaFile.getPackageName());
for (PsiDirectory dir : pkg.getDirectories()) {
PsiFile props=dir.findFile("String_Node_Str");
if (props != null) return props;
}
return null;
}
/** 
 * Finds the smallest   {@link PsiElement} that encompasses the current selection.
 */
@Nullable private PsiElement findSelectedPsiElement(SelectionModel selectionModel,PsiJavaFile javaFile){
PsiElement e=javaFile.findElementAt(selectionModel.getSelectionStart());
if (e == null) return null;
while (true) {
PsiElement p=e.getParent();
TextRange tr=p.getTextRange();
if (selectionModel.getSelectionEnd() <= tr.getEndOffset()) return e;
e=p;
}
}
/** 
 * Copied from the localizer code. Converts a property name to a method name.
 */
protected String toJavaIdentifier(String key){
return key.replace('.','_');
}
}
);
}