void () { PsiFileFactory instance = PsiFileFactory.getInstance(getProject()); PsiJavaFile aFile = (PsiJavaFile)instance .createFileFromText("a.java", JavaFileType.INSTANCE, """ class Foo { void foo(){ final int i = 0; } }"""); PsiClass aClass = aFile.getClasses()[0]; PsiDeclarationStatement firstStatement = (PsiDeclarationStatement)aClass.getMethods()[0].getBody().getStatements()[0]; PsiLocalVariable variable1 = (PsiLocalVariable)firstStatement.getDeclaredElements()[0]; PsiJavaFile aFile2 = (PsiJavaFile)instance .createFileFromText("a.java", JavaFileType.INSTANCE, """ class Foo { void foo(){ int i = 0; } }"""); PsiClass aClass2 = aFile2.getClasses()[0]; PsiDeclarationStatement firstStatement2 = (PsiDeclarationStatement)aClass2.getMethods()[0].getBody().getStatements()[0]; PsiLocalVariable variable2 = (PsiLocalVariable)firstStatement2.getDeclaredElements()[0]; variable1.getModifierList().replace(variable2.getModifierList()); assertEquals("int i = 0;", variable1.getText()); }