use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class SpockUtils method createVariableMap.
// See org.spockframework.compiler.WhereBlockRewriter
public static Map<String, SpockVariableDescriptor> createVariableMap(GrMethod method) {
GrOpenBlock block = method.getBlock();
if (block == null)
return Collections.emptyMap();
PsiElement elementUnderLabel = null;
PsiElement elementAfterLabel = null;
main: for (PsiElement e = block.getFirstChild(); e != null; e = e.getNextSibling()) {
if (e instanceof GrLabeledStatement) {
GrLabeledStatement l = (GrLabeledStatement) e;
elementAfterLabel = l.getNextSibling();
while (true) {
GrStatement statement = l.getStatement();
if ("where".equals(l.getName())) {
elementUnderLabel = statement;
break main;
}
if (statement instanceof GrLabeledStatement) {
l = (GrLabeledStatement) statement;
continue;
}
break;
}
}
}
if (elementUnderLabel == null)
return Collections.emptyMap();
Map<String, SpockVariableDescriptor> res = new HashMap<>();
PsiElement e = elementUnderLabel;
while (e != null) {
if (e instanceof GrBinaryExpression && ((GrBinaryExpression) e).getOperationTokenType() == GroovyElementTypes.COMPOSITE_LSHIFT_SIGN) {
GrBinaryExpression shift = (GrBinaryExpression) e;
GrExpression leftOperand = shift.getLeftOperand();
GrExpression rightOperand = shift.getRightOperand();
if (leftOperand instanceof GrReferenceExpression) {
String name = getNameByReference(leftOperand);
if (name != null) {
SpockVariableDescriptor descriptor = new SpockVariableDescriptor(leftOperand, name);
descriptor.addExpressionOfCollection(rightOperand);
res.put(name, descriptor);
}
} else if (leftOperand instanceof GrListOrMap) {
GrExpression[] variableDefinitions = ((GrListOrMap) leftOperand).getInitializers();
SpockVariableDescriptor[] variables = createVariables(res, Arrays.asList(variableDefinitions));
if (rightOperand instanceof GrListOrMap) {
for (GrExpression expression : ((GrListOrMap) rightOperand).getInitializers()) {
if (expression instanceof GrListOrMap) {
add(variables, Arrays.asList(((GrListOrMap) expression).getInitializers()));
} else {
for (SpockVariableDescriptor variable : variables) {
if (variable != null) {
variable.addExpressionOfCollection(expression);
}
}
}
}
}
}
} else if (e instanceof GrAssignmentExpression) {
GrAssignmentExpression assExpr = (GrAssignmentExpression) e;
GrExpression lValue = assExpr.getLValue();
String name = getNameByReference(lValue);
if (name != null) {
res.put(name, new SpockVariableDescriptor(lValue, name).addExpression(assExpr.getRValue()));
}
} else if (isOrStatement(e)) {
// See org.spockframework.compiler.WhereBlockRewriter#rewriteTableLikeParameterization()
List<GrExpression> variableDefinitions = new ArrayList<>();
splitOr(variableDefinitions, (GrExpression) e);
SpockVariableDescriptor[] variables = createVariables(res, variableDefinitions);
List<GrExpression> row = new ArrayList<>();
PsiElement rowElement = getNext(e, elementUnderLabel, elementAfterLabel);
while (isOrStatement(rowElement)) {
row.clear();
splitOr(row, (GrExpression) rowElement);
add(variables, row);
rowElement = getNext(rowElement, elementUnderLabel, elementAfterLabel);
}
e = rowElement;
continue;
}
e = getNext(e, elementUnderLabel, elementAfterLabel);
}
return res;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class SpockUtils method getNameByReference.
@Nullable
public static String getNameByReference(@Nullable PsiElement expression) {
if (!(expression instanceof GrReferenceExpression))
return null;
PsiElement firstChild = expression.getFirstChild();
if (firstChild != expression.getLastChild() || !PsiImplUtil.isLeafElementOfType(firstChild, GroovyTokenTypes.mIDENT))
return null;
GrReferenceExpression ref = (GrReferenceExpression) expression;
if (ref.isQualified())
return null;
return ref.getReferenceName();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class UnusedDefInspection method isUsedInTopLevelFlowOnly.
private static boolean isUsedInTopLevelFlowOnly(PsiElement element) {
GrVariable var = null;
if (element instanceof GrVariable) {
var = (GrVariable) element;
} else if (element instanceof GrReferenceExpression) {
final PsiElement resolved = ((GrReferenceExpression) element).resolve();
if (resolved instanceof GrVariable)
var = (GrVariable) resolved;
}
if (var != null) {
final GroovyPsiElement scope = ControlFlowUtils.findControlFlowOwner(var);
if (scope == null) {
PsiFile file = var.getContainingFile();
if (file == null) {
LOG.error("no file??? var of type" + var.getClass().getCanonicalName());
return false;
} else {
TextRange range = var.getTextRange();
LOG.error("var: " + var.getName() + ", offset:" + (range != null ? range.getStartOffset() : -1));
return false;
}
}
return ReferencesSearch.search(var, var.getUseScope()).forEach(ref -> ControlFlowUtils.findControlFlowOwner(ref.getElement()) == scope);
}
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class ControlFlowUtils method inferWriteAccessMap.
@NotNull
public static List<BitSet> inferWriteAccessMap(final Instruction[] flow, final GrVariable var) {
final Semilattice<BitSet> sem = new Semilattice<BitSet>() {
@NotNull
@Override
public BitSet join(@NotNull List<BitSet> ins) {
BitSet result = new BitSet(flow.length);
for (BitSet set : ins) {
result.or(set);
}
return result;
}
@Override
public boolean eq(@NotNull BitSet e1, @NotNull BitSet e2) {
return e1.equals(e2);
}
};
DfaInstance<BitSet> dfa = new DfaInstance<BitSet>() {
@Override
public void fun(@NotNull BitSet bitSet, @NotNull Instruction instruction) {
if (!(instruction instanceof ReadWriteVariableInstruction))
return;
if (!((ReadWriteVariableInstruction) instruction).isWrite())
return;
final PsiElement element = instruction.getElement();
if (element instanceof GrVariable && element != var)
return;
if (element instanceof GrReferenceExpression) {
final GrReferenceExpression ref = (GrReferenceExpression) element;
if (ref.isQualified() || ref.resolve() != var) {
return;
}
}
if (!((ReadWriteVariableInstruction) instruction).getVariableName().equals(var.getName())) {
return;
}
bitSet.clear();
bitSet.set(instruction.num());
}
@NotNull
@Override
public BitSet initial() {
return new BitSet(flow.length);
}
};
return new DFAEngine<>(flow, dfa, sem).performForceDFA();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class GrReferenceAdjuster method shortenReferenceInner.
private static <Qualifier extends PsiElement> boolean shortenReferenceInner(@NotNull GrQualifiedReference<Qualifier> ref, boolean addImports, boolean incomplete, boolean useFqInJavadoc, boolean useFqInCode) {
final Qualifier qualifier = ref.getQualifier();
if (qualifier == null || PsiUtil.isSuperReference(qualifier) || cannotShortenInContext(ref)) {
return false;
}
if (ref instanceof GrReferenceExpression) {
final GrTypeArgumentList typeArgs = ((GrReferenceExpression) ref).getTypeArgumentList();
if (typeArgs != null && typeArgs.getTypeArgumentElements().length > 0) {
return false;
}
}
if (!shorteningIsMeaningfully(ref, useFqInJavadoc, useFqInCode))
return false;
final PsiElement resolved = resolveRef(ref, incomplete);
if (resolved == null)
return false;
if (!checkCopyWithoutQualifier(ref, addImports, resolved))
return false;
ref.setQualifier(null);
return true;
}
Aggregations