Skip to content

Instantly share code, notes, and snippets.

Jake Wharton JakeWharton

Block or report user

Report or block JakeWharton

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@JakeWharton
JakeWharton / Java6.java
Last active Nov 15, 2019
A comparison between non-capturing and capturing expressions across Java 6, Java 8, Java 8 with Retrolambda, Kotlin with native function expressions, and Kotlin with Java SAM expression.
View Java6.java
import java.util.Arrays;
class NonCapturing {
public static void main(String... args) {
run(new Runnable() {
@Override public void run() {
System.out.println("Hey!");
}
});
}
@JakeWharton
JakeWharton / visibilities.sh
Last active May 24, 2019
A bash script to count the visibility modifiers in a jar file.
View visibilities.sh
#!/bin/bash
if [ $# != 1 ]; then
echo "Pass jar as sole argument"
exit 1
fi
tmp=`mktemp -d`
unzip -d "$tmp" $1 1> /dev/null
@JakeWharton
JakeWharton / GenericCovariants.java
Created Jan 7, 2016
Unlike synthetic accessor methods, these synthetic covariant methods are hard or impossible to kill. Generics anyone?
View GenericCovariants.java
interface Thing<T> {
T thing();
}
class CharSequenceThing implements Thing<CharSequence> {
@Override public CharSequence thing() {
return "CharSequence!";
}
}
@JakeWharton
JakeWharton / CovariantReturnTypes.java
Created Jan 7, 2016
Covariant return types generate an extra method in bytecode. This compounds in each subclass further specializing the type.
View CovariantReturnTypes.java
interface Thing {
Object thing();
}
class CharSequenceThing implements Thing {
@Override public CharSequence thing() {
return "CharSequence!";
}
}
@JakeWharton
JakeWharton / Oauth1SigningInterceptor.java
Last active Nov 13, 2019
An OkHttp interceptor which does OAuth1 signing. Requires Guava and Java 8, although those dependencies wouldn't be too hard to break if you didn't have them.
View Oauth1SigningInterceptor.java
/*
* Copyright (C) 2015 Jake Wharton
*
* Licensed 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
@JakeWharton
JakeWharton / ShampooRule.java
Last active Jul 8, 2019
Got flaky tests? Shampoo them away with a quick JUnit rule. Apache 2.
View ShampooRule.java
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
/** Got flaky tests? Shampoo them away. */
public final class ShampooRule implements TestRule {
private final int iterations;
public ShampooRule(int iterations) {
if (iterations < 1) throw new IllegalArgumentException("iterations < 1: " + iterations);
@JakeWharton
JakeWharton / build.gradle
Created Mar 29, 2015
A Gradle task for installing all application variants at once. Placed in the public domain.
View build.gradle
def installAll = tasks.create('installAll')
installAll.description = 'Install all applications.'
android.applicationVariants.all { variant ->
installAll.dependsOn(variant.install)
// Ensure we end up in the same group as the other install tasks.
installAll.group = variant.install.group
}
@JakeWharton
JakeWharton / UiThreadRule.java
Last active Nov 23, 2018
A JUnit @rule which runs individual tests on the Android UI thread. http://b.android.com/157356
View UiThreadRule.java
/*
* Copyright (C) 2015 Jake Wharton
*
* Licensed 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
@JakeWharton
JakeWharton / gist:f50f3b4d87e57d8e96e9
Created Feb 7, 2015
Rise and Shine™, unlock and wake up your device automatically when you deploy from the IDE. Put this somewhere in your `src/debug/` code and run it when the application or main activity starts. Apache 2.
View gist:f50f3b4d87e57d8e96e9
/**
* Show the activity over the lockscreen and wake up the device. If you launched the app manually
* both of these conditions are already true. If you deployed from the IDE, however, this will
* save you from hundreds of power button presses and pattern swiping per day!
*/
public static void riseAndShine(Activity activity) {
activity.getWindow().addFlags(FLAG_SHOW_WHEN_LOCKED);
PowerManager power = (PowerManager) activity.getSystemService(POWER_SERVICE);
PowerManager.WakeLock lock =
@JakeWharton
JakeWharton / README.md
Last active Aug 26, 2019
A JUnit @rule which launches an activity when your test starts. Stop extending gross ActivityInstrumentationBarfCase2!
View README.md
You can’t perform that action at this time.