Search in sources :

Example 1 with ParameterEntity

use of php.runtime.reflection.ParameterEntity in project jphp by jphp-compiler.

the class TypeHintingTest method testCoreDouble.

@Test
public void testCoreDouble() {
    ParameterEntity param = new ParameterEntity(null);
    param.setType(HintType.DOUBLE);
    Assert.assertFalse(param.checkTypeHinting(null, new StringMemory("")));
    Assert.assertFalse(param.checkTypeHinting(null, new ObjectMemory(null)));
    Assert.assertFalse(param.checkTypeHinting(null, new ArrayMemory()));
    Assert.assertFalse(param.checkTypeHinting(null, Memory.NULL));
    Assert.assertFalse(param.checkTypeHinting(null, Memory.FALSE));
    Assert.assertFalse(param.checkTypeHinting(null, Memory.TRUE));
    Assert.assertFalse(param.checkTypeHinting(null, new LongMemory(0)));
    Assert.assertTrue(param.checkTypeHinting(null, new DoubleMemory(0)));
}
Also used : ParameterEntity(php.runtime.reflection.ParameterEntity) Test(org.junit.Test)

Example 2 with ParameterEntity

use of php.runtime.reflection.ParameterEntity in project jphp by jphp-compiler.

the class MemoryOperation method get.

@SuppressWarnings("unchecked")
public static MemoryOperation get(final Class<?> type, Type genericTypes, boolean includeParents) {
    MemoryOperation operation = null;
    if (genericTypes instanceof ParameterizedType) {
        operation = genericOperations.get(new ParametrizedClass(type, ((ParameterizedType) genericTypes).getActualTypeArguments()));
    }
    if (operation == null) {
        operation = operations.get(type);
        if (operation == null) {
            if (type.isArray()) {
                MemoryOperation arrayMemoryOperation = new ArrayMemoryOperation(type);
                register(arrayMemoryOperation);
                return arrayMemoryOperation;
            }
            if (Enum.class.isAssignableFrom(type)) {
                return new EnumMemoryOperation((Class<? extends Enum>) type);
            }
            final Class<? extends BaseWrapper> wrapperClass = wrappers.get(type);
            if (wrapperClass != null) {
                Constructor<BaseWrapper> constructor;
                try {
                    constructor = (Constructor<BaseWrapper>) wrapperClass.getConstructor(Environment.class, type);
                } catch (NoSuchMethodException e) {
                    try {
                        constructor = (Constructor<BaseWrapper>) wrapperClass.getConstructor(Environment.class, Object.class);
                    } catch (NoSuchMethodException e1) {
                        throw new CriticalException(e);
                    }
                }
                final Constructor<BaseWrapper> finalConstructor = constructor;
                return new MemoryOperation() {

                    @Override
                    public Class<?>[] getOperationClasses() {
                        return new Class<?>[0];
                    }

                    @Override
                    public Object convert(Environment env, TraceInfo trace, Memory arg) throws Throwable {
                        if (arg.isNull()) {
                            return null;
                        }
                        return arg.toObject(BaseWrapper.class).getWrappedObject();
                    }

                    @Override
                    public Memory unconvert(Environment env, TraceInfo trace, Object arg) throws Throwable {
                        if (arg == null) {
                            return Memory.NULL;
                        }
                        Constructor<BaseWrapper> constructorContext = finalConstructor;
                        Class<? extends BaseWrapper> wrapperClassContext = wrapperClass;
                        if (arg.getClass() != type) {
                            wrapperClassContext = wrappers.get(arg.getClass());
                        }
                        if (wrapperClassContext != null && wrapperClassContext != wrapperClass) {
                            constructorContext = (Constructor<BaseWrapper>) wrapperClassContext.getConstructor(Environment.class, arg.getClass());
                        }
                        try {
                            BaseWrapper instance = constructorContext.newInstance(env, arg);
                            return ObjectMemory.valueOf(instance.__getOriginInstance());
                        } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
                            throw new CriticalException(e);
                        }
                    }

                    @Override
                    public void applyTypeHinting(ParameterEntity parameter) {
                        parameter.setTypeNativeClass(type);
                    }
                };
            } else if (IObject.class.isAssignableFrom(type)) {
                return new IObjectMemoryOperation((Class<? extends IObject>) type);
            } else {
                Class<?> superType = type.getSuperclass();
                if (Object.class != superType && (includeParents || type.isAnonymousClass())) {
                    return get(superType, type.getGenericSuperclass(), includeParents);
                }
            }
        }
    }
    if (operation == null) {
        // new BeanMemoryOperation(type);
        return null;
    }
    if (genericTypes instanceof ParameterizedType) {
        return operation.instance(((ParameterizedType) genericTypes).getActualTypeArguments());
    }
    return operation;
}
Also used : IObject(php.runtime.lang.IObject) Constructor(java.lang.reflect.Constructor) Memory(php.runtime.Memory) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory) TraceInfo(php.runtime.env.TraceInfo) IterableMemoryOperation(php.runtime.memory.support.operation.iterator.IterableMemoryOperation) PropertiesMemoryOperation(php.runtime.memory.support.operation.map.PropertiesMemoryOperation) HashMapMemoryOperation(php.runtime.memory.support.operation.map.HashMapMemoryOperation) LinkedHashMapMemoryOperation(php.runtime.memory.support.operation.map.LinkedHashMapMemoryOperation) SetMemoryOperation(php.runtime.memory.support.operation.collection.SetMemoryOperation) MapMemoryOperation(php.runtime.memory.support.operation.map.MapMemoryOperation) HashSetMemoryOperation(php.runtime.memory.support.operation.collection.HashSetMemoryOperation) EnumerationMemoryOperation(php.runtime.memory.support.operation.collection.EnumerationMemoryOperation) ListMemoryOperation(php.runtime.memory.support.operation.collection.ListMemoryOperation) CriticalException(php.runtime.exceptions.CriticalException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ParameterizedType(java.lang.reflect.ParameterizedType) BaseWrapper(php.runtime.lang.BaseWrapper) ParameterEntity(php.runtime.reflection.ParameterEntity) Environment(php.runtime.env.Environment) IObject(php.runtime.lang.IObject)

Example 3 with ParameterEntity

use of php.runtime.reflection.ParameterEntity in project jphp by jphp-compiler.

the class ReflectionFunctionAbstract method getNumberOfRequiredParameters.

@Signature
public Memory getNumberOfRequiredParameters(Environment env, Memory... args) {
    int cnt = 0;
    ParameterEntity[] parameterEntities = getClosureEntity() == null ? getEntity().getParameters() : getClosureEntity().parameters;
    for (ParameterEntity e : parameterEntities) {
        if (e.getDefaultValue() == null)
            cnt++;
    }
    return LongMemory.valueOf(cnt);
}
Also used : ParameterEntity(php.runtime.reflection.ParameterEntity) Signature(php.runtime.annotation.Reflection.Signature)

Example 4 with ParameterEntity

use of php.runtime.reflection.ParameterEntity in project jphp by jphp-compiler.

the class ReflectionParameter method __construct.

@Signature({ @Arg("function"), @Arg("parameter") })
public Memory __construct(Environment env, Memory... args) {
    ParameterEntity[] parameters = null;
    if (args[0].isClosure()) {
        ClosureEntity tmp = (ClosureEntity) args[0].toValue(ObjectMemory.class).getReflection();
        parameters = tmp.parameters;
    } else if (args[0].isArray()) {
        Invoker invoker = Invoker.valueOf(env, null, args[0]);
        if (invoker == null) {
            exception(env, "%s does not exists", args[0].toString());
            return Memory.NULL;
        }
        parameters = invoker.getParameters();
    } else {
        String name = args[0].toString();
        if (name.contains("::")) {
            Invoker invoker = Invoker.valueOf(env, null, args[0]);
            if (invoker == null) {
                exception(env, "%s does not exists", args[0].toString());
                return Memory.NULL;
            }
            parameters = invoker.getParameters();
        } else {
            FunctionEntity tmp = env.fetchFunction(name);
            functionEntity = tmp;
            if (tmp == null) {
                exception(env, "Function %s does not exist", args[0].toString());
                return Memory.NULL;
            }
            if (tmp.isInternal()) {
                exception(env, "%s(): ReflectionParameter does not support internal functions", tmp.getName());
                return Memory.NULL;
            }
            parameters = tmp.getParameters();
        }
    }
    entity = null;
    String name = args[1].toString();
    if (parameters != null) {
        if (args[1].isNumber()) {
            int index = args[1].toInteger();
            if (index >= 0 && index < parameters.length) {
                entity = parameters[index];
                position = index;
            }
        } else {
            int i = 0;
            for (ParameterEntity e : parameters) {
                if (e.getName().equals(name)) {
                    entity = e;
                    position = i;
                    break;
                }
                i++;
            }
        }
    }
    if (entity == null)
        exception(env, "Parameter %s does not exist", name);
    setEntity(entity);
    return Memory.NULL;
}
Also used : ParameterEntity(php.runtime.reflection.ParameterEntity) AbstractFunctionEntity(php.runtime.reflection.support.AbstractFunctionEntity) FunctionEntity(php.runtime.reflection.FunctionEntity) Invoker(php.runtime.invoke.Invoker) ClosureEntity(php.runtime.reflection.helper.ClosureEntity)

Example 5 with ParameterEntity

use of php.runtime.reflection.ParameterEntity in project jphp by jphp-compiler.

the class InvokeArgumentHelper method makeArguments.

public static Memory[] makeArguments(Environment env, Memory[] args, ParameterEntity[] parameters, String originClassName, String originMethodName, String staticClassName, TraceInfo trace) {
    if (parameters == null) {
        return args;
    }
    Memory[] passed = argsToPassed(env, trace, args, parameters);
    int i = 0;
    if (passed != null) {
        boolean variadicMemoryExists = false;
        for (ParameterEntity param : parameters) {
            Memory arg = passed[i];
            if (param.isVariadic()) {
                ArrayMemory variadicArgs = new ArrayMemory();
                int _i = i;
                while (arg != null) {
                    if (arg instanceof VariadicMemory) {
                        variadicMemoryExists = true;
                        ForeachIterator iterator = arg.getNewIterator(env, param.isReference(), false);
                        if (iterator == null) {
                            env.warning(trace, Messages.ERR_ONLY_ARR_AND_TRAV_CAN_UNPACKED);
                        } else {
                            makeVariadic(iterator, variadicArgs, param, env, trace, _i, originClassName, originMethodName);
                        }
                    } else {
                        if (variadicMemoryExists) {
                            env.error(trace, Messages.ERR_CANNOT_USE_POS_ARG_AFTER_UNPACK.fetch());
                        }
                        arg = typeHintArg(env, trace, param, arg, _i + 1, originClassName, originMethodName, staticClassName);
                        variadicArgs.add(makeValue(param, arg, env, trace));
                    }
                    i++;
                    if (i < passed.length) {
                        arg = passed[i];
                    } else {
                        break;
                    }
                }
                passed[_i] = variadicArgs;
                break;
            }
            passed[i] = makeArg(env, trace, param, arg, i, originClassName, originMethodName, staticClassName);
            i++;
        }
        if (!variadicMemoryExists) {
            makeImmutable(passed, parameters);
        }
    }
    return passed;
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator) ParameterEntity(php.runtime.reflection.ParameterEntity) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) ReferenceMemory(php.runtime.memory.ReferenceMemory) VariadicMemory(php.runtime.memory.helper.VariadicMemory) VariadicMemory(php.runtime.memory.helper.VariadicMemory)

Aggregations

ParameterEntity (php.runtime.reflection.ParameterEntity)28 Test (org.junit.Test)8 ArrayMemory (php.runtime.memory.ArrayMemory)7 Memory (php.runtime.Memory)6 DumpException (php.runtime.loader.dump.io.DumpException)5 DumpInputStream (php.runtime.loader.dump.io.DumpInputStream)5 ObjectMemory (php.runtime.memory.ObjectMemory)5 DumpOutputStream (php.runtime.loader.dump.io.DumpOutputStream)4 ReferenceMemory (php.runtime.memory.ReferenceMemory)4 VariadicMemory (php.runtime.memory.helper.VariadicMemory)3 ClassEntity (php.runtime.reflection.ClassEntity)3 DocumentComment (php.runtime.reflection.DocumentComment)3 MethodEntity (php.runtime.reflection.MethodEntity)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 CriticalException (php.runtime.exceptions.CriticalException)2 Closure (php.runtime.lang.Closure)2 ForeachIterator (php.runtime.lang.ForeachIterator)2 IObject (php.runtime.lang.IObject)2 StringMemory (php.runtime.memory.StringMemory)2