use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class GroovyInlineLocalHandler method createSettings.
/**
* Returns Settings object for referenced definition in case of local variable
*/
@Nullable
private static InlineLocalVarSettings createSettings(final GrVariable variable, Editor editor, boolean invokedOnReference) {
final String localName = variable.getName();
final Project project = variable.getProject();
GrExpression initializer = null;
Instruction writeInstr = null;
Instruction[] flow = null;
//search for initializer to inline
if (invokedOnReference) {
LOG.assertTrue(editor != null, "null editor but invokedOnReference==true");
final PsiReference ref = TargetElementUtil.findReference(editor);
LOG.assertTrue(ref != null);
PsiElement cur = ref.getElement();
if (cur instanceof GrReferenceExpression) {
GrControlFlowOwner controlFlowOwner;
do {
controlFlowOwner = ControlFlowUtils.findControlFlowOwner(cur);
if (controlFlowOwner == null)
break;
flow = controlFlowOwner.getControlFlow();
final List<BitSet> writes = ControlFlowUtils.inferWriteAccessMap(flow, variable);
final PsiElement finalCur = cur;
Instruction instruction = ControlFlowUtils.findInstruction(finalCur, flow);
LOG.assertTrue(instruction != null);
final BitSet prev = writes.get(instruction.num());
if (prev.cardinality() == 1) {
writeInstr = flow[prev.nextSetBit(0)];
final PsiElement element = writeInstr.getElement();
if (element instanceof GrVariable) {
initializer = ((GrVariable) element).getInitializerGroovy();
} else if (element instanceof GrReferenceExpression) {
initializer = TypeInferenceHelper.getInitializerFor((GrReferenceExpression) element);
}
}
PsiElement old_cur = cur;
if (controlFlowOwner instanceof GrClosableBlock) {
cur = controlFlowOwner;
} else {
PsiElement parent = controlFlowOwner.getParent();
if (parent instanceof GrMember)
cur = ((GrMember) parent).getContainingClass();
}
if (cur == old_cur)
break;
} while (initializer == null);
}
} else {
flow = ControlFlowUtils.findControlFlowOwner(variable).getControlFlow();
initializer = variable.getInitializerGroovy();
writeInstr = ContainerUtil.find(flow, instruction -> instruction.getElement() == variable);
}
if (initializer == null || writeInstr == null) {
String message = GroovyRefactoringBundle.message("cannot.find.a.single.definition.to.inline.local.var");
CommonRefactoringUtil.showErrorHint(variable.getProject(), editor, message, INLINE_VARIABLE, HelpID.INLINE_VARIABLE);
return null;
}
int writeInstructionNumber = writeInstr.num();
if (ApplicationManager.getApplication().isUnitTestMode()) {
return new InlineLocalVarSettings(initializer, writeInstructionNumber, flow);
}
final String question = GroovyRefactoringBundle.message("inline.local.variable.prompt.0.1", localName);
RefactoringMessageDialog dialog = new RefactoringMessageDialog(INLINE_VARIABLE, question, HelpID.INLINE_VARIABLE, "OptionPane.questionIcon", true, project);
if (dialog.showAndGet()) {
return new InlineLocalVarSettings(initializer, writeInstructionNumber, flow);
}
return null;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class GroovyMethodInliner method prepareNewMethod.
/**
* Prepare temporary method with non-conflicting local names
*/
@NotNull
private static GrMethod prepareNewMethod(@NotNull GrCallExpression call, @NotNull GrMethod method, @Nullable GrExpression qualifier) throws IncorrectOperationException {
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(method.getProject());
if (method instanceof GrReflectedMethod) {
method = ((GrReflectedMethod) method).getBaseMethod();
}
GrMethod newMethod = factory.createMethodFromText(method.getText(), call);
if (qualifier != null) {
Collection<GroovyInlineMethodUtil.ReferenceExpressionInfo> infos = GroovyInlineMethodUtil.collectReferenceInfo(method);
GroovyInlineMethodUtil.addQualifiersToInnerReferences(newMethod, infos, qualifier);
}
ArrayList<PsiNamedElement> innerDefinitions = new ArrayList<>();
collectInnerDefinitions(newMethod.getBlock(), innerDefinitions);
// there are only local variables and parameters (possible of inner closures)
for (PsiNamedElement namedElement : innerDefinitions) {
String name = namedElement.getName();
if (name != null) {
String newName = qualifier instanceof GrReferenceExpression ? InlineMethodConflictSolver.suggestNewName(name, method, call, ((GrReferenceExpression) qualifier).getReferenceName()) : InlineMethodConflictSolver.suggestNewName(name, method, call);
if (!newName.equals(namedElement.getName())) {
final Collection<PsiReference> refs = ReferencesSearch.search(namedElement).findAll();
for (PsiReference ref : refs) {
PsiElement element = ref.getElement();
if (element instanceof GrReferenceExpression) {
GrExpression newExpr = factory.createExpressionFromText(newName);
((GrReferenceExpression) element).replaceWithExpression(newExpr, false);
}
}
namedElement.setName(newName);
}
}
}
GroovyInlineMethodUtil.replaceParametersWithArguments(call, newMethod);
return newMethod;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class FieldConflictsResolver method fixInitializer.
public GrExpression fixInitializer(GrExpression initializer) {
if (myField == null)
return initializer;
final GrReferenceExpression[] replacedRef = { null };
initializer.accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression expression) {
final GrExpression qualifierExpression = expression.getQualifier();
if (qualifierExpression != null) {
qualifierExpression.accept(this);
} else {
final PsiElement result = expression.resolve();
if (expression.getManager().areElementsEquivalent(result, myField)) {
try {
replacedRef[0] = qualifyReference(expression, myField, myQualifyingClass);
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
}
}
});
if (!initializer.isValid())
return replacedRef[0];
return initializer;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class GrIntroduceFieldDialog method canBeInitializedOutsideBlock.
private static boolean canBeInitializedOutsideBlock(@NotNull GrIntroduceContext context, @NotNull PsiClass clazz) {
final StringPartInfo part = context.getStringPart();
GrExpression expression = context.getExpression();
if (expression != null) {
expression = (GrExpression) PsiUtil.skipParentheses(expression, false);
if (expression == null)
return false;
if (expression instanceof GrReferenceExpression) {
final PsiElement resolved = ((GrReferenceExpression) expression).resolve();
if (PsiUtil.isLocalVariable(resolved)) {
expression = ((GrVariable) resolved).getInitializerGroovy();
if (expression == null)
return false;
}
}
ExpressionChecker visitor = new ExpressionChecker(clazz, expression);
expression.accept(visitor);
return visitor.isResult();
}
if (part != null) {
for (GrStringInjection injection : part.getInjections()) {
GroovyPsiElement scope = injection.getExpression() != null ? injection.getExpression() : injection.getClosableBlock();
assert scope != null;
ExpressionChecker visitor = new ExpressionChecker(clazz, scope);
scope.accept(visitor);
if (!visitor.isResult()) {
return visitor.isResult();
}
}
return true;
} else {
return false;
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class SwitchStatementGenerator method generateIfs.
private static void generateIfs(@NotNull StringBuilder builder, @NotNull ExpressionContext context, @NotNull GrExpression condition, @NotNull GrCaseSection[] caseSections) {
final GrExpression ref;
if (condition instanceof GrReferenceExpression) {
ref = condition;
} else {
final String varName = generateConditionVar(builder, context, condition);
ref = GroovyPsiElementFactory.getInstance(context.project).createExpressionFromText(varName);
}
final GrExpression[] args = { ref };
generateIfFromCaseSection(builder, context, caseSections, 0, args);
}
Aggregations