public GrInplaceConstantIntroducer( GrIntroduceContext context, OccurrencesChooser.ReplaceChoice choice) { super(IntroduceConstantHandler.REFACTORING_NAME, choice, context); myContext = context; myPanel = new GrInplaceIntroduceConstantPanel(); GrVariable localVar = GrIntroduceHandlerBase.resolveLocalVar(context); if (localVar != null) { ArrayList<String> result = ContainerUtil.newArrayList(localVar.getName()); GrExpression initializer = localVar.getInitializerGroovy(); if (initializer != null) { ContainerUtil.addAll( result, GroovyNameSuggestionUtil.suggestVariableNames( initializer, new GroovyInplaceFieldValidator(context), true)); } mySuggestedNames = ArrayUtil.toStringArray(result); } else { GrExpression expression = context.getExpression(); assert expression != null; mySuggestedNames = GroovyNameSuggestionUtil.suggestVariableNames( expression, new GroovyInplaceFieldValidator(context), true); } }
private static PsiElement fixCatchBlock(GrTryCatchStatement tryCatch, PsiClassType[] exceptions) { if (exceptions.length == 0) return tryCatch; final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(tryCatch.getProject()); final GrCatchClause[] clauses = tryCatch.getCatchClauses(); List<String> restricted = ContainerUtil.map( clauses, new Function<GrCatchClause, String>() { @Override @Nullable public String fun(GrCatchClause grCatchClause) { final GrParameter grParameter = grCatchClause.getParameter(); return grParameter != null ? grParameter.getName() : null; } }); restricted = ContainerUtil.skipNulls(restricted); final DefaultGroovyVariableNameValidator nameValidator = new DefaultGroovyVariableNameValidator(tryCatch, restricted); GrCatchClause anchor = clauses.length == 0 ? null : clauses[clauses.length - 1]; for (PsiClassType type : exceptions) { final String[] names = GroovyNameSuggestionUtil.suggestVariableNameByType(type, nameValidator); final GrCatchClause catchClause = factory.createCatchClause(type, names[0]); final GrStatement printStackTrace = factory.createStatementFromText(names[0] + ".printStackTrace()"); catchClause.getBody().addStatementBefore(printStackTrace, null); anchor = tryCatch.addCatchClause(catchClause, anchor); JavaCodeStyleManager.getInstance(anchor.getProject()).shortenClassReferences(anchor); } return tryCatch; }
@NotNull public LinkedHashSet<String> suggestNames() { final GrExpression expression = myContext.getExpression(); final GrVariable var = myContext.getVar(); final StringPartInfo stringPart = myContext.getStringPart(); if (expression != null) { return new LinkedHashSet<String>( Arrays.asList( GroovyNameSuggestionUtil.suggestVariableNames(expression, myValidator, myForStatic))); } else if (stringPart != null) { return new LinkedHashSet<String>( Arrays.asList( GroovyNameSuggestionUtil.suggestVariableNames( stringPart.getLiteral(), myValidator, myForStatic))); } else { assert var != null; return new LinkedHashSet<String>( Arrays.asList( GroovyNameSuggestionUtil.suggestVariableNameByType(var.getType(), myValidator))); } }