Skip to content

Instantly share code, notes, and snippets.

View yenerm's full-sized avatar

Murat Yener yenerm

  • Google
  • San Francisco
View GitHub Profile
@yenerm
yenerm / SomeClass.kt
Last active April 22, 2020 04:27
companion object
<!-- Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
class SomeClass {
//…
companion object {
private var count: Int = 0
fun count() {
count++
}
@yenerm
yenerm / AnotherClass.java
Last active April 22, 2020 04:25
Companion object decompiled to Java
<!-- Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
public final class AnotherClass {
private static int count;
public static final AnotherClass.Counter Counter = new AnotherClass.Counter((DefaultConstructorMarker)null);
public static final class Counter {
public final void count() {
AnotherClass.count = AnotherClass.count + 1;
@yenerm
yenerm / Anonymous.kt
Created April 17, 2020 19:36
anonymous object
<!-- Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
val tempValues = object : {
var value = 2
var anotherValue = 3
var someOtherValue = 4
}
tempValues.value += tempValues.anotherValue
@yenerm
yenerm / Anonymous.java
Created April 17, 2020 19:37
anonymous object decompiled to java
<!-- Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
<undefinedtype> tempValues = new Object() {
private int value = 2;
private int anotherValue = 3;
private int someOtherValue = 4;
// getters and setters for x, y, z
//...
@yenerm
yenerm / Anonymous.kt
Created April 17, 2020 19:39
anonymous class via object expression
<!-- Copyright 2019 Google LLC.
SPDX-License-Identifier: Apache-2.0 -->
val t1 = Thread(object : Runnable {
override fun run() {
//do something
}
})
t1.start()
@yenerm
yenerm / ReifiedDecompiled.java
Last active May 17, 2020 22:51
Decompiled Java
public final void call() {
float value = 123643.0F;
int $i$f$calculate = false;
KClass var5 = Reflection.getOrCreateKotlinClass(Integer.class);
Integer var10000;
if (Intrinsics.areEqual(var5, Reflection.getOrCreateKotlinClass(Float.TYPE))) {
var10000 = (Integer)value;
} else {
if (!Intrinsics.areEqual(var5, Reflection.getOrCreateKotlinClass(Integer.TYPE))) {
throw (Throwable)(new IllegalStateException("Only works with Float and Int"));
@yenerm
yenerm / Reified.kt
Created May 17, 2020 22:52
Kotlin Reified
inline fun <reified T> calculate(value: Float): T {
return when (T::class) {
Float::class -> value as T
Int::class -> value.toInt() as T
else -> throw IllegalStateException("Only works with Float and Int")
}
}
val intCall: Int = calculate(123643)
val floatCall: Float = calculate(123643)
@yenerm
yenerm / ReifiedCallDecompiled.java
Created May 17, 2020 22:53
Reified decompiled into Java code
// Inline function converted to Java from byte code
public static final void printType() {
int $i$f$printType = 0;
Intrinsics.reifiedOperationMarker(4, "T");
Class var1 = Object.class;
boolean var2 = false;
System.out.print(var1);
}
// Caller converted to Java from byte code
@yenerm
yenerm / Reified.kt
Created May 17, 2020 22:55
Inline Reified function
inline fun <reified T> printType() {
print(T::class.java)
}
fun printStringType(){
//calling the reified generic function with String type
printType<String>()
}
@yenerm
yenerm / JavaList.java
Created May 17, 2020 22:56
Java List without generics
List list = new ArrayList();
list.add("First String");
list.add(6); // No error