Example usage for com.google.gwt.core.ext.typeinfo JType getJNISignature

List of usage examples for com.google.gwt.core.ext.typeinfo JType getJNISignature

Introduction

In this page you can find the example usage for com.google.gwt.core.ext.typeinfo JType getJNISignature.

Prototype

String getJNISignature();

Source Link

Document

Returns the turns the <a href="http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#14152"> field descriptor"</a> for a type as specified by the Java Virtual Machine Specification.

Usage

From source file:com.github.nmorel.gwtjackson.rebind.property.FieldWriteAccessor.java

License:Apache License

@Override
protected Accessor getAccessor(final String beanName, final boolean useMethod, final boolean useJsni,
        Object... obj) {/*from ww w .j a v a2  s. c  o  m*/
    final JType[] fieldsType;
    final JClassType enclosingType;
    if (useMethod) {
        fieldsType = method.get().getParameterTypes();
        enclosingType = method.get().getEnclosingType();
    } else {
        fieldsType = new JType[] { field.get().getType() };
        enclosingType = field.get().getEnclosingType();
    }

    String params = parametersToString(fieldsType, new Converter() {
        @Override
        public String convert(int index, JType type) {
            return "$L";
        }
    });

    if (!useJsni) {
        if (useMethod) {
            return new Accessor(CodeBlock.builder()
                    .add(beanName + "." + method.get().getName() + "(" + params + ")", obj).build());
        } else {
            return new Accessor(CodeBlock.builder()
                    .add(beanName + "." + field.get().getName() + " = " + params, obj).build());
        }
    }

    JsniCodeBlockBuilder jsniCode = JsniCodeBlockBuilder.builder();

    Converter typeToVariableName = new Converter() {
        @Override
        public String convert(int index, JType type) {
            return "value" + index;
        }
    };
    String values = parametersToString(fieldsType, typeToVariableName);

    if (useMethod) {
        String jni = parametersToString(fieldsType, null, new Converter() {
            @Override
            public String convert(int index, JType type) {
                return type.getJNISignature();
            }
        });
        jsniCode.addStatement("bean.@$L::$L($L)($L)", enclosingType.getQualifiedSourceName(),
                method.get().getName(), jni, values);
    } else {
        jsniCode.addStatement("bean.@$L::$L = $L", enclosingType.getQualifiedSourceName(),
                field.get().getName(), values);
    }

    MethodSpec.Builder additionalMethodBuilder = MethodSpec.methodBuilder("setValueWithJsni")
            .addModifiers(Modifier.PRIVATE, Modifier.NATIVE).addParameter(typeName(enclosingType), "bean")
            .addCode(jsniCode.build());
    for (int i = 0; i < fieldsType.length; i++) {
        JType fieldType = fieldsType[i];
        additionalMethodBuilder.addParameter(typeName(fieldType), typeToVariableName.convert(i, fieldType));
    }
    MethodSpec additionalMethod = additionalMethodBuilder.build();

    CodeBlock accessor = CodeBlock.builder()
            .add("$N($L, $L)", additionalMethod, beanName, CodeBlock.builder().add(params, obj).build())
            .build();

    return new Accessor(accessor, additionalMethod);
}

From source file:com.gwtent.gen.GenUtils.java

License:Apache License

public static Class<?> gwtTypeToJavaClass(JType type) {
    try {//from   ww  w  . j  a  va  2  s.  co  m
        return Class.forName(
                type.getJNISignature().substring(1, type.getJNISignature().length() - 1).replace('/', '.'));
    } catch (ClassNotFoundException e) {
        throw new CheckedExceptionWrapper("Cann't get class from gwt JClassType." + e.getMessage(), e);
    }
}

From source file:com.hiramchirino.restygwt.rebind.RestServiceClassCreator.java

License:Apache License

private JClassType getCallbackTypeGenericClass(final JClassType callbackType) throws UnableToCompleteException {
    return branch("getCallbackTypeGenericClass()", new Branch<JClassType>() {
        public JClassType execute() throws UnableToCompleteException {

            for (JMethod method : callbackType.getMethods()) {
                debug("checking method: " + method.getName());
                if (method.getName().equals("onSuccess")) {
                    JParameter[] parameters = method.getParameters();
                    debug("checking method params: " + parameters.length);
                    if (parameters.length == 2) {
                        debug("checking first param: " + parameters[0].getType());
                        if (parameters[0].getType() == METHOD_TYPE) {
                            debug("checking 2nd param: " + parameters[1].getType());
                            JType param2Type = parameters[1].getType();
                            JClassType type = param2Type.isClassOrInterface();
                            if (type == null) {
                                error("The type of the callback not supported: "
                                        + param2Type.getJNISignature());
                            }/*from ww w.ja  v  a  2s.c  o m*/
                            debug("match: " + type);
                            return type;
                        }
                    }
                }
            }
            error("The type of the callback could not be determined: "
                    + callbackType.getParameterizedQualifiedSourceName());
            return null;

        }
    });
}

From source file:com.vaadin.server.widgetsetutils.ConnectorBundleLoaderFactory.java

License:Apache License

private void writeJsniInvoker(TreeLogger logger, SplittingSourceWriter w, JClassType type, JMethod method)
        throws UnableToCompleteException {
    w.println("new JsniInvoker() {");
    w.indent();//from   ww w  .java2  s  . c  o m

    w.println("protected native Object jsniInvoke(Object target, %s<Object> params) /*-{ ",
            JsArrayObject.class.getName());
    w.indent();

    JType returnType = method.getReturnType();
    boolean hasReturnType = !"void".equals(returnType.getQualifiedSourceName());

    // Note that void is also a primitive type
    boolean hasPrimitiveReturnType = hasReturnType && returnType.isPrimitive() != null;

    if (hasReturnType) {
        w.print("return ");

        if (hasPrimitiveReturnType) {
            // Integer.valueOf(expression);
            w.print("@%s::valueOf(%s)(", returnType.isPrimitive().getQualifiedBoxedSourceName(),
                    returnType.getJNISignature());

            // Implementation tested briefly, but I don't dare leave it
            // enabled since we are not using it in the framework and we
            // have not tests for it.
            logger.log(Type.ERROR,
                    "JSNI invocation is not yet supported for methods with "
                            + "primitive return types. Change your method "
                            + "to public to be able to use conventional" + " Java invoking instead.");
            throw new UnableToCompleteException();
        }
    }

    JType[] parameterTypes = method.getParameterTypes();

    w.print("target.@%s::" + method.getName() + "(*)(", method.getEnclosingType().getQualifiedSourceName());
    for (int i = 0; i < parameterTypes.length; i++) {
        if (i != 0) {
            w.print(", ");
        }

        w.print("params[" + i + "]");

        JPrimitiveType primitive = parameterTypes[i].isPrimitive();
        if (primitive != null) {
            // param.intValue();
            w.print(".@%s::%sValue()()", primitive.getQualifiedBoxedSourceName(),
                    primitive.getQualifiedSourceName());
        }
    }

    if (hasPrimitiveReturnType) {
        assert hasReturnType;
        w.print(")");
    }

    w.println(");");

    if (!hasReturnType) {
        w.println("return null;");
    }

    w.outdent();
    w.println("}-*/;");

    w.outdent();
    w.print("}");
}

From source file:org.fusesource.restygwt.rebind.RestServiceClassCreator.java

License:Apache License

private JClassType getCallbackTypeGenericClass(final JClassType callbackType) throws UnableToCompleteException {
    return branch("getCallbackTypeGenericClass()", new Branch<JClassType>() {
        public JClassType execute() throws UnableToCompleteException {

            for (JMethod method : callbackType.getOverridableMethods()) {
                debug("checking method: " + method.getName());
                if (method.getName().equals("onSuccess")) {
                    JParameter[] parameters = method.getParameters();
                    debug("checking method params: " + parameters.length);
                    if (parameters.length == 2) {
                        debug("checking first param: " + parameters[0].getType());
                        if (parameters[0].getType() == METHOD_TYPE) {
                            debug("checking 2nd param: " + parameters[1].getType());
                            JType param2Type = parameters[1].getType();
                            JClassType type = param2Type.isClassOrInterface();
                            if (type == null) {
                                error("The type of the callback not supported: "
                                        + param2Type.getJNISignature());
                            }/*from  ww  w.j  a v a 2s  .  c om*/
                            debug("match: " + type);
                            return type;
                        }
                    }
                }
            }
            error("The type of the callback could not be determined: "
                    + callbackType.getParameterizedQualifiedSourceName());
            return null;

        }
    });
}

From source file:org.jboss.errai.codegen.meta.impl.gwt.GWTClass.java

License:Apache License

public static Class<?> getPrimitiveOrClass(final JParameter parm) throws ClassNotFoundException {
    final JType type = parm.getType();
    final String name = type.isArray() != null ? type.getJNISignature().replace("/", ".")
            : type.getQualifiedSourceName();

    if (parm.getType().isPrimitive() != null) {
        final char sig = parm.getType().isPrimitive().getJNISignature().charAt(0);

        switch (sig) {
        case 'Z':
            return boolean.class;
        case 'B':
            return byte.class;
        case 'C':
            return char.class;
        case 'D':
            return double.class;
        case 'F':
            return float.class;
        case 'I':
            return int.class;
        case 'J':
            return long.class;
        case 'S':
            return short.class;
        case 'V':
            return void.class;
        default:/*  w w  w .j  a  v  a  2  s .c  om*/
            return null;
        }
    } else {
        return Class.forName(name, false, Thread.currentThread().getContextClassLoader());
    }
}

From source file:org.jboss.errai.ioc.rebind.ioc.InjectUtil.java

License:Apache License

public static Class<?> getPrimitiveOrClass(JParameter parm) throws ClassNotFoundException {
    JType type = parm.getType();
    String name = type.isArray() != null ? type.getJNISignature().replace("/", ".")
            : type.getQualifiedSourceName();

    if (parm.getType().isPrimitive() != null) {
        char sig = parm.getType().isPrimitive().getJNISignature().charAt(0);

        switch (sig) {
        case 'Z':
            return boolean.class;
        case 'B':
            return byte.class;
        case 'C':
            return char.class;
        case 'D':
            return double.class;
        case 'F':
            return float.class;
        case 'I':
            return int.class;
        case 'J':
            return long.class;
        case 'S':
            return short.class;
        case 'V':
            return void.class;
        default://ww w  .  j a  v  a2s.co  m
            return null;
        }
    } else {
        return Class.forName(name, false, Thread.currentThread().getContextClassLoader());
    }

}