use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField in project intellij-community by JetBrains.
the class ConvertParameterToMapEntryIntention method collectOwnerOccurrences.
private static boolean collectOwnerOccurrences(final Project project, final GrParametersOwner owner, final Collection<PsiElement> occurrences) {
final PsiElement namedElem = getReferencedElement(owner);
if (namedElem == null)
return true;
final Ref<Boolean> result = new Ref<>(true);
final Task task = new Task.Modal(project, GroovyIntentionsBundle.message("find.method.ro.closure.usages.0", owner instanceof GrClosableBlock ? CLOSURE_CAPTION : METHOD_CAPTION), true) {
@Override
public void run(@NotNull final ProgressIndicator indicator) {
final Collection<PsiReference> references = Collections.synchronizedSet(new HashSet<PsiReference>());
final Processor<PsiReference> consumer = psiReference -> {
references.add(psiReference);
return true;
};
ReferencesSearch.search(namedElem).forEach(consumer);
boolean isProperty = ApplicationManager.getApplication().runReadAction(new Computable<Boolean>() {
@Override
public Boolean compute() {
return namedElem instanceof GrField && ((GrField) namedElem).isProperty();
}
});
if (isProperty) {
final GrAccessorMethod[] getters = ApplicationManager.getApplication().runReadAction(new Computable<GrAccessorMethod[]>() {
@Override
public GrAccessorMethod[] compute() {
return ((GrField) namedElem).getGetters();
}
});
for (GrAccessorMethod getter : getters) {
MethodReferencesSearch.search(getter).forEach(consumer);
}
}
for (final PsiReference reference : references) {
ApplicationManager.getApplication().runReadAction(() -> {
final PsiElement element = reference.getElement();
if (element != null) {
occurrences.add(element);
}
});
}
}
@Override
public void onCancel() {
result.set(false);
}
@Override
public void onThrowable(@NotNull Throwable error) {
super.onThrowable(error);
result.set(false);
}
@Override
public void onSuccess() {
result.set(true);
}
};
ProgressManager.getInstance().run(task);
return result.get().booleanValue();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField in project intellij-community by JetBrains.
the class GrVariableInliner method getConflicts.
@Override
@Nullable
public MultiMap<PsiElement, String> getConflicts(@NotNull PsiReference reference, @NotNull PsiElement referenced) {
MultiMap<PsiElement, String> conflicts = new MultiMap<>();
GrExpression expr = (GrExpression) reference.getElement();
if (expr.getParent() instanceof GrAssignmentExpression) {
GrAssignmentExpression parent = (GrAssignmentExpression) expr.getParent();
if (expr.equals(parent.getLValue())) {
conflicts.putValue(expr, GroovyRefactoringBundle.message("local.varaible.is.lvalue"));
}
}
if ((referenced instanceof GrAccessorMethod || referenced instanceof GrField) && expr instanceof GrReferenceExpression) {
final GroovyResolveResult resolveResult = ((GrReferenceExpression) expr).advancedResolve();
if (resolveResult.getElement() instanceof GrAccessorMethod && !resolveResult.isInvokedOnProperty()) {
final PsiElement parent = expr.getParent();
if (!(parent instanceof GrCall && parent instanceof GrExpression)) {
conflicts.putValue(expr, GroovyRefactoringBundle.message("reference.to.accessor.0.is.used", CommonRefactoringUtil.htmlEmphasize(PsiFormatUtil.formatMethod((GrAccessorMethod) resolveResult.getElement(), PsiSubstitutor.EMPTY, PsiFormatUtilBase.SHOW_NAME | PsiFormatUtilBase.SHOW_PARAMETERS, PsiFormatUtilBase.SHOW_TYPE))));
}
}
}
return conflicts;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField in project intellij-community by JetBrains.
the class GroovyInlineLocalProcessor method collectRefs.
private static void collectRefs(final GrVariable variable, Instruction[] flow, final List<BitSet> writes, final int writeInstructionNumber, final ArrayList<UsageInfo> toInline) {
for (Instruction instruction : flow) {
final PsiElement element = instruction.getElement();
if (instruction instanceof ReadWriteVariableInstruction) {
if (((ReadWriteVariableInstruction) instruction).isWrite())
continue;
if (element instanceof GrVariable && element != variable)
continue;
if (!(element instanceof GrReferenceExpression))
continue;
final GrReferenceExpression ref = (GrReferenceExpression) element;
if (ref.isQualified() || ref.resolve() != variable)
continue;
final BitSet prev = writes.get(instruction.num());
if (writeInstructionNumber >= 0 && prev.cardinality() == 1 && prev.get(writeInstructionNumber)) {
toInline.add(new UsageInfo(ref));
} else if (writeInstructionNumber == -1 && prev.cardinality() == 0) {
toInline.add(new ClosureUsage(ref));
}
} else if (element instanceof GrClosableBlock) {
final BitSet prev = writes.get(instruction.num());
if (writeInstructionNumber >= 0 && prev.cardinality() == 1 && prev.get(writeInstructionNumber) || writeInstructionNumber == -1 && prev.cardinality() == 0) {
final Instruction[] closureFlow = ((GrClosableBlock) element).getControlFlow();
collectRefs(variable, closureFlow, ControlFlowUtils.inferWriteAccessMap(closureFlow, variable), -1, toInline);
}
} else if (element instanceof GrAnonymousClassDefinition) {
final BitSet prev = writes.get(instruction.num());
if (writeInstructionNumber >= 0 && prev.cardinality() == 1 && prev.get(writeInstructionNumber) || writeInstructionNumber == -1 && prev.cardinality() == 0) {
((GrAnonymousClassDefinition) element).acceptChildren(new GroovyRecursiveElementVisitor() {
@Override
public void visitField(@NotNull GrField field) {
GrExpression initializer = field.getInitializerGroovy();
if (initializer != null) {
Instruction[] flow = new ControlFlowBuilder(field.getProject()).buildControlFlow(initializer);
collectRefs(variable, flow, ControlFlowUtils.inferWriteAccessMap(flow, variable), -1, toInline);
}
}
@Override
public void visitMethod(@NotNull GrMethod method) {
GrOpenBlock block = method.getBlock();
if (block != null) {
Instruction[] flow = block.getControlFlow();
collectRefs(variable, flow, ControlFlowUtils.inferWriteAccessMap(flow, variable), -1, toInline);
}
}
@Override
public void visitClassInitializer(@NotNull GrClassInitializer initializer) {
GrOpenBlock block = initializer.getBlock();
Instruction[] flow = block.getControlFlow();
collectRefs(variable, flow, ControlFlowUtils.inferWriteAccessMap(flow, variable), -1, toInline);
}
});
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField in project intellij-community by JetBrains.
the class GroovyMethodInliner method collectConflicts.
@NotNull
private static MultiMap<PsiElement, String> collectConflicts(@NotNull GrCallExpression call, @NotNull Collection<GroovyInlineMethodUtil.ReferenceExpressionInfo> infos) {
MultiMap<PsiElement, String> conflicts = new MultiMap<>();
for (GroovyInlineMethodUtil.ReferenceExpressionInfo info : infos) {
if (!PsiUtil.isAccessible(call, info.declaration)) {
if (info.declaration instanceof PsiMethod) {
String className = info.containingClass.getName();
String signature = GroovyRefactoringUtil.getMethodSignature((PsiMethod) info.declaration);
String name = CommonRefactoringUtil.htmlEmphasize(className + "." + signature);
conflicts.putValue(info.declaration, GroovyRefactoringBundle.message("method.is.not.accessible.form.context.0", name));
} else if (info.declaration instanceof PsiField) {
if (!(info.declaration instanceof GrField && ((GrField) info.declaration).getGetters().length > 0)) {
// conflict if field doesn't have implicit getters
String className = info.containingClass.getName();
String name = CommonRefactoringUtil.htmlEmphasize(className + "." + info.getPresentation());
conflicts.putValue(info.declaration, GroovyRefactoringBundle.message("field.is.not.accessible.form.context.0", name));
}
}
}
AnySupers visitor = new AnySupers();
info.expression.accept(visitor);
if (visitor.containsSupers()) {
conflicts.putValue(info.expression, GroovyRefactoringBundle.message("super.reference.is.used"));
}
}
return conflicts;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.GrField in project intellij-community by JetBrains.
the class GroovyLineMarkerProvider method getGroovyCategory.
private static int getGroovyCategory(@NotNull PsiElement element, @NotNull CharSequence documentChars) {
if (element instanceof GrVariableDeclarationImpl) {
GrVariable[] variables = ((GrVariableDeclarationImpl) element).getVariables();
if (variables.length == 1 && variables[0] instanceof GrField && variables[0].getInitializerGroovy() instanceof GrClosableBlock) {
return 2;
}
}
if (element instanceof GrField || element instanceof GrTypeParameter)
return 1;
if (element instanceof GrTypeDefinition || element instanceof GrClassInitializer)
return 2;
if (element instanceof GrMethod) {
if (((GrMethod) element).hasModifierProperty(PsiModifier.ABSTRACT) && !(((GrMethod) element).getBlock() != null && GrTraitUtil.isTrait(((GrMethod) element).getContainingClass()))) {
return 1;
}
TextRange textRange = element.getTextRange();
int start = textRange.getStartOffset();
int end = Math.min(documentChars.length(), textRange.getEndOffset());
int crlf = StringUtil.getLineBreakCount(documentChars.subSequence(start, end));
return crlf == 0 ? 1 : 2;
}
return 0;
}
Aggregations