EA-33153 - check class for writable before "create method" suggestion
authorMaxim.Medvedev <maxim.medvedev@jetbrains.com>
Sun, 29 Jan 2012 09:22:37 +0000 (13:22 +0400)
committerMaxim.Medvedev <maxim.medvedev@jetbrains.com>
Tue, 31 Jan 2012 13:23:08 +0000 (17:23 +0400)
plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GroovyAnnotator.java
plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/intentions/CreateMethodFromUsageFix.java

index f730ea5d9d9116a78c942ed184fd7c7bc8424046..0d684f8bce7ec83c281611f5b5290cd8558c4f77 100644 (file)
@@ -1527,7 +1527,7 @@ public class GroovyAnnotator extends GroovyElementVisitor implements Annotator {
     if (targetClass == null) return;
 
     addDynamicAnnotation(annotation, refExpr);
-    if (targetClass instanceof GrMemberOwner) {
+    if (targetClass instanceof GrMemberOwner && targetClass.isWritable()) {
       if (!(targetClass instanceof GroovyScriptClass)) {
         annotation.registerFix(new CreateFieldFromUsageFix(refExpr, (GrMemberOwner)targetClass));
       }
index fe28f98367c1707effa2e6ceb033dc1368778cba..e051436b712f97736307649e7a93320cbd9b7b4b 100644 (file)
@@ -64,11 +64,11 @@ public class CreateMethodFromUsageFix implements IntentionAction {
   }
 
   public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
-    return myTargetClass.isValid() && myRefExpression.isValid();
+    return myTargetClass.isValid() && myRefExpression.isValid() && myTargetClass.isWritable();
   }
 
   public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
-    StringBuffer methodBuffer = new StringBuffer();
+    StringBuilder methodBuffer = new StringBuilder();
     if (PsiUtil.isInStaticContext(myRefExpression, myTargetClass)) methodBuffer.append("static ");
     methodBuffer.append("Object ").append(myRefExpression.getReferenceName()).append(" (");
     PsiType[] argTypes = PsiUtil.getArgumentTypes(myRefExpression, false);
@@ -79,23 +79,28 @@ public class CreateMethodFromUsageFix implements IntentionAction {
       if (argType == null) argType = TypesUtil.getJavaLangObject(myRefExpression);
       if (i > 0) methodBuffer.append(", ");
       methodBuffer.append("Object o").append(i);
-      paramTypesExpressions[i] = new ChooseTypeExpression(new TypeConstraint[]{SupertypeConstraint.create(argType)}, myRefExpression.getManager());
+      paramTypesExpressions[i] =
+        new ChooseTypeExpression(new TypeConstraint[]{SupertypeConstraint.create(argType)}, myRefExpression.getManager());
     }
     methodBuffer.append(") {\n}");
     GrMethod method = GroovyPsiElementFactory.getInstance(project).createMethodFromText(methodBuffer.toString());
     GrMemberOwner owner = myTargetClass;
-    TypeConstraint[] constraints = GroovyExpectedTypesProvider.calculateTypeConstraints((GrExpression) myRefExpression.getParent());
-    PsiElement parent = myTargetClass instanceof GrTypeDefinition ? ((GrTypeDefinition)myTargetClass).getBody() : ((GroovyScriptClass) myTargetClass).getContainingFile();
+    TypeConstraint[] constraints = GroovyExpectedTypesProvider.calculateTypeConstraints((GrExpression)myRefExpression.getParent());
+    PsiElement parent = myTargetClass instanceof GrTypeDefinition
+                        ? ((GrTypeDefinition)myTargetClass).getBody()
+                        : ((GroovyScriptClass)myTargetClass).getContainingFile();
     if (PsiTreeUtil.isAncestor(parent, myRefExpression, false)) {
       PsiElement prevParent = PsiTreeUtil.findPrevParent(parent, myRefExpression);
       PsiElement sibling = PsiUtil.skipWhitespaces(prevParent.getNextSibling(), true);
       if (sibling != null && GroovyTokenTypes.mSEMI.equals(sibling.getNode().getElementType())) {
         sibling = sibling.getNextSibling();
-      } else {
+      }
+      else {
         sibling = prevParent.getNextSibling();
       }
       method = owner.addMemberDeclaration(method, sibling);
-    } else {
+    }
+    else {
       method = owner.addMemberDeclaration(method, null);
     }