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

The type javax.annotation.Nonnull cannot be resolved #2264

Merged
merged 1 commit into from
Oct 12, 2022
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 @@ -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;
Expand Down Expand Up @@ -839,17 +841,17 @@ private static List<String> getClasspathSubStringFromArtifact(String artifact) {
// groupID:artifactID
String[] splitIds = artifact.split(":");
if (splitIds.length != 2) {
return Collections.emptyList();
return new ArrayList<>();
rgrunber marked this conversation as resolved.
Show resolved Hide resolved
}
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));
}

/**
Expand Down Expand Up @@ -2051,41 +2053,48 @@ public boolean updateAnnotationNullAnalysisOptions(IJavaProject javaProject) {
}

private String getAnnotationType(IJavaProject javaProject, List<String> annotationTypes, Map<String, List<String>> 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;
}
Expand Down
31 changes: 31 additions & 0 deletions org.eclipse.jdt.ls.tests/projects/maven/null-analysis/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>foo.bar</groupId>
<artifactId>null-analysis</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.5</version>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
<scope>runtime</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.sample;

/**
* This is Bar
*/
public class Bar {

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -46,13 +47,16 @@
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;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

import com.google.common.collect.ImmutableList;

/**
* @author Fred Bricon
*/
Expand Down Expand Up @@ -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<String, String> 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;
Expand Down