Skip to content

Instantly share code, notes, and snippets.

@christoph-frick
Last active August 29, 2015 14:06
Show Gist options
  • Save christoph-frick/118857c176c3f06efa16 to your computer and use it in GitHub Desktop.
Save christoph-frick/118857c176c3f06efa16 to your computer and use it in GitHub Desktop.
Disassembly of groovy's generated code for @singleton
/* groovy */
import groovy.transform.CompileStatic
@CompileStatic
@Singleton
class RegularSingleton {
void foo() { println 'foo' }
}
@CompileStatic
@Singleton(lazy=true)
class LazySingleton {
void bar() { println 'bar' }
}
/* disassembly of RegularSingleton */
import groovy.lang.GroovyObject;
import groovy.lang.MetaClass;
import org.codehaus.groovy.runtime.DefaultGroovyMethods;
public class RegularSingleton
implements GroovyObject
{
public static final RegularSingleton instance;
private RegularSingleton()
{
RegularSingleton this;
MetaClass localMetaClass = $getStaticMetaClass();
this.metaClass = localMetaClass;
if ((instance != null ? 1 : 0) != 0) {
throw ((Throwable)new RuntimeException("Can't instantiate singleton RegularSingleton. Use RegularSingleton.instance"));
}
}
public void foo()
{
DefaultGroovyMethods.println(this, "foo");null;
}
public static RegularSingleton getInstance()
{
return instance;
return null;
}
static
{
__$swapInit();
Long localLong1 = Long.valueOf(0L);
__timeStamp__239_neverHappen1411637903931 = localLong1.longValue();
Long localLong2 = Long.valueOf(1411637903931L);
__timeStamp = localLong2.longValue();
RegularSingleton localRegularSingleton = new RegularSingleton();
instance = localRegularSingleton;
}
}
/* disassembly of LazySingleton */
import groovy.lang.GroovyObject;
import groovy.lang.MetaClass;
import org.codehaus.groovy.runtime.BytecodeInterface8;
import org.codehaus.groovy.runtime.ScriptBytecodeAdapter;
import org.codehaus.groovy.runtime.callsite.CallSite;
public class LazySingleton
implements GroovyObject
{
private static volatile LazySingleton instance;
private LazySingleton()
{
LazySingleton this;
CallSite[] arrayOfCallSite = $getCallSiteArray();
MetaClass localMetaClass = $getStaticMetaClass();
this.metaClass = localMetaClass;
if ((!BytecodeInterface8.isOrigZ()) || (BytecodeInterface8.disabledStandardMetaClass()))
{
if (ScriptBytecodeAdapter.compareNotEqual(instance, null)) {
throw ((Throwable)arrayOfCallSite[0].callConstructor(RuntimeException.class, "Can't instantiate singleton LazySingleton. Use LazySingleton.instance"));
}
}
else if (ScriptBytecodeAdapter.compareNotEqual(instance, null)) {
throw ((Throwable)arrayOfCallSite[1].callConstructor(RuntimeException.class, "Can't instantiate singleton LazySingleton. Use LazySingleton.instance"));
}
}
public void bar()
{
CallSite[] arrayOfCallSite = $getCallSiteArray();arrayOfCallSite[2].callCurrent(this, "bar");
}
public static LazySingleton getInstance()
{
CallSite[] arrayOfCallSite = $getCallSiteArray();
if ((!BytecodeInterface8.isOrigZ()) || (__$stMC) || (BytecodeInterface8.disabledStandardMetaClass()))
{
if (ScriptBytecodeAdapter.compareNotEqual(instance, null)) {
return instance;
} else {
synchronized (LazySingleton.class)
{
if (ScriptBytecodeAdapter.compareNotEqual(instance, null))
{
LazySingleton localLazySingleton2 = instance;
return localLazySingleton2;
}
else
{
Object localObject1 = arrayOfCallSite[3].callConstructor(LazySingleton.class);
instance = (LazySingleton)ScriptBytecodeAdapter.castToType(localObject1, LazySingleton.class);
LazySingleton localLazySingleton3 = (LazySingleton)ScriptBytecodeAdapter.castToType(localObject1, LazySingleton.class);
return localLazySingleton3;
}
}
}
}
else if (ScriptBytecodeAdapter.compareNotEqual(instance, null)) {
return instance;
} else {
synchronized (LazySingleton.class)
{
if (ScriptBytecodeAdapter.compareNotEqual(instance, null))
{
LazySingleton localLazySingleton5 = instance;
return localLazySingleton5;
}
else
{
Object localObject2 = arrayOfCallSite[4].callConstructor(LazySingleton.class);
instance = (LazySingleton)ScriptBytecodeAdapter.castToType(localObject2, LazySingleton.class);
LazySingleton localLazySingleton6 = (LazySingleton)ScriptBytecodeAdapter.castToType(localObject2, LazySingleton.class);
return localLazySingleton6;
}
}
}
return null;
}
static
{
__$swapInit();
Long localLong1 = Long.valueOf(0L);
__timeStamp__239_neverHappen1411637853476 = localLong1.longValue();
Long localLong2 = Long.valueOf(1411637853476L);
__timeStamp = localLong2.longValue();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment