@Nullable private static <Type extends PsiElement, List extends PsiElement> Type getNextOfType( List parameterList, PsiElement current, Class<Type> type) { return current != null ? PsiTreeUtil.getNextSiblingOfType(current, type) : PsiTreeUtil.getChildOfType(parameterList, type); }
private static void fixExceptions(PsiElement element, PsiClassType[] exceptions) { if (exceptions.length == 0) return; final GroovyPsiElement context = PsiTreeUtil.getParentOfType( element, GrTryCatchStatement.class, GrClosableBlock.class, GrMethod.class, GroovyFile.class); if (context instanceof GrClosableBlock) { element = generateTryCatch(element, exceptions); } else if (context instanceof GrMethod) { final PsiClassType[] handledExceptions = ((GrMethod) context).getThrowsList().getReferencedTypes(); final List<PsiClassType> psiClassTypes = filterOutExceptions(exceptions, context, handledExceptions); element = generateTryCatch(element, psiClassTypes.toArray(new PsiClassType[psiClassTypes.size()])); } else if (context instanceof GroovyFile) { element = generateTryCatch(element, exceptions); } else if (context instanceof GrTryCatchStatement) { final GrCatchClause[] catchClauses = ((GrTryCatchStatement) context).getCatchClauses(); List<PsiClassType> referencedTypes = ContainerUtil.map( catchClauses, new Function<GrCatchClause, PsiClassType>() { @Override @Nullable public PsiClassType fun(GrCatchClause grCatchClause) { final GrParameter grParameter = grCatchClause.getParameter(); final PsiType type = grParameter != null ? grParameter.getType() : null; if (type instanceof PsiClassType) { return (PsiClassType) type; } else { return null; } } }); referencedTypes = ContainerUtil.skipNulls(referencedTypes); final List<PsiClassType> psiClassTypes = filterOutExceptions( exceptions, context, referencedTypes.toArray(new PsiClassType[referencedTypes.size()])); element = fixCatchBlock( (GrTryCatchStatement) context, psiClassTypes.toArray(new PsiClassType[psiClassTypes.size()])); } // CodeStyleManager.getInstance(element.getProject()).reformat(element); }
private static PsiElement generateTryCatch(PsiElement element, PsiClassType[] exceptions) { if (exceptions.length == 0) return element; GrTryCatchStatement tryCatch = (GrTryCatchStatement) GroovyPsiElementFactory.getInstance(element.getProject()) .createStatementFromText("try{} catch (Exception e){}"); final GrStatement statement = PsiTreeUtil.getParentOfType(element, GrStatement.class); assert statement != null; tryCatch.getTryBlock().addStatementBefore(statement, null); tryCatch = (GrTryCatchStatement) statement.replace(tryCatch); tryCatch.getCatchClauses()[0].delete(); fixCatchBlock(tryCatch, exceptions); return tryCatch; }
@Nullable private static GrExpression createDefaultValue( GroovyPsiElementFactory factory, JavaChangeInfo changeInfo, JavaParameterInfo info, final GrArgumentList list) { if (info.isUseAnySingleVariable()) { final PsiResolveHelper resolveHelper = JavaPsiFacade.getInstance(list.getProject()).getResolveHelper(); final PsiType type = info.getTypeWrapper().getType(changeInfo.getMethod(), list.getManager()); final VariablesProcessor processor = new VariablesProcessor(false) { @Override protected boolean check(PsiVariable var, ResolveState state) { if (var instanceof PsiField && !resolveHelper.isAccessible((PsiField) var, list, null)) return false; if (var instanceof GrVariable && PsiUtil.isLocalVariable(var) && list.getTextRange().getStartOffset() <= var.getTextRange().getStartOffset()) { return false; } if (PsiTreeUtil.isAncestor(var, list, false)) return false; final PsiType _type = var instanceof GrVariable ? ((GrVariable) var).getTypeGroovy() : var.getType(); final PsiType varType = state.get(PsiSubstitutor.KEY).substitute(_type); return type.isAssignableFrom(varType); } @Override public boolean execute(@NotNull PsiElement pe, @NotNull ResolveState state) { super.execute(pe, state); return size() < 2; } }; ResolveUtil.treeWalkUp(list, processor, false); if (processor.size() == 1) { final PsiVariable result = processor.getResult(0); return factory.createExpressionFromText(result.getName(), list); } if (processor.size() == 0) { final PsiClass parentClass = PsiTreeUtil.getParentOfType(list, PsiClass.class); if (parentClass != null) { PsiClass containingClass = parentClass; final Set<PsiClass> containingClasses = new HashSet<PsiClass>(); final PsiElementFactory jfactory = JavaPsiFacade.getElementFactory(list.getProject()); while (containingClass != null) { if (type.isAssignableFrom(jfactory.createType(containingClass, PsiSubstitutor.EMPTY))) { containingClasses.add(containingClass); } containingClass = PsiTreeUtil.getParentOfType(containingClass, PsiClass.class); } if (containingClasses.size() == 1) { return factory.createThisExpression( containingClasses.contains(parentClass) ? null : containingClasses.iterator().next()); } } } } final String value = info.getDefaultValue(); return !StringUtil.isEmpty(value) ? factory.createExpressionFromText(value, list) : null; }