private static void qualify(PsiMember member, PsiElement renamed, String name) { if (!(renamed instanceof GrReferenceExpression)) return; final PsiClass clazz = member.getContainingClass(); if (clazz == null) return; final GrReferenceExpression refExpr = (GrReferenceExpression) renamed; final PsiElement replaced; if (member.hasModifierProperty(GrModifier.STATIC)) { final GrReferenceExpression newRefExpr = GroovyPsiElementFactory.getInstance(member.getProject()) .createReferenceExpressionFromText(clazz.getQualifiedName() + "." + name); replaced = refExpr.replace(newRefExpr); } else { final PsiClass containingClass = PsiTreeUtil.getParentOfType(renamed, PsiClass.class); if (member.getManager().areElementsEquivalent(containingClass, clazz)) { final GrReferenceExpression newRefExpr = GroovyPsiElementFactory.getInstance(member.getProject()) .createReferenceExpressionFromText("this." + name); replaced = refExpr.replace(newRefExpr); } else { final GrReferenceExpression newRefExpr = GroovyPsiElementFactory.getInstance(member.getProject()) .createReferenceExpressionFromText(clazz.getQualifiedName() + ".this." + name); replaced = refExpr.replace(newRefExpr); } } PsiUtil.shortenReferences((GroovyPsiElement) replaced); }
@Nullable public static DuplicatesFinder createDuplicatesFinder(PsiMember member) { PsiElement[] pattern; ReturnValue matchedReturnValue = null; if (member instanceof PsiMethod) { final PsiCodeBlock body = ((PsiMethod) member).getBody(); LOG.assertTrue(body != null); final PsiStatement[] statements = body.getStatements(); pattern = statements; matchedReturnValue = null; if (statements.length != 1 || !(statements[0] instanceof PsiReturnStatement)) { final PsiStatement lastStatement = statements.length > 0 ? statements[statements.length - 1] : null; if (lastStatement instanceof PsiReturnStatement) { final PsiExpression returnValue = ((PsiReturnStatement) lastStatement).getReturnValue(); if (returnValue instanceof PsiReferenceExpression) { final PsiElement resolved = ((PsiReferenceExpression) returnValue).resolve(); if (resolved instanceof PsiVariable) { pattern = new PsiElement[statements.length - 1]; System.arraycopy(statements, 0, pattern, 0, statements.length - 1); matchedReturnValue = new VariableReturnValue((PsiVariable) resolved); } } } } else { final PsiExpression returnValue = ((PsiReturnStatement) statements[0]).getReturnValue(); if (returnValue != null) { pattern = new PsiElement[] {returnValue}; } } } else { pattern = new PsiElement[] {((PsiField) member).getInitializer()}; } if (pattern.length == 0) { return null; } final List<? extends PsiVariable> inputVariables = member instanceof PsiMethod ? Arrays.asList(((PsiMethod) member).getParameterList().getParameters()) : new ArrayList<>(); return new DuplicatesFinder( pattern, new InputVariables( inputVariables, member.getProject(), new LocalSearchScope(pattern), false), matchedReturnValue, new ArrayList<>()); }
public static boolean isInExcludedPackage( @NotNull final PsiMember member, boolean allowInstanceInnerClasses) { final String name = PsiUtil.getMemberQualifiedName(member); if (name == null) return false; if (!member.hasModifierProperty(PsiModifier.STATIC)) { if (member instanceof PsiMethod || member instanceof PsiField) { return false; } if (allowInstanceInnerClasses && member instanceof PsiClass && member.getContainingClass() != null) { return false; } } return ProjectCodeInsightSettings.getSettings(member.getProject()).isExcluded(name); }
@Nullable public static PsiType getQualifiedMemberReferenceType( @Nullable PsiType qualifierType, @NotNull final PsiMember member) { final Ref<PsiSubstitutor> subst = Ref.create(PsiSubstitutor.EMPTY); class MyProcessor extends BaseScopeProcessor implements NameHint, ElementClassHint { @Override public boolean execute(@NotNull PsiElement element, @NotNull ResolveState state) { if (element == member) { subst.set(state.get(PsiSubstitutor.KEY)); } return true; } @Override public String getName(@NotNull ResolveState state) { return member.getName(); } @Override public boolean shouldProcess(DeclarationKind kind) { return member instanceof PsiEnumConstant ? kind == DeclarationKind.ENUM_CONST : member instanceof PsiField ? kind == DeclarationKind.FIELD : kind == DeclarationKind.METHOD; } @Override public <T> T getHint(@NotNull Key<T> hintKey) { return hintKey == NameHint.KEY || hintKey == ElementClassHint.KEY ? (T) this : null; } } PsiScopesUtil.processTypeDeclarations(qualifierType, member, new MyProcessor()); PsiType rawType = member instanceof PsiField ? ((PsiField) member).getType() : member instanceof PsiMethod ? ((PsiMethod) member).getReturnType() : JavaPsiFacade.getElementFactory(member.getProject()) .createType((PsiClass) member); return subst.get().substitute(rawType); }