Skip to content
This repository has been archived by the owner on Nov 3, 2024. It is now read-only.

Commit

Permalink
merged adapted changes from master
Browse files Browse the repository at this point in the history
  • Loading branch information
mplushnikov committed Jan 2, 2021
1 parent c2da4ff commit b484fdc
Show file tree
Hide file tree
Showing 9 changed files with 178 additions and 28 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# https://www.jetbrains.com/intellij-repository/snapshots
#
ideaVersion=2020.1
#ideaVersion=2020.2.1
#ideaVersion=2020.2
#
pluginGroup=de.plushnikov.intellij.plugin
pluginName=lombok-plugin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ public Collection<LombokProblem> verifyAnnotation(@NotNull PsiAnnotation psiAnno
if (null != psiMethod) {
if (psiMethod.hasModifierProperty(PsiModifier.ABSTRACT)) {
problemNewBuilder.addError(LombokBundle.message("inspection.message.synchronized.legal.only.on.concrete.methods"),
PsiQuickFixFactory.createModifierListFix(psiMethod, PsiModifier.ABSTRACT, false, false)
PsiQuickFixFactory.createModifierListFix(psiMethod, PsiModifier.ABSTRACT, false, false)
);
}

@NlsSafe final String lockFieldName = PsiAnnotationUtil.getStringAnnotationValue(psiAnnotation, "value", "");
final String lockFieldName = PsiAnnotationUtil.getStringAnnotationValue(psiAnnotation, "value", "");
if (StringUtil.isNotEmpty(lockFieldName)) {
final PsiClass containingClass = psiMethod.getContainingClass();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.util.RecursionGuard;
import com.intellij.openapi.util.RecursionManager;
import com.intellij.psi.*;
import com.intellij.psi.augment.PsiAugmentProvider;
import com.intellij.psi.impl.source.PsiExtensibleClass;
Expand Down Expand Up @@ -71,7 +73,7 @@ public <Psi extends PsiElement> List<Psi> getAugments(@NotNull PsiElement elemen
}

@NotNull
@Override
// @Override
public <Psi extends PsiElement> List<Psi> getAugments(@NotNull PsiElement element,
@NotNull final Class<Psi> type,
@Nullable String nameHint) {
Expand All @@ -90,14 +92,63 @@ public <Psi extends PsiElement> List<Psi> getAugments(@NotNull PsiElement elemen
return emptyResult;
}

// All invoker of AugmentProvider already make caching
// and we want to try to skip recursive calls completely
final LombokValueProvider<Psi> result;
if (type == PsiField.class) {
result = new FieldLombokProvider<>(type, psiClass, nameHint);
} else if (type == PsiMethod.class) {
result = new MethodLombokProvider<>(type, psiClass, nameHint);
} else {
result = new ClassLombokProvider<>(type, psiClass, nameHint);
}

/// final String message = String.format("Process call for type: %s class: %s", type.getSimpleName(), psiClass.getQualifiedName());
// log.info(">>>" + message);
final List<Psi> result = getPsis(psiClass, type, nameHint);
// log.info("<<<" + message);
return result;
final List<Psi> computed = result.compute();
return null != computed ? computed : emptyResult;
}

private static class FieldLombokProvider<Psi extends PsiElement> extends LombokValueProvider<Psi> {
private static final RecursionGuard<PsiClass> ourGuard = RecursionManager.createGuard("lombok.augment.field");

FieldLombokProvider(Class<Psi> type, PsiClass psiClass, String nameHint) {
super(type, psiClass, ourGuard, nameHint);
}
}

private static class MethodLombokProvider<Psi extends PsiElement> extends LombokValueProvider<Psi> {
private static final RecursionGuard<PsiClass> ourGuard = RecursionManager.createGuard("lombok.augment.method");

MethodLombokProvider(Class<Psi> type, PsiClass psiClass, String nameHint) {
super(type, psiClass, ourGuard, nameHint);
}
}

private static class ClassLombokProvider<Psi extends PsiElement> extends LombokValueProvider<Psi> {
private static final RecursionGuard<PsiClass> ourGuard = RecursionManager.createGuard("lombok.augment.class");

ClassLombokProvider(Class<Psi> type, PsiClass psiClass, String nameHint) {
super(type, psiClass, ourGuard, nameHint);
}
}

private abstract static class LombokValueProvider<Psi extends PsiElement> {
private final Class<Psi> type;
private final PsiClass psiClass;
private final RecursionGuard<PsiClass> recursionGuard;
private final String nameHint;

LombokValueProvider(Class<Psi> type, PsiClass psiClass, RecursionGuard<PsiClass> recursionGuard, String nameHint) {
this.type = type;
this.psiClass = psiClass;
this.recursionGuard = recursionGuard;
this.nameHint = nameHint;
}

public List<Psi> compute() {
return recursionGuard.doPreventingRecursion(psiClass, true, this::computeIntern);
}

private List<Psi> computeIntern() {
return getPsis(psiClass, type, nameHint);
}
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public JComponent createComponent() {

private void initFromSettings() {
myEnableExtensionMethodSupport.setSelected(ProjectSettings.isEnabled(myProject, ProjectSettings.IS_EXTENSION_METHOD_ENABLED, false));

myEnableLombokVersionWarning.setSelected(ProjectSettings.isEnabled(myProject, ProjectSettings.IS_LOMBOK_VERSION_CHECK_ENABLED, false));
}

Expand All @@ -59,8 +58,6 @@ public void apply() {
ProjectSettings.setEnabled(myProject, ProjectSettings.IS_LOMBOK_VERSION_CHECK_ENABLED, myEnableLombokVersionWarning.isSelected());

// Redo code checking and highlighting.
LombokAugmentProvider.onConfigChange();

PsiManager.getInstance(myProject).dropPsiCaches();
DaemonCodeAnalyzer.getInstance(myProject).restart();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public final class LombokProcessorUtil {
@NonNls
private static final String ACCESS_LEVEL_NONE = "NONE";

private static final Map<Integer, String> ACCESS_LEVEL_MAP = new HashMap<>() {{
private static final Map<Integer, String> ACCESS_LEVEL_MAP = new HashMap<Integer, String>() {{
put(PsiUtil.ACCESS_LEVEL_PUBLIC, ACCESS_LEVEL_PUBLIC);
put(PsiUtil.ACCESS_LEVEL_PACKAGE_LOCAL, ACCESS_LEVEL_PACKAGE_LOCAL);
put(PsiUtil.ACCESS_LEVEL_PROTECTED, ACCESS_LEVEL_PROTECTED);
put(PsiUtil.ACCESS_LEVEL_PRIVATE, ACCESS_LEVEL_PRIVATE);
}};

private static final Map<String, String> VALUE_ACCESS_LEVEL_MAP = new HashMap<>() {{
private static final Map<String, String> VALUE_ACCESS_LEVEL_MAP = new HashMap<String, String>() {{
put(ACCESS_LEVEL_PUBLIC, PsiModifier.PUBLIC);
put(ACCESS_LEVEL_PACKAGE_LOCAL, PsiModifier.PACKAGE_LOCAL);
put(ACCESS_LEVEL_PROTECTED, PsiModifier.PROTECTED);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,107 @@
package de.plushnikov.intellij.plugin.util;

import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiJavaCodeReferenceElement;
import com.intellij.psi.PsiModifierList;
import com.intellij.psi.PsiModifierListOwner;
import com.intellij.psi.*;
import com.intellij.psi.impl.source.SourceJavaCodeReference;
import com.intellij.util.ArrayUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.Objects;
import java.util.regex.Pattern;
import java.util.stream.Stream;

public class PsiAnnotationSearchUtil {
public final class PsiAnnotationSearchUtil {

@Nullable
public static PsiAnnotation findAnnotation(@NotNull PsiModifierListOwner psiModifierListOwner, @NotNull String annotationFQN) {
return psiModifierListOwner.getAnnotation(annotationFQN);
return findAnnotationQuick(psiModifierListOwner.getModifierList(), annotationFQN);
}

@Nullable
public static PsiAnnotation findAnnotation(@NotNull PsiModifierListOwner psiModifierListOwner, @NotNull String... annotationFQNs) {
return Stream.of(annotationFQNs).map(psiModifierListOwner::getAnnotation).filter(Objects::nonNull).findAny().orElse(null);
return findAnnotationQuick(psiModifierListOwner.getModifierList(), annotationFQNs);
}

public static boolean isAnnotatedWith(@NotNull PsiModifierListOwner psiModifierListOwner, @NotNull String annotationFQN) {
return psiModifierListOwner.hasAnnotation(annotationFQN);
@Nullable
private static PsiAnnotation findAnnotationQuick(@Nullable PsiAnnotationOwner annotationOwner, @NotNull String qualifiedName) {
if (annotationOwner == null) {
return null;
}

PsiAnnotation[] annotations = annotationOwner.getAnnotations();
if (annotations.length == 0) {
return null;
}

final String shortName = StringUtil.getShortName(qualifiedName);

for (PsiAnnotation annotation : annotations) {
PsiJavaCodeReferenceElement referenceElement = annotation.getNameReferenceElement();
if (null != referenceElement) {
final String referenceName = referenceElement.getReferenceName();
if (shortName.equals(referenceName)) {

if (referenceElement.isQualified() && referenceElement instanceof SourceJavaCodeReference) {
String possibleFullQualifiedName = ((SourceJavaCodeReference) referenceElement).getClassNameText();
if (qualifiedName.equals(possibleFullQualifiedName)) {
return annotation;
}
}

final String annotationQualifiedName = annotation.getQualifiedName();
if (null != annotationQualifiedName && qualifiedName.endsWith(annotationQualifiedName)) {
return annotation;
}
}
}
}

return null;
}

@Nullable
private static PsiAnnotation findAnnotationQuick(@Nullable PsiAnnotationOwner annotationOwner, @NotNull String... qualifiedNames) {
if (annotationOwner == null || qualifiedNames.length == 0) {
return null;
}

PsiAnnotation[] annotations = annotationOwner.getAnnotations();
if (annotations.length == 0) {
return null;
}

final String[] shortNames = new String[qualifiedNames.length];
for (int i = 0; i < qualifiedNames.length; i++) {
shortNames[i] = StringUtil.getShortName(qualifiedNames[i]);
}

for (PsiAnnotation annotation : annotations) {
final PsiJavaCodeReferenceElement referenceElement = annotation.getNameReferenceElement();
if (null != referenceElement) {
final String referenceName = referenceElement.getReferenceName();
if (ArrayUtil.find(shortNames, referenceName) > -1) {

if (referenceElement.isQualified() && referenceElement instanceof SourceJavaCodeReference) {
final String possibleFullQualifiedName = ((SourceJavaCodeReference) referenceElement).getClassNameText();

if (ArrayUtil.find(qualifiedNames, possibleFullQualifiedName) > -1) {
return annotation;
}
}

final String annotationQualifiedName = annotation.getQualifiedName();
if (ArrayUtil.find(qualifiedNames, annotationQualifiedName) > -1) {
return annotation;
}
}
}
}

return null;
}

public static boolean isAnnotatedWith(@NotNull PsiModifierListOwner psiModifierListOwner, @NotNull String annotationTypeName) {
return null != findAnnotation(psiModifierListOwner, annotationTypeName);
}

public static boolean isNotAnnotatedWith(@NotNull PsiModifierListOwner psiModifierListOwner, String annotationTypeName) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.plushnikov.intellij.plugin;

import com.intellij.openapi.util.RecursionManager;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
Expand Down Expand Up @@ -35,6 +36,9 @@ public void setUp() throws Exception {
super.setUp();

loadLombokLibrary();

//TODO disable assertions for the moment
RecursionManager.disableMissedCacheAssertions(myFixture.getProjectDisposable());
}

protected void loadLombokLibrary() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.plushnikov.intellij.plugin.inspection;

import com.intellij.openapi.util.RecursionManager;
import com.intellij.openapi.util.registry.Registry;
import com.intellij.testFramework.LightProjectDescriptor;
import com.siyeh.ig.LightJavaInspectionTestCase;
Expand All @@ -16,6 +17,9 @@ public void setUp() throws Exception {
LombokTestUtil.loadLombokLibrary(myFixture.getProjectDisposable(), getModule());

Registry.get("platform.random.idempotence.check.rate").setValue(1, getTestRootDisposable());

//TODO disable assertions for the moment
RecursionManager.disableMissedCacheAssertions(myFixture.getProjectDisposable());
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,26 @@
public class SingularPerformanceIssueDemo {
@Singular
private Collection<java.lang.String> number_0_strings;

@Singular
private Collection<java.lang.String> number_1_strings;
@Singular
private Collection<java.lang.String> number_2_strings;
@Singular
private Collection<java.lang.String> number_3_strings;
@Singular
private Collection<java.lang.String> number_4_strings;
@Singular
private Collection<java.lang.String> number_5_strings;
@Singular
private Collection<java.lang.String> number_6_strings;
@Singular
private Collection<java.lang.String> number_7_strings;
@Singular
private Collection<java.lang.String> number_8_strings;
@Singular
private Collection<java.lang.String> number_9_strings;
@Singular
private Collection<java.lang.String> number_10_strings;

public static void main(String[] args) {
for (int i = 0; i < 20; i++) {
Expand Down

0 comments on commit b484fdc

Please sign in to comment.