Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore code lenses of byte-code generated methods #828

Merged
merged 1 commit into from
Oct 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -772,8 +772,7 @@ public static URI toURI(String uriString) {
}

public static boolean isHiddenGeneratedElement(IJavaElement element) {
// generated elements are tagged with javax.annotation.Generated and
// they need to be filtered out
// generated elements are annotated with @Generated and they need to be filtered out
if (element instanceof IAnnotatable) {
try {
IAnnotation[] annotations = ((IAnnotatable) element).getAnnotations();
Expand All @@ -792,7 +791,7 @@ public static boolean isHiddenGeneratedElement(IJavaElement element) {
}

private static boolean isSilencedGeneratedAnnotation(IAnnotation annotation) throws JavaModelException {
if ("javax.annotation.Generated".equals(annotation.getElementName())) {
if ("javax.annotation.Generated".equals(annotation.getElementName()) || "javax.annotation.processing.Generated".equals(annotation.getElementName())) {
IMemberValuePair[] memberValuePairs = annotation.getMemberValuePairs();
for (IMemberValuePair m : memberValuePairs) {
if ("value".equals(m.getMemberName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;

import org.eclipse.core.resources.ResourcesPlugin;
Expand Down Expand Up @@ -56,8 +58,8 @@ public class CodeLensHandler {

private static final String JAVA_SHOW_REFERENCES_COMMAND = "java.show.references";
private static final String JAVA_SHOW_IMPLEMENTATIONS_COMMAND = "java.show.implementations";
private static final String IMPLEMENTATION_TYPE = "implementations";
private static final String REFERENCES_TYPE = "references";
public static final String IMPLEMENTATION_TYPE = "implementations";
public static final String REFERENCES_TYPE = "references";

private final PreferenceManager preferenceManager;

Expand Down Expand Up @@ -181,19 +183,19 @@ public List<CodeLens> getCodeLensSymbols(String uri, IProgressMonitor monitor) {
try {
ITypeRoot typeRoot = unit != null ? unit : classFile;
IJavaElement[] elements = typeRoot.getChildren();
ArrayList<CodeLens> lenses = new ArrayList<>(elements.length);
LinkedHashSet<CodeLens> lenses = new LinkedHashSet<>(elements.length);
collectCodeLenses(typeRoot, elements, lenses, monitor);
if (monitor.isCanceled()) {
lenses.clear();
}
return lenses;
return new ArrayList<>(lenses);
} catch (JavaModelException e) {
JavaLanguageServerPlugin.logException("Problem getting code lenses for" + unit.getElementName(), e);
}
return Collections.emptyList();
}

private void collectCodeLenses(ITypeRoot typeRoot, IJavaElement[] elements, ArrayList<CodeLens> lenses,
private void collectCodeLenses(ITypeRoot typeRoot, IJavaElement[] elements, Collection<CodeLens> lenses,
IProgressMonitor monitor)
throws JavaModelException {
for (IJavaElement element : elements) {
Expand All @@ -202,27 +204,51 @@ private void collectCodeLenses(ITypeRoot typeRoot, IJavaElement[] elements, Arra
}
if (element.getElementType() == IJavaElement.TYPE) {
collectCodeLenses(typeRoot, ((IType) element).getChildren(), lenses, monitor);
} else if (element.getElementType() != IJavaElement.METHOD || JDTUtils.isHiddenGeneratedElement(element)) {
} else if (element.getElementType() == IJavaElement.METHOD) {
if (JDTUtils.isHiddenGeneratedElement(element)) {
continue;
}
//ignore element if method range overlaps the type range, happens for generated bytcode, i.e. with lombok
IJavaElement parentType = element.getAncestor(IJavaElement.TYPE);
if (parentType != null && overlaps(((ISourceReference) parentType).getNameRange(), ((ISourceReference) element).getNameRange())) {
continue;
}
} else {//neither a type nor a method, we bail
continue;
}

if (preferenceManager.getPreferences().isReferencesCodeLensEnabled()) {
CodeLens lens = getCodeLens(REFERENCES_TYPE, element, typeRoot);
lenses.add(lens);
if (lens != null) {
lenses.add(lens);
}
}
if (preferenceManager.getPreferences().isImplementationsCodeLensEnabled() && element instanceof IType) {
IType type = (IType) element;
if (type.isInterface() || Flags.isAbstract(type.getFlags())) {
CodeLens lens = getCodeLens(IMPLEMENTATION_TYPE, element, typeRoot);
lenses.add(lens);
if (lens != null) {
lenses.add(lens);
}
}
}
}
}

private boolean overlaps(ISourceRange typeRange, ISourceRange methodRange) {
if (typeRange == null || methodRange == null) {
return false;
}
//method range is overlapping if it appears before or actually overlaps the type's range
return methodRange.getOffset() < typeRange.getOffset() || methodRange.getOffset() >= typeRange.getOffset() && methodRange.getOffset() <= (typeRange.getOffset() + typeRange.getLength());
}

private CodeLens getCodeLens(String type, IJavaElement element, ITypeRoot typeRoot) throws JavaModelException {
CodeLens lens = new CodeLens();
ISourceRange r = ((ISourceReference) element).getNameRange();
if (r == null) {
return null;
}
CodeLens lens = new CodeLens();
final Range range = JDTUtils.toRange(typeRoot, r.getOffset(), r.getLength());
lens.setRange(range);
String uri = ResourceUtils.toClientUri(JDTUtils.toUri(typeRoot));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ public void testGetCodeLensSymbols() throws Exception {
}

@Test
@SuppressWarnings("unchecked")
public void testGetCodeLensSymbolsForClass() throws Exception {
Preferences implementationsCodeLenses = Preferences.createFrom(Collections.singletonMap(Preferences.IMPLEMENTATIONS_CODE_LENS_ENABLED_KEY, "true"));
Mockito.reset(preferenceManager);
Expand All @@ -168,7 +169,11 @@ public void testGetCodeLensSymbolsForClass() throws Exception {
String uri = codeLensParams.getTextDocument().getUri();
assertFalse(uri.isEmpty());
List<CodeLens> lenses = handler.getCodeLensSymbols(uri, monitor);
assertEquals("Found " + lenses, 3, lenses.size());
assertEquals("Found " + lenses, 2, lenses.size());
List<Object> data = (List<Object>) lenses.get(0).getData();
assertTrue("Unexpected type " + data, data.contains(CodeLensHandler.REFERENCES_TYPE));
data = (List<Object>) lenses.get(1).getData();
assertTrue("Unexpected type " + data, data.contains(CodeLensHandler.IMPLEMENTATION_TYPE));
}

@Test
Expand Down