I don't have to much experience in multi-threading. So not sure if I got right the following Java code decompiled from Kotlin.
Here is the Kotlin code:
companion object {
#Volatile private var INSTANCE: SomeDatabase? = null
fun getInstance(context: Context): SomeDatabase =
INSTANCE ?: synchronized(this) {
INSTANCE ?: buildDatabase(context).also { INSTANCE = it }
}
}
Here is the decompiled code in Java:
SomeDatabase var10000 = ((SomeDatabase.Companion)this).getINSTANCE();
if (var10000 == null) {
synchronized(this){}
SomeDatabase var4;
try {
var10000 = SomeDatabase.Companion.getINSTANCE();
if (var10000 == null) {
...
var10000 = var4;
}
return var10000;
Doesn't this mean that code is actually not synchronized because of empty block in synchronized(this){}?
If you look at the bytecode itself without decompiling it, you'll see that the synchronization happens as it should - this here is a version stripped of lots of load and store operations, line numbers, etc., but what matters is where the MONITORENTER and MONITOREXIT instructions are:
public final getInstance(Landroid/content/Context;)Lcom/example/SomeDatabase;
LDC "context"
INVOKESTATIC kotlin/jvm/internal/Intrinsics.checkParameterIsNotNull (Ljava/lang/Object;Ljava/lang/String;)V
INVOKESTATIC com/example/Foo.access$getINSTANCE$cp ()Lcom/example/SomeDatabase;
MONITORENTER
INVOKESTATIC com/example/Foo.access$getINSTANCE$cp ()Lcom/example/SomeDatabase;
INVOKESTATIC com/example/FooKt.buildDatabase (Landroid/content/Context;)Lcom/example/SomeDatabase;
INVOKESTATIC com/example/Foo.access$setINSTANCE$cp (Lcom/example/SomeDatabase;)V
MONITOREXIT
MONITOREXIT
ARETURN
The issue you're seeing is not a bug in the compiler, but rather an issue with the decompiler, which isn't particularly rare. It's quite the challenge to decompile arbitrary generated bytecode back to Java.
Related
I have a class that has a nullable variable that is written by multiple thread, something like
class A {
var s: String? = null //var accessed by multiple threads
fun doStuff() {
if (s != null) {
//not safe
}
}
}
To counter this, I usually make an immutable copy like so
class A {
var s: String? = null //var accessed by multiple threads
fun doStuff() {
val sCopy = s
if (sCopy != null) {
//safe now
}
}
}
I read about concurrency tools like volatile, synchronized etc but I'm not good at multithreading yet to know how should I handle it and I'm pretty sure there is better ways to achieve this than creating an immutable value, how would you do it?
You can simply reuse Java tooling in these cases.
For most simple cases, you would create an object (can be of any type) that will be used as the lock and use that lock for every single access to your state. There's the synchronized function which even let's you return a value directy:
val anyLock = Any()
fun syncWithArbitraryObjTest(): Int = synchronized(anyLock) { 123 }
Note that synchronized is a function in Kotlin which returns a value. This makes it more powerful than Java's synchronized keyword. Technically it doesn’t make a difference on which object you lock, as long as you use the same object for all relevant code blocks that need be synchronized with each other.
If you choose to use a specific Lock object, e.g. an instance of ReentrantLock, you may use withLock instead:
val lock = ReentrantLock()
fun syncWithLockTest(): Int = lock.withLock { 123 }
Last but not least, your code val sCopy = s is no copy of s, it's simply a second variable pointing to the same object; this code isn't any safer.
You need to lock both the writes and the reads to your variable, like this:
class A {
var s: String? = null //var accessed by multiple threads
set(s) {
synchronized(this) {
field = s
}
}
fun doStuff() {
synchronized(this) {
//safe now
}
}
}
You need the synchronized in the setter beacuse otherwise the value might be overwritten while a thread is in doStuff.
Though in general it's better to avoid situations where multiple threads can write the same variable.
I'm trying to create a haxe.ds.HashMap where the keys are an object I don't control. Thus, they don't implement the hashCode method and I can't change them to.
I would really like to use an abstract to accomplish this, but I'm getting some compile time errors.
Here is the code I'm playing with:
import haxe.ds.HashMap;
abstract IntArrayKey( Array<Int> ) from Array<Int> {
inline public function new( i: Array<Int> ) {
this = i;
}
public function hashCode(): Int {
// General warning: Don't copy the following line. Seriously don't.
return this.length;
}
}
class Test {
static function main() {
var hash = new HashMap<IntArrayKey, Bool>();
}
}
The compile errors are:
Test.hx:15: characters 19-51 : Constraint check failure for haxe.ds.HashMap.K
Test.hx:15: characters 19-51 : IntArrayKey should be { hashCode : Void -> Int }
But the moment I change my abstract over to a class, it compiles fine:
import haxe.ds.HashMap;
class IntArrayKey {
private var _i: Array<Int>;
inline public function new( i: Array<Int> ) {
this._i = i;
}
public function hashCode(): Int {
// General warning: Don't copy the following line. Seriously don't.
return this._i.length;
}
}
class Test {
static function main() {
var hash = new HashMap<IntArrayKey, Bool>();
}
}
It's the exact same hashCode implementation, just a different context. Is there some way to accomplish this? Or is it a language limitation?
As far as I know, abstracts currently can't satisfy type requirements like this, quoting from the code:
abstract HashMap<K:{ function hashCode():Int; }, V >(HashMapData<K,V>) {
So, I doubt you could do that in a meaningful way.
Important point would be that while abstracts can sometimes provide overhead-free abstractions which is quite useful for optimizations, the time needed to instantiate(probably hidden from sight with abstract Name(Holder) to Holder having #:from Array<Int> and #:to Array<Int>) holder for your array which will have the required method isn't that high(compared to usual runtime overheads), and unless it is a really frequent code, should be your first way to go.
However, the HashMap code itself is quite short and simple: here.
You could just copy it and make it work with your example. Maybe you could even forge a better yet generic version by using interfaces(though I'm not sure if abstracts can actually implement them).
In NDepend 4 (v4.1.0.6871) I am using the default Design query "Boxing/unboxing should be avoided":
warnif percentage > 5 from m in Application.Methods where
m.IsUsingBoxing ||
m.IsUsingUnboxing
select new { m, m.NbLinesOfCode, m.IsUsingBoxing, m.IsUsingUnboxing }
It is reporting the following method (Inspired by and stolen from Jon Skeet's MiscUtil) as using boxing:
public static void ThrowIfNull<T>(this T target, string name) where T : class
{
if (target == null)
{
throw new ArgumentNullException(name ?? string.Empty);
}
}
I don't understand how this method is possibly using boxing. I am not using a cast anywhere.
I tried the following version just in case the null coalescing operator was using boxing somehow behind the scenes:
public static void ThrowIfNull<T>(this T target, string name) where T : class
{
if (target == null)
{
throw new ArgumentNullException(name);
}
}
... but I had no luck with that either, NDepend still reported that the method was using boxing.
Any ideas?
By decompiling this method with .NET Reflector we see that indeed this method is using the box IL instruction. Despite the fact that you are using the class generic constraint, the compiler still emits a box instruction for verifiability issue. More explanation on this here.
.method public hidebysig static void ThrowIfNull<class T>(!!T target, string name) cil managed
{
.custom instance void [mscorlib]System.Runtime.CompilerServices.ExtensionAttribute::.ctor()
.maxstack 2
.locals init (
[0] bool flag)
L_0000: nop
L_0001: ldarg.0
L_0002: box !!T <-----------
L_0007: ldnull
L_0008: ceq
L_000a: ldc.i4.0
L_000b: ceq
L_000d: stloc.0
L_000e: ldloc.0
L_000f: brtrue.s L_0022
L_0011: nop
L_0012: ldarg.1
L_0013: dup
L_0014: brtrue.s L_001c
L_0016: pop
L_0017: ldsfld string [mscorlib]System.String::Empty
L_001c: newobj instance void [mscorlib]System.ArgumentNullException::.ctor(string)
L_0021: throw
L_0022: ret
}
So I want to add methods to JDK classes like InputStream, File, etc. I'm trying to figure out what is the best way to do that, but it seems there are several options for doing it. One way is do this by adding methods into the metaClass property on the Class like so:
InputStream.metaClass.copy = { OutputStream out ->
long total = 0
byte[] buffer = new byte[8096]
int len
while ((len = read(buffer)) >= 0) {
out.write(buffer, 0, len)
total += len
}
out.flush()
return delegate
}
Another way is using dynamic mixins like this:
class EnhancedInputStream {
static {
InputStream.metaClass.mixin( EnhancedInputStream )
}
public InputStream copy( OutputStream out ) {
long total = 0
byte[] buffer = new byte[8096]
int len
while ((len = mixinOwner.read(buffer)) >= 0) {
out.write(buffer, 0, len)
total += len
}
out.flush()
return mixinOwner
}
}
So the first question is do dynamic Mixins replace the use of using metaClass + Closure to create mixins? The examples of dynamic mixins don't really discuss scoping rules in any detail that I can find. Which leads me to the next point.
You can see in the first code sample using metaClass I used delegate to get access to the this pointer of the class I was adding methods to. What is the equivalent way to do that using dynamic Mixins? All examples I've found are stateless (pointless really). I found one example mentioning a special member mixinOwner that could be used in place of delegate. Is that true?
Second you'll see I used a static block in EnhancedInputStream to add the mixin dynamically to InputStream. When using metaClass what is the best way to add those? Another static block with import statement?
I suppose I really want just a compile time Mixin where I can define the #Mixin on the source of the mixin instead of destination because I didn't write the destination. Like
#MixinInto(File)
public class EnhancedFileMixin {
public void zip( File output ) {
// .....
}
}
But that doesn't appear to exist in Groovy land. So what's the best approach to reach this using metaClass or dynamic mixins?
I guess the nearest to #MixinInto would be the magic package convention. I couldn't mix it into a interface, but i managed to mix it into a FileInputStream, if that suits your case. I guess you can add state using the MetaClass which comes in the constructor.
To write a class to be mixed into InputStream. It needs to be:
In the package groovy.runtime.metaclass.java.io
Named FileInputStreamMetaClass (exactly)
Compiled and put into the classpath
Extend DelegatingMetaClass
It can only intercept the GroovyObject methods, so it is not so straightforward. If you are in for a pure dynamic groovy, it is great:
package groovy.runtime.metaclass.java.io
class FileInputStreamMetaClass extends DelegatingMetaClass {
FileInputStreamMetaClass(MetaClass meta) {
super(meta)
println "built FileInputStreamMetaClass"
}
Object invokeMethod(Object delegate, String method, Object[] args) {
switch (method) {
case "copy":
return "i'm copying stuff"
default:
return super.invokeMethod(delegate, method, args)
}
}
}
Compiling:
$ groovyc FileInputStreamMetaClass.groovy
$ groovy -cp . InputTest.groovy
A test:
InputStream input = new FileInputStream("/tmp/test.tmp")
assert input.copy() == "i'm copying stuff"
A bit cumbersome.
I'd go for Extensions any time of the day. Three files:
// file META-INF/services/org.codehaus.groovy.runtime.ExtensionModule
moduleName=InputExtensionModule
moduleVersion=0.1
extensionClasses=InputStreamExtension
The extension:
class InputStreamExtension {
static String copy(InputStream input) {
"copying stuff, doc"
}
}
The test:
def input = new FileInputStream("/tmp/test.tmp")
assert input.copy() == "copying stuff, doc"
Compile and run:
$ groovyc InputStreamExtension.groovy
$ groovy ISExtensionTest.groovy
And i think the extension is the perfect place to use the static { mixin } block. With some changes:
class InputStreamExtension {
static {
InputStream.mixin InputStreamMixin
}
static String copy(InputStream input) { "copying stuff, doc" }
}
#Category(InputStream)
class InputStreamMixin {
Object pop() {
"input mixin pop"
}
}
A new test:
def input = new FileInputStream("/tmp/test.tmp")
assert input.copy() == "copying stuff, doc"
assert input.pop() == "input mixin pop"
Well I finally figured it out on my own. Essentially the this reference refers to the instance of the Mixin which doesn't do us much good. However, you can use the "as" keyword to convert that to the instance of the target class. For example:
class MyMixin {
static {
File mixin MyMixin
}
File foo() {
return this as File
}
}
File f = new File()
println( f.foo().equals( f ) )
As for mixinOwner and owner references that the jira bug refers to. They don't exist. This is the only way to get a reference to the instance that the mixin was added to.
I wrote up a longer blogpost about it because I thought this was important information for future Groovy programmers since there is zero official docs discussing this.
http://wrongnotes.blogspot.com/2013/06/groovy-mixins-and-this-pointer.html
I am glad you asked this question. To answer a very important question:
I suppose I really want just a compile time Mixin where I can define the #Mixin on the source of the mixin instead of destination because I didn't write the destination.
You cannot achieve this by #Mixin but we do have something in Groovy which will help you out. It is called #Category. Let me go through your example again to show you how you can actually effectively use this in category. Have a look at the below script:
#Category(InputStream)
class InputStreamCategory{
def copy(OutputStream out){
long total = 0
byte[] buffer = new byte[8096]
int len
while ((len = this.read(buffer)) >= 0) {
out.write(buffer, 0, len)
total += len
}
out.flush()
return this
}
}
class MyUtil{
def str = 'This is a dummy String!!!!'
InputStream inputS = new ByteArrayInputStream(str.bytes)
OutputStream outputS = new ByteArrayOutputStream()
def copy(){
use(InputStreamCategory){
inputS.copy(outputS)
println "Printing Output Stream: " + outputS
}
}
}
def util = new MyUtil()
util.copy()
//Prints:
Printing Output Stream: This is a dummy String!!!!
Explanation:-
#Category(InputStream) sets the this in InputStreamCategory and in your util class you just use the newly added method copy to InputStream. The benefit for using category is that you get hold of the caller object in this case inputS. The first parameter passed into a category always refers to the caller. You can have different categories for different implementations like FileCategory etc and then create an Utility Class to use those Categories. Therefore, you would end up with utilities like zip, copy, etc.
You could get detail information about the same from the api. I also highly recommend going through Category and Mixin Transformations.
To answer the first question:-
do dynamic Mixins replace the use of using metaClass + Closure to create mixins?
No they do not. metaClass implementation does not create a Mixin. It just adds another method in the metaData registry about the class while runtime. You get an handle to the delegate. On the other hand #Mixin gives you the ability to inherit pre-defined properties.
I have a weird problem and I would like if someone can enlighten me on why is this happening. I have a protected method in a base abstract class as following:
protected T ForExistingEntity<T>(TEntity entity, object key, Func<Entity, T> action) {
entity = GetByKey(key);
if (entity != null)
return action(entity);
return default(T);
}
My original call from an inherited class was as follows:
return base.ForExistingEntity(
new MyEntity(), key, e => {
e.someFiled = 5;
return base.Update(e);
}
);
When this code executes, an exception get raised at the line that reads:
return action(entity);
in the base abstract class. The exception is:
System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
Now when I modify my call as following:
return base.ForExistingEntity(
new MyEntity(), key, e => {
e.someFiled = 5;
return Update(e);
}
);
it runs normally without any issues.
Edit:
The Update method is located in the base abstract class and looks like this:
public virtual bool Update(TEntity entity) {
Condition.Requires(entity, "entity")
.IsNotNull();
if (ValidateEntity(entity))
return Update(entity, true);
return false;
}
I am starting to think that this is happening because of Update being virtual and the call actually originates in the base class itself? The exception isn't very helpfull anyway.
This seems to be a known C# compiler bug involving calling a base virtual method from an anonymous method within a generic class. Don't hesitate to upvote this bug on connect if you want it solved. Fortunately, the workaround is quite simple here.
I had the same issue. I had .NET 4.5 framework installed. When I uninstalled this and replaced with .NET 4.0 framework, this problem went away (i.e. 4.0.30319 when I look at the top right corner of my VS2010 info)