use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class GroovyTestGenerator method addSuperClass.
private static void addSuperClass(@NotNull GrTypeDefinition targetClass, @NotNull Project project, @Nullable String superClassName) throws IncorrectOperationException {
if (superClassName == null)
return;
GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(project);
PsiClass superClass = findClass(project, superClassName);
GrCodeReferenceElement superClassRef;
if (superClass != null) {
superClassRef = factory.createCodeReferenceElementFromClass(superClass);
} else {
superClassRef = factory.createCodeReferenceElementFromText(superClassName);
}
GrExtendsClause extendsClause = targetClass.getExtendsClause();
if (extendsClause == null) {
extendsClause = (GrExtendsClause) targetClass.addAfter(factory.createExtendsClause(), targetClass.getNameIdentifierGroovy());
}
extendsClause.add(superClassRef);
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class GrPullUpHelper method postProcessMember.
@Override
public void postProcessMember(PsiMember member) {
((GrMember) member).accept(myExplicitSuperDeleter);
((GrMember) member).accept(myThisSuperAdjuster);
GroovyChangeContextUtil.decodeContextInfo(member, null, null);
((GroovyPsiElement) member).accept(new GroovyRecursiveElementVisitor() {
@Override
public void visitReferenceExpression(@NotNull GrReferenceExpression referenceExpression) {
if (processRef(referenceExpression))
return;
super.visitReferenceExpression(referenceExpression);
}
@Override
public void visitCodeReferenceElement(@NotNull GrCodeReferenceElement refElement) {
if (processRef(refElement))
return;
super.visitCodeReferenceElement(refElement);
}
private boolean processRef(@NotNull GrReferenceElement<? extends GroovyPsiElement> refElement) {
final PsiElement qualifier = refElement.getQualifier();
if (qualifier != null) {
final Boolean preserveQualifier = qualifier.getCopyableUserData(PRESERVE_QUALIFIER);
if (preserveQualifier != null && !preserveQualifier) {
refElement.setQualifier(null);
return true;
}
}
return false;
}
});
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class GrPullUpHelper method replaceMovedMemberTypeParameters.
public static void replaceMovedMemberTypeParameters(final PsiElement member, final Iterable<PsiTypeParameter> parametersIterable, final PsiSubstitutor substitutor, final GroovyPsiElementFactory factory) {
final Map<PsiElement, PsiElement> replacement = new LinkedHashMap<>();
for (PsiTypeParameter parameter : parametersIterable) {
PsiType substitutedType = substitutor.substitute(parameter);
PsiType type = substitutedType != null ? substitutedType : TypeConversionUtil.erasure(factory.createType(parameter));
PsiElement scopeElement = member instanceof GrField ? member.getParent() : member;
for (PsiReference reference : ReferencesSearch.search(parameter, new LocalSearchScope(scopeElement))) {
final PsiElement element = reference.getElement();
final PsiElement parent = element.getParent();
if (parent instanceof PsiTypeElement) {
replacement.put(parent, factory.createTypeElement(type));
} else if (element instanceof GrCodeReferenceElement && type instanceof PsiClassType) {
replacement.put(element, factory.createReferenceElementByType((PsiClassType) type));
}
}
}
final JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(member.getProject());
for (PsiElement element : replacement.keySet()) {
if (element.isValid()) {
final PsiElement replaced = element.replace(replacement.get(element));
codeStyleManager.shortenClassReferences(replaced);
}
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class GrRefactoringConflictsUtil method checkUsedElements.
public static void checkUsedElements(PsiMember member, PsiElement scope, @NotNull Set<GrMember> membersToMove, @Nullable Set<PsiMethod> abstractMethods, @Nullable PsiClass targetClass, @NotNull PsiElement context, MultiMap<PsiElement, String> conflicts) {
final Set<PsiMember> moving = new HashSet<>(membersToMove);
if (abstractMethods != null) {
moving.addAll(abstractMethods);
}
if (scope instanceof GrReferenceExpression) {
GrReferenceExpression refExpr = (GrReferenceExpression) scope;
PsiElement refElement = refExpr.resolve();
if (refElement instanceof PsiMember) {
if (!RefactoringHierarchyUtil.willBeInTargetClass(refElement, moving, targetClass, false)) {
GrExpression qualifier = refExpr.getQualifierExpression();
PsiClass accessClass = (PsiClass) (qualifier != null ? PsiUtil.getAccessObjectClass(qualifier).getElement() : null);
RefactoringConflictsUtil.checkAccessibility((PsiMember) refElement, context, accessClass, member, conflicts);
}
}
} else if (scope instanceof GrNewExpression) {
final GrNewExpression newExpression = (GrNewExpression) scope;
final GrAnonymousClassDefinition anonymousClass = newExpression.getAnonymousClassDefinition();
if (anonymousClass != null) {
if (!RefactoringHierarchyUtil.willBeInTargetClass(anonymousClass, moving, targetClass, false)) {
RefactoringConflictsUtil.checkAccessibility(anonymousClass, context, anonymousClass, member, conflicts);
}
} else {
final PsiMethod refElement = newExpression.resolveMethod();
if (refElement != null) {
if (!RefactoringHierarchyUtil.willBeInTargetClass(refElement, moving, targetClass, false)) {
RefactoringConflictsUtil.checkAccessibility(refElement, context, null, member, conflicts);
}
}
}
} else if (scope instanceof GrCodeReferenceElement) {
GrCodeReferenceElement refExpr = (GrCodeReferenceElement) scope;
PsiElement refElement = refExpr.resolve();
if (refElement instanceof PsiMember) {
if (!RefactoringHierarchyUtil.willBeInTargetClass(refElement, moving, targetClass, false)) {
RefactoringConflictsUtil.checkAccessibility((PsiMember) refElement, context, null, member, conflicts);
}
}
}
for (PsiElement child : scope.getChildren()) {
if (child instanceof PsiWhiteSpace || child instanceof PsiComment)
continue;
checkUsedElements(member, child, membersToMove, abstractMethods, targetClass, context, conflicts);
}
}
use of org.jetbrains.plugins.groovy.lang.psi.api.types.GrCodeReferenceElement in project intellij-community by JetBrains.
the class AnnotationChecker method checkApplicability.
public void checkApplicability(@NotNull GrAnnotation annotation, @Nullable PsiAnnotationOwner owner) {
final GrCodeReferenceElement ref = annotation.getClassReference();
final PsiElement resolved = ref.resolve();
if (resolved == null)
return;
assert resolved instanceof PsiClass;
PsiClass anno = (PsiClass) resolved;
String qname = anno.getQualifiedName();
if (!anno.isAnnotationType() && GrAnnotationCollector.findAnnotationCollector(anno) == null) {
if (qname != null) {
myHolder.createErrorAnnotation(ref, GroovyBundle.message("class.is.not.annotation", qname));
}
return;
}
for (CustomAnnotationChecker checker : CustomAnnotationChecker.EP_NAME.getExtensions()) {
if (checker.checkApplicability(myHolder, annotation))
return;
}
String description = CustomAnnotationChecker.isAnnotationApplicable(annotation, owner);
if (description != null) {
myHolder.createErrorAnnotation(ref, description).registerFix(new GrRemoveAnnotationIntention());
}
}
Aggregations