use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class CustomAnnotationChecker method checkAnnotationValueByType.
public static void checkAnnotationValueByType(@NotNull AnnotationHolder holder, @NotNull GrAnnotationMemberValue value, @Nullable PsiType ltype, boolean skipArrays) {
final GlobalSearchScope resolveScope = value.getResolveScope();
final PsiManager manager = value.getManager();
if (value instanceof GrExpression) {
final PsiType rtype;
if (value instanceof GrClosableBlock) {
rtype = PsiType.getJavaLangClass(manager, resolveScope);
} else {
rtype = ((GrExpression) value).getType();
}
if (rtype != null && !checkAnnoTypeAssignable(ltype, rtype, value, skipArrays)) {
holder.createErrorAnnotation(value, GroovyBundle.message("cannot.assign", rtype.getPresentableText(), ltype.getPresentableText()));
}
} else if (value instanceof GrAnnotation) {
final PsiElement resolved = ((GrAnnotation) value).getClassReference().resolve();
if (resolved instanceof PsiClass) {
final PsiClassType rtype = JavaPsiFacade.getElementFactory(value.getProject()).createType((PsiClass) resolved, PsiSubstitutor.EMPTY);
if (!checkAnnoTypeAssignable(ltype, rtype, value, skipArrays)) {
holder.createErrorAnnotation(value, GroovyBundle.message("cannot.assign", rtype.getPresentableText(), ltype.getPresentableText()));
}
}
} else if (value instanceof GrAnnotationArrayInitializer) {
if (ltype instanceof PsiArrayType) {
final PsiType componentType = ((PsiArrayType) ltype).getComponentType();
final GrAnnotationMemberValue[] initializers = ((GrAnnotationArrayInitializer) value).getInitializers();
for (GrAnnotationMemberValue initializer : initializers) {
checkAnnotationValueByType(holder, initializer, componentType, false);
}
} else {
final PsiType rtype = TypesUtil.getTupleByAnnotationArrayInitializer((GrAnnotationArrayInitializer) value);
if (!checkAnnoTypeAssignable(ltype, rtype, value, skipArrays)) {
holder.createErrorAnnotation(value, GroovyBundle.message("cannot.assign", rtype.getPresentableText(), ltype.getPresentableText()));
}
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class GrInspectionUtil method replaceExpression.
public static void replaceExpression(GrExpression expression, String newExpression) {
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(expression.getProject());
final GrExpression newCall = factory.createExpressionFromText(newExpression);
expression.replaceWithExpression(newCall, true);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class GrHighlightUtil method isScriptPropertyAccess.
private static boolean isScriptPropertyAccess(GrReferenceExpression refExpr) {
final GrExpression qualifier = refExpr.getQualifierExpression();
if (qualifier == null) {
final PsiClass clazz = PsiTreeUtil.getParentOfType(refExpr, PsiClass.class);
if (clazz == null) {
//script
return true;
}
//in class, a property should normally be defined, so it's not a declaration
return false;
}
final PsiType type = qualifier.getType();
if (type instanceof PsiClassType && !(qualifier instanceof GrReferenceExpression && ((GrReferenceExpression) qualifier).resolve() instanceof GroovyScriptClass)) {
final PsiClassType classType = (PsiClassType) type;
final PsiClass psiClass = classType.resolve();
if (psiClass instanceof GroovyScriptClass) {
return true;
}
}
return false;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class JavaStylePropertiesInvocationInspection method buildVisitor.
@NotNull
@Override
protected BaseInspectionVisitor buildVisitor() {
return new BaseInspectionVisitor() {
@Override
public void visitMethodCallExpression(@NotNull GrMethodCallExpression methodCallExpression) {
super.visitMethodCallExpression(methodCallExpression);
visitMethodCall(methodCallExpression);
}
@Override
public void visitApplicationStatement(@NotNull GrApplicationStatement applicationStatement) {
super.visitApplicationStatement(applicationStatement);
visitMethodCall(applicationStatement);
}
private void visitMethodCall(GrMethodCall methodCall) {
if (JavaStylePropertiesUtil.isPropertyAccessor(methodCall)) {
final String message = GroovyInspectionBundle.message("java.style.property.access");
final GrExpression expression = methodCall.getInvokedExpression();
if (expression instanceof GrReferenceExpression) {
PsiElement referenceNameElement = ((GrReferenceExpression) expression).getReferenceNameElement();
registerError(referenceNameElement, message, myFixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
}
}
};
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression in project intellij-community by JetBrains.
the class GroovyPointlessArithmeticInspection method calculateReplacementExpression.
private static String calculateReplacementExpression(GrExpression expression) {
final GrBinaryExpression exp = (GrBinaryExpression) expression;
final IElementType sign = exp.getOperationTokenType();
final GrExpression lhs = exp.getLeftOperand();
final GrExpression rhs = exp.getRightOperand();
assert rhs != null;
if (GroovyTokenTypes.mPLUS == sign) {
if (isZero(lhs)) {
return rhs.getText();
} else {
return lhs.getText();
}
}
if (GroovyTokenTypes.mMINUS == sign) {
return lhs.getText();
}
if (GroovyTokenTypes.mSTAR == sign) {
if (isOne(lhs)) {
return rhs.getText();
} else if (isOne(rhs)) {
return lhs.getText();
} else {
return "0";
}
}
if (GroovyTokenTypes.mDIV == sign) {
return lhs.getText();
}
return "";
}
Aggregations