package org.cafebabepy.runtime.object.java;

import org.cafebabepy.runtime.AbstractPyObject;
import org.cafebabepy.runtime.PyObject;
import org.cafebabepy.runtime.Python;

import java.util.List;
import java.util.Optional;

/**
 * Created by yotchang4s on 2017/06/03.
 */
abstract class AbstractJavaPyObject extends AbstractPyObject {

    protected final PyObject type;

    protected AbstractJavaPyObject(Python runtime, PyObject type) {
        super(runtime, true);

        if (!type.isType()) {
            this.runtime.newRaiseTypeError("'" + type.getFullName() + "' is not type");
        }

        this.type = type;
    }

    @Override
    public final PyObject getType() {
        return this.type;
    }

    @Override
    public List<PyObject> getTypes() {
        return getType().getTypes();
    }

    @Override
    public List<PyObject> getBases() {
        return getType().getBases();
    }

    @Override
    public final Optional<String> getModuleName() {
        return Optional.empty();
    }

    @Override
    public String getName() {
        return getType().getName();
    }

    @Override
    public boolean isType() {
        return false;
    }

    @Override
    public boolean isModule() {
        return false;
    }

    @Override
    public String asJavaString() {
        int hashCode = System.identityHashCode(this);

        return "0x" + Integer.toHexString(hashCode);
    }
}

@@ -364,16 +364,53 @@
  type;%0A%0A
+    private volatile String string;%0A%0A
     prot
@@ -1026,22 +1026,31 @@
 urn 
-Optional.empty
+getType().getModuleName
 ();%0A
@@ -1353,105 +1353,288 @@
    i
-nt hashCode = System.identityHashCode(this);%0A%0A        return %220x%22 + Integer.toHexString(hashCode)
+f (this.string == null) %7B%0A            synchronized (this) %7B%0A                int hashCode = System.identityHashCode(this);%0A                this.string = %22%3C%22 + getFullName() + %22 object at 0x%22 + Integer.toHexString(hashCode) + %22%3E%22;%0A            %7D%0A        %7D%0A%0A        return this.string
 ;%0A  

Fix to cache asJavaString