public static boolean checkSideEffects( PsiElement element, PsiVariable variable, List<PsiElement> sideEffects) { if (sideEffects == null || element == null) return false; if (element instanceof PsiMethodCallExpression) { final PsiMethod psiMethod = ((PsiMethodCallExpression) element).resolveMethod(); if (psiMethod == null || !PropertyUtil.isSimpleGetter(psiMethod) && !PropertyUtil.isSimpleSetter(psiMethod)) { sideEffects.add(element); return true; } } if (element instanceof PsiNewExpression) { PsiNewExpression newExpression = (PsiNewExpression) element; if (newExpression.getArrayDimensions().length == 0 && newExpression.getArrayInitializer() == null && !isSideEffectFreeConstructor(newExpression)) { sideEffects.add(element); return true; } } if (element instanceof PsiAssignmentExpression && !(((PsiAssignmentExpression) element).getLExpression() instanceof PsiReferenceExpression && ((PsiReferenceExpression) ((PsiAssignmentExpression) element).getLExpression()) .resolve() == variable)) { sideEffects.add(element); return true; } PsiElement[] children = element.getChildren(); for (PsiElement child : children) { checkSideEffects(child, variable, sideEffects); } return !sideEffects.isEmpty(); }
private static void collectPsiStatements(PsiElement root, Set<PsiStatement> collected) { if (root instanceof PsiStatement) { collected.add((PsiStatement) root); } for (PsiElement element : root.getChildren()) { collectPsiStatements(element, collected); } }
@Override public void visitElement(PsiElement element) { final RefManagerExtension extension = getExtension(element.getLanguage()); if (extension != null) { extension.visitElement(element); } for (PsiElement aChildren : element.getChildren()) { aChildren.accept(this); } }
@Nullable private static Map<PsiFile, PsiClass[]> convertToTopLevelClasses( final PsiElement[] elements, final boolean fromUpdate, String relativePath, Map<PsiFile, String> relativeMap) { final Map<PsiFile, PsiClass[]> result = new HashMap<PsiFile, PsiClass[]>(); for (PsiElement element : elements) { final PsiElement navigationElement = element.getNavigationElement(); LOG.assertTrue(navigationElement != null, element); final PsiFile containingFile = navigationElement.getContainingFile(); if (!(containingFile instanceof PsiClassOwner && ProjectRootsUtil.isOutsideSourceRoot(containingFile))) { PsiClass[] topLevelClasses = getTopLevelClasses(element); if (topLevelClasses == null) { if (element instanceof PsiDirectory) { if (!fromUpdate) { final String name = ((PsiDirectory) element).getName(); final String path = relativePath != null ? (relativePath.length() > 0 ? (relativePath + "/") : "") + name : null; final Map<PsiFile, PsiClass[]> map = convertToTopLevelClasses(element.getChildren(), fromUpdate, path, relativeMap); if (map == null) return null; for (Map.Entry<PsiFile, PsiClass[]> entry : map.entrySet()) { fillResultsMap(result, entry.getKey(), entry.getValue()); } } continue; } if (!(element instanceof PsiFileSystemItem)) return null; } fillResultsMap(result, containingFile, topLevelClasses); if (relativeMap != null) { relativeMap.put(containingFile, relativePath); } } } if (result.isEmpty()) { return null; } else { boolean hasClasses = false; for (PsiClass[] classes : result.values()) { if (classes != null) { hasClasses = true; break; } } return hasClasses ? result : null; } }
private static void addReferenceExpressions( ArrayList<PsiElement> array, PsiElement scope, PsiElement referee) { PsiElement[] children = scope.getChildren(); for (PsiElement child : children) { if (child instanceof PsiReferenceExpression) { PsiElement ref = ((PsiReferenceExpression) child).resolve(); if (ref != null && PsiEquivalenceUtil.areElementsEquivalent(ref, referee)) { array.add(child); } } addReferenceExpressions(array, child, referee); } }
public static <T extends PsiElement> T getLastChildByType( @NotNull PsiElement root, @NotNull Class<? extends T>... elementTypes) { PsiElement[] children = root.getChildren(); for (int i = children.length - 1; i >= 0; i--) { if (PsiTreeUtil.instanceOf(children[i], elementTypes)) { //noinspection unchecked return (T) children[i]; } } return null; }
private static void addExpressionOccurrences( PsiExpression expr, List<PsiExpression> array, PsiElement scope) { PsiElement[] children = scope.getChildren(); for (PsiElement child : children) { if (child instanceof PsiExpression) { if (areExpressionsEquivalent( RefactoringUtil.unparenthesizeExpression((PsiExpression) child), expr)) { array.add((PsiExpression) child); continue; } } addExpressionOccurrences(expr, array, child); } }
private static int getSeqNumber(@NotNull PsiClass aClass) { // sequence number of this class among its parent' child classes named the same PsiElement parent = aClass.getParent(); if (parent == null) return -1; int seqNo = 0; for (PsiElement child : parent.getChildren()) { if (child == aClass) return seqNo; if (child instanceof PsiClass && Comparing.strEqual(aClass.getName(), ((PsiClass) child).getName())) { seqNo++; } } return -1; }
private static List<PsiElement> getTopLevelRegExpChars(String regExpText, Project project) { @SuppressWarnings("deprecation") PsiFile file = PsiFileFactory.getInstance(project).createFileFromText("A.regexp", regExpText); List<PsiElement> result = null; final PsiElement[] children = file.getChildren(); for (PsiElement child : children) { PsiElement[] grandChildren = child.getChildren(); if (grandChildren.length != 1) return Collections .emptyList(); // a | b, more than one branch, can not predict in current way for (PsiElement grandGrandChild : grandChildren[0].getChildren()) { if (result == null) result = new ArrayList<>(); result.add(grandGrandChild); } } return result != null ? result : Collections.<PsiElement>emptyList(); }
private static boolean writtenInside(PsiVariable variable, PsiElement element) { if (element instanceof PsiAssignmentExpression) { PsiAssignmentExpression assignmentExpression = (PsiAssignmentExpression) element; PsiExpression lExpression = assignmentExpression.getLExpression(); if (lExpression instanceof PsiReferenceExpression && ((PsiReferenceExpression) lExpression).resolve() == variable) return true; } else if (PsiUtil.isIncrementDecrementOperation(element)) { PsiElement operand = element instanceof PsiPostfixExpression ? ((PsiPostfixExpression) element).getOperand() : ((PsiPrefixExpression) element).getOperand(); if (operand instanceof PsiReferenceExpression && ((PsiReferenceExpression) operand).resolve() == variable) return true; } PsiElement[] children = element.getChildren(); for (PsiElement child : children) { if (writtenInside(variable, child)) return true; } return false; }
private static void addNamesToImport( @NotNull Set<String> names, @NotNull PsiElement scope, @NotNull String thisPackageName, @NotNull Set<String> namesToImportStaticly, PsiFile context) { if (scope instanceof PsiImportList) return; final LinkedList<PsiElement> stack = new LinkedList<PsiElement>(); stack.add(scope); while (!stack.isEmpty()) { final PsiElement child = stack.removeFirst(); if (child instanceof PsiImportList) continue; stack.addAll(Arrays.asList(child.getChildren())); for (final PsiReference reference : child.getReferences()) { if (!(reference instanceof PsiJavaReference)) continue; final PsiJavaReference javaReference = (PsiJavaReference) reference; if (javaReference instanceof JavaClassReference) { if (((JavaClassReference) javaReference).getContextReference() != null) continue; } PsiJavaCodeReferenceElement referenceElement = null; if (reference instanceof PsiJavaCodeReferenceElement) { referenceElement = (PsiJavaCodeReferenceElement) child; if (referenceElement.getQualifier() != null) { continue; } if (reference instanceof PsiJavaCodeReferenceElementImpl && ((PsiJavaCodeReferenceElementImpl) reference).getKind() == PsiJavaCodeReferenceElementImpl.CLASS_IN_QUALIFIED_NEW_KIND) { continue; } } final JavaResolveResult resolveResult = javaReference.advancedResolve(true); PsiElement refElement = resolveResult.getElement(); if (refElement == null && referenceElement != null) { refElement = ResolveClassUtil.resolveClass(referenceElement); // might be uncomplete code } PsiElement currentFileResolveScope = resolveResult.getCurrentFileResolveScope(); if (!(currentFileResolveScope instanceof PsiImportStatementBase)) continue; if (context != null && currentFileResolveScope instanceof JspxImportStatement && context != ((JspxImportStatement) currentFileResolveScope).getDeclarationFile()) { continue; } if (refElement != null) { // Add names imported statically if (referenceElement != null) { if (currentFileResolveScope instanceof PsiImportStaticStatement) { PsiImportStaticStatement importStaticStatement = (PsiImportStaticStatement) currentFileResolveScope; String name = importStaticStatement.getImportReference().getCanonicalText(); if (importStaticStatement.isOnDemand()) { String refName = referenceElement.getReferenceName(); if (refName != null) name = name + "." + refName; } names.add(name); namesToImportStaticly.add(name); continue; } } if (refElement instanceof PsiClass) { String qName = ((PsiClass) refElement).getQualifiedName(); if (hasPackage(qName, thisPackageName)) continue; names.add(qName); } } } } }
@NotNull public static PsiElement[] findStatementsInRange( @NotNull PsiFile file, int startOffset, int endOffset) { Language language = findJavaOrLikeLanguage(file); if (language == null) return PsiElement.EMPTY_ARRAY; FileViewProvider viewProvider = file.getViewProvider(); PsiElement element1 = viewProvider.findElementAt(startOffset, language); PsiElement element2 = viewProvider.findElementAt(endOffset - 1, language); if (element1 instanceof PsiWhiteSpace) { startOffset = element1.getTextRange().getEndOffset(); element1 = file.findElementAt(startOffset); } if (element2 instanceof PsiWhiteSpace) { endOffset = element2.getTextRange().getStartOffset(); element2 = file.findElementAt(endOffset - 1); } if (element1 == null || element2 == null) return PsiElement.EMPTY_ARRAY; PsiElement parent = PsiTreeUtil.findCommonParent(element1, element2); if (parent == null) return PsiElement.EMPTY_ARRAY; while (true) { if (parent instanceof PsiStatement) { parent = parent.getParent(); break; } if (parent instanceof PsiCodeBlock) break; // if (JspPsiUtil.isInJspFile(parent) && parent instanceof PsiFile) break; if (parent instanceof PsiCodeFragment) break; if (parent == null || parent instanceof PsiFile) return PsiElement.EMPTY_ARRAY; parent = parent.getParent(); } if (!parent.equals(element1)) { while (!parent.equals(element1.getParent())) { element1 = element1.getParent(); } } if (startOffset != element1.getTextRange().getStartOffset()) return PsiElement.EMPTY_ARRAY; if (!parent.equals(element2)) { while (!parent.equals(element2.getParent())) { element2 = element2.getParent(); } } if (endOffset != element2.getTextRange().getEndOffset()) return PsiElement.EMPTY_ARRAY; if (parent instanceof PsiCodeBlock && parent.getParent() instanceof PsiBlockStatement && element1 == ((PsiCodeBlock) parent).getLBrace() && element2 == ((PsiCodeBlock) parent).getRBrace()) { return new PsiElement[] {parent.getParent()}; } /* if(parent instanceof PsiCodeBlock && parent.getParent() instanceof PsiBlockStatement) { return new PsiElement[]{parent.getParent()}; } */ PsiElement[] children = parent.getChildren(); ArrayList<PsiElement> array = new ArrayList<PsiElement>(); boolean flag = false; for (PsiElement child : children) { if (child.equals(element1)) { flag = true; } if (flag && !(child instanceof PsiWhiteSpace)) { array.add(child); } if (child.equals(element2)) { break; } } for (PsiElement element : array) { if (!(element instanceof PsiStatement || element instanceof PsiWhiteSpace || element instanceof PsiComment)) { return PsiElement.EMPTY_ARRAY; } } return PsiUtilCore.toPsiElementArray(array); }