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 / JavaListCaller.java
Created May 17, 2020 22:59
explicit cast and can result in runtime exception
String str = (String)list.get(1); // needs explicit cast, throws exception
@yenerm
yenerm / JavaListGenerics.java
Created May 17, 2020 22:57
Java List <String>
List<String> list = new ArrayList<>();
list.add("First String");
list.add(6); // Compile error
String str = list.get(0); // no cast needed
@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
@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 / 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: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 / 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 / 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 / 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: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