@Override public void visitArrayInitializerExpression( PsiArrayInitializerExpression expression) { super.visitArrayInitializerExpression(expression); final PsiType type = expression.getType(); if (type == null) { return; } final PsiType componentType = type.getDeepComponentType(); if (!(componentType instanceof PsiPrimitiveType)) { return; } final int numElements = calculateNumElements(expression); if (numElements <= m_limit) { return; } registerError(expression, Integer.valueOf(numElements)); }
@Override public void visitArrayInitializerExpression( PsiArrayInitializerExpression expression) { super.visitArrayInitializerExpression(expression); final PsiExpression[] initializers = expression.getInitializers(); if (initializers.length > 0) { return; } if (expression.getParent() instanceof PsiNewExpression) { return; } if (ExpressionUtils.isDeclaredConstant(expression)) { return; } registerError(expression); }
@Override public void visitArrayInitializerExpression( PsiArrayInitializerExpression expression) { super.visitArrayInitializerExpression(expression); final PsiExpression[] initializers = expression.getInitializers(); boolean hasDecimalLiteral = false; boolean hasOctalLiteral = false; for (final PsiExpression initializer : initializers) { if (initializer instanceof PsiLiteralExpression) { final PsiLiteralExpression literal = (PsiLiteralExpression)initializer; if (isDecimalLiteral(literal)) { hasDecimalLiteral = true; } if (isOctalLiteral(literal)) { hasOctalLiteral = true; } } } if (hasOctalLiteral && hasDecimalLiteral) { registerError(expression); } }
@Override public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException { if (!(psiElement instanceof PsiArrayInitializerExpression)) return; PsiArrayInitializerExpression expr = (PsiArrayInitializerExpression)psiElement; final Document doc = editor.getDocument(); final String exprText = expr.getText(); final TextRange textRange = expr.getTextRange(); final int endOffset = textRange.getEndOffset(); int caretOffset = editor.getCaretModel().getOffset(); final int startOffset = textRange.getStartOffset(); if (caretOffset > startOffset && caretOffset < endOffset) { final int index = exprText.indexOf('\n', caretOffset - startOffset); if (index >= 0) { doc.insertString(index + startOffset, "}"); return; } } if (!exprText.endsWith("}")) { doc.insertString(endOffset, "}"); } }
public void verifyVariable(@NotNull final PsiLocalVariable psiLocalVariable, @NotNull final ProblemsHolder holder) { boolean isVal = isSameName(psiLocalVariable.getTypeElement().getText()); boolean isVar = isVar(psiLocalVariable.getTypeElement().getText()); final String ann = isVal ? "val" : "var"; if (isVal || isVar) { final PsiExpression initializer = psiLocalVariable.getInitializer(); if (initializer == null) { holder.registerProblem(psiLocalVariable, "'" + ann + "' on a local variable requires an initializer expression", ProblemHighlightType.ERROR); } else if (initializer instanceof PsiArrayInitializerExpression) { holder.registerProblem(psiLocalVariable, "'" + ann + "' is not compatible with array initializer expressions. Use the full form (new int[] { ... } instead of just { ... })", ProblemHighlightType.ERROR); } else if (initializer instanceof PsiLambdaExpression) { holder.registerProblem(psiLocalVariable, "'" + ann + "' is not allowed with lambda expressions.", ProblemHighlightType.ERROR); } else if (isVal) { final PsiElement typeParentParent = psiLocalVariable.getParent(); if (typeParentParent instanceof PsiDeclarationStatement && typeParentParent.getParent() instanceof PsiForStatement) { holder.registerProblem(psiLocalVariable, "'" + ann + "' is not allowed in old-style for loops", ProblemHighlightType.ERROR); } } } }
@Override public void visitArrayInitializerExpression(PsiArrayInitializerExpression expression) { super.visitArrayInitializerExpression(expression); final PsiExpression[] initializers = expression.getInitializers(); if(initializers.length > 0) { return; } if(expression.getParent() instanceof PsiNewExpression) { return; } if(ExpressionUtils.isDeclaredConstant(expression)) { return; } registerError(expression); }
@Override public boolean isApplicableTo(PsiElement e) { if (e instanceof PsiArrayInitializerExpression) { final PsiElement gParent = e.getParent(); if (gParent instanceof PsiNewExpression && gParent.getParent() instanceof PsiVariable) { return true; } } return false; }
@Override protected void doUnwrap(PsiElement element, Context context) throws IncorrectOperationException { final PsiArrayInitializerExpression arrayInitializerExpression = (PsiArrayInitializerExpression)element; final PsiElement newExpression = arrayInitializerExpression.getParent(); context.extractElement(arrayInitializerExpression, newExpression); context.deleteExactly(newExpression); }
@Override public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException { if (!(psiElement instanceof PsiArrayInitializerExpression)) return; PsiArrayInitializerExpression expr = (PsiArrayInitializerExpression)psiElement; if (!expr.getText().endsWith("}")) { PsiErrorElement err = ContainerUtil.findInstance(expr.getChildren(), PsiErrorElement.class); int endOffset = (err != null ? err : expr).getTextRange().getEndOffset(); editor.getDocument().insertString(endOffset, "}"); } }
private int calculateNumElements(PsiExpression expression) { if (expression instanceof PsiArrayInitializerExpression) { final PsiArrayInitializerExpression arrayExpression = (PsiArrayInitializerExpression)expression; final PsiExpression[] initializers = arrayExpression.getInitializers(); int out = 0; for (final PsiExpression initializer : initializers) { out += calculateNumElements(initializer); } return out; } return 1; }
public boolean satisfiedBy(@NotNull PsiElement element) { if (!(element instanceof PsiArrayInitializerExpression)) { return false; } final PsiArrayInitializerExpression arrayInitializerExpression = (PsiArrayInitializerExpression)element; if (arrayInitializerExpression.getType() == null) { return false; } final PsiElement parent = element.getParent(); return !(parent instanceof PsiNewExpression); }
@Override protected void processIntention(@NotNull PsiElement element) throws IncorrectOperationException { final PsiArrayInitializerExpression arrayInitializerExpression = (PsiArrayInitializerExpression)element; final PsiType type = arrayInitializerExpression.getType(); if (type == null) { return; } final String typeText = type.getCanonicalText(); final String newExpressionText = "new " + typeText + arrayInitializerExpression.getText(); replaceExpression(newExpressionText, arrayInitializerExpression); }
@Override public void visitArrayInitializerExpression( PsiArrayInitializerExpression expression) { if (passed) { return; } super.visitArrayInitializerExpression(expression); final PsiExpression[] initializers = expression.getInitializers(); for (final PsiExpression initializer : initializers) { if (VariableAccessUtils.mayEvaluateToVariable(initializer, variable)) { passed = true; } } }
@Override public void apply(Editor editor, JavaSmartEnterProcessor processor, PsiElement psiElement) throws IncorrectOperationException { if(!(psiElement instanceof PsiArrayInitializerExpression)) { return; } PsiArrayInitializerExpression expr = (PsiArrayInitializerExpression) psiElement; final Document doc = editor.getDocument(); final String exprText = expr.getText(); final TextRange textRange = expr.getTextRange(); final int endOffset = textRange.getEndOffset(); int caretOffset = editor.getCaretModel().getOffset(); final int startOffset = textRange.getStartOffset(); if(caretOffset > startOffset && caretOffset < endOffset) { final int index = exprText.indexOf('\n', caretOffset - startOffset); if(index >= 0) { doc.insertString(index + startOffset, "}"); return; } } if(!exprText.endsWith("}")) { doc.insertString(endOffset, "}"); } }
@RequiredReadAction @NotNull @Override public PsiElement[] getMovableSubElements(@NotNull PsiElement element) { if(element instanceof PsiParameterList) { return ((PsiParameterList) element).getParameters(); } else if(element instanceof PsiExpressionList) { return ((PsiExpressionList) element).getExpressions(); } else if(element instanceof PsiAnnotationParameterList) { return ((PsiAnnotationParameterList) element).getAttributes(); } else if(element instanceof PsiArrayInitializerExpression) { return ((PsiArrayInitializerExpression) element).getInitializers(); } else if(element instanceof PsiClass && ((PsiClass) element).isEnum()) { PsiEnumConstant[] enumConstants = PsiTreeUtil.getChildrenOfType(element, PsiEnumConstant.class); if(enumConstants != null) { return enumConstants; } } return PsiElement.EMPTY_ARRAY; }
@Override public void doFix(@NotNull final Project project, final ProblemDescriptor descriptor) { final PsiElement nameElement = descriptor.getPsiElement(); final PsiLocalVariable variable = (PsiLocalVariable)nameElement.getParent(); PsiExpression initializer = variable.getInitializer(); if (initializer == null) { return; } if (initializer instanceof PsiArrayInitializerExpression) { final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); final PsiType type = initializer.getType(); final String typeText; if (type == null) { typeText = ""; } else { typeText = type.getCanonicalText(); } initializer = factory.createExpressionFromText("new " + typeText + initializer.getText(), variable); } final PsiMember member = PsiTreeUtil.getParentOfType(variable, PsiMember.class); final Query<PsiReference> search = ReferencesSearch.search(variable, new LocalSearchScope(member)); final Collection<PsiElement> replacedElements = new ArrayList<PsiElement>(); final Collection<PsiReference> references = search.findAll(); for (PsiReference reference : references) { final PsiElement replacedElement = reference.getElement().replace(initializer); replacedElements.add(replacedElement); } HighlightUtils.highlightElements(replacedElements); variable.delete(); }
private PsiType processLocalVariableInitializer( final PsiExpression psiExpression ) { PsiType result = null; if( null != psiExpression && !(psiExpression instanceof PsiArrayInitializerExpression) ) { if( psiExpression instanceof PsiConditionalExpression ) { result = RecursionManager.doPreventingRecursion( psiExpression, true, () -> { final PsiExpression thenExpression = ((PsiConditionalExpression)psiExpression).getThenExpression(); final PsiExpression elseExpression = ((PsiConditionalExpression)psiExpression).getElseExpression(); final PsiType thenType = null != thenExpression ? thenExpression.getType() : null; final PsiType elseType = null != elseExpression ? elseExpression.getType() : null; if( thenType == null ) { return elseType; } if( elseType == null ) { return thenType; } if( TypeConversionUtil.isAssignable( thenType, elseType, false ) ) { return thenType; } if( TypeConversionUtil.isAssignable( elseType, thenType, false ) ) { return elseType; } return thenType; } ); } else { result = RecursionManager.doPreventingRecursion( psiExpression, true, psiExpression::getType ); } if( psiExpression instanceof PsiNewExpression ) { final PsiJavaCodeReferenceElement reference = ((PsiNewExpression)psiExpression).getClassOrAnonymousClassReference(); if( reference != null ) { final PsiReferenceParameterList parameterList = reference.getParameterList(); if( parameterList != null ) { final PsiTypeElement[] elements = parameterList.getTypeParameterElements(); if( elements.length == 1 && elements[0].getType() instanceof PsiDiamondType ) { result = TypeConversionUtil.erasure( result ); } } } } } return result; }
@Override public void visitArrayInitializerExpression(PsiArrayInitializerExpression expression) { mVisitor.report("PsiArrayInitializerExpression", expression.getText(), expression); super.visitExpression(expression); }
private PsiType processLocalVariableInitializer(final PsiExpression psiExpression) { PsiType result = null; if (null != psiExpression && !(psiExpression instanceof PsiArrayInitializerExpression)) { if (psiExpression instanceof PsiConditionalExpression) { result = RecursionManager.doPreventingRecursion(psiExpression, true, new Computable<PsiType>() { @Override public PsiType compute() { final PsiExpression thenExpression = ((PsiConditionalExpression) psiExpression).getThenExpression(); final PsiExpression elseExpression = ((PsiConditionalExpression) psiExpression).getElseExpression(); final PsiType thenType = null != thenExpression ? thenExpression.getType() : null; final PsiType elseType = null != elseExpression ? elseExpression.getType() : null; if (thenType == null) { return elseType; } if (elseType == null) { return thenType; } if (TypeConversionUtil.isAssignable(thenType, elseType, false)) { return thenType; } if (TypeConversionUtil.isAssignable(elseType, thenType, false)) { return elseType; } return thenType; } }); } else { result = RecursionManager.doPreventingRecursion(psiExpression, true, new Computable<PsiType>() { @Override public PsiType compute() { return psiExpression.getType(); } }); } if (psiExpression instanceof PsiNewExpression) { final PsiJavaCodeReferenceElement reference = ((PsiNewExpression) psiExpression).getClassOrAnonymousClassReference(); if (reference != null) { final PsiReferenceParameterList parameterList = reference.getParameterList(); if (parameterList != null) { final PsiTypeElement[] elements = parameterList.getTypeParameterElements(); if (elements.length == 1 && elements[0].getType() instanceof PsiDiamondType) { result = TypeConversionUtil.erasure(result); } } } } } return result; }