use of org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor in project intellij-community by JetBrains.
the class GroovyDuplicateSwitchBranchInspection method collectCaseLabels.
private static GrCaseLabel[] collectCaseLabels(final GrSwitchStatement containingStatelent) {
final Set<GrCaseLabel> labels = new HashSet<>();
final GroovyRecursiveElementVisitor visitor = new GroovyRecursiveElementVisitor() {
@Override
public void visitCaseLabel(@NotNull GrCaseLabel grCaseLabel) {
super.visitCaseLabel(grCaseLabel);
labels.add(grCaseLabel);
}
@Override
public void visitSwitchStatement(@NotNull GrSwitchStatement grSwitchStatement) {
if (containingStatelent.equals(grSwitchStatement)) {
super.visitSwitchStatement(grSwitchStatement);
}
}
};
containingStatelent.accept(visitor);
return labels.toArray(new GrCaseLabel[labels.size()]);
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor in project intellij-community by JetBrains.
the class GrReassignedLocalVarsChecker method isReassignedVarImpl.
private static boolean isReassignedVarImpl(@NotNull final GrVariable resolved) {
final GrControlFlowOwner variableScope = PsiTreeUtil.getParentOfType(resolved, GrCodeBlock.class, GroovyFile.class);
if (variableScope == null)
return false;
final String name = resolved.getName();
final Ref<Boolean> isReassigned = Ref.create(false);
for (PsiElement scope = resolved.getParent().getNextSibling(); scope != null; scope = scope.getNextSibling()) {
if (scope instanceof GroovyPsiElement) {
((GroovyPsiElement) scope).accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitClosure(@NotNull GrClosableBlock closure) {
if (getUsedVarsInsideBlock(closure).contains(name)) {
isReassigned.set(true);
}
}
@Override
public void visitElement(@NotNull GroovyPsiElement element) {
if (isReassigned.get())
return;
super.visitElement(element);
}
});
if (isReassigned.get())
break;
}
}
return isReassigned.get();
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor in project intellij-community by JetBrains.
the class ImportStaticIntention method processIntention.
@Override
protected void processIntention(@NotNull PsiElement element, @NotNull final Project project, final Editor editor) throws IncorrectOperationException {
final PsiElement resolved = resolve(element);
if (!(resolved instanceof PsiMember))
return;
final PsiClass containingClass = ((PsiMember) resolved).getContainingClass();
if (containingClass == null)
return;
String originalName = ((PsiMember) resolved).getName();
final String name = resolved instanceof PsiMethod && GroovyPropertyUtils.isSimplePropertyAccessor((PsiMethod) resolved) ? GroovyPropertyUtils.getPropertyName((PsiMethod) resolved) : originalName;
final String qname = containingClass.getQualifiedName();
if (name == null)
return;
final PsiFile containingFile = element.getContainingFile();
if (!(containingFile instanceof GroovyFile))
return;
final GroovyFile file = (GroovyFile) containingFile;
file.accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression expression) {
super.visitReferenceExpression(expression);
if (name.equals(expression.getReferenceName())) {
PsiElement resolved = expression.resolve();
if (resolved != null) {
expression.putUserData(TEMP_REFERENT_USER_DATA, resolved);
}
}
}
});
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
final GrImportStatement tempImport = factory.createImportStatementFromText(qname + "." + name, true, false, null);
final GrImportStatement importStatement = file.addImport(tempImport);
boolean isAnythingShortened = shortenUsages(resolved, containingFile);
if (!isAnythingShortened) {
importStatement.delete();
return;
}
file.accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression expression) {
super.visitReferenceExpression(expression);
GrTypeArgumentList typeArgumentList = expression.getTypeArgumentList();
if (typeArgumentList != null && typeArgumentList.getFirstChild() != null) {
expression.putUserData(TEMP_REFERENT_USER_DATA, null);
return;
}
if (name.equals(expression.getReferenceName())) {
if (expression.isQualified()) {
GrExpression qualifier = expression.getQualifierExpression();
if (qualifier instanceof GrReferenceExpression) {
PsiElement aClass = ((GrReferenceExpression) qualifier).resolve();
if (aClass == ((PsiMember) resolved).getContainingClass()) {
GrReferenceAdjuster.shortenReference(expression);
}
}
} else {
PsiElement referent = expression.getUserData(TEMP_REFERENT_USER_DATA);
if (referent instanceof PsiMember && ((PsiMember) referent).hasModifierProperty(PsiModifier.STATIC) && referent != expression.resolve()) {
expression.bindToElement(referent);
}
}
}
expression.putUserData(TEMP_REFERENT_USER_DATA, null);
}
});
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor in project intellij-community by JetBrains.
the class ConvertJunitAssertionToAssertStatementIntention method getReplacementElement.
@Nullable
private static GrStatement getReplacementElement(@NotNull PsiMethod method, @NotNull GrMethodCall methodCall) {
String replacementStatement = getReplacementStatement(method, methodCall);
if (replacementStatement == null)
return null;
GrExpression[] arguments = methodCall.getArgumentList().getExpressionArguments();
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(method.getProject());
GrAssertStatement statement = (GrAssertStatement) factory.createStatementFromText(replacementStatement);
final Map<GrExpression, GrExpression> replaceMap = new HashMap<>();
statement.acceptChildren(new GroovyRecursiveElementVisitor() {
@Override
public void visitExpression(@NotNull GrExpression expression) {
Matcher matcher = PATTERN.matcher(expression.getText());
if (matcher.matches()) {
int index = Integer.parseInt(matcher.group(1));
replaceMap.put(expression, arguments[index]);
} else {
super.visitExpression(expression);
}
}
});
for (Map.Entry<GrExpression, GrExpression> entry : replaceMap.entrySet()) {
entry.getKey().replaceWithExpression(entry.getValue(), true);
}
return statement;
}
use of org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor in project intellij-community by JetBrains.
the class GroovyExtractMethodHandler method findConflicts.
private static boolean findConflicts(InitialInfo info) {
//new ConflictsDialog()
final MultiMap<PsiElement, String> conflicts = new MultiMap<>();
final PsiElement declarationOwner = info.getContext().getParent();
GroovyRecursiveElementVisitor visitor = new GroovyRecursiveElementVisitor() {
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
super.visitReferenceExpression(referenceExpression);
GroovyResolveResult resolveResult = referenceExpression.advancedResolve();
PsiElement resolveContext = resolveResult.getCurrentFileResolveContext();
if (resolveContext != null && !(resolveContext instanceof GrImportStatement) && !PsiTreeUtil.isAncestor(declarationOwner, resolveContext, true) && !skipResult(resolveResult)) {
conflicts.putValue(referenceExpression, GroovyRefactoringBundle.message("ref.0.will.not.be.resolved.outside.of.current.context", referenceExpression.getText()));
}
}
//skip 'print' and 'println'
private boolean skipResult(GroovyResolveResult result) {
PsiElement element = result.getElement();
if (element instanceof PsiMethod) {
String name = ((PsiMethod) element).getName();
if (!name.startsWith("print"))
return false;
if (element instanceof GrGdkMethod)
element = ((GrGdkMethod) element).getStaticMethod();
PsiClass aClass = ((PsiMethod) element).getContainingClass();
if (aClass != null) {
String qname = aClass.getQualifiedName();
return GroovyCommonClassNames.DEFAULT_GROOVY_METHODS.equals(qname);
}
}
return false;
}
};
GrStatement[] statements = info.getStatements();
for (GrStatement statement : statements) {
statement.accept(visitor);
}
if (conflicts.isEmpty())
return false;
if (ApplicationManager.getApplication().isUnitTestMode()) {
throw new BaseRefactoringProcessor.ConflictsInTestsException(conflicts.values());
}
ConflictsDialog dialog = new ConflictsDialog(info.getProject(), conflicts);
dialog.show();
return !dialog.isOK();
}
Aggregations