Search in sources :

Example 1 with ReferencedLibraries

use of org.eclipse.jdt.ls.core.internal.preferences.Preferences.ReferencedLibraries in project eclipse.jdt.ls by eclipse.

the class InvisibleProjectBuildSupportTest method testDynamicReferenceLibraries.

@Test
public void testDynamicReferenceLibraries() throws Exception {
    File projectFolder = createSourceFolderWithMissingLibs("dynamicLibDetection");
    IProject project = importRootFolder(projectFolder, "Test.java");
    List<IMarker> errors = ResourceUtils.getErrorMarkers(project);
    assertEquals("Unexpected errors " + ResourceUtils.toString(errors), 2, errors.size());
    ReferencedLibraries libraries = new ReferencedLibraries();
    libraries.getInclude().add("lib/foo.jar");
    libraries.getInclude().add("library/**/*.jar");
    libraries.getExclude().add("library/sources/**");
    libraries.getSources().put("library/bar.jar", "library/sources/bar-src.jar");
    preferenceManager.getPreferences().setReferencedLibraries(libraries);
    IJavaProject javaProject = JavaCore.create(project);
    int[] jobInvocations = new int[1];
    IJobChangeListener listener = new JobChangeAdapter() {

        @Override
        public void scheduled(IJobChangeEvent event) {
            if (event.getJob() instanceof UpdateClasspathJob) {
                jobInvocations[0] = jobInvocations[0] + 1;
            }
        }
    };
    File originBinary = new File(getSourceProjectDirectory(), "eclipse/source-attachment/foo.jar");
    File originSource = new File(getSourceProjectDirectory(), "eclipse/source-attachment/foo-sources.jar");
    File libFolder = Files.createDirectories(projectFolder.toPath().resolve("lib")).toFile();
    File libraryFolder = Files.createDirectories(projectFolder.toPath().resolve("library")).toFile();
    File sourcesFolder = Files.createDirectories(libraryFolder.toPath().resolve("sources")).toFile();
    try {
        Job.getJobManager().addJobChangeListener(listener);
        {
            // Include following jars (with detected source jar)
            // - /lib/foo.jar
            // - /lib/foo-sources.jar
            File fooBinary = new File(libFolder, "foo.jar");
            File fooSource = new File(libFolder, "foo-sources.jar");
            FileUtils.copyFile(originBinary, fooBinary);
            FileUtils.copyFile(originSource, fooSource);
            projectsManager.fileChanged(fooBinary.toURI().toString(), CHANGE_TYPE.CREATED);
            waitForBackgroundJobs();
            IClasspathEntry[] classpath = javaProject.getRawClasspath();
            Optional<IClasspathEntry> fooEntry = Arrays.stream(classpath).filter(c -> c.getPath().lastSegment().equals("foo.jar")).findFirst();
            assertTrue("Cannot find foo binary", fooEntry.isPresent());
            assertEquals("Update classpath job should have been invoked 1 times", 1, jobInvocations[0]);
            assertEquals("Unexpected classpath:\n" + JavaProjectHelper.toString(classpath), 3, classpath.length);
            assertEquals("foo.jar", fooEntry.get().getPath().lastSegment());
            assertEquals("foo-sources.jar", fooEntry.get().getSourceAttachmentPath().lastSegment());
        }
        {
            // Include following jars (with manually addding source map):
            // - /library/bar.jar
            // - /library/sources/bar-src.jar
            File barBinary = new File(libraryFolder, "bar.jar");
            File barSource = new File(sourcesFolder, "bar-src.jar");
            FileUtils.copyFile(originBinary, barBinary);
            FileUtils.copyFile(originSource, barSource);
            projectsManager.fileChanged(barBinary.toURI().toString(), CHANGE_TYPE.CREATED);
            waitForBackgroundJobs();
            IClasspathEntry[] classpath = javaProject.getRawClasspath();
            Optional<IClasspathEntry> barEntry = Arrays.stream(classpath).filter(c -> c.getPath().lastSegment().equals("bar.jar")).findFirst();
            assertTrue("Cannot find bar binary", barEntry.isPresent());
            assertEquals("Update classpath job should have been invoked 2 times", 2, jobInvocations[0]);
            assertEquals("Unexpected classpath:\n" + JavaProjectHelper.toString(classpath), 4, classpath.length);
            assertEquals("bar.jar", barEntry.get().getPath().lastSegment());
            assertEquals("bar-src.jar", barEntry.get().getSourceAttachmentPath().lastSegment());
        }
        {
            // Include following jars (will be excluded):
            // - /library/sources/exclude.jar
            // - /library/sources/exclude-sources.jar
            File excludeBinary = new File(sourcesFolder, "exclude.jar");
            File excludeSource = new File(sourcesFolder, "exclude-sources.jar");
            FileUtils.copyFile(originBinary, excludeBinary);
            FileUtils.copyFile(originSource, excludeSource);
            // won't send update request
            projectsManager.fileChanged(excludeBinary.toURI().toString(), CHANGE_TYPE.CREATED);
            waitForBackgroundJobs();
            assertEquals("Update classpath job should still have been invoked 2 times", 2, jobInvocations[0]);
            assertEquals("Classpath length should still be 4", 4, javaProject.getRawClasspath().length);
        }
    } finally {
        Job.getJobManager().removeJobChangeListener(listener);
    }
}
Also used : ReferencedLibraries(org.eclipse.jdt.ls.core.internal.preferences.Preferences.ReferencedLibraries) IJavaProject(org.eclipse.jdt.core.IJavaProject) Optional(java.util.Optional) IJobChangeListener(org.eclipse.core.runtime.jobs.IJobChangeListener) JobChangeAdapter(org.eclipse.core.runtime.jobs.JobChangeAdapter) IMarker(org.eclipse.core.resources.IMarker) IJobChangeEvent(org.eclipse.core.runtime.jobs.IJobChangeEvent) File(java.io.File) IProject(org.eclipse.core.resources.IProject) Test(org.junit.Test) HoverHandlerTest(org.eclipse.jdt.ls.core.internal.handlers.HoverHandlerTest)

Example 2 with ReferencedLibraries

use of org.eclipse.jdt.ls.core.internal.preferences.Preferences.ReferencedLibraries in project eclipse.jdt.ls by eclipse.

the class InvisibleProjectBuildSupportTest method testUpdateReferencedLibraries.

@Test
public void testUpdateReferencedLibraries() throws Exception {
    IProject project = copyAndImportFolder("singlefile/simple", "src/App.java");
    NavigateToDefinitionHandler handler = new NavigateToDefinitionHandler(preferenceManager);
    String uri = ClassFileUtil.getURI(project, "App");
    TextDocumentIdentifier identifier = new TextDocumentIdentifier(uri);
    List<? extends Location> definitions = handler.definition(new TextDocumentPositionParams(identifier, new Position(0, 13)), monitor);
    // The original mylib.jar is an empty jar, so the GTD is not available
    assertEquals(0, definitions.size());
    // replace it which contains the class 'mylib.A'
    IPath projectRealPath = ProjectUtils.getProjectRealFolder(project);
    IPath newLibPath = projectRealPath.append("mylib.jar");
    IPath referencedLibraryPath = projectRealPath.append("lib/mylib.jar");
    FileUtils.copyFile(newLibPath.toFile(), referencedLibraryPath.toFile());
    List<String> include = Arrays.asList("lib/**/*.jar");
    ReferencedLibraries libraries = new ReferencedLibraries(new HashSet<>(include));
    UpdateClasspathJob.getInstance().updateClasspath(JavaCore.create(project), libraries);
    waitForBackgroundJobs();
    definitions = handler.definition(new TextDocumentPositionParams(identifier, new Position(0, 13)), monitor);
    assertEquals(1, definitions.size());
}
Also used : ReferencedLibraries(org.eclipse.jdt.ls.core.internal.preferences.Preferences.ReferencedLibraries) TextDocumentIdentifier(org.eclipse.lsp4j.TextDocumentIdentifier) NavigateToDefinitionHandler(org.eclipse.jdt.ls.core.internal.handlers.NavigateToDefinitionHandler) IPath(org.eclipse.core.runtime.IPath) Position(org.eclipse.lsp4j.Position) TextDocumentPositionParams(org.eclipse.lsp4j.TextDocumentPositionParams) IProject(org.eclipse.core.resources.IProject) Test(org.junit.Test) HoverHandlerTest(org.eclipse.jdt.ls.core.internal.handlers.HoverHandlerTest)

Example 3 with ReferencedLibraries

use of org.eclipse.jdt.ls.core.internal.preferences.Preferences.ReferencedLibraries in project eclipse.jdt.ls by eclipse.

the class InvisibleProjectBuildSupportTest method testDynamicSourceLookups.

@Test
public void testDynamicSourceLookups() throws Exception {
    IProject project = copyAndImportFolder("singlefile/downloadSources", "UsingRemark.java");
    // place remark artifact in lib folder
    File remarkFile = DependencyUtil.getArtifact("com.kotcrab.remark", "remark", "1.2.0", null);
    IPath projectRealPath = ProjectUtils.getProjectRealFolder(project);
    IPath remarkCopy = projectRealPath.append("lib/remark.jar");
    FileUtils.copyFile(remarkFile, remarkCopy.toFile());
    // update classpath
    List<String> include = Arrays.asList("lib/**/*.jar");
    ReferencedLibraries libraries = new ReferencedLibraries(new HashSet<>(include));
    UpdateClasspathJob.getInstance().updateClasspath(JavaCore.create(project), libraries);
    waitForBackgroundJobs();
    List<IMarker> errors = ResourceUtils.getErrorMarkers(project);
    assertEquals("Unexpected errors " + ResourceUtils.toString(errors), 0, errors.size());
    IJavaProject javaProject = JavaCore.create(project);
    IClasspathEntry remark = JavaProjectHelper.findJarEntry(javaProject, "remark.jar");
    assertNotNull(remark);
    // construct hover request
    URI standalone = new File(projectRealPath.toFile(), "UsingRemark.java").toURI();
    String payload = HoverHandlerTest.createHoverRequest(standalone, 2, 3);
    TextDocumentPositionParams position = getParams(payload);
    // perform hover
    HoverHandler handler = new HoverHandler(preferenceManager);
    Hover hover = handler.hover(position, monitor);
    if (hover.getContents().getLeft().size() < 2) {
        JobHelpers.waitForDownloadSourcesJobs(60000);
        waitForBackgroundJobs();
        hover = handler.hover(position, monitor);
    }
    // verify library has source attachment
    remark = JavaProjectHelper.findJarEntry(javaProject, "remark.jar");
    assertNotNull(remark.getSourceAttachmentPath());
    assertNotNull(hover);
    String javadoc = hover.getContents().getLeft().get(1).getLeft();
    assertTrue("Unexpected Javadoc:" + javadoc, javadoc.contains("The class that manages converting HTML to Markdown"));
    // add another artifact to lib folder
    File jsoupFile = DependencyUtil.getArtifact("org.jsoup", "jsoup", "1.9.2", null);
    IPath jsoupCopy = projectRealPath.append("lib/jsoup.jar");
    FileUtils.copyFile(jsoupFile, jsoupCopy.toFile());
    // update classpath
    UpdateClasspathJob.getInstance().updateClasspath(JavaCore.create(project), libraries);
    waitForBackgroundJobs();
    // perform hover
    hover = handler.hover(position, monitor);
    if (hover.getContents().getLeft().size() < 2) {
        JobHelpers.waitForDownloadSourcesJobs(60000);
        waitForBackgroundJobs();
        hover = handler.hover(position, monitor);
    }
    // verify original library source attachment persists
    assertNotNull(remark.getSourceAttachmentPath());
    assertNotNull(hover);
    javadoc = hover.getContents().getLeft().get(1).getLeft();
    assertTrue("Unexpected Javadoc:" + javadoc, javadoc.contains("The class that manages converting HTML to Markdown"));
}
Also used : ReferencedLibraries(org.eclipse.jdt.ls.core.internal.preferences.Preferences.ReferencedLibraries) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) URI(java.net.URI) HoverHandler(org.eclipse.jdt.ls.core.internal.handlers.HoverHandler) IProject(org.eclipse.core.resources.IProject) IJavaProject(org.eclipse.jdt.core.IJavaProject) Hover(org.eclipse.lsp4j.Hover) IMarker(org.eclipse.core.resources.IMarker) TextDocumentPositionParams(org.eclipse.lsp4j.TextDocumentPositionParams) File(java.io.File) Test(org.junit.Test) HoverHandlerTest(org.eclipse.jdt.ls.core.internal.handlers.HoverHandlerTest)

Example 4 with ReferencedLibraries

use of org.eclipse.jdt.ls.core.internal.preferences.Preferences.ReferencedLibraries in project eclipse.jdt.ls by eclipse.

the class InvisibleProjectBuildSupport method fileChanged.

@Override
public boolean fileChanged(IResource resource, CHANGE_TYPE changeType, IProgressMonitor monitor) throws CoreException {
    if (resource == null || !applies(resource.getProject())) {
        return false;
    }
    refresh(resource, changeType, monitor);
    String resourcePath = resource.getLocation().toOSString();
    IProject project = resource.getProject();
    IPath projectFolder = ProjectUtils.getProjectRealFolder(project);
    ReferencedLibraries libraries = JavaLanguageServerPlugin.getPreferencesManager().getPreferences().getReferencedLibraries();
    for (String pattern : libraries.getExclude()) {
        if (matchPattern(projectFolder, pattern, resourcePath)) {
            // skip if excluded
            return false;
        }
    }
    for (String pattern : libraries.getInclude()) {
        if (matchPattern(projectFolder, pattern, resourcePath)) {
            UpdateClasspathJob.getInstance().updateClasspath(JavaCore.create(project), libraries);
            // update if included in any pattern
            return false;
        }
    }
    IPath settings = projectFolder.append(ProjectUtils.SETTINGS);
    if (settings.equals(resource.getLocation())) {
        if (CHANGE_TYPE.CREATED.equals(changeType)) {
            IFolder settingsLinkFolder = project.getFolder(ProjectUtils.SETTINGS);
            if (!settingsLinkFolder.isLinked()) {
                settingsLinkFolder.createLink(resource.getLocationURI(), IResource.REPLACE, null);
                IJavaProject javaProject = JavaCore.create(project);
                JVMConfigurator.configureJVMSettings(javaProject);
            }
        } else if (CHANGE_TYPE.DELETED.equals(changeType)) {
            if (resource.isLinked()) {
                resource.delete(IResource.FORCE, monitor);
                IJavaProject javaProject = JavaCore.create(project);
                JVMConfigurator.configureJVMSettings(javaProject);
            }
        }
    }
    return false;
}
Also used : ReferencedLibraries(org.eclipse.jdt.ls.core.internal.preferences.Preferences.ReferencedLibraries) IJavaProject(org.eclipse.jdt.core.IJavaProject) IPath(org.eclipse.core.runtime.IPath) IProject(org.eclipse.core.resources.IProject) IFolder(org.eclipse.core.resources.IFolder)

Example 5 with ReferencedLibraries

use of org.eclipse.jdt.ls.core.internal.preferences.Preferences.ReferencedLibraries in project eclipse.jdt.ls by eclipse.

the class InvisibleProjectBuildSupportTest method testImportReferencedLibrariesConfiguration.

@Test
@SuppressWarnings("serial")
public void testImportReferencedLibrariesConfiguration() throws Exception {
    {
        // Test import of configuration without specifying referenced libraries
        Map<String, Object> configuration = new HashMap<>();
        Preferences preferences = Preferences.createFrom(configuration);
        ReferencedLibraries libraries = Preferences.JAVA_PROJECT_REFERENCED_LIBRARIES_DEFAULT;
        assertEquals("Configuration with no corresponding field", libraries, preferences.getReferencedLibraries());
    }
    {
        // Test import of referenced libraries with a shortcut list
        List<String> include = Arrays.asList("libraries/**/*.jar");
        Map<String, Object> configuration = new HashMap<>();
        configuration.put(Preferences.JAVA_PROJECT_REFERENCED_LIBRARIES_KEY, include);
        Preferences preferences = Preferences.createFrom(configuration);
        ReferencedLibraries libraries = new ReferencedLibraries(new HashSet<>(include));
        assertEquals("Configuration with shortcut include array", libraries, preferences.getReferencedLibraries());
    }
    {
        // Test import of referenced libraries object with include
        List<String> include = Arrays.asList("libraries/**/*.jar");
        Map<String, Object> configuration = new HashMap<>();
        configuration.put(Preferences.JAVA_PROJECT_REFERENCED_LIBRARIES_KEY, new HashMap<String, Object>() {

            {
                put("include", include);
            }
        });
        Preferences preferences = Preferences.createFrom(configuration);
        ReferencedLibraries libraries = new ReferencedLibraries(new HashSet<>(include), new HashSet<>(), new HashMap<>());
        assertEquals("Configuration with include", libraries, preferences.getReferencedLibraries());
    }
    {
        // Test import of referenced libraries object with include and exclude
        List<String> include = Arrays.asList("libraries/**/*.jar");
        List<String> exclude = Arrays.asList("libraries/sources/**");
        Map<String, Object> configuration = new HashMap<>();
        configuration.put(Preferences.JAVA_PROJECT_REFERENCED_LIBRARIES_KEY, new HashMap<String, Object>() {

            {
                put("include", include);
                put("exclude", exclude);
            }
        });
        Preferences preferences = Preferences.createFrom(configuration);
        ReferencedLibraries libraries = new ReferencedLibraries(new HashSet<>(include), new HashSet<>(exclude), new HashMap<>());
        assertEquals("Configuration with include and exclude", libraries, preferences.getReferencedLibraries());
    }
    {
        // Test import of referenced libraries object with include and sources
        List<String> include = Arrays.asList("libraries/**/*.jar");
        Map<String, String> sources = new HashMap<>() {

            {
                put("libraries/foo.jar", "libraries/foo-src.jar");
            }
        };
        Map<String, Object> configuration = new HashMap<>();
        configuration.put(Preferences.JAVA_PROJECT_REFERENCED_LIBRARIES_KEY, new HashMap<String, Object>() {

            {
                put("include", include);
                put("sources", sources);
            }
        });
        Preferences preferences = Preferences.createFrom(configuration);
        ReferencedLibraries libraries = new ReferencedLibraries(new HashSet<>(include), new HashSet<>(), sources);
        assertEquals("Configuration with include and sources", libraries, preferences.getReferencedLibraries());
    }
    {
        // Test import of referenced libraries object with include, exclude and sources
        List<String> include = Arrays.asList("libraries/**/*.jar");
        List<String> exclude = Arrays.asList("libraries/sources/**");
        Map<String, String> sources = new HashMap<>() {

            {
                put("libraries/foo.jar", "libraries/foo-src.jar");
            }
        };
        Map<String, Object> configuration = new HashMap<>();
        configuration.put(Preferences.JAVA_PROJECT_REFERENCED_LIBRARIES_KEY, new HashMap<String, Object>() {

            {
                put("include", include);
                put("exclude", exclude);
                put("sources", sources);
            }
        });
        Preferences preferences = Preferences.createFrom(configuration);
        ReferencedLibraries libraries = new ReferencedLibraries(new HashSet<>(include), new HashSet<>(exclude), sources);
        assertEquals("Configuration with include, exclude and sources", libraries, preferences.getReferencedLibraries());
    }
    {
        // Test import of referenced libraries with exclude and sources
        List<String> exclude = Arrays.asList("libraries/sources/**");
        Map<String, String> sources = new HashMap<>() {

            {
                put("libraries/foo.jar", "libraries/foo-src.jar");
            }
        };
        Map<String, Object> configuration = new HashMap<>();
        configuration.put(Preferences.JAVA_PROJECT_REFERENCED_LIBRARIES_KEY, new HashMap<String, Object>() {

            {
                put("exclude", exclude);
                put("sources", sources);
            }
        });
        Preferences preferences = Preferences.createFrom(configuration);
        ReferencedLibraries libraries = new ReferencedLibraries(new HashSet<>(), new HashSet<>(exclude), sources);
        assertEquals("Configuration with exclude and sources", libraries, preferences.getReferencedLibraries());
    }
    {
        // Test import of referenced libraries with only exclude
        List<String> exclude = Arrays.asList("libraries/sources/**");
        Map<String, Object> configuration = new HashMap<>();
        configuration.put(Preferences.JAVA_PROJECT_REFERENCED_LIBRARIES_KEY, new HashMap<String, Object>() {

            {
                put("exclude", exclude);
            }
        });
        Preferences preferences = Preferences.createFrom(configuration);
        ReferencedLibraries libraries = new ReferencedLibraries(new HashSet<>(), new HashSet<>(exclude), new HashMap<>());
        assertEquals("Configuration with exclude", libraries, preferences.getReferencedLibraries());
    }
    {
        // Test import of referenced libraries with only sources
        Map<String, String> sources = new HashMap<>() {

            {
                put("libraries/foo.jar", "libraries/foo-src.jar");
            }
        };
        Map<String, Object> configuration = new HashMap<>();
        configuration.put(Preferences.JAVA_PROJECT_REFERENCED_LIBRARIES_KEY, new HashMap<String, Object>() {

            {
                put("sources", sources);
            }
        });
        Preferences preferences = Preferences.createFrom(configuration);
        ReferencedLibraries libraries = new ReferencedLibraries(new HashSet<>(), new HashSet<>(), sources);
        assertEquals("Configuration with sources", libraries, preferences.getReferencedLibraries());
    }
}
Also used : ReferencedLibraries(org.eclipse.jdt.ls.core.internal.preferences.Preferences.ReferencedLibraries) HashMap(java.util.HashMap) List(java.util.List) Preferences(org.eclipse.jdt.ls.core.internal.preferences.Preferences) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet) Test(org.junit.Test) HoverHandlerTest(org.eclipse.jdt.ls.core.internal.handlers.HoverHandlerTest)

Aggregations

ReferencedLibraries (org.eclipse.jdt.ls.core.internal.preferences.Preferences.ReferencedLibraries)6 HoverHandlerTest (org.eclipse.jdt.ls.core.internal.handlers.HoverHandlerTest)5 Test (org.junit.Test)5 IProject (org.eclipse.core.resources.IProject)4 IPath (org.eclipse.core.runtime.IPath)3 IJavaProject (org.eclipse.jdt.core.IJavaProject)3 File (java.io.File)2 IMarker (org.eclipse.core.resources.IMarker)2 TextDocumentPositionParams (org.eclipse.lsp4j.TextDocumentPositionParams)2 URI (java.net.URI)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 IFolder (org.eclipse.core.resources.IFolder)1 IJobChangeEvent (org.eclipse.core.runtime.jobs.IJobChangeEvent)1 IJobChangeListener (org.eclipse.core.runtime.jobs.IJobChangeListener)1 JobChangeAdapter (org.eclipse.core.runtime.jobs.JobChangeAdapter)1 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)1