Search in sources :

Example 1 with StubStrategy

use of org.wildfly.iiop.openjdk.rmi.marshal.strategy.StubStrategy in project wildfly by wildfly.

the class IIOPStubCompiler method generateMethodCode.

/**
     * Generates the code of a given method within a stub class.
     *
     * @param asm           the <code>ProxyAssembler</code> used to assemble
     *                      the method code
     * @param superclass    the superclass of the stub class within which the
     *                      method will be generated
     * @param m             a <code>Method</code> instance describing the
     *                      method declaration by an RMI/IDL interface
     * @param idlName       a string with the method name mapped to IDL
     * @param strategyField a string with the name of the strategy field that
     *                      will be associated with the generated method
     * @param initMethod    a string with the name of the static initialization
     *                      method that will be associated with the generated
     *                      method.
     */
private static void generateMethodCode(ClassFile asm, Class<?> superclass, Method m, String idlName, String strategyField, String initMethod) {
    Class<?> returnType = m.getReturnType();
    Class<?>[] paramTypes = m.getParameterTypes();
    Class<?>[] exceptions = m.getExceptionTypes();
    // Generate a static field with the StubStrategy for the method
    asm.addField(Modifier.PRIVATE + Modifier.STATIC, strategyField, StubStrategy.class);
    // Generate the method code
    final CodeAttribute ca = asm.addMethod(m).getCodeAttribute();
    // The method code issues a call
    // super.invoke*(idlName, strategyField, args)
    ca.aload(0);
    ca.ldc(idlName);
    ca.getstatic(asm.getName(), strategyField, StubStrategy.class);
    // Push args
    if (paramTypes.length == 0) {
        ca.iconst(0);
        ca.anewarray(Object.class.getName());
    //asm.pushField(Util.class, "NOARGS");
    } else {
        ca.iconst(paramTypes.length);
        ca.anewarray(Object.class.getName());
        int index = 1;
        for (int j = 0; j < paramTypes.length; j++) {
            Class<?> type = paramTypes[j];
            ca.dup();
            ca.iconst(j);
            if (!type.isPrimitive()) {
                // object or array
                ca.aload(index);
            } else if (type.equals(double.class)) {
                ca.dload(index);
                Boxing.boxDouble(ca);
                index++;
            } else if (type.equals(long.class)) {
                ca.lload(index);
                Boxing.boxLong(ca);
                index++;
            } else if (type.equals(float.class)) {
                ca.fload(index);
                Boxing.boxFloat(ca);
            } else {
                ca.iload(index);
                Boxing.boxIfNessesary(ca, DescriptorUtils.makeDescriptor(type));
            }
            index++;
            ca.aastore();
        }
    }
    // Generate the call to an invoke* method ot the superclass
    String invoke = "invoke";
    String ret = "Ljava/lang/Object;";
    if (returnType.isPrimitive() && returnType != Void.TYPE) {
        String typeName = returnType.getName();
        invoke += (Character.toUpperCase(typeName.charAt(0)) + typeName.substring(1));
        ret = DescriptorUtils.makeDescriptor(returnType);
    }
    ca.invokevirtual(superclass.getName(), invoke, "(Ljava/lang/String;Lorg/wildfly/iiop/openjdk/rmi/marshal/strategy/StubStrategy;[Ljava/lang/Object;)" + ret);
    if (!returnType.isPrimitive() && returnType != Object.class) {
        ca.checkcast(returnType);
    }
    ca.returnInstruction();
    // Generate a static method that initializes the method's strategy field
    final CodeAttribute init = asm.addMethod(Modifier.PRIVATE + Modifier.STATIC, initMethod, "V").getCodeAttribute();
    int i;
    int len;
    // Push first argument for StubStrategy constructor:
    // array with abbreviated names of the param marshallers
    len = paramTypes.length;
    init.iconst(len);
    init.anewarray(String.class.getName());
    for (i = 0; i < len; i++) {
        init.dup();
        init.iconst(i);
        init.ldc(CDRStream.abbrevFor(paramTypes[i]));
        init.aastore();
    }
    // Push second argument for StubStrategy constructor:
    // array with exception repository ids
    len = exceptions.length;
    int n = 0;
    for (i = 0; i < len; i++) {
        if (!RemoteException.class.isAssignableFrom(exceptions[i])) {
            n++;
        }
    }
    init.iconst(n);
    init.anewarray(String.class.getName());
    try {
        int j = 0;
        for (i = 0; i < len; i++) {
            if (!RemoteException.class.isAssignableFrom(exceptions[i])) {
                init.dup();
                init.iconst(j);
                init.ldc(ExceptionAnalysis.getExceptionAnalysis(exceptions[i]).getExceptionRepositoryId());
                init.aastore();
                j++;
            }
        }
    } catch (RMIIIOPViolationException e) {
        throw EjbLogger.ROOT_LOGGER.exceptionRepositoryNotFound(exceptions[i].getName(), e.getLocalizedMessage());
    }
    // Push third argument for StubStrategy constructor:
    // array with exception class names
    init.iconst(n);
    init.anewarray(String.class.getName());
    int j = 0;
    for (i = 0; i < len; i++) {
        if (!RemoteException.class.isAssignableFrom(exceptions[i])) {
            init.dup();
            init.iconst(j);
            init.ldc(exceptions[i].getName());
            init.aastore();
            j++;
        }
    }
    // Push fourth argument for StubStrategy constructor:
    // abbreviated name of the return value marshaller
    init.ldc(CDRStream.abbrevFor(returnType));
    // Push fifth argument for StubStrategy constructor:
    // null (no ClassLoader specified)
    init.aconstNull();
    // Constructs the StubStrategy
    init.invokestatic(StubStrategy.class.getName(), "forMethod", "([Ljava/lang/String;[Ljava/lang/String;[Ljava/lang/String;Ljava/lang/String;Ljava/lang/ClassLoader;)Lorg/wildfly/iiop/openjdk/rmi/marshal/strategy/StubStrategy;");
    // Set the strategy field of this stub class
    init.putstatic(asm.getName(), strategyField, StubStrategy.class);
    init.returnInstruction();
}
Also used : CodeAttribute(org.jboss.classfilewriter.code.CodeAttribute) StubStrategy(org.wildfly.iiop.openjdk.rmi.marshal.strategy.StubStrategy) RMIIIOPViolationException(org.wildfly.iiop.openjdk.rmi.RMIIIOPViolationException) RemoteException(java.rmi.RemoteException)

Aggregations

RemoteException (java.rmi.RemoteException)1 CodeAttribute (org.jboss.classfilewriter.code.CodeAttribute)1 RMIIIOPViolationException (org.wildfly.iiop.openjdk.rmi.RMIIIOPViolationException)1 StubStrategy (org.wildfly.iiop.openjdk.rmi.marshal.strategy.StubStrategy)1