Skip to content

Commit 2ea4fdf

Browse files
committed
[Fix] Get Files.AcceptAllFilter without reflection.
1 parent 2135c1b commit 2ea4fdf

2 files changed

Lines changed: 210 additions & 5 deletions

File tree

Lines changed: 207 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,207 @@
1+
/*
2+
* Copyright (c) 2020 Hai Zhang <dreaming.in.code.zh@gmail.com>
3+
* All Rights Reserved.
4+
*/
5+
6+
package me.zhanghai.android.files.provider.remote
7+
8+
import java8.nio.channels.SeekableByteChannel
9+
import java8.nio.file.AccessMode
10+
import java8.nio.file.CopyOption
11+
import java8.nio.file.DirectoryStream
12+
import java8.nio.file.FileStore
13+
import java8.nio.file.FileSystem
14+
import java8.nio.file.Files
15+
import java8.nio.file.LinkOption
16+
import java8.nio.file.OpenOption
17+
import java8.nio.file.Path
18+
import java8.nio.file.PathMatcher
19+
import java8.nio.file.WatchEvent
20+
import java8.nio.file.WatchKey
21+
import java8.nio.file.WatchService
22+
import java8.nio.file.attribute.BasicFileAttributes
23+
import java8.nio.file.attribute.FileAttribute
24+
import java8.nio.file.attribute.FileAttributeView
25+
import java8.nio.file.attribute.UserPrincipalLookupService
26+
import java8.nio.file.spi.FileSystemProvider
27+
import java.io.File
28+
import java.net.URI
29+
30+
val filesAcceptAllFilter: DirectoryStream.Filter<in Path> = run {
31+
var capturedFilter: DirectoryStream.Filter<in Path>? = null
32+
val path = object : StubPath() {
33+
override fun getFileSystem(): FileSystem =
34+
object : StubFileSystem() {
35+
override fun provider(): FileSystemProvider =
36+
object : StubFileSystemProvider() {
37+
override fun newDirectoryStream(
38+
dir: Path,
39+
filter: DirectoryStream.Filter<in Path>
40+
): DirectoryStream<Path> {
41+
capturedFilter = filter
42+
return StubDirectoryStream()
43+
}
44+
}
45+
}
46+
}
47+
Files.newDirectoryStream(path)
48+
capturedFilter!!
49+
}
50+
51+
private open class StubPath : Path {
52+
override fun toFile(): File = throw AssertionError()
53+
54+
override fun isAbsolute(): Boolean = throw AssertionError()
55+
56+
override fun getFileName(): Path = throw AssertionError()
57+
58+
override fun getName(index: Int): Path = throw AssertionError()
59+
60+
override fun subpath(beginIndex: Int, endIndex: Int): Path = throw AssertionError()
61+
62+
override fun endsWith(other: Path): Boolean = throw AssertionError()
63+
64+
override fun endsWith(other: String): Boolean = throw AssertionError()
65+
66+
override fun register(
67+
watcher: WatchService,
68+
events: Array<out WatchEvent.Kind<*>>,
69+
vararg modifiers: WatchEvent.Modifier
70+
): WatchKey = throw AssertionError()
71+
72+
override fun register(watcher: WatchService, vararg events: WatchEvent.Kind<*>): WatchKey =
73+
throw AssertionError()
74+
75+
override fun iterator(): MutableIterator<Path> = throw AssertionError()
76+
77+
override fun relativize(other: Path): Path = throw AssertionError()
78+
79+
override fun toUri(): URI = throw AssertionError()
80+
81+
override fun toRealPath(vararg options: LinkOption): Path = throw AssertionError()
82+
83+
override fun normalize(): Path = throw AssertionError()
84+
85+
override fun getParent(): Path = throw AssertionError()
86+
87+
override fun compareTo(other: Path): Int = throw AssertionError()
88+
89+
override fun getNameCount(): Int = throw AssertionError()
90+
91+
override fun startsWith(other: Path): Boolean = throw AssertionError()
92+
93+
override fun startsWith(other: String): Boolean = throw AssertionError()
94+
95+
override fun getFileSystem(): FileSystem = throw AssertionError()
96+
97+
override fun getRoot(): Path = throw AssertionError()
98+
99+
override fun resolveSibling(other: Path): Path = throw AssertionError()
100+
101+
override fun resolveSibling(other: String): Path = throw AssertionError()
102+
103+
override fun resolve(other: Path): Path = throw AssertionError()
104+
105+
override fun resolve(other: String): Path = throw AssertionError()
106+
107+
override fun toAbsolutePath(): Path = throw AssertionError()
108+
}
109+
110+
private open class StubFileSystem : FileSystem() {
111+
override fun getSeparator(): String = throw AssertionError()
112+
113+
override fun newWatchService(): WatchService = throw AssertionError()
114+
115+
override fun supportedFileAttributeViews(): MutableSet<String> = throw AssertionError()
116+
117+
override fun isReadOnly(): Boolean = throw AssertionError()
118+
119+
override fun getFileStores(): MutableIterable<FileStore> = throw AssertionError()
120+
121+
override fun getPath(first: String, vararg more: String): Path = throw AssertionError()
122+
123+
override fun provider(): FileSystemProvider = throw AssertionError()
124+
125+
override fun isOpen(): Boolean = throw AssertionError()
126+
127+
override fun getUserPrincipalLookupService(): UserPrincipalLookupService =
128+
throw AssertionError()
129+
130+
override fun close() = throw AssertionError()
131+
132+
override fun getPathMatcher(syntaxAndPattern: String): PathMatcher = throw AssertionError()
133+
134+
override fun getRootDirectories(): MutableIterable<Path> = throw AssertionError()
135+
}
136+
137+
private open class StubFileSystemProvider : FileSystemProvider() {
138+
override fun checkAccess(path: Path, vararg modes: AccessMode) = throw AssertionError()
139+
140+
override fun copy(source: Path, target: Path, vararg options: CopyOption) =
141+
throw AssertionError()
142+
143+
override fun <V : FileAttributeView> getFileAttributeView(
144+
path: Path,
145+
type: Class<V>,
146+
vararg options: LinkOption
147+
): V = throw AssertionError()
148+
149+
override fun isSameFile(path: Path, path2: Path): Boolean = throw AssertionError()
150+
151+
override fun newFileSystem(uri: URI, env: MutableMap<String, *>): FileSystem =
152+
throw AssertionError()
153+
154+
override fun getScheme(): String = throw AssertionError()
155+
156+
override fun isHidden(path: Path): Boolean = throw AssertionError()
157+
158+
override fun newDirectoryStream(
159+
dir: Path,
160+
filter: DirectoryStream.Filter<in Path>
161+
): DirectoryStream<Path> = throw AssertionError()
162+
163+
override fun newByteChannel(
164+
path: Path,
165+
options: MutableSet<out OpenOption>,
166+
vararg attrs: FileAttribute<*>
167+
): SeekableByteChannel = throw AssertionError()
168+
169+
override fun delete(path: Path) = throw AssertionError()
170+
171+
override fun <A : BasicFileAttributes> readAttributes(
172+
path: Path,
173+
type: Class<A>,
174+
vararg options: LinkOption
175+
): A = throw AssertionError()
176+
177+
override fun readAttributes(
178+
path: Path,
179+
attributes: String,
180+
vararg options: LinkOption
181+
): MutableMap<String, Any> = throw AssertionError()
182+
183+
override fun getFileSystem(uri: URI): FileSystem = throw AssertionError()
184+
185+
override fun getPath(uri: URI): Path = throw AssertionError()
186+
187+
override fun getFileStore(path: Path): FileStore = throw AssertionError()
188+
189+
override fun setAttribute(
190+
path: Path,
191+
attribute: String,
192+
value: Any,
193+
vararg options: LinkOption
194+
) = throw AssertionError()
195+
196+
override fun move(source: Path, target: Path, vararg options: CopyOption) =
197+
throw AssertionError()
198+
199+
override fun createDirectory(dir: Path, vararg attrs: FileAttribute<*>) =
200+
throw AssertionError()
201+
}
202+
203+
private open class StubDirectoryStream<T> : DirectoryStream<T> {
204+
override fun iterator(): MutableIterator<T> = throw AssertionError()
205+
206+
override fun close() = throw AssertionError()
207+
}

app/src/main/java/me/zhanghai/android/files/provider/remote/RemoteFileSystemProvider.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,9 @@ abstract class RemoteFileSystemProvider(
7777
directory: Path,
7878
filter: DirectoryStream.Filter<in Path>
7979
): DirectoryStream<Path> {
80-
val filter = when {
81-
filter is Parcelable -> filter
82-
// Allow Files.AcceptAllFilter, but make it Parcelable.
83-
filter.javaClass.enclosingClass == Files::class.java ->
84-
ParcelableAcceptAllFilter.instance
80+
val filter = when (filter) {
81+
is Parcelable -> filter
82+
filesAcceptAllFilter -> ParcelableAcceptAllFilter.instance
8583
else -> throw IllegalArgumentException("$filter is not Parcelable")
8684
}
8785
return remoteInterface.get().call { exception ->

0 commit comments

Comments
 (0)