use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression 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.GrReferenceExpression in project intellij-community by JetBrains.
the class GrDeprecatedAPIUsageInspection method buildVisitor.
@NotNull
@Override
protected BaseInspectionVisitor buildVisitor() {
return new BaseInspectionVisitor() {
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression ref) {
super.visitReferenceExpression(ref);
checkRef(ref);
}
@Override
public void visitCodeReferenceElement(@NotNull GrCodeReferenceElement ref) {
super.visitCodeReferenceElement(ref);
checkRef(ref);
}
private void checkRef(GrReferenceElement ref) {
PsiElement resolved = ref.resolve();
if (isDeprecated(resolved)) {
PsiElement toHighlight = getElementToHighlight(ref);
registerError(toHighlight, GroovyBundle.message("0.is.deprecated", ref.getReferenceName()), LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.LIKE_DEPRECATED);
}
}
@NotNull
public PsiElement getElementToHighlight(@NotNull GrReferenceElement refElement) {
final PsiElement refNameElement = refElement.getReferenceNameElement();
return refNameElement != null ? refNameElement : refElement;
}
private boolean isDeprecated(PsiElement resolved) {
if (resolved instanceof PsiDocCommentOwner) {
return ((PsiDocCommentOwner) resolved).isDeprecated();
}
if (resolved instanceof PsiModifierListOwner && PsiImplUtil.isDeprecatedByAnnotation((PsiModifierListOwner) resolved)) {
return true;
}
return false;
}
};
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class GrReassignedInClosureLocalVarInspection method buildVisitor.
@NotNull
@Override
protected BaseInspectionVisitor buildVisitor() {
return new BaseInspectionVisitor() {
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
super.visitReferenceExpression(referenceExpression);
if (!PsiUtil.isLValue(referenceExpression))
return;
final PsiElement resolved = referenceExpression.resolve();
if (!PsiUtil.isLocalVariable(resolved))
return;
final PsiType checked = GrReassignedLocalVarsChecker.getReassignedVarType(referenceExpression, false);
if (checked == null)
return;
final GrControlFlowOwner varFlowOwner = ControlFlowUtils.findControlFlowOwner(resolved);
final GrControlFlowOwner refFlorOwner = ControlFlowUtils.findControlFlowOwner(referenceExpression);
if (isOtherScopeAndType(referenceExpression, checked, varFlowOwner, refFlorOwner)) {
String flowDescription = getFlowDescription(refFlorOwner);
final String message = GroovyInspectionBundle.message("local.var.0.is.reassigned", ((GrNamedElement) resolved).getName(), flowDescription);
registerError(referenceExpression, message, LocalQuickFix.EMPTY_ARRAY, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
}
};
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class GrChangeVariableType method doFix.
@Override
protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException {
final PsiElement element = descriptor.getPsiElement();
final PsiElement parent = element.getParent();
try {
final PsiType type = JavaPsiFacade.getElementFactory(project).createTypeFromText(myType, element);
if (parent instanceof GrVariable) {
((GrVariable) parent).setType(type);
} else if (element instanceof GrReferenceExpression && parent instanceof GrAssignmentExpression && ((GrAssignmentExpression) parent).getLValue() == element) {
final PsiElement resolved = ((GrReferenceExpression) element).resolve();
if (resolved instanceof GrVariable && !(resolved instanceof GrParameter)) {
((GrVariable) resolved).setType(type);
}
}
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression in project intellij-community by JetBrains.
the class UnnecessaryQualifiedReferenceInspection method canBeSimplified.
private static boolean canBeSimplified(PsiElement element) {
if (PsiTreeUtil.getParentOfType(element, PsiComment.class) != null)
return false;
if (element instanceof GrCodeReferenceElement) {
if (PsiTreeUtil.getParentOfType(element, GrImportStatement.class, GrPackageDefinition.class) != null)
return false;
} else if (element instanceof GrReferenceExpression) {
if (!PsiImplUtil.seemsToBeQualifiedClassName((GrReferenceExpression) element))
return false;
} else {
return false;
}
final GrReferenceElement ref = (GrReferenceElement) element;
if (ref.getQualifier() == null)
return false;
if (!(ref.getContainingFile() instanceof GroovyFileBase))
return false;
final PsiElement resolved = ref.resolve();
if (!(resolved instanceof PsiClass))
return false;
final String name = ((PsiClass) resolved).getName();
if (name == null)
return false;
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(element.getProject());
final GrReferenceExpression shortedRef = factory.createReferenceExpressionFromText(name, element);
final GroovyResolveResult resolveResult = shortedRef.advancedResolve();
if (element.getManager().areElementsEquivalent(resolved, resolveResult.getElement())) {
return true;
}
final PsiClass containingClass = ((PsiClass) resolved).getContainingClass();
if (containingClass != null && !GroovyCodeStyleSettingsFacade.getInstance(containingClass.getProject()).insertInnerClassImports()) {
return false;
}
return resolveResult.getElement() == null || !resolveResult.isAccessible() || !resolveResult.isStaticsOK();
}
Aggregations