question: @ NotNull public < T extends PsiElement > PsiTypedQuery < T > filter ( @ NotNull final Class < T > clazz ) { final Set < PsiElement > result = new HashSet < PsiElement > ( Arrays . asList ( myPsiElements ) ) ; for ( final PsiElement element : myPsiElements ) { if ( ! ( clazz . isInstance ( element ) ) ) { result . remove ( element ) ; } } @ SuppressWarnings ( "unchecked" ) final List < T > toAdd = ( List < T > ) new ArrayList < PsiElement > ( result ) ; return new PsiTypedQuery < T > ( clazz , toAdd ) ; }
target:  Filter elements by class