Search in sources :

Example 1 with Function

use of php.runtime.common.Function in project jphp by jphp-compiler.

the class ClassStmtCompiler method writeCopiedMethod.

protected void writeCopiedMethod(ClassStmtToken.Alias alias, String methodName, ClassEntity trait) {
    final MethodEntity methodEntity = fetchClassAndCheck(alias.getTrait()).findMethod(methodName.toLowerCase());
    String name = alias.getName();
    if (name == null)
        name = methodName;
    MethodEntity origin = entity.findMethod(name.toLowerCase());
    if (origin != null) {
        if (origin.getClazz() == entity) {
            if (origin.getTrait() != null) {
                compiler.getEnvironment().error(entity.getTrace(), Messages.ERR_TRAIT_METHOD_COLLISION.fetch(methodName, trait.getName(), origin.getTrait().getName(), entity.getName()));
            }
            return;
        }
    }
    if (methodEntity == null) {
        compiler.getEnvironment().error(entity.getTrace(), Messages.ERR_METHOD_NOT_FOUND.fetch(alias.getTrait(), methodName));
        return;
    }
    MethodEntity dup = methodEntity.duplicateForInject();
    dup.setClazz(entity);
    dup.setTrait(trait);
    if (alias.getName() != null)
        dup.setName(alias.getName());
    if (alias.getModifier() != null)
        dup.setModifier(alias.getModifier());
    MethodNodeImpl methodNode = MethodNodeImpl.duplicate(methodEntity.getAdditionalData("methodNode", MethodNode.class, new Function<MethodNode>() {

        @Override
        public MethodNode call() {
            ClassNode classNode = methodEntity.getClazz().getAdditionalData("classNode", ClassNode.class, new Function<ClassNode>() {

                @Override
                public ClassNode call() {
                    ClassReader classReader;
                    if (methodEntity.getClazz().getData() != null)
                        classReader = new ClassReader(methodEntity.getClazz().getData());
                    else {
                        try {
                            classReader = new ClassReader(methodEntity.getClazz().getName());
                        } catch (IOException e) {
                            throw new CriticalException(e);
                        }
                    }
                    ClassNode classNode = new ClassNode();
                    classReader.accept(classNode, 0);
                    return classNode;
                }
            });
            for (Object m : classNode.methods) {
                MethodNode method = (MethodNode) m;
                if (method.name.equals(methodEntity.getInternalName())) {
                    return method;
                }
            }
            throw new CriticalException("Cannot find MethodNode for method - " + methodEntity.getName() + "(" + methodEntity.getSignatureString(true) + ")");
        }
    }));
    if (origin != null) {
        dup.setPrototype(origin);
    }
    dup.setInternalName(dup.getName() + "$" + entity.nextMethodIndex());
    methodNode.name = dup.getInternalName();
    ClassEntity.SignatureResult result = entity.addMethod(dup, null);
    result.check(compiler.getEnvironment());
    node.methods.add(methodNode);
}
Also used : MethodNodeImpl(org.develnext.jphp.core.compiler.jvm.node.MethodNodeImpl) IOException(java.io.IOException) CriticalException(php.runtime.exceptions.CriticalException) Function(php.runtime.common.Function) ClassReader(org.objectweb.asm.ClassReader) BaseObject(php.runtime.lang.BaseObject)

Example 2 with Function

use of php.runtime.common.Function in project jphp by jphp-compiler.

the class JTreeXEventProvider method register.

@Override
public void register(final Environment env, final JTreeX component, final ComponentProperties properties) {
    final ObjectMemory self = new ObjectMemory(new UITree(env, component));
    component.getContent().addTreeSelectionListener(new TreeSelectionListener() {

        @Override
        public void valueChanged(final TreeSelectionEvent e) {
            trigger(env, properties, "selected", new Function<Memory[]>() {

                @Override
                public Memory[] call() {
                    Memory oldNode = Memory.NULL;
                    if (e.getOldLeadSelectionPath() != null) {
                        oldNode = new ObjectMemory(new WrapTreeNode(env, (DefaultMutableTreeNode) e.getOldLeadSelectionPath().getLastPathComponent()));
                    }
                    Memory node = Memory.NULL;
                    if (e.getNewLeadSelectionPath() != null) {
                        node = new ObjectMemory(new WrapTreeNode(env, (DefaultMutableTreeNode) e.getNewLeadSelectionPath().getLastPathComponent()));
                    }
                    return new Memory[] { self, node, oldNode, TrueMemory.valueOf(e.isAddedPath()) };
                }
            });
        }
    });
    component.getContent().addTreeWillExpandListener(new TreeWillExpandListener() {

        @Override
        public void treeWillExpand(final TreeExpansionEvent event) {
            trigger(env, properties, "willExpand", new Function<Memory[]>() {

                @Override
                public Memory[] call() {
                    return new Memory[] { self, new ObjectMemory(new WrapTreeNode(env, (DefaultMutableTreeNode) event.getPath().getLastPathComponent())) };
                }
            });
        }

        @Override
        public void treeWillCollapse(final TreeExpansionEvent event) {
            trigger(env, properties, "willCollapse", new Function<Memory[]>() {

                @Override
                public Memory[] call() {
                    return new Memory[] { self, new ObjectMemory(new WrapTreeNode(env, (DefaultMutableTreeNode) event.getPath().getLastPathComponent())) };
                }
            });
        }
    });
    component.getContent().addTreeExpansionListener(new TreeExpansionListener() {

        @Override
        public void treeExpanded(final TreeExpansionEvent event) {
            trigger(env, properties, "expanded", new Function<Memory[]>() {

                @Override
                public Memory[] call() {
                    return new Memory[] { self, new ObjectMemory(new WrapTreeNode(env, (DefaultMutableTreeNode) event.getPath().getLastPathComponent())) };
                }
            });
        }

        @Override
        public void treeCollapsed(final TreeExpansionEvent event) {
            trigger(env, properties, "collapsed", new Function<Memory[]>() {

                @Override
                public Memory[] call() {
                    return new Memory[] { self, new ObjectMemory(new WrapTreeNode(env, (DefaultMutableTreeNode) event.getPath().getLastPathComponent())) };
                }
            });
        }
    });
}
Also used : Function(php.runtime.common.Function) DefaultMutableTreeNode(javax.swing.tree.DefaultMutableTreeNode) ObjectMemory(php.runtime.memory.ObjectMemory) TrueMemory(php.runtime.memory.TrueMemory) Memory(php.runtime.Memory) ObjectMemory(php.runtime.memory.ObjectMemory) WrapTreeNode(org.develnext.jphp.swing.classes.components.tree.WrapTreeNode) UITree(org.develnext.jphp.swing.classes.components.tree.UITree)

Aggregations

Function (php.runtime.common.Function)2 IOException (java.io.IOException)1 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)1 MethodNodeImpl (org.develnext.jphp.core.compiler.jvm.node.MethodNodeImpl)1 UITree (org.develnext.jphp.swing.classes.components.tree.UITree)1 WrapTreeNode (org.develnext.jphp.swing.classes.components.tree.WrapTreeNode)1 ClassReader (org.objectweb.asm.ClassReader)1 Memory (php.runtime.Memory)1 CriticalException (php.runtime.exceptions.CriticalException)1 BaseObject (php.runtime.lang.BaseObject)1 ObjectMemory (php.runtime.memory.ObjectMemory)1 TrueMemory (php.runtime.memory.TrueMemory)1