PsiFile file = PsiManager.getInstance(project).findFile(virtualFile); PsiElement[] elements = file.getChildren(); for (PsiElement element : elements) { if (element instanceof PsiMethod) { System.out.println("Method found: " + element.getText()); } }
PsiFile file = PsiManager.getInstance(project).findFile(virtualFile); PsiElementVisitor visitor = new PsiElementVisitor() { @Override public void visitElement(PsiElement element) { if (element instanceof PsiVariable) { String oldName = element.getText(); String newName = "new_" + oldName; ((PsiNameIdentifierOwner) element).setName(newName); } element.acceptChildren(this); } }; file.accept(visitor);This code loads a Java file and creates a PsiElementVisitor that visits all PsiVariable elements. For each variable, it renames it by adding a "new_" prefix to its name. The new name is set using the setName() method on the PsiNameIdentifierOwner interface. Finally, the visitChildren() method is called to recurse into the element's children. This process changes the variable names in the file's AST. Both of these examples use the com.intellij.psi package in the IntelliJ IDEA Java SDK.