Package org.jplastic.core

Examples of org.jplastic.core.InstructionBuilder


            String[] constructorTypes = consTypes.toArray(new String[consTypes.size()]);

            MethodNode cons = new MethodNode(ACC_PUBLIC, CONSTRUCTOR_NAME, nameCache.toMethodDescriptor("void",
                    constructorTypes), null, null);

            InstructionBuilder builder = newBuilder(cons);

            // First three arguments go to the super-class

            builder.loadThis();
            builder.loadArgument(0);
            builder.loadArgument(1);
            builder.loadArgument(2);
            builder.invokeConstructor(AbstractMethodInvocation.class, Object.class, InstanceContext.class,
                    MethodAdvice[].class);

            for (int i = 0; i < description.argumentTypes.length; i++)
            {
                String name = "p" + i;
                String type = description.argumentTypes[i];

                builder.loadThis();
                builder.loadArgument(3 + i);
                builder.putField(invocationClassName, name, type);
            }

            builder.returnResult();

            invocationClassNode.methods.add(cons);

            return constructorTypes;
        }
View Full Code Here


            return newBuilder(mn);
        }

        private void createReturnValueGetter()
        {
            InstructionBuilder builder = newMethod("getReturnValue", Object.class);

            if (isVoid)
            {
                builder.loadNull().returnResult();
            }
            else
            {
                builder.loadThis().getField(invocationClassName, RETURN_VALUE, description.returnType)
                        .boxPrimitive(description.returnType).returnResult();
            }
        }
View Full Code Here

            }
        }

        private void addReturnValueSetter()
        {
            InstructionBuilder builder = newMethod("setReturnValue", MethodInvocation.class, Object.class);

            if (isVoid)
            {
                builder.throwException(IllegalArgumentException.class, String
                        .format("Method %s of class %s is void, setting a return value is not allowed.", description,
                                className));
            }
            else
            {
                builder.loadThis().loadArgument(0);
                builder.castOrUnbox(description.returnType);
                builder.putField(invocationClassName, RETURN_VALUE, description.returnType);

                builder.loadThis().returnResult();
            }
        }
View Full Code Here

            }
        }

        private void createGetParameter()
        {
            InstructionBuilder builder = newMethod("getParameter", Object.class, int.class);

            if (description.argumentTypes.length == 0)
            {
                indexOutOfRange(builder);
            }
            else
            {
                builder.loadArgument(0);
                builder.startSwitch(0, description.argumentTypes.length - 1, new SwitchCallback()
                {

                    public void doSwitch(SwitchBlock block)
                    {
                        for (int i = 0; i < description.argumentTypes.length; i++)
                        {
                            final int index = i;

                            block.addCase(i, false, new InstructionBuilderCallback()
                            {

                                public void doBuild(InstructionBuilder builder)
                                {
                                    String type = description.argumentTypes[index];

                                    builder.loadThis();
                                    builder.getField(invocationClassName, "p" + index, type).boxPrimitive(type)
                                            .returnResult();
                                }
                            });
                        }
                    }
View Full Code Here

            builder.throwException(IllegalArgumentException.class, "Parameter index out of range.");
        }

        private void createSetParameter()
        {
            InstructionBuilder builder = newMethod("setParameter", MethodInvocation.class, int.class, Object.class);

            if (description.argumentTypes.length == 0)
            {
                indexOutOfRange(builder);
            }
            else
            {
                builder.loadArgument(0).startSwitch(0, description.argumentTypes.length - 1, new SwitchCallback()
                {

                    public void doSwitch(SwitchBlock block)
                    {
                        for (int i = 0; i < description.argumentTypes.length; i++)
                        {
                            final int index = i;

                            block.addCase(i, true, new InstructionBuilderCallback()
                            {

                                public void doBuild(InstructionBuilder builder)
                                {
                                    String type = description.argumentTypes[index];

                                    builder.loadThis();
                                    builder.loadArgument(1).castOrUnbox(type);
                                    builder.putField(invocationClassName, "p" + index, type);
                                }
                            });
                        }
                    }
                });

                builder.loadThis().returnResult();
            }
        }
View Full Code Here

        }

        /** Invoke the "new" method, and deal with the return value and/or thrown exceptions. */
        private void createProceedToAdvisedMethod()
        {
            InstructionBuilder builder = newMethod("proceedToAdvisedMethod", void.class);

            if (!isVoid)
                builder.loadThis();

            builder.loadThis().invoke(AbstractMethodInvocation.class, Object.class, "getInstance").checkcast(className);

            // Load up each parameter
            for (int i = 0; i < description.argumentTypes.length; i++)
            {
                String type = description.argumentTypes[i];

                builder.loadThis().getField(invocationClassName, "p" + i, type);
            }

            builder.startTryCatch(new TryCatchCallback()
            {
                public void doBlock(TryCatchBlock block)
                {
                    block.addTry(new InstructionBuilderCallback()
                    {

                        public void doBuild(InstructionBuilder builder)
                        {
                            builder.invokeVirtual(className, description.returnType, newMethodName,
                                    description.argumentTypes);

                            if (!isVoid)
                                builder.putField(invocationClassName, RETURN_VALUE, description.returnType);

                            builder.returnResult();
                        }
                    });

                    for (String exceptionName : description.checkedExceptionTypes)
                    {
                        block.addCatch(exceptionName, new InstructionBuilderCallback()
                        {
                            public void doBuild(InstructionBuilder builder)
                            {
                                builder.loadThis().swap();
                                builder.invoke(AbstractMethodInvocation.class, MethodInvocation.class,
                                        "setCheckedException", Exception.class);

                                builder.returnResult();
                            }
                        });
                    }
                }
            });
View Full Code Here

            advisedMethodNode.tryCatchBlocks.clear();

            if (advisedMethodNode.localVariables != null)
                advisedMethodNode.localVariables.clear();

            InstructionBuilder builder = newBuilder(description, advisedMethodNode);

            builder.newInstance(invocationClassName).dupe(0);

            // Now load up the parameters to the constructor

            builder.loadThis();
            builder.loadThis().getField(className, getInstanceContextFieldName(), constructorTypes[1]);
            builder.loadThis().getField(className, fieldName, constructorTypes[2]);

            // Load up the actual method parameters

            builder.loadArguments();
            builder.invokeConstructor(invocationClassName, constructorTypes);

            // That leaves an instance of the invocation class on the stack. If the method is void
            // and throws no checked exceptions, then the variable actually isn't used. This code
            // should be refactored a bit once there are tests for those cases.

            builder.startVariable("invocation", invocationClassName, new InstructionBuilderCallback()
            {
                public void doBuild(InstructionBuilder builder)
                {
                    builder.dupe(0).storeVariable("invocation");

                    builder.invoke(AbstractMethodInvocation.class, MethodInvocation.class, "proceed");

                    if (description.checkedExceptionTypes.length > 0)
                    {
                        builder.invoke(MethodInvocation.class, boolean.class, "didThrowCheckedException");

                        builder.ifZero(null, new InstructionBuilderCallback()
                        {
                            public void doBuild(InstructionBuilder builder)
                            {
                                builder.loadVariable("invocation").loadTypeConstant(Exception.class);
                                builder.invokeVirtual(invocationClassName, Throwable.class.getName(),
                                        "getCheckedException", Class.class.getName());
                                builder.throwException();
                            }
                        });
                    }

                    if (!isVoid)
                        builder.loadVariable("invocation").getField(invocationClassName, RETURN_VALUE,
                                description.returnType);

                    builder.returnResult();
                }
            });
        }
View Full Code Here

TOP

Related Classes of org.jplastic.core.InstructionBuilder

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.