mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
RecordAugmentProvider: transform ellipsis types and simplify
GitOrigin-RevId: 4490bbff05c3958862cf470154fd34db56a65c3f
This commit is contained in:
committed by
intellij-monorepo-bot
parent
8fcc253f8e
commit
b437398eab
@@ -11,12 +11,13 @@ import com.intellij.psi.impl.source.PsiExtensibleClass;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.intellij.util.ObjectUtils.tryCast;
|
||||
|
||||
public class RecordAugmentProvider extends PsiAugmentProvider {
|
||||
@NotNull
|
||||
@Override
|
||||
@@ -91,37 +92,27 @@ public class RecordAugmentProvider extends PsiAugmentProvider {
|
||||
if (name == null) return null;
|
||||
String typeText = getTypeText(component, RecordAugmentProvider::hasTargetApplicableForMethod);
|
||||
if (typeText == null) return null;
|
||||
return factory.createMethodFromText("public " + typeText + " " + name + "(){}", component.getContainingClass());
|
||||
return factory.createMethodFromText("public " + typeText + " " + name + "(){ return " + name + "; }", component.getContainingClass());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static String getTypeText(@NotNull PsiRecordComponent component, Predicate<PsiAnnotation> annotationPredicate) {
|
||||
PsiType type = component.getType();
|
||||
if (type instanceof PsiEllipsisType) type = ((PsiEllipsisType)type).toArrayType();
|
||||
PsiTypeElement typeElement = component.getTypeElement();
|
||||
if (typeElement == null) return null;
|
||||
String annotations = Arrays.stream(component.getAnnotations())
|
||||
.filter(annotationPredicate)
|
||||
.map(annotation -> annotation.getText())
|
||||
.collect(Collectors.joining(" "));
|
||||
return annotations + " " + typeElement.getText();
|
||||
return annotations + " " + type.getCanonicalText();
|
||||
}
|
||||
|
||||
private static boolean hasTargetApplicableForField(PsiAnnotation annotation) {
|
||||
Set<PsiAnnotation.TargetType> targets = getTargets(annotation);
|
||||
if (targets == null) return false;
|
||||
return targets.contains(PsiAnnotation.TargetType.TYPE) || targets.contains(PsiAnnotation.TargetType.FIELD);
|
||||
return AnnotationTargetUtil.findAnnotationTarget(annotation, PsiAnnotation.TargetType.TYPE_USE, PsiAnnotation.TargetType.FIELD) != null;
|
||||
}
|
||||
|
||||
private static boolean hasTargetApplicableForMethod(PsiAnnotation annotation) {
|
||||
Set<PsiAnnotation.TargetType> targets = getTargets(annotation);
|
||||
if (targets == null) return false;
|
||||
return targets.contains(PsiAnnotation.TargetType.TYPE) || targets.contains(PsiAnnotation.TargetType.METHOD);
|
||||
}
|
||||
|
||||
private static Set<PsiAnnotation.TargetType> getTargets(PsiAnnotation annotation) {
|
||||
PsiJavaCodeReferenceElement element = annotation.getNameReferenceElement();
|
||||
if (element == null) return null;
|
||||
PsiClass annotationClass = tryCast(element.resolve(), PsiClass.class);
|
||||
if (annotationClass == null) return null;
|
||||
return AnnotationTargetUtil.getAnnotationTargets(annotationClass);
|
||||
return AnnotationTargetUtil.findAnnotationTarget(annotation, PsiAnnotation.TargetType.TYPE_USE, PsiAnnotation.TargetType.METHOD) != null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user