void () { myFixture.configureByText("X.java", """ import java.lang.annotation.*; class X { @Target(ElementType.TYPE_USE) @interface Anno {} void test() { @Anno <caret>String s; } } """); PsiElement element = getFile().findElementAt(getEditor().getCaretModel().getOffset()); PsiTypeElement typeElement = PsiTreeUtil.getParentOfType(element, PsiTypeElement.class); assertNotNull(typeElement); CanonicalTypes.Type wrapper = CanonicalTypes.createTypeWrapper(typeElement.getType()); String text = wrapper.getTypeText(); assertEquals("@Anno String", text); PsiTypeCodeFragment fragment = JavaCodeFragmentFactory.getInstance(getProject()).createTypeCodeFragment(text, typeElement, false); wrapper.addImportsTo(fragment); assertEquals("java.lang.String,X.Anno", fragment.importsToString()); }