use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class FieldAnnotationChecker method checkApplicability.
@Override
public boolean checkApplicability(@NotNull AnnotationHolder holder, @NotNull GrAnnotation annotation) {
final String qname = annotation.getQualifiedName();
if (!GroovyCommonClassNames.GROOVY_TRANSFORM_FIELD.equals(qname))
return false;
checkScriptField(holder, annotation);
PsiElement annoParent = annotation.getParent();
PsiElement ownerToUse = annoParent instanceof PsiModifierList ? annoParent.getParent() : annoParent;
if (!(ownerToUse instanceof GrVariableDeclaration)) {
return false;
} else {
GrVariableDeclaration declaration = (GrVariableDeclaration) ownerToUse;
if (declaration.getVariables().length != 1 || !PsiUtil.isLocalVariable(declaration.getVariables()[0])) {
return false;
}
}
if (!GrAnnotationImpl.isAnnotationApplicableTo(annotation, PsiAnnotation.TargetType.LOCAL_VARIABLE)) {
GrCodeReferenceElement ref = annotation.getClassReference();
String target = JavaErrorMessages.message("annotation.target.LOCAL_VARIABLE");
String description = JavaErrorMessages.message("annotation.not.applicable", ref.getText(), target);
holder.createErrorAnnotation(ref, description);
}
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class TypeCheckedAnnotationChecker method checkArgumentList.
@Override
public boolean checkArgumentList(@NotNull AnnotationHolder holder, @NotNull GrAnnotation annotation) {
final GrCodeReferenceElement classReference = annotation.getClassReference();
PsiElement resolved = classReference.resolve();
if (!(resolved instanceof PsiClass && GroovyCommonClassNames.GROOVY_TRANSFORM_TYPE_CHECKED.equals(((PsiClass) resolved).getQualifiedName()))) {
return false;
}
String sdkVersion = GroovyConfigUtils.getInstance().getSDKVersion(annotation);
if (!("2.1".equals(sdkVersion) || "2.1.0".equals(sdkVersion)))
return false;
GrAnnotationNameValuePair[] attributes = annotation.getParameterList().getAttributes();
checkAnnotationArguments(holder, (PsiClass) resolved, classReference, attributes, false);
return true;
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement 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.types.GrCodeReferenceElement 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();
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class GroovyFileImpl method setPackageName.
@Override
public void setPackageName(String packageName) {
final ASTNode fileNode = getNode();
final GrPackageDefinition currentPackage = getPackageDefinition();
if (packageName == null || packageName.isEmpty()) {
if (currentPackage != null) {
final ASTNode currNode = currentPackage.getNode();
fileNode.removeChild(currNode);
}
return;
}
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(getProject());
final GrPackageDefinition newPackage = (GrPackageDefinition) factory.createTopElementFromText("package " + packageName);
if (currentPackage != null) {
final GrCodeReferenceElement packageReference = currentPackage.getPackageReference();
if (packageReference != null) {
GrCodeReferenceElement ref = newPackage.getPackageReference();
if (ref != null) {
packageReference.replace(ref);
}
return;
}
}
final ASTNode newNode = newPackage.getNode();
if (currentPackage != null) {
final ASTNode currNode = currentPackage.getNode();
fileNode.replaceChild(currNode, newNode);
} else {
ASTNode anchor = fileNode.getFirstChildNode();
if (anchor != null && anchor.getElementType() == GroovyTokenTypes.mSH_COMMENT) {
anchor = anchor.getTreeNext();
fileNode.addLeaf(GroovyTokenTypes.mNLS, "\n", anchor);
}
fileNode.addChild(newNode, anchor);
if (anchor != null && !anchor.getText().startsWith("\n\n")) {
fileNode.addLeaf(GroovyTokenTypes.mNLS, "\n", anchor);
}
}
}
Aggregations