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

Commit

Permalink
Try to prevent recursion calls for issue #17 and issue #34
Browse files Browse the repository at this point in the history
  • Loading branch information
Michail Plushnikov committed Mar 10, 2014
1 parent d8ffcce commit 88881ce
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 12 deletions.
3 changes: 0 additions & 3 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package de.plushnikov.intellij.plugin.provider;

import com.intellij.psi.PsiElement;

public class AugmentCallData {
private final PsiElement element;
private final Class type;

public <Psi extends PsiElement> AugmentCallData(PsiElement element, Class<Psi> type) {
this.element = element;
this.type = type;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

AugmentCallData that = (AugmentCallData) o;

return element.equals(that.element) && type.equals(that.type);
}

@Override
public int hashCode() {
int result = element.hashCode();
result = 31 * result + type.hashCode();
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Provides support for lombok generated elements
Expand All @@ -37,6 +39,13 @@ public class LombokAugmentProvider extends PsiAugmentProvider {
private static final Logger log = Logger.getInstance(LombokAugmentProvider.class.getName());
private static final String LOMBOK_PREFIX_MARKER = "lombok.";

private final static ThreadLocal<Set<AugmentCallData>> recursionBreaker = new ThreadLocal<Set<AugmentCallData>>() {
@Override
protected Set<AugmentCallData> initialValue() {
return new HashSet<AugmentCallData>();
}
};

public LombokAugmentProvider() {
log.debug("LombokAugmentProvider created");
}
Expand All @@ -54,6 +63,13 @@ public <Psi extends PsiElement> List<Psi> getAugments(@NotNull PsiElement elemen
if (!(type.isAssignableFrom(PsiMethod.class) || type.isAssignableFrom(PsiField.class) || type.isAssignableFrom(PsiClass.class))) {
return emptyResult;
}

final AugmentCallData currentAugmentData = new AugmentCallData(element, type);
if (recursionBreaker.get().contains(currentAugmentData)) {
log.debug("Prevented recursion call");
return emptyResult;
}

// skip processing during index rebuild
final Project project = element.getProject();
if (DumbService.getInstance(project).isDumb()) {
Expand All @@ -64,17 +80,22 @@ public <Psi extends PsiElement> List<Psi> getAugments(@NotNull PsiElement elemen
return emptyResult;
}

final PsiClass psiClass = (PsiClass) element;
recursionBreaker.get().add(currentAugmentData);
try {
final PsiClass psiClass = (PsiClass) element;

final boolean isLombokPresent = UserMapKeys.isLombokPossiblePresent(element) || checkImportSection(psiClass);
if (!isLombokPresent) {
if (log.isDebugEnabled()) {
log.debug(String.format("Skipped call for type: %s class: %s", type, psiClass.getQualifiedName()));
final boolean isLombokPresent = UserMapKeys.isLombokPossiblePresent(element) || checkImportSection(psiClass);
if (!isLombokPresent) {
if (log.isDebugEnabled()) {
log.debug(String.format("Skipped call for type: %s class: %s", type, psiClass.getQualifiedName()));
}
return emptyResult;
}
return emptyResult;
}

return process(type, project, psiClass);
return process(type, project, psiClass);
} finally {
recursionBreaker.get().remove(currentAugmentData);
}
}

private <Psi extends PsiElement> List<Psi> process(@NotNull Class<Psi> type, @NotNull Project project, @NotNull PsiClass psiClass) {
Expand Down
3 changes: 2 additions & 1 deletion lombok-plugin/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@
<ul>
<li>0.8.3
<ol>
<li>TODO...</li>
<li>Fixed #17: Cyclic parent child relation</li>
<li>Fixed #34: Lombok plugin crashes</li>
</ol>
</li>
<li>0.8.2
Expand Down

0 comments on commit 88881ce

Please sign in to comment.