diff --git a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithCastSurrounder.java b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithCastSurrounder.java
index 7311aa4dac03..84803828a7ef 100644
--- a/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithCastSurrounder.java
+++ b/java/java-impl/src/com/intellij/codeInsight/generation/surroundWith/JavaWithCastSurrounder.java
@@ -25,6 +25,7 @@ import com.intellij.openapi.editor.RangeMarker;
import com.intellij.openapi.editor.ScrollType;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange;
+import com.intellij.psi.PsiBinaryExpression;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiType;
import com.intellij.refactoring.introduceField.ElementToWorkOn;
@@ -44,7 +45,8 @@ class JavaWithCastSurrounder extends JavaExpressionSurrounder {
public TextRange surroundExpression(final Project project, final Editor editor, PsiExpression expr) throws IncorrectOperationException {
assert expr.isValid();
PsiType[] types = GuessManager.getInstance(project).guessTypeToCast(expr);
- final Template template = generateTemplate(project, expr.getText(), types);
+ String exprText = expr instanceof PsiBinaryExpression ? "(" + expr.getText() + ")" : expr.getText();
+ final Template template = generateTemplate(project, exprText, types);
TextRange range;
if (expr.isPhysical()) {
range = expr.getTextRange();
diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundBinaryWithCast.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundBinaryWithCast.java
new file mode 100644
index 000000000000..bd4c9bf756ef
--- /dev/null
+++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundBinaryWithCast.java
@@ -0,0 +1,7 @@
+class Test {
+ void foo(int a, double b) {
+ if (a + b) {
+
+ }
+ }
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundBinaryWithCast_after.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundBinaryWithCast_after.java
new file mode 100644
index 000000000000..d72d903be7a6
--- /dev/null
+++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundBinaryWithCast_after.java
@@ -0,0 +1,7 @@
+class Test {
+ void foo(int a, double b) {
+ if ((() (a + b))) {
+
+ }
+ }
+}
\ No newline at end of file
diff --git a/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundNonExpressionWithCast_after.java b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundNonExpressionWithCast_after.java
index 26f446823122..659e44293c6e 100644
--- a/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundNonExpressionWithCast_after.java
+++ b/java/java-tests/testData/codeInsight/generation/surroundWith/java/SurroundNonExpressionWithCast_after.java
@@ -1,7 +1,7 @@
class Test {
void foo() {
String a = "", b = "", c = "";
- if (a + (() b + c)) {
+ if (a + (() (b + c))) {
}
}
diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/generation/surroundWith/JavaSurroundWithTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/generation/surroundWith/JavaSurroundWithTest.java
index 9094d244ae80..8356309aba85 100644
--- a/java/java-tests/testSrc/com/intellij/codeInsight/generation/surroundWith/JavaSurroundWithTest.java
+++ b/java/java-tests/testSrc/com/intellij/codeInsight/generation/surroundWith/JavaSurroundWithTest.java
@@ -106,7 +106,18 @@ public class JavaSurroundWithTest extends LightCodeInsightTestCase {
doTest(getTestName(false), new JavaWithNotSurrounder());
}
- private void doTest(@NotNull String fileName, final Surrounder surrounder) throws Exception {
+ public void testSurroundBinaryWithCast() {
+ final TemplateManagerImpl templateManager = (TemplateManagerImpl)TemplateManager.getInstance(getProject());
+ templateManager.setTemplateTesting(true);
+ try {
+ doTest(getTestName(false), new JavaWithCastSurrounder());
+ }
+ finally {
+ templateManager.setTemplateTesting(false);
+ }
+ }
+
+ private void doTest(@NotNull String fileName, final Surrounder surrounder) {
configureByFile(BASE_PATH + fileName + ".java");
SurroundWithHandler.invoke(getProject(), getEditor(), getFile(), surrounder);
checkResultByFile(BASE_PATH + fileName + "_after.java");
diff --git a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/surroundWith/SurroundExpressionTest.java b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/surroundWith/SurroundExpressionTest.java
index 7eb43b286ac3..d615404d4ec8 100644
--- a/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/surroundWith/SurroundExpressionTest.java
+++ b/plugins/groovy/test/org/jetbrains/plugins/groovy/lang/surroundWith/SurroundExpressionTest.java
@@ -15,6 +15,7 @@ public class SurroundExpressionTest extends SurroundTestCase {
public void testType_cast2() throws Exception { doTest(new TypeCastSurrounder()); }
public void testWhile1() throws Exception { doTest(new WhileExprSurrounder()); }
public void testWith2() throws Exception { doTest(new WithExprSurrounder()); }
+ public void testBinaryWithCast() throws Exception { doTest(new TypeCastSurrounder()); }
@Override
protected String getBasePath() {
diff --git a/plugins/groovy/testdata/groovy/surround/expr/binaryWithCast.test b/plugins/groovy/testdata/groovy/surround/expr/binaryWithCast.test
new file mode 100644
index 000000000000..cf498cd0427e
--- /dev/null
+++ b/plugins/groovy/testdata/groovy/surround/expr/binaryWithCast.test
@@ -0,0 +1,3 @@
+a + b
+-----
+(() (a + b))
\ No newline at end of file
diff --git a/plugins/groovy/testdata/groovy/surround/expr/type_cast1.test b/plugins/groovy/testdata/groovy/surround/expr/type_cast1.test
index fc381f74e4f6..d40773e67492 100644
--- a/plugins/groovy/testdata/groovy/surround/expr/type_cast1.test
+++ b/plugins/groovy/testdata/groovy/surround/expr/type_cast1.test
@@ -1,3 +1,3 @@
expr
-----
-(() expr)
\ No newline at end of file
+(() expr)
\ No newline at end of file