Search in sources :

Example 11 with CriticalException

use of php.runtime.exceptions.CriticalException in project jphp by jphp-compiler.

the class CompileScope method registerExtension.

public void registerExtension(Extension extension) {
    long t = System.currentTimeMillis();
    if (extensions.containsKey(extension.getName()))
        return;
    // required
    for (String dep : extension.getRequiredExtensions()) {
        try {
            Extension el = (Extension) Class.forName(dep).newInstance();
            registerExtension(el);
        } catch (Exception e) {
            throw new CriticalException(e);
        }
    }
    // optional
    for (String dep : extension.getOptionalExtensions()) {
        try {
            Extension el = (Extension) Class.forName(dep).newInstance();
            registerExtension(el);
        } catch (ClassNotFoundException e) {
        // do nothing ...
        } catch (Exception e) {
            throw new CriticalException(e);
        }
    }
    // conflicts
    for (String dep : extension.getConflictExtensions()) {
        if (extensions.containsKey(dep))
            throw new ConflictException("'" + dep + "' extension conflicts with '" + extension.getClass().getName() + "'");
    }
    extension.onRegister(this);
    compileConstantMap.putAll(extension.getConstants());
    compileFunctionSpecMap.putAll(extension.getFunctions());
    for (Class<?> clazz : extension.getClasses().values()) {
        registerLazyClass(extension, clazz);
    }
    for (CompileFunctionSpec function : extension.getFunctions().values()) {
        functionMap.put(function.getLowerName(), new CompileFunctionEntity(extension, function));
    }
    extensions.put(extension.getName().toLowerCase(), extension);
    if (Startup.isTracing()) {
        Startup.traceWithTime("Register extension '" + extension.getName() + "'", t);
    }
}
Also used : Extension(php.runtime.ext.support.Extension) JavaExtension(php.runtime.ext.JavaExtension) CoreExtension(php.runtime.ext.CoreExtension) NetExtension(php.runtime.ext.NetExtension) CompileFunctionSpec(php.runtime.ext.support.compile.CompileFunctionSpec) ConflictException(php.runtime.exceptions.ConflictException) CriticalException(php.runtime.exceptions.CriticalException) CriticalException(php.runtime.exceptions.CriticalException) JavaException(php.runtime.ext.java.JavaException) ErrorException(php.runtime.lang.spl.ErrorException) ConflictException(php.runtime.exceptions.ConflictException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 12 with CriticalException

use of php.runtime.exceptions.CriticalException in project jphp by jphp-compiler.

the class ExpressionStmtCompiler method write.

@SuppressWarnings("unchecked")
public <T extends Token> T write(Class<? extends Token> clazz, T token) {
    BaseStatementCompiler<T> cmp = (BaseStatementCompiler<T>) getCompiler(clazz);
    if (cmp == null)
        throw new CriticalException("Cannot find compiler for " + clazz.getName());
    cmp.write(token);
    return token;
}
Also used : CriticalException(php.runtime.exceptions.CriticalException)

Example 13 with CriticalException

use of php.runtime.exceptions.CriticalException in project jphp by jphp-compiler.

the class ClassWrapper method onWrapProperties.

protected void onWrapProperties(ClassEntity classEntity) {
    Reflection.Signature signature = nativeClass.getAnnotation(Reflection.Signature.class);
    if (signature != null) {
        for (Reflection.Arg arg : signature.value()) {
            onWrapProperty(classEntity, arg);
        }
    }
    for (Field field : nativeClass.getDeclaredFields()) {
        Reflection.Property property = field.getAnnotation(Reflection.Property.class);
        if (property != null) {
            onWrapCompileProperty(classEntity, field, property);
        }
    }
    Reflection.WrapInterface interfaces = nativeClass.getAnnotation(Reflection.WrapInterface.class);
    if (interfaces != null && interfaces.wrapFields() && BaseWrapper.class.isAssignableFrom(nativeClass)) {
        Class<?> bindClass = MemoryOperation.getClassOfWrapper((Class<? extends php.runtime.lang.BaseWrapper<Object>>) nativeClass);
        for (Class _interface : interfaces.value()) {
            for (Field field : _interface.getDeclaredFields()) {
                try {
                    Field _field = bindClass.getDeclaredField(field.getName());
                    int mods = _field.getModifiers();
                    if ((/*Modifier.isProtected(mods) || */
                    Modifier.isPublic(mods)) && !Modifier.isStatic(mods)) {
                        if (interfaces.skipConflicts() && MemoryOperation.get(_field.getType(), _field.getGenericType()) == null) {
                            continue;
                        }
                        PropertyEntity entity = new WrapCompilePropertyEntity(classEntity.getContext(), _field);
                        entity.setName(_field.getName());
                        classEntity.addProperty(entity);
                    }
                } catch (NoSuchFieldException e) {
                    throw new CriticalException(e);
                }
            }
        }
    }
}
Also used : Reflection(php.runtime.annotation.Reflection) CriticalException(php.runtime.exceptions.CriticalException) Field(java.lang.reflect.Field) BaseWrapper(php.runtime.lang.BaseWrapper) IObject(php.runtime.lang.IObject)

Example 14 with CriticalException

use of php.runtime.exceptions.CriticalException in project jphp by jphp-compiler.

the class ClassWrapper method onWrapMethods.

protected void onWrapMethods(ClassEntity classEntity) {
    Reflection.WrapInterface interfaces = nativeClass.getAnnotation(Reflection.WrapInterface.class);
    Set<Class<?>> wrapInterfaces = new HashSet<Class<?>>();
    if (interfaces != null) {
        wrapInterfaces.addAll(Arrays.asList(interfaces.value()));
    }
    for (Class<?> cls : nativeClass.getDeclaredClasses()) {
        if (cls.getSimpleName().equals("WrappedInterface") && cls.getDeclaringClass() == nativeClass) {
            if (!cls.isInterface()) {
                throw new CriticalException("WrappedInterface class must be interface");
            }
            if (!BaseWrapper.class.isAssignableFrom(nativeClass)) {
                throw new CriticalException("To use WrappedInterface, your class must be inheritances from the BaseWrapper class");
            }
            wrapInterfaces.add(cls);
            break;
        }
    }
    if (!wrapInterfaces.isEmpty() && BaseWrapper.class.isAssignableFrom(nativeClass)) {
        Class<?> bindClass = MemoryOperation.getClassOfWrapper((Class<? extends php.runtime.lang.BaseWrapper<Object>>) nativeClass);
        for (Class _interface : wrapInterfaces) {
            for (Method method : _interface.getDeclaredMethods()) {
                try {
                    if (method.isAnnotationPresent(Reflection.Property.class)) {
                        String name = method.getName();
                        MethodEntity getterEntity;
                        try {
                            getterEntity = onWrapWrapCompileMethod(classEntity, bindClass.getMethod("get" + name.substring(0, 1).toUpperCase() + name.substring(1)), method, false);
                        } catch (NoSuchMethodException e) {
                            if (method.getReturnType() == Boolean.TYPE || method.getReturnType() == Boolean.class) {
                                getterEntity = onWrapWrapCompileMethod(classEntity, bindClass.getMethod("is" + name.substring(0, 1).toUpperCase() + name.substring(1)), method, false);
                            } else {
                                throw e;
                            }
                        }
                        MethodEntity setterEntity = null;
                        try {
                            setterEntity = onWrapWrapCompileMethod(classEntity, bindClass.getMethod("set" + name.substring(0, 1).toUpperCase() + name.substring(1), method.getReturnType()), method, false);
                        } catch (NoSuchMethodException e) {
                        // nop
                        }
                        String propertyName = method.getAnnotation(Reflection.Property.class).value();
                        if (propertyName.isEmpty()) {
                            propertyName = name;
                        }
                        PropertyEntity propertyEntity = getPropertyOfMethod(getterEntity, propertyName);
                        propertyEntity.setGetter(getterEntity);
                        propertyEntity.setSetter(setterEntity);
                        if (setterEntity != null && method.isAnnotationPresent(Reflection.Nullable.class)) {
                            ParameterEntity setterArg = setterEntity.getParameters(1)[0];
                            setterArg.setNullable(true);
                        }
                        if (_interface.isInterface()) {
                            getterEntity.setAbstractable(false);
                            getterEntity.setAbstract(false);
                            if (getterEntity.isPublic()) {
                                getterEntity.setModifier(php.runtime.common.Modifier.PROTECTED);
                            }
                            if (setterEntity != null) {
                                setterEntity.setAbstractable(false);
                                setterEntity.setAbstract(false);
                                if (setterEntity.isPublic()) {
                                    setterEntity.setModifier(php.runtime.common.Modifier.PROTECTED);
                                }
                            }
                        }
                        classEntity.addProperty(propertyEntity).check(null);
                    } else {
                        MethodEntity entity = onWrapWrapCompileMethod(classEntity, bindClass.getDeclaredMethod(method.getName(), method.getParameterTypes()), method, interfaces != null && interfaces.skipConflicts());
                        if (_interface.isInterface()) {
                            entity.setAbstractable(false);
                            entity.setAbstract(false);
                        }
                    }
                } catch (NoSuchMethodException e) {
                    boolean _throw = true;
                    if (interfaces != null && interfaces.skipConflicts()) {
                        for (Class<?> aClass : interfaces.value()) {
                            if (aClass == _interface) {
                                _throw = false;
                            }
                        }
                    }
                    if (_throw) {
                        throw new CriticalException(e);
                    }
                }
            }
        }
    }
    for (Method method : nativeClass.getDeclaredMethods()) {
        Reflection.Signature signature = getSignature(method);
        if (signature != null || method.isAnnotationPresent(Reflection.Getter.class) || method.isAnnotationPresent(Reflection.Setter.class)) {
            Class<?>[] types = method.getParameterTypes();
            if (method.getReturnType() == Memory.class && types.length == 2 && types[0] == Environment.class && types[1] == Memory[].class) {
                onWrapMethod(classEntity, method);
            } else {
                onWrapCompileMethod(classEntity, method);
            }
        }
    }
}
Also used : Memory(php.runtime.Memory) Reflection(php.runtime.annotation.Reflection) Method(java.lang.reflect.Method) CriticalException(php.runtime.exceptions.CriticalException) BaseWrapper(php.runtime.lang.BaseWrapper) IObject(php.runtime.lang.IObject) HashSet(java.util.HashSet)

Example 15 with CriticalException

use of php.runtime.exceptions.CriticalException in project jphp by jphp-compiler.

the class ObjectInvokeHelper method invokeMethod.

public static Memory invokeMethod(Memory object, String methodName, String methodLowerName, Environment env, TraceInfo trace, Memory[] args) throws Throwable {
    object = object.toValue();
    Memory[] passed = null;
    boolean doublePop = false;
    if (object.type != Memory.Type.OBJECT) {
        env.error(trace, ErrorType.E_RECOVERABLE_ERROR, Messages.ERR_CANNOT_CALL_OF_NON_OBJECT.fetch(methodName));
        return Memory.NULL;
    }
    IObject iObject = ((ObjectMemory) object).value;
    ClassEntity clazz = iObject.getReflection();
    MethodEntity method;
    if (methodName == null) {
        method = clazz.methodMagicInvoke;
    } else {
        method = clazz.findMethod(methodLowerName);
        if (method != null && method.isContextDepends()) {
            ClassEntity context = env.getLastClassOnStack();
            if (context != null) {
                MethodEntity contextMethod = context.findMethod(methodLowerName);
                if (contextMethod != null) {
                    method = contextMethod;
                }
            }
        }
        if (method == null && ((method = clazz.methodMagicCall) != null)) {
            clazz.methodMagicCall.setModifier(Modifier.PUBLIC);
            passed = new Memory[] { new StringMemory(methodName), ArrayMemory.of(args) };
            doublePop = true;
        }
    }
    String className = clazz.getName();
    if (method == null) {
        if (methodName == null)
            methodName = "__invoke";
        env.error(trace, ErrorType.E_ERROR, Messages.ERR_CALL_TO_UNDEFINED_METHOD.fetch(className + "::" + methodName));
        return Memory.NULL;
    }
    InvokeHelper.checkAccess(env, trace, method);
    if (passed == null) {
        passed = InvokeHelper.makeArguments(env, args, method.getParameters(args == null ? 0 : args.length), className, methodName, trace);
    }
    Memory result = method.getImmutableResult();
    if (result != null) {
        return result;
    }
    try {
        if (trace != null) {
            String staticClass = className;
            if (iObject instanceof Closure) {
                staticClass = ((Closure) iObject).getScope();
            }
            String stackClass = clazz.isHiddenInCallStack() ? staticClass : method.getClazz().getName();
            env.pushCall(trace, iObject, args, methodName, stackClass, staticClass);
            if (doublePop) {
                env.pushCall(trace, iObject, passed, method.getName(), stackClass, staticClass);
            }
        }
        return method.invokeDynamic(iObject, env, passed);
    } catch (NoClassDefFoundError e) {
        throw new CriticalException("Unable to call method " + className + "::" + methodName + "(), " + e.getMessage());
    } finally {
        if (trace != null) {
            env.popCall();
            if (doublePop)
                env.popCall();
        }
    }
}
Also used : ClassEntity(php.runtime.reflection.ClassEntity) IObject(php.runtime.lang.IObject) Closure(php.runtime.lang.Closure) ObjectMemory(php.runtime.memory.ObjectMemory) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) ReferenceMemory(php.runtime.memory.ReferenceMemory) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory) MethodEntity(php.runtime.reflection.MethodEntity) StringMemory(php.runtime.memory.StringMemory) CriticalException(php.runtime.exceptions.CriticalException)

Aggregations

CriticalException (php.runtime.exceptions.CriticalException)19 Memory (php.runtime.Memory)7 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 IObject (php.runtime.lang.IObject)5 ObjectMemory (php.runtime.memory.ObjectMemory)4 ClassEntity (php.runtime.reflection.ClassEntity)4 IOException (java.io.IOException)3 ValueExprToken (org.develnext.jphp.core.tokenizer.token.expr.ValueExprToken)3 BaseWrapper (php.runtime.lang.BaseWrapper)3 StringMemory (php.runtime.memory.StringMemory)3 Field (java.lang.reflect.Field)2 OpenEchoTagToken (org.develnext.jphp.core.tokenizer.token.OpenEchoTagToken)2 Token (org.develnext.jphp.core.tokenizer.token.Token)2 ClassExprToken (org.develnext.jphp.core.tokenizer.token.expr.ClassExprToken)2 OperatorExprToken (org.develnext.jphp.core.tokenizer.token.expr.OperatorExprToken)2 Reflection (php.runtime.annotation.Reflection)2 Environment (php.runtime.env.Environment)2 ArrayMemory (php.runtime.memory.ArrayMemory)2 ReferenceMemory (php.runtime.memory.ReferenceMemory)2 Constructor (java.lang.reflect.Constructor)1