@NotNull @Override public PsiField createField(@NotNull final String name, @NotNull final PsiType type) throws IncorrectOperationException { PsiUtil.checkIsIdentifier(myManager, name); if (PsiType.NULL.equals(type)) { throw new IncorrectOperationException("Cannot create field with type \"null\"."); } @NonNls final String text = "class _Dummy_ { private " + type.getCanonicalText() + " " + name + "; }"; final PsiJavaFile aFile = createDummyJavaFile(text); final PsiClass[] classes = aFile.getClasses(); if (classes.length < 1) { throw new IncorrectOperationException("Class was not created " + text); } final PsiClass psiClass = classes[0]; final PsiField[] fields = psiClass.getFields(); if (fields.length < 1) { throw new IncorrectOperationException("Field was not created " + text); } PsiField field = fields[0]; field = (PsiField) JavaCodeStyleManager.getInstance(myManager.getProject()).shortenClassReferences(field); return (PsiField) CodeStyleManager.getInstance(myManager.getProject()).reformat(field); }
private PsiClass createArrayClass(String text, LanguageLevel level) { PsiClass psiClass = ((PsiExtensibleClass) createClassFromText(text, null)).getOwnInnerClasses().get(0); ensureNonWritable(psiClass); PsiFile file = psiClass.getContainingFile(); ((PsiJavaFileBaseImpl) file).clearCaches(); PsiUtil.FILE_LANGUAGE_LEVEL_KEY.set(file, level); return psiClass; }
@NotNull @Override public PsiClassType getArrayClassType( @NotNull final PsiType componentType, @NotNull final LanguageLevel languageLevel) { final PsiClass arrayClass = getArrayClass(languageLevel); final PsiTypeParameter[] typeParameters = arrayClass.getTypeParameters(); PsiSubstitutor substitutor = PsiSubstitutor.EMPTY; if (typeParameters.length == 1) { substitutor = substitutor.put(typeParameters[0], componentType); } return createType(arrayClass, substitutor); }
@NotNull @Override public PsiImportStatement createImportStatement(@NotNull final PsiClass aClass) throws IncorrectOperationException { if (aClass instanceof PsiAnonymousClass) { throw new IncorrectOperationException("Cannot create import statement for anonymous class."); } else if (aClass.getParent() instanceof PsiDeclarationStatement) { throw new IncorrectOperationException("Cannot create import statement for local class."); } final PsiJavaFile aFile = createDummyJavaFile("import " + aClass.getQualifiedName() + ";"); final PsiImportStatementBase statement = extractImport(aFile, false); return (PsiImportStatement) CodeStyleManager.getInstance(myManager.getProject()).reformat(statement); }
@NotNull @Override public PsiClassType createType(@NotNull final PsiClass aClass, final PsiType parameter) { final PsiTypeParameter[] typeParameters = aClass.getTypeParameters(); assert typeParameters.length == 1 : aClass; return createType(aClass, PsiSubstitutor.EMPTY.put(typeParameters[0], parameter)); }
@NotNull @Override public PsiJavaCodeReferenceElement createClassReferenceElement(@NotNull final PsiClass aClass) { final String text; if (aClass instanceof PsiAnonymousClass) { text = ((PsiAnonymousClass) aClass).getBaseClassType().getPresentableText(); } else { text = aClass.getName(); } return new LightClassReference(myManager, text, aClass); }
@NotNull @Override public PsiReferenceExpression createReferenceExpression(@NotNull final PsiClass aClass) throws IncorrectOperationException { final String text; if (aClass instanceof PsiAnonymousClass) { text = ((PsiAnonymousClass) aClass).getBaseClassType().getPresentableText(); } else { text = aClass.getName(); } return new LightClassReferenceExpression(myManager, text, aClass); }
@NotNull @Override public PsiJavaCodeReferenceElement createClassReferenceElement(@NotNull final PsiClass aClass) { final String text; if (aClass instanceof PsiAnonymousClass) { text = ((PsiAnonymousClass) aClass).getBaseClassType().getPresentableText(); } else { text = aClass.getName(); } if (text == null) { throw new IncorrectOperationException("Invalid class: " + aClass); } return new LightClassReference(myManager, text, aClass); }
private static void ensureNonWritable(PsiClass arrayClass) { try { arrayClass.getContainingFile().getViewProvider().getVirtualFile().setWritable(false); } catch (IOException ignored) { } }