@Override public void invoke(@NotNull Project project, Editor editor, PsiFile file) { if (!FileModificationService.getInstance().prepareFileForWrite(myConstructor.getContainingFile())) return; try { PsiStatement superCall = JavaPsiFacade.getInstance(myConstructor.getProject()).getElementFactory().createStatementFromText("super();",null); PsiCodeBlock body = myConstructor.getBody(); PsiJavaToken lBrace = body.getLBrace(); body.addAfter(superCall, lBrace); lBrace = (PsiJavaToken) new PsiMatcherImpl(body) .firstChild(PsiMatchers.hasClass(PsiExpressionStatement.class)) .firstChild(PsiMatchers.hasClass(PsiMethodCallExpression.class)) .firstChild(PsiMatchers.hasClass(PsiExpressionList.class)) .firstChild(PsiMatchers.hasClass(PsiJavaToken.class)) .dot(PsiMatchers.hasText("(")) .getElement(); editor.getCaretModel().moveToOffset(lBrace.getTextOffset()+1); UndoUtil.markPsiFileForUndo(file); } catch (IncorrectOperationException e) { LOG.error(e); } }
private static boolean isParameterUsedRecursively(@NotNull PsiElement element, @NotNull Collection<PsiReference> array) { if (!(element instanceof PsiParameter)) return false; PsiParameter parameter = (PsiParameter)element; PsiElement scope = parameter.getDeclarationScope(); if (!(scope instanceof PsiMethod)) return false; PsiMethod method = (PsiMethod)scope; int paramIndex = ArrayUtilRt.find(method.getParameterList().getParameters(), parameter); for (PsiReference reference : array) { if (!(reference instanceof PsiElement)) return false; PsiElement argument = (PsiElement)reference; PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)new PsiMatcherImpl(argument) .dot(PsiMatchers.hasClass(PsiReferenceExpression.class)) .parent(PsiMatchers.hasClass(PsiExpressionList.class)) .parent(PsiMatchers.hasClass(PsiMethodCallExpression.class)) .getElement(); if (methodCallExpression == null) return false; PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression(); if (method != methodExpression.resolve()) return false; PsiExpressionList argumentList = methodCallExpression.getArgumentList(); PsiExpression[] arguments = argumentList.getExpressions(); int argumentIndex = ArrayUtilRt.find(arguments, argument); if (paramIndex != argumentIndex) return false; } return true; }
private static boolean isParameterUsedRecursively(@NotNull PsiElement element, @NotNull List<PsiReference> array) { if (!(element instanceof PsiParameter)) return false; PsiParameter parameter = (PsiParameter)element; PsiElement scope = parameter.getDeclarationScope(); if (!(scope instanceof PsiMethod)) return false; PsiMethod method = (PsiMethod)scope; int paramIndex = ArrayUtilRt.find(method.getParameterList().getParameters(), parameter); for (PsiReference reference : array) { if (!(reference instanceof PsiElement)) return false; PsiElement argument = (PsiElement)reference; PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression)new PsiMatcherImpl(argument) .dot(PsiMatchers.hasClass(PsiReferenceExpression.class)) .parent(PsiMatchers.hasClass(PsiExpressionList.class)) .parent(PsiMatchers.hasClass(PsiMethodCallExpression.class)) .getElement(); if (methodCallExpression == null) return false; PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression(); if (method != methodExpression.resolve()) return false; PsiExpressionList argumentList = methodCallExpression.getArgumentList(); PsiExpression[] arguments = argumentList.getExpressions(); int argumentIndex = ArrayUtilRt.find(arguments, argument); if (paramIndex != argumentIndex) return false; } return true; }
private static PsiElement getEnclosingStaticClass(@NotNull PsiKeyword keyword, @NotNull Class<?> parentClass) { return new PsiMatcherImpl(keyword).dot(PsiMatchers.hasText(PsiModifier.STATIC)).parent(PsiMatchers.hasClass (PsiModifierList.class)).parent(PsiMatchers.hasClass(parentClass)).parent(PsiMatchers.hasClass (PsiClass.class)).dot(JavaMatchers.hasModifier(PsiModifier.STATIC, false)).parent(PsiMatchers.hasClass (PsiClass.class, PsiDeclarationStatement.class, PsiNewExpression.class, PsiEnumConstant.class)).getElement(); }
static void visitConstructorChain(PsiMethod constructor, ConstructorVisitorInfo info) { while (true) { if (constructor == null) return; final PsiCodeBlock body = constructor.getBody(); if (body == null) return; final PsiStatement[] statements = body.getStatements(); if (statements.length == 0) return; final PsiStatement statement = statements[0]; final PsiElement element = new PsiMatcherImpl(statement) .dot(PsiMatchers.hasClass(PsiExpressionStatement.class)) .firstChild(PsiMatchers.hasClass(PsiMethodCallExpression.class)) .firstChild(PsiMatchers.hasClass(PsiReferenceExpression.class)) .firstChild(PsiMatchers.hasClass(PsiKeyword.class)) .dot(PsiMatchers.hasText(PsiKeyword.THIS)) .parent(null) .parent(null) .getElement(); if (element == null) return; PsiMethodCallExpression methodCall = (PsiMethodCallExpression)element; PsiMethod method = methodCall.resolveMethod(); if (method == null) return; if (info.visitedConstructors != null && info.visitedConstructors.contains(method)) { info.recursivelyCalledConstructor = method; return; } if (info.visitedConstructors == null) info.visitedConstructors = new ArrayList<PsiMethod>(5); info.visitedConstructors.add(method); constructor = method; } }
@Override public void invoke(@NotNull Project project, Editor editor, PsiFile file) { PsiStatement superCall = JavaPsiFacade.getInstance(myConstructor.getProject()).getElementFactory().createStatementFromText(myCall, null); PsiCodeBlock body = myConstructor.getBody(); PsiJavaToken lBrace = body.getLBrace(); body.addAfter(superCall, lBrace); lBrace = (PsiJavaToken) new PsiMatcherImpl(body).firstChild(PsiMatchers.hasClass(PsiExpressionStatement.class)).firstChild(PsiMatchers.hasClass(PsiMethodCallExpression.class)).firstChild (PsiMatchers.hasClass(PsiExpressionList.class)).firstChild(PsiMatchers.hasClass(PsiJavaToken.class)).dot(PsiMatchers.hasText("(")).getElement(); editor.getCaretModel().moveToOffset(lBrace.getTextOffset() + 1); UndoUtil.markPsiFileForUndo(file); }
@Nullable private static HighlightInfo checkStaticClassDeclarationInInnerClass(PsiKeyword keyword) { // keyword points to 'class' or 'interface' or 'enum' if(new PsiMatcherImpl(keyword).parent(PsiMatchers.hasClass(PsiClass.class)).dot(JavaMatchers.hasModifier (PsiModifier.STATIC, true)).parent(PsiMatchers.hasClass(PsiClass.class)).dot(JavaMatchers.hasModifier (PsiModifier.STATIC, false)).parent(PsiMatchers.hasClass(PsiClass.class, PsiDeclarationStatement.class, PsiNewExpression.class, PsiEnumConstant.class)).getElement() == null) { return null; } PsiClass aClass = (PsiClass) keyword.getParent(); if(PsiUtilCore.hasErrorElementChild(aClass)) { return null; } // highlight 'static' keyword if any, or class or interface if not PsiElement context = null; PsiModifierList modifierList = aClass.getModifierList(); if(modifierList != null) { for(PsiElement element : modifierList.getChildren()) { if(Comparing.equal(element.getText(), PsiModifier.STATIC)) { context = element; break; } } } TextRange range = context != null ? context.getTextRange() : HighlightNamesUtil.getClassDeclarationTextRange (aClass); String message = JavaErrorMessages.message("static.declaration.in.inner.class"); HighlightInfo info = HighlightInfo.newHighlightInfo(HighlightInfoType.ERROR).range(range) .descriptionAndTooltip(message).create(); if(context != keyword) { QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createModifierListFix(aClass, PsiModifier.STATIC, false, false)); } PsiClass containingClass = aClass.getContainingClass(); if(containingClass != null) { QuickFixAction.registerQuickFixAction(info, QUICK_FIX_FACTORY.createModifierListFix(containingClass, PsiModifier.STATIC, true, false)); } return info; }
private static boolean isParameterUsedRecursively(@NotNull PsiElement element, @NotNull Collection<PsiReference> array) { if(!(element instanceof PsiParameter)) { return false; } PsiParameter parameter = (PsiParameter) element; PsiElement scope = parameter.getDeclarationScope(); if(!(scope instanceof PsiMethod)) { return false; } PsiMethod method = (PsiMethod) scope; int paramIndex = ArrayUtilRt.find(method.getParameterList().getParameters(), parameter); for(PsiReference reference : array) { if(!(reference instanceof PsiElement)) { return false; } PsiElement argument = (PsiElement) reference; PsiMethodCallExpression methodCallExpression = (PsiMethodCallExpression) new PsiMatcherImpl(argument).dot(PsiMatchers.hasClass(PsiReferenceExpression.class)).parent(PsiMatchers.hasClass (PsiExpressionList.class)).parent(PsiMatchers.hasClass(PsiMethodCallExpression.class)).getElement(); if(methodCallExpression == null) { return false; } PsiReferenceExpression methodExpression = methodCallExpression.getMethodExpression(); if(method != methodExpression.resolve()) { return false; } PsiExpressionList argumentList = methodCallExpression.getArgumentList(); PsiExpression[] arguments = argumentList.getExpressions(); int argumentIndex = ArrayUtilRt.find(arguments, argument); if(paramIndex != argumentIndex) { return false; } } return true; }