Example usage for com.google.gwt.editor.rebind.model ModelUtils getQualifiedBaseSourceName

List of usage examples for com.google.gwt.editor.rebind.model ModelUtils getQualifiedBaseSourceName

Introduction

In this page you can find the example usage for com.google.gwt.editor.rebind.model ModelUtils getQualifiedBaseSourceName.

Prototype

public static String getQualifiedBaseSourceName(JType type) 

Source Link

Document

Given a JType, return the source name of the class that is most proximately assignable to the type.

Usage

From source file:com.google.web.bindery.autobean.gwt.rebind.AutoBeanFactoryGenerator.java

License:Apache License

private String getBaseMethodDeclaration(JMethod jmethod) {
    // Foo foo, Bar bar, Baz baz
    StringBuilder parameters = new StringBuilder();
    for (JParameter param : jmethod.getParameters()) {
        parameters.append(",").append(ModelUtils.getQualifiedBaseSourceName(param.getType())).append(" ")
                .append(param.getName());
    }/*from w  ww .j  ava 2  s  .  c o m*/
    if (parameters.length() > 0) {
        parameters = parameters.deleteCharAt(0);
    }

    StringBuilder throwsDeclaration = new StringBuilder();
    if (jmethod.getThrows().length > 0) {
        for (JType thrown : jmethod.getThrows()) {
            throwsDeclaration.append(". ").append(ModelUtils.getQualifiedBaseSourceName(thrown));
        }
        throwsDeclaration.deleteCharAt(0);
        throwsDeclaration.insert(0, "throws ");
    }
    String returnName = ModelUtils.getQualifiedBaseSourceName(jmethod.getReturnType());
    assert !returnName.contains("extends");
    return String.format("%s %s(%s) %s", returnName, jmethod.getName(), parameters, throwsDeclaration);
}

From source file:com.google.web.bindery.autobean.gwt.rebind.AutoBeanFactoryGenerator.java

License:Apache License

/**
 * For interfaces that consist of nothing more than getters and setters,
 * create a map-based implementation that will allow the AutoBean's internal
 * state to be easily consumed./*from   ww w  .  j  ava  2s .  com*/
 */
private void writeCreateSimpleBean(SourceWriter sw, AutoBeanType type) {
    sw.println("@Override protected %s createSimplePeer() {", type.getPeerType().getQualifiedSourceName());
    sw.indent();
    // return new FooIntf() {
    sw.println("return new %s() {", type.getPeerType().getQualifiedSourceName());
    sw.indent();
    sw.println("private final %s data = %s.this.data;", Splittable.class.getCanonicalName(),
            type.getQualifiedSourceName());
    for (AutoBeanMethod method : type.getMethods()) {
        JMethod jmethod = method.getMethod();
        JType returnType = jmethod.getReturnType();
        sw.println("public %s {", getBaseMethodDeclaration(jmethod));
        sw.indent();
        switch (method.getAction()) {
        case GET: {
            String castType;
            if (returnType.isPrimitive() != null) {
                castType = returnType.isPrimitive().getQualifiedBoxedSourceName();
                // Boolean toReturn = Other.this.getOrReify("foo");
                sw.println("%s toReturn = %s.this.getOrReify(\"%s\");", castType, type.getSimpleSourceName(),
                        method.getPropertyName());
                // return toReturn == null ? false : toReturn;
                sw.println("return toReturn == null ? %s : toReturn;",
                        returnType.isPrimitive().getUninitializedFieldExpression());
            } else if (returnType
                    .equals(context.getTypeOracle().findType(Splittable.class.getCanonicalName()))) {
                sw.println("return data.isNull(\"%1$s\") ? null : data.get(\"%1$s\");",
                        method.getPropertyName());
            } else {
                // return (ReturnType) Outer.this.getOrReify(\"foo\");
                castType = ModelUtils.getQualifiedBaseSourceName(returnType);
                sw.println("return (%s) %s.this.getOrReify(\"%s\");", castType, type.getSimpleSourceName(),
                        method.getPropertyName());
            }
        }
            break;
        case SET:
        case SET_BUILDER: {
            JParameter param = jmethod.getParameters()[0];
            // Other.this.setProperty("foo", parameter);
            sw.println("%s.this.setProperty(\"%s\", %s);", type.getSimpleSourceName(), method.getPropertyName(),
                    param.getName());
            if (JBeanMethod.SET_BUILDER.equals(method.getAction())) {
                sw.println("return this;");
            }
            break;
        }
        case CALL:
            // return com.example.Owner.staticMethod(Outer.this, param,
            // param);
            JMethod staticImpl = method.getStaticImpl();
            if (!returnType.equals(JPrimitiveType.VOID)) {
                sw.print("return ");
            }
            sw.print("%s.%s(%s.this", staticImpl.getEnclosingType().getQualifiedSourceName(),
                    staticImpl.getName(), type.getSimpleSourceName());
            for (JParameter param : jmethod.getParameters()) {
                sw.print(", %s", param.getName());
            }
            sw.println(");");
            break;
        default:
            throw new RuntimeException();
        }
        sw.outdent();
        sw.println("}");
    }
    sw.outdent();
    sw.println("};");
    sw.outdent();
    sw.println("}");
}

From source file:com.google.web.bindery.autobean.gwt.rebind.AutoBeanFactoryGenerator.java

License:Apache License

/**
 * Create the shim instance of the AutoBean's peer type that lets us hijack
 * the method calls. Using a shim type, as opposed to making the AutoBean
 * implement the peer type directly, means that there can't be any conflicts
 * between methods in the peer type and methods declared in the AutoBean
 * implementation./*from  ww w.j a va2 s. com*/
 */
private void writeShim(SourceWriter sw, AutoBeanType type) throws UnableToCompleteException {
    // private final FooImpl shim = new FooImpl() {
    sw.println("private final %1$s shim = new %1$s() {", type.getPeerType().getQualifiedSourceName());
    sw.indent();
    for (AutoBeanMethod method : type.getMethods()) {
        JMethod jmethod = method.getMethod();
        String methodName = jmethod.getName();
        JParameter[] parameters = jmethod.getParameters();
        if (isObjectMethodImplementedByShim(jmethod)) {
            // Skip any methods declared on Object, since we have special handling
            continue;
        }

        // foo, bar, baz
        StringBuilder arguments = new StringBuilder();
        {
            for (JParameter param : parameters) {
                arguments.append(",").append(param.getName());
            }
            if (arguments.length() > 0) {
                arguments = arguments.deleteCharAt(0);
            }
        }

        sw.println("public %s {", getBaseMethodDeclaration(jmethod));
        sw.indent();

        switch (method.getAction()) {
        case GET:
            /*
             * The getter call will ensure that any non-value return type is
             * definitely wrapped by an AutoBean instance.
             */
            sw.println("%s toReturn = %s.this.getWrapped().%s();",
                    ModelUtils.getQualifiedBaseSourceName(jmethod.getReturnType()), type.getSimpleSourceName(),
                    methodName);

            // Non-value types might need to be wrapped
            writeReturnWrapper(sw, type, method);
            sw.println("return toReturn;");
            break;
        case SET:
        case SET_BUILDER:
            // getWrapped().setFoo(foo);
            sw.println("%s.this.getWrapped().%s(%s);", type.getSimpleSourceName(), methodName,
                    parameters[0].getName());
            // FooAutoBean.this.set("setFoo", foo);
            sw.println("%s.this.set(\"%s\", %s);", type.getSimpleSourceName(), methodName,
                    parameters[0].getName());
            if (JBeanMethod.SET_BUILDER.equals(method.getAction())) {
                sw.println("return this;");
            }
            break;
        case CALL:
            // XXX How should freezing and calls work together?
            // sw.println("checkFrozen();");
            if (JPrimitiveType.VOID.equals(jmethod.getReturnType())) {
                // getWrapped().doFoo(params);
                sw.println("%s.this.getWrapped().%s(%s);", type.getSimpleSourceName(), methodName, arguments);
                // call("doFoo", null, params);
                sw.println("%s.this.call(\"%s\", null%s %s);", type.getSimpleSourceName(), methodName,
                        arguments.length() > 0 ? "," : "", arguments);
            } else {
                // Type toReturn = getWrapped().doFoo(params);
                sw.println("%s toReturn = %s.this.getWrapped().%s(%s);",
                        ModelUtils.ensureBaseType(jmethod.getReturnType()).getQualifiedSourceName(),
                        type.getSimpleSourceName(), methodName, arguments);
                // Non-value types might need to be wrapped
                writeReturnWrapper(sw, type, method);
                // call("doFoo", toReturn, params);
                sw.println("%s.this.call(\"%s\", toReturn%s %s);", type.getSimpleSourceName(), methodName,
                        arguments.length() > 0 ? "," : "", arguments);
                sw.println("return toReturn;");
            }
            break;
        default:
            throw new RuntimeException();
        }
        sw.outdent();
        sw.println("}");
    }

    // Delegate equals(), hashCode(), and toString() to wrapped object
    sw.println("@Override public boolean equals(Object o) {");
    sw.indentln("return this == o || getWrapped().equals(o);");
    sw.println("}");
    sw.println("@Override public int hashCode() {");
    sw.indentln("return getWrapped().hashCode();");
    sw.println("}");
    sw.println("@Override public String toString() {");
    sw.indentln("return getWrapped().toString();");
    sw.println("}");

    // End of shim field declaration and assignment
    sw.outdent();
    sw.println("};");
}

From source file:com.google.web.bindery.requestfactory.gwt.rebind.model.RequestFactoryModel.java

License:Apache License

private EntityProxyModel getEntityProxyType(JClassType entityProxyType) throws UnableToCompleteException {
    entityProxyType = ModelUtils.ensureBaseType(entityProxyType);
    EntityProxyModel toReturn = peers.get(entityProxyType);
    if (toReturn == null) {
        EntityProxyModel.Builder inProgress = peerBuilders.get(entityProxyType);
        if (inProgress != null) {
            toReturn = inProgress.peek();
        }/*from w  w  w.  j  av a  2 s. com*/
    }
    if (toReturn == null) {
        EntityProxyModel.Builder builder = new EntityProxyModel.Builder();
        peerBuilders.put(entityProxyType, builder);

        // Validate possible super-proxy types first
        for (JClassType supertype : entityProxyType.getFlattenedSupertypeHierarchy()) {
            List<EntityProxyModel> superTypes = new ArrayList<EntityProxyModel>();
            if (supertype != entityProxyType && shouldAttemptProxyValidation(supertype)) {
                superTypes.add(getEntityProxyType(supertype));
            }
            builder.setSuperProxyTypes(superTypes);
        }

        builder.setQualifiedBinaryName(ModelUtils.getQualifiedBaseBinaryName(entityProxyType));
        builder.setQualifiedSourceName(ModelUtils.getQualifiedBaseSourceName(entityProxyType));
        if (entityProxyInterface.isAssignableFrom(entityProxyType)) {
            builder.setType(Type.ENTITY);
        } else if (valueProxyInterface.isAssignableFrom(entityProxyType)) {
            builder.setType(Type.VALUE);
        } else {
            poison("The type %s is not assignable to either %s or %s",
                    entityProxyInterface.getQualifiedSourceName(),
                    valueProxyInterface.getQualifiedSourceName());
            // Cannot continue, since knowing the behavior is crucial
            die(poisonedMessage());
        }

        // Get the server domain object type
        ProxyFor proxyFor = entityProxyType.getAnnotation(ProxyFor.class);
        ProxyForName proxyForName = entityProxyType.getAnnotation(ProxyForName.class);
        JsonRpcProxy jsonRpcProxy = entityProxyType.getAnnotation(JsonRpcProxy.class);
        if (proxyFor == null && proxyForName == null && jsonRpcProxy == null) {
            poison("The %s type does not have a @%s, @%s, or @%s annotation",
                    entityProxyType.getQualifiedSourceName(), ProxyFor.class.getSimpleName(),
                    ProxyForName.class.getSimpleName(), JsonRpcProxy.class.getSimpleName());
        }

        // Look at the methods declared on the EntityProxy
        List<RequestMethod> requestMethods = new ArrayList<RequestMethod>();
        Map<String, JMethod> duplicatePropertyGetters = new HashMap<String, JMethod>();
        for (JMethod method : entityProxyType.getInheritableMethods()) {
            if (method.getEnclosingType().equals(entityProxyInterface)) {
                // Ignore methods on EntityProxy
                continue;
            }
            RequestMethod.Builder methodBuilder = new RequestMethod.Builder();
            methodBuilder.setDeclarationMethod(entityProxyType, method);

            JType transportedType;
            String name = method.getName();
            if (JBeanMethod.GET.matches(method)) {
                transportedType = method.getReturnType();
                String propertyName = JBeanMethod.GET.inferName(method);
                JMethod previouslySeen = duplicatePropertyGetters.get(propertyName);
                if (previouslySeen == null) {
                    duplicatePropertyGetters.put(propertyName, method);
                } else {
                    poison("Duplicate accessors for property %s: %s() and %s()", propertyName,
                            previouslySeen.getName(), method.getName());
                }

            } else if (JBeanMethod.SET.matches(method) || JBeanMethod.SET_BUILDER.matches(method)) {
                transportedType = method.getParameters()[0].getType();

            } else if (name.equals("stableId") && method.getParameters().length == 0) {
                // Ignore any overload of stableId
                continue;
            } else {
                poison("The method %s is neither a getter nor a setter", method.getReadableDeclaration());
                continue;
            }
            validateTransportableType(methodBuilder, transportedType, false);
            RequestMethod requestMethod = methodBuilder.build();
            requestMethods.add(requestMethod);
        }
        builder.setExtraTypes(checkExtraTypes(entityProxyType, false)).setRequestMethods(requestMethods);

        toReturn = builder.build();
        peers.put(entityProxyType, toReturn);
        peerBuilders.remove(entityProxyType);
    }
    return toReturn;
}

From source file:de.csenk.gwt.commons.bean.rebind.observe.impl.ObservableBeanFactoryCreator.java

License:Apache License

/**
 * @param beanWriter/*ww w.  j ava 2  s.c o m*/
 * @param beanModel
 */
private void writeShimFields(SourceWriter beanWriter, ObservableBeanModel beanModel) {
    for (ObservableBeanPropertyModel propertyModel : beanModel.getProperties().values()) {
        beanWriter.println("private final %1$s<%2$s> %3$s = new %1$s<%2$s>() {",
                AbstractObservableProperty.class.getSimpleName(),
                ModelUtils.getQualifiedBaseSourceName(propertyModel.getType()), propertyModel.getName());
        beanWriter.indent();
        beanWriter.println();

        beanWriter.println("public void setValue(%s value) {",
                ModelUtils.getQualifiedBaseSourceName(propertyModel.getType()));
        beanWriter.indentln("%s.this.observed.%s(value);", beanModel.getSimpleImplementationTypeName(),
                propertyModel.getSetter().getMethod().getName());
        beanWriter.println("}");
        beanWriter.println();

        beanWriter.println("public %s getValue() {",
                ModelUtils.getQualifiedBaseSourceName(propertyModel.getType()));
        beanWriter.indentln("return %s.this.observed.%s();", beanModel.getSimpleImplementationTypeName(),
                propertyModel.getGetter().getMethod().getName());
        beanWriter.println("}");
        beanWriter.println();

        beanWriter.outdent();
        beanWriter.println("};");
        beanWriter.println();
    }
}

From source file:de.csenk.gwt.commons.bean.rebind.SourceGeneration.java

License:Apache License

/**
 * @param method// w w w . j a va2s  .  c o  m
 * @return
 */
public static String getBaseMethodDeclaration(JMethod method) {
    final List<String> paramDeclarations = Lists.transform(Lists.newArrayList(method.getParameters()),
            new Function<JParameter, String>() {

                @Override
                @Nullable
                public String apply(@Nullable JParameter input) {
                    return String.format("%s %s", ModelUtils.getQualifiedBaseSourceName(input.getType()),
                            input.getName());
                }

            });

    final List<String> throwDeclarations = Lists.transform(Lists.newArrayList(method.getThrows()),
            new Function<JType, String>() {

                @Override
                @Nullable
                public String apply(@Nullable JType input) {
                    return ModelUtils.getQualifiedBaseSourceName(input);
                }

            });

    final String paramListDeclaration = Joiner.on(", ").join(paramDeclarations);
    final String throwsDeclaration = throwDeclarations.size() > 0
            ? String.format("throws %s", Joiner.on(", ").join(throwDeclarations))
            : "";
    final String returnName = ModelUtils.getQualifiedBaseSourceName(method.getReturnType());

    return String.format("%s %s(%s) %s", returnName, method.getName(), paramListDeclaration, throwsDeclaration);
}