EA-33153 - check class for writable before "create method" suggestion

This commit is contained in:
Maxim.Medvedev
2012-01-31 17:23:08 +04:00
parent a9b5676eef
commit 0a09d401c7
2 changed files with 13 additions and 8 deletions
@@ -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));
}
@@ -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);
}