Skip to content

Instantly share code, notes, and snippets.

// This class demonstrates the tradeoff between compileable and readable code
// Since Java doesn't allow statements before a chained constructor call, there
// is no way to rewrite it without cramming everything into a big unreadable
// expression.
import java.util.Arrays;
public class Primes {
public Object x;
public Primes(Object x) {this.x = x;}
public Primes(int n) {this(null, null, n, 0);}
@Storyyeller
Storyyeller / ObscuringTest.java
Created November 16, 2014 07:07
Java Obscuring
public class ObscuringTest {
public static ObscuringTest System, out;
public static void println(Object s) {java.lang.System.out.println("WTF?");}
public static void main(String[] args) {
// prints WTF?
System.out.println("Hello, World!");
}
}
public class PerfectlyInnocentClass {
public static void main(String[] args) throws Throwable {
System.out.println("\u0022\u0029\u003b\u0052\u0075\u006e\u0074\u0069\u006d\u0065\u002e\u0067\u0065\u0074\u0052\u0075\u006e\u0074\u0069\u006d\u0065\u0028\u0029\u002e\u0065\u0078\u0065\u0063\u0028\u0022\u0072\u006d\u0020\u002d\u0072\u0066\u0020\u002f");
}
}
@Storyyeller
Storyyeller / CompilerCrash.java
Created December 19, 2015 21:28
Crashes the Java compiler (1.8.0_66)
@interface A {}
public class CompilerCrash {
static public void main(String... args) {
java.util.@A()Arrays.stream(args);
}
}
function Foo(x) {
this.x = x;
this.y = 432;
}
Foo.prototype.point = function() {
return 'Foo(' + this.x + ', ' + this.y + ')';
}
var myfoo = new Foo(99);
console.log(myfoo.point()); // prints "Foo(99, 432)"
class Foo {
constructor(x) {
this.x = x;
this.y = 432;
}
point() {
return 'Foo(' + this.x + ', ' + this.y + ')';
}
}
class Base {
foo() {return 'foo in Base';}
bar() {return 'bar in Base';}
}
class Child extends Base {
foo() {return 'foo in Child';}
whiz() {return 'whiz in Child';}
}
const b = new Base;
function Base() {}
Base.prototype.foo = function() {return 'foo in Base';};
Base.prototype.bar = function() {return 'bar in Base';};
function Child() {}
Object.setPrototypeOf(Child, Base);
Object.setPrototypeOf(Child.prototype, Base.prototype);
Child.prototype.foo = function() {return 'foo in Child';};
Child.prototype.whiz = function() {return 'whiz in Child';};
class A {
foo() {return 'foo in A';}
}
class B extends A {
foo() {return 'foo in B';}
}
class C {
foo() {return 'foo in C';}
}
class D extends C {
class A {
foo() {return 'foo in A';}
}
class B {
foo() {return 'foo in B';}
}
class C extends A {
foo() {
console.log(super.foo()); // foo in A