Skip to content

Commit

Permalink
Merge pull request square#5078 from square/jwilson.0520.jvmstatic
Browse files Browse the repository at this point in the history
Don't use @JvmStatic for internal calls
  • Loading branch information
JakeWharton authored May 20, 2019
2 parents ac2618a + 2e0dfa2 commit 5d3b903
Show file tree
Hide file tree
Showing 41 changed files with 157 additions and 183 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ object DnsRecordCodec {
private const val TYPE_PTR = 0x000c
private val ASCII = StandardCharsets.US_ASCII

@JvmStatic
fun encodeQuery(host: String, type: Int): ByteString = Buffer().apply {
writeShort(0) // query id
writeShort(256) // flags with recursion
Expand All @@ -62,7 +61,6 @@ object DnsRecordCodec {
}.readByteString()

@Throws(Exception::class)
@JvmStatic
fun decodeAnswers(hostname: String, byteString: ByteString): List<InetAddress> {
val result = ArrayList<InetAddress>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class DnsRecordCodecTest {
}

private String encodeQuery(String host, int type) {
return DnsRecordCodec.encodeQuery(host, type).base64Url().replace("=", "");
return DnsRecordCodec.INSTANCE.encodeQuery(host, type).base64Url().replace("=", "");
}

@Test public void testGoogleDotComEncodingWithIPv6() {
Expand All @@ -44,30 +44,37 @@ private String encodeQuery(String host, int type) {
}

@Test public void testGoogleDotComDecodingFromCloudflare() throws Exception {
List<InetAddress> encoded = DnsRecordCodec.decodeAnswers("test.com", ByteString.decodeHex(
"00008180000100010000000006676f6f676c6503636f6d0000010001c00c00010001000000430004d83ad54e"));
List<InetAddress> encoded = DnsRecordCodec.INSTANCE.decodeAnswers("test.com",
ByteString.decodeHex("00008180000100010000000006676f6f676c6503636f6d0000010001c00c000100010"
+ "00000430004d83ad54e"));

assertThat(encoded).containsExactly(InetAddress.getByName("216.58.213.78"));
}

@Test public void testGoogleDotComDecodingFromGoogle() throws Exception {
List<InetAddress> decoded = DnsRecordCodec.decodeAnswers("test.com", ByteString.decodeHex(
"0000818000010003000000000567726170680866616365626f6f6b03636f6d0000010001c00c0005000100000a6d000603617069c012c0300005000100000cde000c04737461720463313072c012c042000100010000003b00049df00112"));
List<InetAddress> decoded = DnsRecordCodec.INSTANCE.decodeAnswers("test.com",
ByteString.decodeHex("0000818000010003000000000567726170680866616365626f6f6b03636f6d0000010"
+ "001c00c0005000100000a6d000603617069c012c0300005000100000cde000c04737461720463313072c"
+ "012c042000100010000003b00049df00112"));

assertThat(decoded).containsExactly(InetAddress.getByName("157.240.1.18"));
}

@Test public void testGoogleDotComDecodingFromGoogleIPv6() throws Exception {
List<InetAddress> decoded = DnsRecordCodec.decodeAnswers("test.com", ByteString.decodeHex(
"0000818000010003000000000567726170680866616365626f6f6b03636f6d00001c0001c00c0005000100000a1b000603617069c012c0300005000100000b1f000c04737461720463313072c012c042001c00010000003b00102a032880f0290011faceb00c00000002"));
List<InetAddress> decoded = DnsRecordCodec.INSTANCE.decodeAnswers("test.com",
ByteString.decodeHex("0000818000010003000000000567726170680866616365626f6f6b03636f6d00001c0"
+ "001c00c0005000100000a1b000603617069c012c0300005000100000b1f000c04737461720463313072c"
+ "012c042001c00010000003b00102a032880f0290011faceb00c00000002"));

assertThat(decoded).containsExactly(InetAddress.getByName("2a03:2880:f029:11:face:b00c:0:2"));
}

@Test public void testGoogleDotComDecodingNxdomainFailure() throws Exception {
try {
DnsRecordCodec.decodeAnswers("sdflkhfsdlkjdf.ee", ByteString.decodeHex(
"0000818300010000000100000e7364666c6b686673646c6b6a64660265650000010001c01b00060001000007070038026e7303746c64c01b0a686f73746d61737465720d6565737469696e7465726e6574c01b5adb12c100000e10000003840012750000000e10"));
DnsRecordCodec.INSTANCE.decodeAnswers("sdflkhfsdlkjdf.ee", ByteString.decodeHex("000081830001"
+ "0000000100000e7364666c6b686673646c6b6a64660265650000010001c01b00060001000007070038026e"
+ "7303746c64c01b0a686f73746d61737465720d6565737469696e7465726e6574c01b5adb12c100000e1000"
+ "0003840012750000000e10"));
fail();
} catch (UnknownHostException uhe) {
assertThat(uhe.getMessage()).isEqualTo("sdflkhfsdlkjdf.ee: NXDOMAIN");
Expand Down
1 change: 0 additions & 1 deletion okhttp/src/main/java/okhttp3/Cache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,6 @@ class Cache internal constructor(
fun key(url: HttpUrl): String = url.toString().encodeUtf8().md5().hex()

@Throws(IOException::class)
@JvmStatic
internal fun readInt(source: BufferedSource): Int {
try {
val result = source.readDecimalLong()
Expand Down
2 changes: 0 additions & 2 deletions okhttp/src/main/java/okhttp3/HttpUrl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1421,7 +1421,6 @@ class HttpUrl internal constructor(
* Returns the index of the ':' in `input` that is after scheme characters. Returns -1 if
* `input` does not have a scheme that starts at `pos`.
*/
@JvmStatic
private fun schemeDelimiterOffset(input: String, pos: Int, limit: Int): Int {
if (limit - pos < 2) return -1

Expand Down Expand Up @@ -1493,7 +1492,6 @@ class HttpUrl internal constructor(
}

companion object {
@JvmStatic
private val HEX_DIGITS =
charArrayOf('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F')
internal const val USERNAME_ENCODE_SET = " \"':;<=>@[]^`{}|/\\?#"
Expand Down
2 changes: 0 additions & 2 deletions okhttp/src/main/java/okhttp3/internal/Internal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ import okhttp3.internal.connection.RealConnectionPool
* packages.
*/
object Internal {
@JvmStatic
fun realConnectionPool(connectionPool: ConnectionPool): RealConnectionPool = connectionPool.delegate

@JvmStatic
fun exchange(response: Response): Exchange? = response.exchange
}
4 changes: 1 addition & 3 deletions okhttp/src/main/java/okhttp3/internal/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ object Util {
}
}

@JvmStatic fun threadFactory(
fun threadFactory(
name: String,
daemon: Boolean
): ThreadFactory = ThreadFactory { runnable ->
Expand Down Expand Up @@ -232,13 +232,11 @@ object Util {
}

/** Returns true if [host] is not a host name and might be an IP address. */
@JvmStatic
fun verifyAsIpAddress(host: String): Boolean {
return VERIFY_AS_IP_ADDRESS.matches(host)
}

/** Returns a [Locale.US] formatted [String]. */
@JvmStatic
fun format(format: String, vararg args: Any): String {
return String.format(Locale.US, format, *args)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,6 @@ class DiskLruCache internal constructor(
* @param valueCount the number of values per cache entry. Must be positive.
* @param maxSize the maximum number of bytes this cache should use to store
*/
@JvmStatic
fun create(
fileSystem: FileSystem,
directory: File,
Expand Down
2 changes: 0 additions & 2 deletions okhttp/src/main/java/okhttp3/internal/cache2/Relay.kt
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,6 @@ class Relay private constructor(
* close that when they're done. Otherwise a handle to [file] will be leaked.
*/
@Throws(IOException::class)
@JvmStatic
fun edit(
file: File,
upstream: Source,
Expand All @@ -333,7 +332,6 @@ class Relay private constructor(
* close that when they're done. Otherwise a handle to [file] will be leaked.
*/
@Throws(IOException::class)
@JvmStatic
fun read(file: File): Relay {
val randomAccessFile = RandomAccessFile(file, "rw")
val fileOperator = FileOperator(randomAccessFile.channel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,6 @@ class RealConnection(
private const val NPE_THROW_WITH_NULL = "throw with null exception"
private const val MAX_TUNNEL_ATTEMPTS = 21

@JvmStatic
fun newTestConnection(
connectionPool: RealConnectionPool,
route: Route,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ class RouteSelector(

companion object {
/** Obtain a host string containing either an actual host name or a numeric IP address. */
@JvmStatic
val InetSocketAddress.socketHost: String get() {
// The InetSocketAddress was specified with a string (either a numeric IP or a host name). If
// it is a name, all IPs for that name should be tried. If it is an IP address, only that IP
Expand Down
4 changes: 1 addition & 3 deletions okhttp/src/main/java/okhttp3/internal/http/HttpDate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import okhttp3.internal.Util.UTC
import java.text.DateFormat
import java.text.ParsePosition
import java.text.SimpleDateFormat
import java.util.Locale
import java.util.Date
import java.util.Locale

/**
* Best-effort parser for HTTP dates.
Expand Down Expand Up @@ -69,7 +69,6 @@ object HttpDate {
private val BROWSER_COMPATIBLE_DATE_FORMATS = arrayOfNulls<DateFormat>(BROWSER_COMPATIBLE_DATE_FORMAT_STRINGS.size)

/** Returns the date for [value]. Returns null if the value couldn't be parsed. */
@JvmStatic
fun parse(value: String): Date? {
if (value.isEmpty()) {
return null
Expand Down Expand Up @@ -109,7 +108,6 @@ object HttpDate {
}

/** Returns the string for [value]. */
@JvmStatic
fun format(value: Date): String {
return STANDARD_DATE_FORMAT.get().format(value)
}
Expand Down
5 changes: 0 additions & 5 deletions okhttp/src/main/java/okhttp3/internal/http/HttpMethod.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,24 @@
package okhttp3.internal.http

object HttpMethod {
@JvmStatic
fun invalidatesCache(method: String): Boolean = (method == "POST" ||
method == "PATCH" ||
method == "PUT" ||
method == "DELETE" ||
method == "MOVE") // WebDAV

@JvmStatic
fun requiresRequestBody(method: String): Boolean = (method == "POST" ||
method == "PUT" ||
method == "PATCH" ||
method == "PROPPATCH" || // WebDAV
method == "REPORT") // CalDAV/CardDAV (defined in WebDAV Versioning)

@JvmStatic
fun permitsRequestBody(method: String): Boolean = !(method == "GET" || method == "HEAD")

@JvmStatic
fun redirectsWithBody(method: String): Boolean =
// (WebDAV) redirects should also maintain the request body
method == "PROPFIND"

@JvmStatic
fun redirectsToGet(method: String): Boolean =
// All requests but PROPFIND should redirect to a GET request.
method != "PROPFIND"
Expand Down
7 changes: 2 additions & 5 deletions okhttp/src/main/java/okhttp3/internal/http/RequestLine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/
package okhttp3.internal.http

import java.net.HttpURLConnection
import java.net.Proxy
import okhttp3.HttpUrl
import okhttp3.Request
import java.net.HttpURLConnection
import java.net.Proxy

object RequestLine {

Expand All @@ -27,7 +27,6 @@ object RequestLine {
* [HttpURLConnection.getHeaderFields], so it needs to be set even if the transport is
* HTTP/2.
*/
@JvmStatic
fun get(request: Request, proxyType: Proxy.Type): String {
val result = StringBuilder()
result.append(request.method())
Expand All @@ -47,7 +46,6 @@ object RequestLine {
* Returns true if the request line should contain the full URL with host and port (like "GET
* http://android.com/foo HTTP/1.1") or only the path (like "GET /foo HTTP/1.1").
*/
@JvmStatic
private fun includeAuthorityInRequestLine(request: Request, proxyType: Proxy.Type): Boolean {
return !request.isHttps && proxyType == Proxy.Type.HTTP
}
Expand All @@ -56,7 +54,6 @@ object RequestLine {
* Returns the path to request, like the '/' in 'GET / HTTP/1.1'. Never empty, even if the request
* URL is. Includes the query component if it exists.
*/
@JvmStatic
fun requestPath(url: HttpUrl): String {
val path = url.encodedPath()
val query = url.encodedQuery()
Expand Down
2 changes: 0 additions & 2 deletions okhttp/src/main/java/okhttp3/internal/http/StatusLine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@ class StatusLine(
const val HTTP_PERM_REDIRECT = 308
const val HTTP_CONTINUE = 100

@JvmStatic
fun get(response: Response): StatusLine {
return StatusLine(response.protocol(), response.code(), response.message())
}

@JvmStatic
@Throws(IOException::class)
fun parse(statusLine: String): StatusLine {
// H T T P / 1 . 1 2 0 0 T e m p o r a r y R e d i r e c t
Expand Down
2 changes: 0 additions & 2 deletions okhttp/src/main/java/okhttp3/internal/http2/Http2.kt
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ object Http2 {
* `<< 0x0000000f 12 HEADERS END_HEADERS|END_STREAM
* ```
*/
@JvmStatic
fun frameLog(
inbound: Boolean,
streamId: Int,
Expand All @@ -125,7 +124,6 @@ object Http2 {
* in binary.
*/
// Visible for testing.
@JvmStatic
fun formatFlags(type: Int, flags: Int): String {
if (flags == 0) return ""
when (type) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,6 @@ class ConscryptPlatform private constructor() : Platform() {
}

companion object {
@JvmStatic
fun buildIfSupported(): ConscryptPlatform? = try {
// Trigger an early exception over a fatal error, prefer a RuntimeException over Error.
Class.forName("org.conscrypt.Conscrypt\$Version")
Expand All @@ -119,7 +118,6 @@ class ConscryptPlatform private constructor() : Platform() {
null
}

@JvmStatic @JvmOverloads
fun atLeastVersion(major: Int, minor: Int = 0, patch: Int = 0): Boolean {
val conscryptVersion = Conscrypt.version()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ class Jdk8WithJettyBootPlatform(
}

companion object {
@JvmStatic
fun buildIfSupported(): Platform? {
val jvmVersion = System.getProperty("java.specification.version", "unknown")
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ class Jdk9Platform(
}

companion object {
@JvmStatic
fun buildIfSupported(): Jdk9Platform? =
try {
// Find JDK 9 methods
Expand Down
4 changes: 0 additions & 4 deletions okhttp/src/main/java/okhttp3/internal/platform/Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,13 @@ open class Platform {
fun alpnProtocolNames(protocols: List<Protocol>) =
protocols.filter { it != Protocol.HTTP_1_0 }.map { it.toString() }

@JvmStatic
val isConscryptPreferred: Boolean
get() {
val preferredProvider = Security.getProviders()[0].name
return "Conscrypt" == preferredProvider
}

/** Attempt to match the host runtime to a capable Platform implementation. */
@JvmStatic
private fun findPlatform(): Platform {
val android = AndroidPlatform.buildIfSupported()

Expand Down Expand Up @@ -233,7 +231,6 @@ open class Platform {
* Returns the concatenation of 8-bit, length prefixed protocol names.
* http://tools.ietf.org/html/draft-agl-tls-nextprotoneg-04#page-4
*/
@JvmStatic
fun concatLengthPrefixed(protocols: List<Protocol>): ByteArray {
val result = Buffer()
for (protocol in alpnProtocolNames(protocols)) {
Expand All @@ -243,7 +240,6 @@ open class Platform {
return result.readByteArray()
}

@JvmStatic
fun <T> readFieldOrNull(instance: Any, fieldType: Class<T>, fieldName: String): T? {
var c: Class<*> = instance.javaClass
while (c != Any::class.java) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,10 @@ abstract class CertificateChainCleaner {
abstract fun clean(chain: List<Certificate>, hostname: String): List<Certificate>

companion object {
@JvmStatic
fun get(trustManager: X509TrustManager): CertificateChainCleaner {
return Platform.get().buildCertificateChainCleaner(trustManager)
}

@JvmStatic
fun get(vararg caCerts: X509Certificate): CertificateChainCleaner {
return BasicCertificateChainCleaner(BasicTrustRootIndex(*caCerts))
}
Expand Down
4 changes: 0 additions & 4 deletions okhttp/src/main/java/okhttp3/internal/ws/WebSocketProtocol.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ object WebSocketProtocol {
/** Used when an empty close frame was received (i.e., without a status code). */
internal const val CLOSE_NO_STATUS_CODE = 1005

@JvmStatic
fun toggleMask(cursor: Buffer.UnsafeCursor, key: ByteArray) {
var keyIndex = 0
val keyLength = key.size
Expand All @@ -120,7 +119,6 @@ object WebSocketProtocol {
} while (cursor.next() != -1)
}

@JvmStatic
fun closeCodeExceptionMessage(code: Int): String? {
return if (code < 1000 || code >= 5000) {
"Code must be in range [1000,5000): $code"
Expand All @@ -131,15 +129,13 @@ object WebSocketProtocol {
}
}

@JvmStatic
fun validateCloseCode(code: Int) {
val message = closeCodeExceptionMessage(code)
if (message != null) {
throw IllegalArgumentException(message)
}
}

@JvmStatic
fun acceptHeader(key: String): String {
return (key + ACCEPT_MAGIC).encodeUtf8().sha1().base64()
}
Expand Down
Loading

0 comments on commit 5d3b903

Please sign in to comment.