@NotNull @Override public PsiCatchSection createCatchSection( @NotNull final PsiType exceptionType, @NotNull final String exceptionName, @Nullable final PsiElement context) throws IncorrectOperationException { if (!(exceptionType instanceof PsiClassType || exceptionType instanceof PsiDisjunctionType)) { throw new IncorrectOperationException("Unexpected type:" + exceptionType); } @NonNls final String text = "catch (" + exceptionType.getCanonicalText() + " " + exceptionName + ") {}"; final DummyHolder holder = DummyHolderFactory.createHolder( myManager, new JavaDummyElement(text, CATCH_SECTION, level(context)), context); final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(holder.getTreeElement().getFirstChildNode()); if (!(element instanceof PsiCatchSection)) { throw new IncorrectOperationException( "Incorrect catch section '" + text + "'. Parsed element: " + element); } final Project project = myManager.getProject(); final JavaPsiImplementationHelper helper = JavaPsiImplementationHelper.getInstance(project); helper.setupCatchBlock(exceptionName, exceptionType, context, (PsiCatchSection) element); final CodeStyleManager styleManager = CodeStyleManager.getInstance(project); final PsiCatchSection catchSection = (PsiCatchSection) styleManager.reformat(element); GeneratedMarkerVisitor.markGenerated(catchSection); return catchSection; }
@NotNull @Override public PsiDeclarationStatement createVariableDeclarationStatement( @NotNull String name, @NotNull PsiType type, @Nullable PsiExpression initializer) throws IncorrectOperationException { if (!isIdentifier(name)) { throw new IncorrectOperationException("\"" + name + "\" is not an identifier."); } if (PsiType.NULL.equals(type)) { throw new IncorrectOperationException("Cannot create variable with type \"null\"."); } String text = "X " + name + (initializer != null ? " = x" : "") + ";"; PsiDeclarationStatement statement = (PsiDeclarationStatement) createStatementFromText(text, null); PsiVariable variable = (PsiVariable) statement.getDeclaredElements()[0]; replace(variable.getTypeElement(), createTypeElement(type), text); boolean generateFinalLocals = JavaCodeStyleSettingsFacade.getInstance(myManager.getProject()).isGenerateFinalLocals(); PsiUtil.setModifierProperty(variable, PsiModifier.FINAL, generateFinalLocals); if (initializer != null) { replace(variable.getInitializer(), initializer, text); } GeneratedMarkerVisitor.markGenerated(statement); return statement; }
@NotNull @Override public PsiTypeParameter createTypeParameterFromText( @NotNull final String text, final PsiElement context) throws IncorrectOperationException { final PsiTypeParameter typeParameter = super.createTypeParameterFromText(text, context); GeneratedMarkerVisitor.markGenerated(typeParameter); return typeParameter; }
@NotNull @Override public PsiMethod createMethodFromText( @NotNull final String text, final PsiElement context, final LanguageLevel level) throws IncorrectOperationException { final PsiMethod method = super.createMethodFromText(text, context, level); GeneratedMarkerVisitor.markGenerated(method); return method; }
@NotNull @Override public PsiStatement createStatementFromText( @NotNull final String text, @Nullable final PsiElement context) throws IncorrectOperationException { final PsiStatement statement = super.createStatementFromText(text, context); GeneratedMarkerVisitor.markGenerated(statement); return statement; }
@NotNull @Override public PsiField createFieldFromText( @NotNull final String text, @Nullable final PsiElement context) throws IncorrectOperationException { final PsiField psiField = super.createFieldFromText(text, context); GeneratedMarkerVisitor.markGenerated(psiField); return psiField; }
@NotNull @Override public PsiExpression createExpressionFromText( @NotNull final String text, @Nullable final PsiElement context) throws IncorrectOperationException { final PsiExpression expression = super.createExpressionFromText(text, context); GeneratedMarkerVisitor.markGenerated(expression); return expression; }
@NotNull @Override public PsiCodeBlock createCodeBlockFromText( @NotNull final String text, @Nullable final PsiElement context) throws IncorrectOperationException { final PsiCodeBlock psiCodeBlock = super.createCodeBlockFromText(text, context); GeneratedMarkerVisitor.markGenerated(psiCodeBlock); return psiCodeBlock; }
@NotNull @Override public PsiAnnotation createAnnotationFromText( @NotNull final String annotationText, @Nullable final PsiElement context) throws IncorrectOperationException { final PsiAnnotation psiAnnotation = super.createAnnotationFromText(annotationText, context); GeneratedMarkerVisitor.markGenerated(psiAnnotation); return psiAnnotation; }
private static PsiField createField( PsiLocalVariable local, PsiType forcedType, String fieldName, boolean includeInitializer) { @NonNls StringBuilder pattern = new StringBuilder(); pattern.append("var "); pattern.append(fieldName); PsiType type = local.getType(); if (local.getInitializer() == null) { includeInitializer = false; } else { type = local.getInitializer().getType(); } pattern.append(": ").append(type.getPresentableText()); if (includeInitializer) { pattern.append(" = ").append(local.getInitializer().getText()); } final PsiElement psiElement = GosuPsiParseUtil.parseProgramm( pattern.toString(), PsiManager.getInstance(local.getProject()), null); final GosuFieldImpl field = PsiTreeUtil.findChildOfType(psiElement, GosuFieldImpl.class); GeneratedMarkerVisitor.markGenerated(field); try { final PsiModifierList modifierList = local.getModifierList(); if (modifierList != null) { for (PsiAnnotation annotation : modifierList.getAnnotations()) { field.getModifierList().add(annotation.copy()); } } return field; } catch (IncorrectOperationException e) { LOG.error(e); return null; } }
@NotNull @Override public PsiParameter createParameter(@NotNull final String name, @NotNull final PsiType type) throws IncorrectOperationException { PsiUtil.checkIsIdentifier(myManager, name); if (PsiType.NULL.equals(type)) { throw new IncorrectOperationException("Cannot create parameter with type \"null\"."); } final String text = type.getCanonicalText() + " " + name; PsiParameter parameter = createParameterFromText(text, null); final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(myManager.getProject()); PsiUtil.setModifierProperty( parameter, PsiModifier.FINAL, JavaCodeStyleSettingsFacade.getInstance(myManager.getProject()) .isGenerateFinalParameters()); GeneratedMarkerVisitor.markGenerated(parameter); parameter = (PsiParameter) JavaCodeStyleManager.getInstance(myManager.getProject()) .shortenClassReferences(parameter); return (PsiParameter) codeStyleManager.reformat(parameter); }
public void run() { try { final boolean rebindNeeded2 = !myVariableName.equals(myFieldName) || myRebindNeeded; final PsiReference[] refs; if (rebindNeeded2) { refs = ReferencesSearch.search(myLocal, GlobalSearchScope.projectScope(myProject), false) .toArray(new PsiReference[0]); } else { refs = null; } final JavaPsiFacade facade = JavaPsiFacade.getInstance(myProject); PsiVariable psiVariable = facade.getResolveHelper().resolveAccessibleReferencedVariable(myFieldName, myLocal); if (psiVariable != null && (!psiVariable.equals(myLocal))) { CommonRefactoringUtil.showErrorMessage( GosuIntroduceFieldHandler.REFACTORING_NAME, LOCAL_VARIABLE_WITH_THIS_NAME_ALREADY_EXISTS, HelpID.INTRODUCE_FIELD, myProject); return; } if (refs != null) { for (PsiReference occur : refs) { psiVariable = facade .getResolveHelper() .resolveAccessibleReferencedVariable(myFieldName, (PsiElement) occur); if (psiVariable != null && (psiVariable.getName().equals(myFieldName))) { CommonRefactoringUtil.showErrorMessage( GosuIntroduceFieldHandler.REFACTORING_NAME, LOCAL_VARIABLE_WITH_THIS_NAME_ALREADY_EXISTS, HelpID.INTRODUCE_FIELD, myProject); return; } } } final PsiMethod enclosingConstructor = GosuBaseExpressionToFieldHandler.getEnclosingConstructor(myDestinationClass, myLocal); myField = mySettings.isIntroduceEnumConstant() ? EnumConstantsUtil.createEnumConstant(myDestinationClass, myLocal, myFieldName) : createField( myLocal, mySettings.getForcedType(), myFieldName, myInitializerPlace == IN_FIELD_DECLARATION); myField = (PsiField) myDestinationClass.add(myField); GosuBaseExpressionToFieldHandler.setModifiers(myField, mySettings); if (!mySettings.isIntroduceEnumConstant()) { VisibilityUtil.fixVisibility(myOccurences, myField, mySettings.getFieldVisibility()); } myLocal.normalizeDeclaration(); // PsiDeclarationStatement declarationStatement = (PsiDeclarationStatement) // myLocal.getParent(); final GosuBaseExpressionToFieldHandler.InitializationPlace finalInitializerPlace; if (myLocal.getInitializer() == null) { finalInitializerPlace = IN_FIELD_DECLARATION; } else { finalInitializerPlace = myInitializerPlace; } final PsiElementFactory factory = JavaPsiFacade.getInstance(myProject).getElementFactory(); switch (finalInitializerPlace) { case IN_FIELD_DECLARATION: // declarationStatement.delete(); myLocal.delete(); break; case IN_CURRENT_METHOD: PsiStatement statement = createAssignment(myLocal, myFieldName, null); // myAssignmentStatement = (PsiStatement) // declarationStatement.replace(statement); myAssignmentStatement = (PsiStatement) myLocal.replace(statement); GeneratedMarkerVisitor.markGenerated(myAssignmentStatement); break; case IN_CONSTRUCTOR: myAssignmentStatement = addInitializationToConstructors(myLocal, myField, enclosingConstructor, factory); GeneratedMarkerVisitor.markGenerated(myAssignmentStatement); break; case IN_SETUP_METHOD: // myAssignmentStatement = addInitializationToSetUp(myLocal, myField, // factory); } if (enclosingConstructor != null && myInitializerPlace == IN_CONSTRUCTOR) { PsiStatement statement = createAssignment(myLocal, myFieldName, null); // myAssignmentStatement = (PsiStatement) // declarationStatement.replace(statement); myAssignmentStatement = (PsiStatement) myLocal.replace(statement); GeneratedMarkerVisitor.markGenerated(myAssignmentStatement); } if (rebindNeeded2) { for (final PsiReference reference : refs) { if (reference != null) { // expr = GosuRefactoringUtil.outermostParenthesizedExpression(expr); GosuRefactoringUtil.replaceOccurenceWithFieldRef( (PsiExpression) reference, myField, myDestinationClass); // replaceOccurenceWithFieldRef((PsiExpression)reference, field, aaClass); } } // GosuRefactoringUtil.renameVariableReferences(local, pPrefix + fieldName, // GlobalSearchScope.projectScope(myProject)); } ClassLord.doImportAndStick( mySettings.getForcedType().getCanonicalText(), myDestinationClass.getContainingFile()); } catch (IncorrectOperationException e) { LOG.error(e); } }