diff --git a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java index 215d5beb06..1a5e5145ff 100644 --- a/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java +++ b/org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java @@ -39,9 +39,11 @@ import org.eclipse.core.internal.resources.PreferenceInitializer; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.preferences.DefaultScope; import org.eclipse.core.runtime.preferences.IEclipsePreferences; import org.eclipse.core.runtime.preferences.InstanceScope; +import org.eclipse.jdt.core.IClasspathEntry; import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.IType; @@ -839,17 +841,17 @@ private static List getClasspathSubStringFromArtifact(String artifact) { // groupID:artifactID String[] splitIds = artifact.split(":"); if (splitIds.length != 2) { - return Collections.emptyList(); + return new ArrayList<>(); } String groupId = splitIds[0]; String artifactId = splitIds[1]; String gradleStyleClasspath = Paths.get(groupId, artifactId).toString(); String[] groupIdSplitByDot = groupId.split("\\."); if (groupIdSplitByDot.length < 1) { - return Collections.emptyList(); + return new ArrayList<>(); } String mavenStyleClasspath = Paths.get("", groupIdSplitByDot).resolve(artifactId).toString(); - return Arrays.asList(gradleStyleClasspath, mavenStyleClasspath); + return new ArrayList<>(Arrays.asList(gradleStyleClasspath, mavenStyleClasspath)); } /** @@ -2051,41 +2053,48 @@ public boolean updateAnnotationNullAnalysisOptions(IJavaProject javaProject) { } private String getAnnotationType(IJavaProject javaProject, List annotationTypes, Map> classpathStorage) { - try { - ClasspathResult result = ProjectCommand.getClasspathsFromJavaProject(javaProject, new ProjectCommand.ClasspathOptions()); - for (String annotationType : annotationTypes) { - if (classpathStorage.keySet().contains(annotationType)) { - // for known types, check the classpath to achieve a better performance - for (String classpath : result.classpaths) { - for (String classpathSubString : classpathStorage.get(annotationType)) { - if (classpath.contains(classpathSubString)) { - return annotationType; + if (!annotationTypes.isEmpty()) { + try { + ClasspathResult result = ProjectCommand.getClasspathsFromJavaProject(javaProject, new ProjectCommand.ClasspathOptions()); + for (String annotationType : annotationTypes) { + if (classpathStorage.keySet().contains(annotationType)) { + // for known types, check the classpath to achieve a better performance + for (String classpath : result.classpaths) { + IClasspathEntry classpathEntry = javaProject.getClasspathEntryFor(new Path(classpath)); + if (classpathEntry != null && classpathEntry.isTest()) { + continue; + } + for (String classpathSubString : classpathStorage.get(annotationType)) { + if (classpath.contains(classpathSubString)) { + return annotationType; + } } } - } - } else { - // for unknown types, try to find type in the project - try { - IType type = javaProject.findType(annotationType); - if (type != null) { - IJavaElement fragmentRoot = type.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); - if (fragmentRoot != null) { - String classpath = fragmentRoot.getPath().toOSString(); - if (classpathStorage.containsKey(annotationType)) { - classpathStorage.get(annotationType).add(classpath); - } else { - classpathStorage.put(annotationType, Arrays.asList(classpath)); + } else { + // for unknown types, try to find type in the project + try { + IType type = javaProject.findType(annotationType); + if (type != null) { + IJavaElement fragmentRoot = type.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); + IClasspathEntry classpathEntry = javaProject.getClasspathEntryFor(fragmentRoot.getPath()); + if (classpathEntry == null || !classpathEntry.isTest()) { + String classpath = fragmentRoot.getPath().toOSString(); + if (classpathStorage.containsKey(annotationType)) { + classpathStorage.get(annotationType).add(classpath); + } else { + classpathStorage.put(annotationType, new ArrayList<>(Arrays.asList(classpath))); + } + return annotationType; } } - return annotationType; + } catch (JavaModelException e) { + continue; } - } catch (JavaModelException e) { - continue; } } + } catch (CoreException | URISyntaxException e) { + JavaLanguageServerPlugin.logException(e); } - } catch (CoreException | URISyntaxException e) { - JavaLanguageServerPlugin.logException(e); } return null; } diff --git a/org.eclipse.jdt.ls.tests/projects/maven/null-analysis/pom.xml b/org.eclipse.jdt.ls.tests/projects/maven/null-analysis/pom.xml new file mode 100644 index 0000000000..ed56a1b8d4 --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/maven/null-analysis/pom.xml @@ -0,0 +1,31 @@ + + 4.0.0 + foo.bar + null-analysis + 0.0.1-SNAPSHOT + + + + maven-compiler-plugin + 3.8.0 + + 11 + + + + + + + org.apache.commons + commons-lang3 + 3.5 + + + com.google.code.findbugs + jsr305 + 3.0.2 + runtime + + + \ No newline at end of file diff --git a/org.eclipse.jdt.ls.tests/projects/maven/null-analysis/src/main/java/org/sample/Bar.java b/org.eclipse.jdt.ls.tests/projects/maven/null-analysis/src/main/java/org/sample/Bar.java new file mode 100644 index 0000000000..73c3dc0f11 --- /dev/null +++ b/org.eclipse.jdt.ls.tests/projects/maven/null-analysis/src/main/java/org/sample/Bar.java @@ -0,0 +1,8 @@ +package org.sample; + +/** + * This is Bar + */ +public class Bar { + +} diff --git a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/MavenProjectImporterTest.java b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/MavenProjectImporterTest.java index dcf48a654d..67135fb611 100644 --- a/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/MavenProjectImporterTest.java +++ b/org.eclipse.jdt.ls.tests/src/org/eclipse/jdt/ls/core/internal/managers/MavenProjectImporterTest.java @@ -27,6 +27,7 @@ import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Map; import org.apache.commons.io.FileUtils; import org.eclipse.core.resources.IFile; @@ -46,6 +47,7 @@ import org.eclipse.jdt.ls.core.internal.ProjectUtils; import org.eclipse.jdt.ls.core.internal.ResourceUtils; import org.eclipse.jdt.ls.core.internal.WorkspaceHelper; +import org.eclipse.jdt.ls.core.internal.handlers.BuildWorkspaceHandler; import org.eclipse.jdt.ls.core.internal.handlers.ProgressReporterManager; import org.eclipse.lsp4j.jsonrpc.CancelChecker; import org.junit.After; @@ -53,6 +55,8 @@ import org.junit.runner.RunWith; import org.mockito.junit.MockitoJUnitRunner; +import com.google.common.collect.ImmutableList; + /** * @author Fred Bricon */ @@ -363,6 +367,28 @@ public void avoidImportDuplicatedProjects() throws Exception { } } + // https://github.com/redhat-developer/vscode-java/issues/2712 + @Test + public void testNullAnalysisDisabled() throws Exception { + this.preferenceManager.getPreferences().setNonnullTypes(ImmutableList.of("javax.annotation.Nonnull", "org.eclipse.jdt.annotation.NonNull")); + this.preferenceManager.getPreferences().setNullableTypes(ImmutableList.of("org.eclipse.jdt.annotation.Nullable", "javax.annotation.Nonnull")); + try { + IProject project = importMavenProject("null-analysis"); + assertIsJavaProject(project); + if (this.preferenceManager.getPreferences().updateAnnotationNullAnalysisOptions()) { + BuildWorkspaceHandler buildWorkspaceHandler = new BuildWorkspaceHandler(JavaLanguageServerPlugin.getProjectsManager()); + buildWorkspaceHandler.buildWorkspace(true, new NullProgressMonitor()); + } + IJavaProject javaProject = JavaCore.create(project); + Map options = javaProject.getOptions(true); + assertEquals(JavaCore.DISABLED, options.get(JavaCore.COMPILER_ANNOTATION_NULL_ANALYSIS)); + } finally { + this.preferenceManager.getPreferences().setNonnullTypes(Collections.emptyList()); + this.preferenceManager.getPreferences().setNullableTypes(Collections.emptyList()); + this.preferenceManager.getPreferences().updateAnnotationNullAnalysisOptions(); + } + } + private static class MavenUpdateProjectJobSpy extends JobChangeAdapter { int updateProjectJobCalled;