@Override public void visitAnonymousClass(@NotNull PsiAnonymousClass anonymousClass) { if (anonymousClass instanceof PsiEnumConstantInitializer) { return; } final PsiMember containingMember = PsiTreeUtil.getParentOfType(anonymousClass, PsiMember.class); if (containingMember == null || containingMember.hasModifierProperty(PsiModifier.STATIC)) { return; } final PsiJavaCodeReferenceElement reference = anonymousClass.getBaseClassReference(); if (reference.resolve() == null) { // don't warn on broken code return; } final PsiClass containingClass = PsiTreeUtil.getParentOfType(anonymousClass, PsiClass.class); if (containingClass == null) { return; } if (containingClass.getContainingClass() != null && !containingClass.hasModifierProperty(PsiModifier.STATIC)) { // strictly speaking can be named static inner class but not when part of the current // containing class return; } final InnerClassReferenceVisitor visitor = new InnerClassReferenceVisitor(anonymousClass); anonymousClass.accept(visitor); if (!visitor.canInnerClassBeStatic()) { return; } if (hasReferenceToLocalClass(anonymousClass)) { return; } registerClassError(anonymousClass); }
protected final void registerClassError(@NotNull PsiClass aClass, Object... infos) { final PsiElement nameIdentifier; if (aClass instanceof PsiEnumConstantInitializer) { final PsiEnumConstantInitializer enumConstantInitializer = (PsiEnumConstantInitializer) aClass; final PsiEnumConstant enumConstant = enumConstantInitializer.getEnumConstant(); nameIdentifier = enumConstant.getNameIdentifier(); } else if (aClass instanceof PsiAnonymousClass) { final PsiAnonymousClass anonymousClass = (PsiAnonymousClass) aClass; nameIdentifier = anonymousClass.getBaseClassReference(); } else { nameIdentifier = aClass.getNameIdentifier(); } if (nameIdentifier == null) { registerError(aClass.getContainingFile(), infos); } else { registerError(nameIdentifier, infos); } }
private static void changeNewOperatorType( PsiNewExpression originalExpression, PsiType toType, final Editor editor) throws IncorrectOperationException { PsiNewExpression newExpression; PsiElementFactory factory = JavaPsiFacade.getInstance(originalExpression.getProject()).getElementFactory(); int caretOffset; TextRange selection; if (toType instanceof PsiArrayType) { final PsiExpression[] originalExpressionArrayDimensions = originalExpression.getArrayDimensions(); caretOffset = 0; @NonNls String text = "new " + toType.getDeepComponentType().getCanonicalText() + "["; if (originalExpressionArrayDimensions.length > 0) { text += originalExpressionArrayDimensions[0].getText(); } else { text += "0"; caretOffset = -2; } text += "]"; for (int i = 1; i < toType.getArrayDimensions(); i++) { text += "["; String arrayDimension = ""; if (originalExpressionArrayDimensions.length > i) { arrayDimension = originalExpressionArrayDimensions[i].getText(); text += arrayDimension; } text += "]"; if (caretOffset < 0) { caretOffset -= arrayDimension.length() + 2; } } newExpression = (PsiNewExpression) factory.createExpressionFromText(text, originalExpression); if (caretOffset < 0) { selection = new TextRange(caretOffset, caretOffset + 1); } else { selection = null; } } else { final PsiAnonymousClass anonymousClass = originalExpression.getAnonymousClass(); newExpression = (PsiNewExpression) factory.createExpressionFromText( "new " + toType.getCanonicalText() + "()" + (anonymousClass != null ? "{}" : ""), originalExpression); PsiExpressionList argumentList = originalExpression.getArgumentList(); if (argumentList == null) return; newExpression.getArgumentList().replace(argumentList); if (anonymousClass == null) { // just to prevent useless inference if (PsiDiamondTypeUtil.canCollapseToDiamond(newExpression, originalExpression, toType)) { final PsiElement paramList = PsiDiamondTypeUtil.replaceExplicitWithDiamond( newExpression.getClassOrAnonymousClassReference().getParameterList()); newExpression = PsiTreeUtil.getParentOfType(paramList, PsiNewExpression.class); } } if (anonymousClass != null) { final PsiAnonymousClass newAnonymousClass = (PsiAnonymousClass) newExpression.getAnonymousClass().replace(anonymousClass); final PsiClass aClass = PsiUtil.resolveClassInType(toType); assert aClass != null; newAnonymousClass .getBaseClassReference() .replace(factory.createClassReferenceElement(aClass)); } selection = null; caretOffset = -1; } PsiElement element = originalExpression.replace(newExpression); editor.getCaretModel().moveToOffset(element.getTextRange().getEndOffset() + caretOffset); editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); if (selection != null) { selection = selection.shiftRight(element.getTextRange().getEndOffset()); editor.getSelectionModel().setSelection(selection.getStartOffset(), selection.getEndOffset()); } }