Last active
June 5, 2018 07:24
-
-
Save bondolo/18d651387e8670000354 to your computer and use it in GitHub Desktop.
An investigation of inner class behaviour. Prints information about a class and it's enclosing class.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* public domain | |
* | |
* https://creativecommons.org/publicdomain/zero/1.0/ | |
* | |
*/ | |
package test; | |
import java.lang.reflect.Modifier; | |
import java.util.concurrent.Callable; | |
public class Thing implements Callable<String> { | |
public static String whoami(Class<?> clazz) { | |
return (Modifier.isStatic(clazz.getModifiers()) ? "static " : " ") + clazz.getName() + " : " + clazz.getEnclosingClass(); | |
} | |
public final static Callable<String> STATIC_ANON = new Callable<String>() { | |
@Override | |
public String call() { | |
Class<?> clazz = getClass(); | |
return whoami(clazz); | |
} | |
}; | |
public final Callable<String> INSTANCE_ANON = new Callable<String>() { | |
@Override | |
public String call() { | |
Class<?> clazz = getClass(); | |
return whoami(clazz); | |
} | |
}; | |
public final static Callable<String> STATIC_STATIC = new Static(); | |
public static class Static implements Callable<String> { | |
@Override | |
public String call() { | |
Class<?> clazz = getClass(); | |
return whoami(clazz); | |
} | |
}; | |
public final Callable<String> INSTANCE_STATIC = new Static(); | |
public final Callable<String> INSTANCE_INNER = new Inner(); | |
class Inner implements Callable<String> { | |
@Override | |
public String call() { | |
Class<?> clazz = getClass(); | |
return whoami(clazz); | |
} | |
}; | |
public final static Callable<String> ASSOCIATED_STATIC = new Associated(); | |
public final Callable<String> INSTANCE_ASSOCIATED = new Associated(); | |
public final static Callable<String> ASSOCIATED_ANON_STATIC = new Associated() { | |
@Override | |
public String call() { | |
Class<?> clazz = getClass(); | |
return whoami(clazz); | |
} | |
}; | |
public final Callable<String> INSTANCE_ANON_ASSOCIATED = new Associated() { | |
@Override | |
public String call() { | |
Class<?> clazz = getClass(); | |
return whoami(clazz); | |
} | |
}; | |
@Override | |
public String call() throws Exception { | |
Callable<String> ANONYMOUS_INSTANCE = new Callable<String>() { | |
@Override | |
public String call() { | |
Class<?> clazz = getClass(); | |
return whoami(clazz); | |
} | |
}; | |
return ANONYMOUS_INSTANCE.call(); | |
} | |
public static void main(String... args) throws Exception { | |
class Local implements Callable<String> { | |
@Override | |
public String call() { | |
Class<?> clazz = getClass(); | |
return whoami(clazz); | |
} | |
} | |
final Callable<String> LOCAL = new Local(); | |
final Callable<String> ANONYMOUS_STATIC = new Callable<String>() { | |
@Override | |
public String call() { | |
Class<?> clazz = getClass(); | |
return whoami(clazz); | |
} | |
}; | |
Thing thing = new Thing(); | |
System.out.println("Thing: " + Thing.whoami(Thing.class)); | |
System.out.println("thing: " + Thing.whoami(thing.getClass())); | |
System.out.println("STATIC_ANON: " + Thing.STATIC_ANON.call()); | |
System.out.println("INSTANCE_ANON: " + thing.INSTANCE_ANON.call()); | |
System.out.println("INSTANCE_STATIC: " + thing.INSTANCE_STATIC.call()); | |
System.out.println("INSTANCE_INNER: " + thing.INSTANCE_INNER.call()); | |
System.out.println("ASSOCIATED_STATIC: " + Thing.ASSOCIATED_STATIC.call()); | |
System.out.println("ASSOCIATED_ANON_STATIC: " + Thing.ASSOCIATED_ANON_STATIC.call()); | |
System.out.println("INSTANCE_ASSOCIATED: " + thing.INSTANCE_ASSOCIATED.call()); | |
System.out.println("INSTANCE_ANON_ASSOCIATED: " + thing.INSTANCE_ANON_ASSOCIATED.call()); | |
System.out.println("LOCAL: " + LOCAL.call()); | |
System.out.println("ANONYMOUS: " + ANONYMOUS_STATIC.call()); | |
System.out.println("ANONYMOUS_INSTANCE" + thing.call()); | |
} | |
} | |
class Associated implements Callable<String> { | |
@Override | |
public String call() { | |
Class<?> clazz = getClass(); | |
return Thing.whoami(clazz); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output of revised version is: