mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-01-06 03:21:12 +07:00
Revert "Augment records in dumb mode, extract annotation filtration to annotation provider"
This reverts commit c101b096 GitOrigin-RevId: d3991dbd578e36758ab45bcf921d9e27febfe84d
This commit is contained in:
committed by
intellij-monorepo-bot
parent
2873a624ed
commit
a6623419bb
@@ -1,7 +1,7 @@
|
||||
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.psi.impl;
|
||||
|
||||
import com.intellij.openapi.project.DumbAware;
|
||||
import com.intellij.codeInsight.AnnotationTargetUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.augment.PsiAugmentProvider;
|
||||
import com.intellij.psi.impl.light.LightMethod;
|
||||
@@ -14,10 +14,13 @@ import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
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;
|
||||
|
||||
public class RecordAugmentProvider extends PsiAugmentProvider implements DumbAware {
|
||||
public class RecordAugmentProvider extends PsiAugmentProvider {
|
||||
@NotNull
|
||||
@Override
|
||||
protected <Psi extends PsiElement> List<Psi> getAugments(@NotNull PsiElement element, @NotNull Class<Psi> type) {
|
||||
@@ -102,7 +105,7 @@ public class RecordAugmentProvider extends PsiAugmentProvider implements DumbAwa
|
||||
private static PsiField createRecordField(@NotNull PsiRecordComponent component, @NotNull PsiElementFactory factory) {
|
||||
String name = component.getName();
|
||||
if (hasForbiddenType(component)) return null;
|
||||
String typeText = getTypeText(component);
|
||||
String typeText = getTypeText(component, RecordAugmentProvider::hasTargetApplicableForField);
|
||||
if (typeText == null) return null;
|
||||
return factory.createFieldFromText("private final " + typeText + " " + name + ";", component.getContainingClass());
|
||||
}
|
||||
@@ -112,7 +115,7 @@ public class RecordAugmentProvider extends PsiAugmentProvider implements DumbAwa
|
||||
String name = component.getName();
|
||||
if (name == null) return null;
|
||||
if (hasForbiddenType(component)) return null;
|
||||
String typeText = getTypeText(component);
|
||||
String typeText = getTypeText(component, RecordAugmentProvider::hasTargetApplicableForMethod);
|
||||
if (typeText == null) return null;
|
||||
return factory.createMethodFromText("public " + typeText + " " + name + "(){ return " + name + "; }", component.getContainingClass());
|
||||
}
|
||||
@@ -123,11 +126,23 @@ public class RecordAugmentProvider extends PsiAugmentProvider implements DumbAwa
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static String getTypeText(@NotNull PsiRecordComponent component) {
|
||||
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;
|
||||
return type.getCanonicalText();
|
||||
String annotations = Arrays.stream(component.getAnnotations())
|
||||
.filter(annotationPredicate)
|
||||
.map(annotation -> annotation.getText())
|
||||
.collect(Collectors.joining(" "));
|
||||
return annotations + " " + type.getCanonicalText();
|
||||
}
|
||||
|
||||
private static boolean hasTargetApplicableForField(PsiAnnotation annotation) {
|
||||
return AnnotationTargetUtil.findAnnotationTarget(annotation, PsiAnnotation.TargetType.TYPE_USE, PsiAnnotation.TargetType.FIELD) != null;
|
||||
}
|
||||
|
||||
private static boolean hasTargetApplicableForMethod(PsiAnnotation annotation) {
|
||||
return AnnotationTargetUtil.findAnnotationTarget(annotation, PsiAnnotation.TargetType.TYPE_USE, PsiAnnotation.TargetType.METHOD) != null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.psi.impl.light;
|
||||
|
||||
import com.intellij.codeInsight.AnnotationTargetUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.ElementPresentationUtil;
|
||||
import com.intellij.psi.util.CachedValueProvider;
|
||||
import com.intellij.psi.util.CachedValuesManager;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.ui.IconManager;
|
||||
import com.intellij.ui.icons.RowIcon;
|
||||
@@ -13,10 +10,8 @@ import com.intellij.util.BitUtil;
|
||||
import com.intellij.util.PlatformIcons;
|
||||
import com.intellij.util.VisibilityIcons;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class LightRecordField extends LightField implements LightRecordMember {
|
||||
private final @NotNull PsiRecordComponent myRecordComponent;
|
||||
@@ -58,34 +53,6 @@ public class LightRecordField extends LightField implements LightRecordMember {
|
||||
return containingClass.getContainingFile();
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull PsiType getType() {
|
||||
return CachedValuesManager.getCachedValue(this, () -> {
|
||||
PsiType type = myRecordComponent.getType()
|
||||
.annotate(() -> Arrays.stream(myRecordComponent.getAnnotations())
|
||||
.filter(LightRecordField::hasApplicableAnnotationTarget)
|
||||
.toArray(PsiAnnotation[]::new)
|
||||
);
|
||||
return CachedValueProvider.Result.create(type, this);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public PsiAnnotation @NotNull [] getAnnotations() {
|
||||
return getType().getAnnotations();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasAnnotation(@NotNull String fqn) {
|
||||
PsiType type = getType();
|
||||
return type.hasAnnotation(fqn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable PsiAnnotation getAnnotation(@NotNull String fqn) {
|
||||
return getType().findAnnotation(fqn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Icon getElementIcon(final int flags) {
|
||||
final RowIcon baseIcon =
|
||||
@@ -100,8 +67,4 @@ public class LightRecordField extends LightField implements LightRecordMember {
|
||||
public PsiElement getContext() {
|
||||
return getContainingClass();
|
||||
}
|
||||
|
||||
private static boolean hasApplicableAnnotationTarget(PsiAnnotation annotation) {
|
||||
return AnnotationTargetUtil.findAnnotationTarget(annotation, PsiAnnotation.TargetType.TYPE_USE, PsiAnnotation.TargetType.FIELD) != null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,8 @@
|
||||
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.psi.impl.light;
|
||||
|
||||
import com.intellij.codeInsight.AnnotationTargetUtil;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.impl.ElementPresentationUtil;
|
||||
import com.intellij.psi.util.CachedValueProvider;
|
||||
import com.intellij.psi.util.CachedValuesManager;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.ui.IconManager;
|
||||
import com.intellij.ui.icons.RowIcon;
|
||||
@@ -13,10 +10,8 @@ import com.intellij.util.BitUtil;
|
||||
import com.intellij.util.PlatformIcons;
|
||||
import com.intellij.util.VisibilityIcons;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.Arrays;
|
||||
|
||||
public class LightRecordMethod extends LightMethod implements LightRecordMember {
|
||||
private final @NotNull PsiRecordComponent myRecordComponent;
|
||||
@@ -57,38 +52,6 @@ public class LightRecordMethod extends LightMethod implements LightRecordMember
|
||||
return containingClass.getContainingFile();
|
||||
}
|
||||
|
||||
@Override
|
||||
public PsiType getReturnType() {
|
||||
return CachedValuesManager.getCachedValue(this, () -> {
|
||||
PsiType type = myRecordComponent.getType()
|
||||
.annotate(() -> Arrays.stream(myRecordComponent.getAnnotations())
|
||||
.filter(LightRecordMethod::hasTargetApplicableForMethod)
|
||||
.toArray(PsiAnnotation[]::new)
|
||||
);
|
||||
return CachedValueProvider.Result.create(type, this);
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public PsiAnnotation @NotNull [] getAnnotations() {
|
||||
PsiType returnType = getReturnType();
|
||||
if (returnType == null) return PsiAnnotation.EMPTY_ARRAY;
|
||||
return returnType.getAnnotations();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasAnnotation(@NotNull String fqn) {
|
||||
PsiType returnType = getReturnType();
|
||||
return returnType != null && returnType.hasAnnotation(fqn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable PsiAnnotation getAnnotation(@NotNull String fqn) {
|
||||
PsiType returnType = getReturnType();
|
||||
if (returnType == null) return null;
|
||||
return returnType.findAnnotation(fqn);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Icon getElementIcon(final int flags) {
|
||||
final RowIcon baseIcon =
|
||||
@@ -103,8 +66,4 @@ public class LightRecordMethod extends LightMethod implements LightRecordMember
|
||||
public PsiElement getContext() {
|
||||
return getContainingClass();
|
||||
}
|
||||
|
||||
private static boolean hasTargetApplicableForMethod(PsiAnnotation annotation) {
|
||||
return AnnotationTargetUtil.findAnnotationTarget(annotation, PsiAnnotation.TargetType.TYPE_USE, PsiAnnotation.TargetType.METHOD) != null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.java.psi
|
||||
|
||||
import com.intellij.openapi.command.WriteCommandAction
|
||||
import com.intellij.openapi.diagnostic.DefaultLogger
|
||||
import com.intellij.openapi.project.DumbServiceImpl
|
||||
import com.intellij.psi.*
|
||||
import com.intellij.psi.impl.light.LightRecordMethod
|
||||
import com.intellij.psi.impl.source.PsiClassReferenceType
|
||||
import com.intellij.psi.impl.source.PsiImmediateClassType
|
||||
import com.intellij.testFramework.EdtTestUtil
|
||||
import com.intellij.testFramework.PsiTestUtil
|
||||
import com.intellij.testFramework.fixtures.LightJavaCodeInsightFixtureTestCase
|
||||
import com.intellij.util.IdempotenceChecker
|
||||
@@ -180,17 +177,6 @@ class JavaPsiTest extends LightJavaCodeInsightFixtureTestCase {
|
||||
assert 1 == clazz.methods.size() // only constructor
|
||||
}
|
||||
|
||||
void "test record has members in dumb mode"() {
|
||||
final DumbServiceImpl dumbService = DumbServiceImpl.getInstance(getProject());
|
||||
EdtTestUtil.runInEdtAndWait({ -> dumbService.setDumb(true) })
|
||||
def clazz = configureFile("record A(int i)").classes[0]
|
||||
def methods = clazz.findMethodsByName("i")
|
||||
assert 1 == methods.size()
|
||||
def method = methods.first()
|
||||
assert method.getReturnType() == PsiPrimitiveType.INT
|
||||
assert method instanceof LightRecordMethod
|
||||
}
|
||||
|
||||
private PsiJavaFile configureFile(String text) {
|
||||
myFixture.configureByText("a.java", text) as PsiJavaFile
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.java.psi.resolve;
|
||||
|
||||
import com.intellij.lang.jvm.JvmParameter;
|
||||
@@ -44,8 +44,7 @@ public class ResolveRecordMethodsTest extends LightResolveTestCase {
|
||||
PsiElement target = resolve();
|
||||
assertTrue(target instanceof PsiField);
|
||||
PsiField targetField = (PsiField)target;
|
||||
PsiType type = targetField.getType();
|
||||
assertEquals(PsiType.INT, type);
|
||||
assertEquals(PsiType.INT, targetField.getType());
|
||||
|
||||
PsiJavaFile file = (PsiJavaFile)getFile();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user