From 0a09d401c7e595712ebfe1ef0205b97e225f995e Mon Sep 17 00:00:00 2001 From: "Maxim.Medvedev" Date: Sun, 29 Jan 2012 13:22:37 +0400 Subject: [PATCH] EA-33153 - check class for writable before "create method" suggestion --- .../groovy/annotator/GroovyAnnotator.java | 2 +- .../intentions/CreateMethodFromUsageFix.java | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GroovyAnnotator.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GroovyAnnotator.java index f730ea5d9d91..0d684f8bce7e 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GroovyAnnotator.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/GroovyAnnotator.java @@ -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)); } diff --git a/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/intentions/CreateMethodFromUsageFix.java b/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/intentions/CreateMethodFromUsageFix.java index fe28f98367c1..e051436b712f 100644 --- a/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/intentions/CreateMethodFromUsageFix.java +++ b/plugins/groovy/src/org/jetbrains/plugins/groovy/annotator/intentions/CreateMethodFromUsageFix.java @@ -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); }