Repository navigation
Modify FileUtil.toPath to never return null
#9820
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
Open
jtulach
wants to merge
16
commits into
apache:master
Choose a base branch
from
jtulach:jtulach/FileObjectAndPath
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,318
−8
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
1e50301
More tests for existing FileUtil.toPath
jtulach 35ecd73
Testing also FileUtil.toFileObject(path)
jtulach e47e642
Renaming via Path tests
jtulach ff9af80
Use Files.probeMimeType
jtulach 9b03fd0
Running the PathTestHid on masterfs
jtulach bc3c106
Fallback implementation of Path for all FileObjects
jtulach b22a3be
Emulation of Path on writeable MemoryFileSystem
jtulach 283aee0
Adding PathTestHid to all the filesystem test suites
jtulach 4070b8c
Make sure URIs are the same for FileObject and its Path
jtulach 5018133
Lazily initialize instance of NioFsForFileSystem
jtulach 866393b
Deriving getScheme() from the FS's root URI scheme
jtulach 7965336
Fixed endWith
jtulach 5a06037
Identity, equality and hash tests
jtulach b987cdd
pollEvents implemented
jtulach 8381dbb
Implementation of Path.copy
jtulach 5319023
Documenting the toPath behavior
jtulach File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...ilesystems/src/org/netbeans/modules/openide/filesystems/declmime/NioFileTypeDetector.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.netbeans.modules.openide.filesystems.declmime; | ||
|
|
||
| import java.nio.file.Path; | ||
| import java.nio.file.spi.FileTypeDetector; | ||
|
|
||
| import org.openide.filesystems.FileObject; | ||
| import org.openide.filesystems.FileUtil; | ||
| import org.openide.util.lookup.ServiceProvider; | ||
|
|
||
| @ServiceProvider(service = FileTypeDetector.class, position = 100) | ||
| public final class NioFileTypeDetector extends FileTypeDetector { | ||
| @Override | ||
| public String probeContentType(Path path) { | ||
| FileObject fileObject = FileUtil.toFileObject(path); | ||
| return fileObject == null ? null : fileObject.getMIMEType(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
platform/openide.filesystems/src/org/openide/filesystems/NioFsForFileSystem.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, ANY KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.openide.filesystems; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.file.FileStore; | ||
| import java.nio.file.FileSystem; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.PathMatcher; | ||
| import java.nio.file.WatchService; | ||
| import java.nio.file.attribute.UserPrincipalLookupService; | ||
| import java.nio.file.spi.FileSystemProvider; | ||
| import java.util.Collections; | ||
| import java.util.Set; | ||
|
|
||
| final class NioFsForFileSystem extends FileSystem { | ||
| private final org.openide.filesystems.FileSystem fileSystem; | ||
| private final NioPathProvider provider; | ||
|
|
||
| NioFsForFileSystem(org.openide.filesystems.FileSystem fileSystem) { | ||
| this.fileSystem = fileSystem; | ||
| this.provider = new NioPathProvider(this); | ||
| } | ||
|
|
||
| org.openide.filesystems.FileSystem getFileSystem() { | ||
| return fileSystem; | ||
| } | ||
|
|
||
| @Override | ||
| public FileSystemProvider provider() { | ||
| return provider; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isOpen() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isReadOnly() { | ||
| return fileSystem.isReadOnly(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getSeparator() { | ||
| return "/"; | ||
| } | ||
|
|
||
| @Override | ||
| public Iterable<Path> getRootDirectories() { | ||
| return Collections.singletonList(NioPathForFileObject.create(fileSystem.getRoot(), this)); | ||
| } | ||
|
|
||
| @Override | ||
| public Iterable<FileStore> getFileStores() { | ||
| return Collections.emptyList(); | ||
| } | ||
|
|
||
| @Override | ||
| public Set<String> supportedFileAttributeViews() { | ||
| return Collections.singleton("basic"); | ||
| } | ||
|
|
||
| @Override | ||
| public Path getPath(String first, String... more) { | ||
| StringBuilder value = new StringBuilder(first); | ||
| for (String part : more) { | ||
| if (!value.isEmpty() && value.charAt(value.length() - 1) != '/') { | ||
| value.append('/'); | ||
| } | ||
| value.append(part); | ||
| } | ||
| String normalized = normalizedPath(value.toString()); | ||
| FileObject fo = fileSystem.findResource(normalized); | ||
| String name = normalizeName(value.toString()); | ||
| if (!value.toString().startsWith("/")) { | ||
| return NioPathForFileObject.relative(fo, this, name); | ||
| } | ||
| return NioPathForFileObject.create(fo, this, name); | ||
| } | ||
|
|
||
| private String normalizedPath(String value) { | ||
| if (value.isEmpty()) { | ||
| return ""; | ||
| } | ||
| String result = value.replace('\\', '/'); | ||
| while (result.startsWith("/")) { | ||
| result = result.substring(1); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| private String normalizeName(String value) { | ||
| String result = value.replace('\\', '/'); | ||
| while (result.startsWith("/")) { | ||
| result = result.substring(1); | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| public PathMatcher getPathMatcher(String syntaxAndPattern) { | ||
| throw new UnsupportedOperationException("PathMatcher is not supported"); | ||
| } | ||
|
|
||
| @Override | ||
| public UserPrincipalLookupService getUserPrincipalLookupService() { | ||
| throw new UnsupportedOperationException("User lookup is not supported"); | ||
| } | ||
|
|
||
| @Override | ||
| public WatchService newWatchService() throws IOException { | ||
| return new NioWatchService(); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Files.probeContentTypeloads installed detectors withServiceLoader.load(FileTypeDetector.class, ClassLoader.getSystemClassLoader())(checked in the JDK 21 sources). In unit tests the module is on the system classpath, so this works. In the running IDE,openide.filesystemslives inplatform/coreand is loaded byMainImpl.BootClassLoader, not the system classloader, so I'd expect this detector never to be found there. Also, it's registered globally, so it would be asked about everyPathin the JVM, not just the virtual ones; returningnullearly for non-NioPathForFileObjectpaths would keep it out of the way.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
openide.util.lookuporopenide.utiland ...FileTypeDetectors found in theLookup.getDefault()?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Your suggestion sounds right to me.
openide.utilandopenide.util.lookupboth havemodule.jar.dir=lib, so they are on the launcher classpath, which is whatClassLoader.getSystemClassLoader()sees. A detector registered there viaMETA-INF/serviceswould be found by the JDK. Since it would queryLookup.getDefault()on every call, it also doesn't matter that the JDK caches the list of installed detectors on first use: modules enabled later would still be reached. ThenNioFileTypeDetectoritself would only need a@ServiceProviderregistration, and it could returnnullearly for paths that aren'tNioPathForFileObjects.