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)));
}
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;
}
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);
}
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;
}
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;
}
Aggregations