Search in sources :

Example 21 with IObject

use of php.runtime.lang.IObject in project jphp by jphp-compiler.

the class ClassEntity method cloneObject.

public <T extends IObject> T cloneObject(T value, Environment env, TraceInfo trace) throws Throwable {
    IObject copy = this.newObjectWithoutConstruct(env);
    ForeachIterator iterator = value.getProperties().foreachIterator(false, false);
    ArrayMemory props = copy.getProperties();
    while (iterator.next()) {
        Object key = iterator.getKey();
        if (key instanceof String) {
            String name = (String) key;
            if (name.indexOf('\0') > -1)
                name = name.substring(name.lastIndexOf('\0') + 1);
            PropertyEntity entity = properties.get(name);
            if (entity != null) {
                if (props.getByScalar(entity.getSpecificName()) == null)
                    props.put(entity.getSpecificName(), iterator.getValue().toImmutable());
            } else
                props.put(key, iterator.getValue().toImmutable());
        } else
            props.put(key, iterator.getValue().toImmutable());
    }
    if (methodMagicClone != null) {
        ObjectInvokeHelper.invokeMethod(copy, methodMagicClone, env, trace, null, true);
    }
    return (T) copy;
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator) IObject(php.runtime.lang.IObject) IObject(php.runtime.lang.IObject)

Example 22 with IObject

use of php.runtime.lang.IObject in project jphp by jphp-compiler.

the class ClassEntity method newObject.

public <T extends IObject> T newObject(Environment env, TraceInfo trace, boolean doConstruct, Memory... args) throws Throwable {
    if (isAbstract) {
        env.error(trace, "Cannot instantiate abstract class %s", name);
    } else if (type == Type.INTERFACE) {
        env.error(trace, "Cannot instantiate interface %s", name);
    } else if (type == Type.TRAIT) {
        env.error(trace, "Cannot instantiate trait %s", name);
    }
    IObject object;
    try {
        if (nativeConstructor == null) {
            env.error(trace, ErrorType.E_CORE_ERROR, "Cannot find a java constructor %s(Environment, ClassEntity)", getName());
        }
        object = (IObject) nativeConstructor.newInstance(env, this);
    } catch (InvocationTargetException e) {
        env.__throwException(e);
        return null;
    }
    ArrayMemory props = object.getProperties();
    for (PropertyEntity property : getProperties()) {
        if (id == property.clazz.getId() && property.getGetter() == null) {
            props.putAsKeyString(property.getSpecificName(), property.getDefaultValue(env).toImmutable());
        }
    }
    ClassEntity tmp = parent;
    while (tmp != null) {
        long otherId = tmp.getId();
        for (PropertyEntity property : tmp.getProperties()) {
            if (property.getClazz().getId() == otherId && property.getGetter() == null) {
                if (property.modifier != Modifier.PROTECTED || props.getByScalar(property.getName()) == null)
                    props.getByScalarOrCreate(property.getSpecificName(), property.getDefaultValue(env).toImmutable());
            }
        }
        tmp = tmp.parent;
    }
    if (doConstruct && methodConstruct != null) {
        ObjectInvokeHelper.invokeMethod(object, methodConstruct, env, trace, args, true);
    }
    return (T) object;
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) IObject(php.runtime.lang.IObject) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 23 with IObject

use of php.runtime.lang.IObject in project jphp by jphp-compiler.

the class ClassEntity method newMock.

public <T extends IObject> T newMock(Environment env) throws Throwable {
    if (nativeConstructor == null) {
        env.error(env.trace(), ErrorType.E_CORE_ERROR, "Cannot find a java constructor %s(Environment, ClassEntity)", getName());
    }
    try {
        IObject object = (IObject) nativeConstructor.newInstance(env, this);
        object.setAsMock();
        return (T) object;
    } catch (InstantiationException e) {
        return null;
    }
}
Also used : IObject(php.runtime.lang.IObject)

Aggregations

IObject (php.runtime.lang.IObject)23 ObjectMemory (php.runtime.memory.ObjectMemory)13 Memory (php.runtime.Memory)9 ArrayMemory (php.runtime.memory.ArrayMemory)8 ReferenceMemory (php.runtime.memory.ReferenceMemory)4 StringMemory (php.runtime.memory.StringMemory)4 ClassEntity (php.runtime.reflection.ClassEntity)4 CriticalException (php.runtime.exceptions.CriticalException)3 ForeachIterator (php.runtime.lang.ForeachIterator)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Test (org.junit.Test)2 Serializable (php.runtime.lang.spl.Serializable)2 MethodEntity (php.runtime.reflection.MethodEntity)2 Constructor (java.lang.reflect.Constructor)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 LinkedHashSet (java.util.LinkedHashSet)1 NativeObject (org.develnext.jphp.ext.pna.classes.NativeObject)1 Environment (php.runtime.env.Environment)1 TraceInfo (php.runtime.env.TraceInfo)1 BaseWrapper (php.runtime.lang.BaseWrapper)1