use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class GroovyTrivialIfInspection method isSimplifiableImplicitAssignment.
public static boolean isSimplifiableImplicitAssignment(GrIfStatement ifStatement) {
if (ifStatement.getElseBranch() != null) {
return false;
}
GrStatement thenBranch = ifStatement.getThenBranch();
thenBranch = ConditionalUtils.stripBraces(thenBranch);
final PsiElement nextStatement = PsiTreeUtil.skipSiblingsBackward(ifStatement, PsiWhiteSpace.class);
if (!(nextStatement instanceof GrStatement)) {
return false;
}
GrStatement elseBranch = (GrStatement) nextStatement;
elseBranch = ConditionalUtils.stripBraces(elseBranch);
if (ConditionalUtils.isAssignment(thenBranch, "true") && ConditionalUtils.isAssignment(elseBranch, "false")) {
final GrAssignmentExpression thenExpression = (GrAssignmentExpression) thenBranch;
final GrAssignmentExpression elseExpression = (GrAssignmentExpression) elseBranch;
final IElementType thenSign = thenExpression.getOperationTokenType();
final IElementType elseSign = elseExpression.getOperationTokenType();
if (!thenSign.equals(elseSign)) {
return false;
}
final GrExpression thenLhs = thenExpression.getLValue();
final GrExpression elseLhs = elseExpression.getLValue();
return EquivalenceChecker.expressionsAreEquivalent(thenLhs, elseLhs);
} else {
return false;
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class GroovyTrivialIfInspection method isSimplifiableAssignment.
public static boolean isSimplifiableAssignment(GrIfStatement ifStatement) {
GrStatement thenBranch = ifStatement.getThenBranch();
thenBranch = ConditionalUtils.stripBraces(thenBranch);
GrStatement elseBranch = ifStatement.getElseBranch();
elseBranch = ConditionalUtils.stripBraces(elseBranch);
if (ConditionalUtils.isAssignment(thenBranch, "true") && ConditionalUtils.isAssignment(elseBranch, "false")) {
final GrAssignmentExpression thenExpression = (GrAssignmentExpression) thenBranch;
final GrAssignmentExpression elseExpression = (GrAssignmentExpression) elseBranch;
final IElementType thenSign = thenExpression.getOperationTokenType();
final IElementType elseSign = elseExpression.getOperationTokenType();
if (!thenSign.equals(elseSign)) {
return false;
}
final GrExpression thenLhs = thenExpression.getLValue();
final GrExpression elseLhs = elseExpression.getLValue();
return EquivalenceChecker.expressionsAreEquivalent(thenLhs, elseLhs);
} else {
return false;
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class GroovyConstructorUsagesSearcher method processGroovyConstructorUsages.
private static boolean processGroovyConstructorUsages(GrCodeReferenceElement element, final Processor<GrNewExpression> newExpressionProcessor, final LiteralConstructorSearcher literalProcessor) {
PsiElement parent = element.getParent();
if (parent instanceof GrAnonymousClassDefinition) {
parent = parent.getParent();
}
if (parent instanceof GrNewExpression) {
return newExpressionProcessor.process((GrNewExpression) parent);
}
if (parent instanceof GrTypeElement) {
final GrTypeElement typeElement = (GrTypeElement) parent;
final PsiElement grandpa = typeElement.getParent();
if (grandpa instanceof GrVariableDeclaration) {
final GrVariable[] vars = ((GrVariableDeclaration) grandpa).getVariables();
if (vars.length == 1) {
final GrVariable variable = vars[0];
if (!checkLiteralInstantiation(variable.getInitializerGroovy(), literalProcessor)) {
return false;
}
}
} else if (grandpa instanceof GrMethod) {
final GrMethod method = (GrMethod) grandpa;
if (typeElement == method.getReturnTypeElementGroovy()) {
ControlFlowUtils.visitAllExitPoints(method.getBlock(), new ControlFlowUtils.ExitPointVisitor() {
@Override
public boolean visitExitPoint(Instruction instruction, @Nullable GrExpression returnValue) {
if (!checkLiteralInstantiation(returnValue, literalProcessor)) {
return false;
}
return true;
}
});
}
} else if (grandpa instanceof GrTypeCastExpression) {
final GrTypeCastExpression cast = (GrTypeCastExpression) grandpa;
if (cast.getCastTypeElement() == typeElement && !checkLiteralInstantiation(cast.getOperand(), literalProcessor)) {
return false;
}
} else if (grandpa instanceof GrSafeCastExpression) {
final GrSafeCastExpression cast = (GrSafeCastExpression) grandpa;
if (cast.getCastTypeElement() == typeElement && !checkLiteralInstantiation(cast.getOperand(), literalProcessor)) {
return false;
}
}
}
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class GroovyUntypedAccessInspection method buildVisitor.
@Override
@NotNull
protected BaseInspectionVisitor buildVisitor() {
return new BaseInspectionVisitor() {
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression refExpr) {
super.visitReferenceExpression(refExpr);
if (PsiUtil.isThisOrSuperRef(refExpr))
return;
GroovyResolveResult resolveResult = refExpr.advancedResolve();
PsiElement resolved = resolveResult.getElement();
if (resolved != null) {
if (GrHighlightUtil.isDeclarationAssignment(refExpr) || resolved instanceof PsiPackage)
return;
} else {
GrExpression qualifier = refExpr.getQualifierExpression();
if (qualifier == null && GrHighlightUtil.isDeclarationAssignment(refExpr))
return;
}
final PsiType refExprType = refExpr.getType();
if (refExprType == null) {
if (resolved != null) {
registerError(refExpr);
}
} else if (refExprType instanceof PsiClassType && ((PsiClassType) refExprType).resolve() == null) {
registerError(refExpr);
}
}
};
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression 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;
}
Aggregations