Example usage for com.google.gwt.user.rebind SourceWriter print

List of usage examples for com.google.gwt.user.rebind SourceWriter print

Introduction

In this page you can find the example usage for com.google.gwt.user.rebind SourceWriter print.

Prototype

void print(String s);

Source Link

Usage

From source file:de.knightsoftnet.validators.rebind.GwtSpecificValidatorCreator.java

License:Apache License

private void writeValidateGetterCall(final SourceWriter sw, final PropertyDescriptor ppropertyDescription,
        final boolean useValue, final boolean honorValid) {
    // validateProperty_get<<field>>(context, violations,
    sw.print(this.validateMethodGetterName(ppropertyDescription));
    sw.print("(context, ");
    sw.print("violations, ");

    // object, object.getMyProp(),
    // or/*from   w w w.j a v  a  2s  . c o m*/
    // null, (MyType) value,
    if (useValue) {
        sw.print("null, ");
        sw.print("(");
        sw.print(this.getQualifiedSourceNonPrimitiveType(
                this.beanHelper.getElementType(ppropertyDescription, false)));
        sw.print(") value");
    } else {
        sw.print("object, ");
        final JMethod method = this.beanType.findMethod(asGetter(ppropertyDescription), NO_ARGS);
        if (method.isPublic()) {
            sw.print("object.");
            sw.print(asGetter(ppropertyDescription));
            sw.print("()");
        } else {
            this.gettersToWrap.add(method);
            sw.print(this.toWrapperName(method) + "(object)");
        }
    }
    sw.print(", ");

    // honorValid, groups);
    sw.print(Boolean.toString(honorValid));
    sw.println(", groups);");
}

From source file:de.knightsoftnet.validators.rebind.GwtSpecificValidatorCreator.java

License:Apache License

private void writeValidateIterable(final SourceWriter sw, final PropertyDescriptor ppropertyDescription) {
    // int i = 0;
    sw.println("int i = 0;");

    // for (Object instance : value) {
    sw.println("for(Object instance : value) {");
    sw.indent();//from   w  ww  . j ava2  s  .  com

    // if(instance != null && !context.alreadyValidated(instance)) {
    sw.println(" if (instance != null  && !context.alreadyValidated(instance)) {");
    sw.indent();

    // violations.addAll(
    sw.println("violations.addAll(");
    sw.indent();
    sw.indent();

    // context.getValidator().validate(
    sw.println("context.getValidator().validate(");
    sw.indent();
    sw.indent();

    final Class<?> elementClass = ppropertyDescription.getElementClass();
    if (elementClass.isArray() || List.class.isAssignableFrom(elementClass)) {
        // context.appendIndex("myProperty",i++),
        sw.print("context.appendIndex(\"");
        sw.print(ppropertyDescription.getPropertyName());
        sw.println("\",i),");
    } else {
        // context.appendIterable("myProperty"),
        sw.print("context.appendIterable(\"");
        sw.print(ppropertyDescription.getPropertyName());
        sw.println("\"),");
    }

    // instance, groups));
    sw.println("instance, groups));");
    sw.outdent();
    sw.outdent();
    sw.outdent();
    sw.outdent();

    // }
    sw.outdent();
    sw.println("}");

    // i++;
    sw.println("i++;");

    // }
    sw.outdent();
    sw.println("}");
}

From source file:de.knightsoftnet.validators.rebind.GwtSpecificValidatorCreator.java

License:Apache License

private void writeValidateMap(final SourceWriter sw, final PropertyDescriptor ppropertyDescription) {
    // for (Entry<?, Type> entry : value.entrySet()) {
    sw.print("for(");
    sw.print(Entry.class.getCanonicalName());
    sw.println("<?, ?> entry : value.entrySet()) {");
    sw.indent();/* w ww.ja v a  2  s .c  o  m*/

    // if(entry.getValue() != null &&
    // !context.alreadyValidated(entry.getValue())) {
    sw.println(" if (entry.getValue() != null && !context.alreadyValidated(entry.getValue())) {");
    sw.indent();

    // violations.addAll(
    sw.println("violations.addAll(");
    sw.indent();
    sw.indent();

    // context.getValidator().validate(
    sw.println("context.getValidator().validate(");
    sw.indent();
    sw.indent();

    // context.appendKey("myProperty",entry.getKey()),
    sw.print("context.appendKey(\"");
    sw.print(ppropertyDescription.getPropertyName());
    sw.println("\",entry.getKey()),");

    // entry.getValue(), groups));
    sw.println("entry.getValue(), groups));");
    sw.outdent();
    sw.outdent();
    sw.outdent();
    sw.outdent();

    // }
    sw.outdent();
    sw.println("}");

    // }
    sw.outdent();
    sw.println("}");
}

From source file:de.knightsoftnet.validators.rebind.GwtSpecificValidatorCreator.java

License:Apache License

private void writeValidatePropertyCall(final SourceWriter sw, final PropertyDescriptor property,
        final boolean useValue, final boolean honorValid) {
    if (useValue) {
        // boolean valueTypeMatches = false;
        sw.println("boolean valueTypeMatches = false;");
    }/*from  www  . j  a va 2 s. co m*/
    if (this.beanHelper.hasGetter(property)) {
        if (useValue) {
            // if ( value == null || value instanceof propertyType) {
            sw.print("if (value == null || value instanceof ");
            sw.print(this.getQualifiedSourceNonPrimitiveType(this.beanHelper.getElementType(property, false)));
            sw.println(") {");
            sw.indent();

            // valueTypeMatches = true;
            sw.println("valueTypeMatches = true;");
        }
        // validate_getMyProperty
        this.writeValidateGetterCall(sw, property, useValue, honorValid);
        if (useValue) {
            // }
            sw.outdent();
            sw.println("}");
        }
    }

    if (this.beanHelper.hasField(property)) {
        if (useValue) {
            // if ( value == null || value instanceof propertyType) {
            sw.print("if ( value == null || value instanceof ");
            sw.print(this.getQualifiedSourceNonPrimitiveType(this.beanHelper.getElementType(property, true)));
            sw.println(") {");
            sw.indent();

            // valueTypeMatches = true;
            sw.println("valueTypeMatches = true;");
        }
        // validate_myProperty
        this.writeValidateFieldCall(sw, property, useValue, honorValid);
        if (useValue) {
            // } else
            sw.outdent();
            sw.println("}");
        }
    }

    if (useValue && (this.beanHelper.hasGetter(property) || this.beanHelper.hasField(property))) {
        // if(!valueTypeMatches) {
        sw.println("if (!valueTypeMatches)  {");
        sw.indent();

        // throw new ValidationException(value.getClass +
        // " is not a valid type for " + propertyName);
        sw.print("throw new ValidationException");
        sw.println("(value.getClass() +\" is not a valid type for \"+ propertyName);");

        // }
        sw.outdent();
        sw.println("}");
    }
}

From source file:de.knightsoftnet.validators.rebind.GwtSpecificValidatorCreator.java

License:Apache License

private void writeValidatePropertyGroups(final SourceWriter sw) throws UnableToCompleteException {
    // public <T> void validatePropertyGroups(
    sw.println("public <T> void validatePropertyGroups(");

    // GwtValidationContext<T> context, BeanType object, String propertyName,
    // Set<ConstraintViolation<T>> violations, Class<?>... groups) throws ValidationException {
    sw.indent();// w w  w. jav a2s.c  o  m
    sw.indent();
    sw.println("GwtValidationContext<T> context,");
    sw.println(this.beanHelper.getTypeCanonicalName() + " object,");
    sw.println("String propertyName,");
    sw.println("Set<ConstraintViolation<T>> violations,");
    sw.println("Class<?>... groups) throws ValidationException {");
    sw.outdent();

    for (final PropertyDescriptor property : this.beanHelper.getBeanDescriptor().getConstrainedProperties()) {
        // if (propertyName.equals(myPropety)) {
        sw.print("if (propertyName.equals(\"");
        sw.print(property.getPropertyName());
        sw.println("\")) {");
        sw.indent();

        this.writeValidatePropertyCall(sw, property, false, false);

        // validate all super classes and interfaces
        this.writeValidateInheritance(sw, this.beanHelper.getClazz(), Stage.PROPERTY, property);

        // }
        sw.outdent();
        sw.print("} else ");
    }

    this.writeIfPropertyNameNotFound(sw);

    // }
    sw.outdent();
    sw.println("}");
}

From source file:de.knightsoftnet.validators.rebind.GwtSpecificValidatorCreator.java

License:Apache License

private void writeValidatePropertyMethod(final SourceWriter sw, final PropertyDescriptor ppropertyDescription,
        final boolean useField) throws UnableToCompleteException {
    final Class<?> elementClass = ppropertyDescription.getElementClass();
    final JType elementType = this.beanHelper.getElementType(ppropertyDescription, useField);

    // private final <T> void validateProperty_{get}<p>(
    sw.print("private final <T> void ");
    if (useField) {
        sw.print(this.validateMethodFieldName(ppropertyDescription));
    } else {/*from w ww  . jav a 2 s.c  o m*/
        sw.print(this.validateMethodGetterName(ppropertyDescription));
    }
    sw.println("(");
    sw.indent();
    sw.indent();

    // final GwtValidationContext<T> context,
    sw.println("final GwtValidationContext<T> context,");

    // final Set<ConstraintViolation<T>> violations,
    sw.println("final Set<ConstraintViolation<T>> violations,");

    // BeanType object,
    sw.println(this.beanHelper.getTypeCanonicalName() + " object,");

    // final <Type> value,
    sw.print("final ");
    sw.print(elementType.getParameterizedQualifiedSourceName());
    sw.println(" value,");

    // boolean honorValid,
    sw.println("boolean honorValid,");

    // Class<?>... groups) {
    sw.println("Class<?>... groups) {");
    sw.outdent();

    // only write the checks if the property is constrained in some way
    if (this.isPropertyConstrained(ppropertyDescription, useField)) {
        // context = context.append("myProperty");
        sw.print("final GwtValidationContext<T> myContext = context.append(\"");
        sw.print(ppropertyDescription.getPropertyName());
        sw.println("\");");

        // only check this property if the TraversableResolver says we can

        // Node leafNode = myContext.getPath().getLeafNode();
        sw.println("Node leafNode = myContext.getPath().getLeafNode();");
        // PathImpl path = myContext.getPath().getPathWithoutLeafNode();
        sw.println("PathImpl path = myContext.getPath().getPathWithoutLeafNode();");
        // boolean isReachable;
        sw.println("boolean isReachable;");
        // try {
        sw.println("try {");
        sw.indent();
        // isReachable = myContext.getTraversableResolver().isReachable(object, leafNode,
        // myContext.getRootBeanClass(), path, ElementType);
        sw.println("isReachable = myContext.getTraversableResolver().isReachable(object, "
                + "leafNode, myContext.getRootBeanClass(), path, "
                + (useField ? asLiteral(ElementType.FIELD) : asLiteral(ElementType.METHOD)) + ");");
        // } catch (Exception e) {
        sw.outdent();
        sw.println("} catch (Exception e) {");
        sw.indent();
        // throw new ValidationException("TraversableResolver isReachable caused an exception", e);
        sw.println("throw new ValidationException(\"TraversableResolver isReachable caused an "
                + "exception\", e);");
        // }
        sw.outdent();
        sw.println("}");
        // if (isReachable) {
        sw.println("if (isReachable) {");
        sw.indent();

        // TODO(nchalko) move this out of here to the Validate method
        if (ppropertyDescription.isCascaded() && this.hasValid(ppropertyDescription, useField)) {

            // if (honorValid && value != null) {
            sw.println("if (honorValid && value != null) {");
            sw.indent();
            // boolean isCascadable;
            sw.println("boolean isCascadable;");
            // try {
            sw.println("try {");
            sw.indent();
            // isCascadable = myContext.getTraversableResolver().isCascadable(object, leafNode,
            // myContext.getRootBeanClass(), path, ElementType)
            sw.println("isCascadable = myContext.getTraversableResolver().isCascadable(object, "
                    + "leafNode, myContext.getRootBeanClass(), path, "
                    + (useField ? asLiteral(ElementType.FIELD) : asLiteral(ElementType.METHOD)) + ");");
            // } catch (Exception e) {
            sw.outdent();
            sw.println("} catch (Exception e) {");
            sw.indent();
            // throw new ValidationException("TraversableResolver isReachable caused an exception", e);
            sw.println("throw new ValidationException(\"TraversableResolver isCascadable caused an "
                    + "exception\", e);");
            // }
            sw.outdent();
            sw.println("}");
            // if (isCascadable) {
            sw.println("if (isCascadable) {");
            sw.indent();

            if (isIterableOrMap(elementClass)) {
                final JClassType associationType = this.beanHelper.getAssociationType(ppropertyDescription,
                        useField);
                this.createBeanHelper(associationType);
                if (Map.class.isAssignableFrom(elementClass)) {
                    this.writeValidateMap(sw, ppropertyDescription);
                } else {
                    this.writeValidateIterable(sw, ppropertyDescription);
                }
            } else {
                this.createBeanHelper(elementClass);

                // if (!context.alreadyValidated(value)) {
                sw.println(" if (!context.alreadyValidated(value)) {");
                sw.indent();

                // violations.addAll(myContext.getValidator().validate(context, value,
                // groups));
                sw.print("violations.addAll(");
                sw.println("myContext.getValidator().validate(myContext, value, groups));");

                // }
                sw.outdent();
                sw.println("}");
            }

            // }
            sw.outdent();
            sw.println("}");
            // }
            sw.outdent();
            sw.println("}");
        }

        // It is possible for an annotation with the exact same values to be set on
        // both the field and the getter.
        // Keep track of the ones we have used to make sure we don't duplicate.
        final Set<Object> includedAnnotations = Sets.newHashSet();
        int count = 0;
        for (final ConstraintDescriptor<?> constraint : ppropertyDescription.getConstraintDescriptors()) {
            if (this.areConstraintDescriptorGroupsValid(constraint)) {
                final Object annotation = constraint.getAnnotation();
                if (this.hasMatchingAnnotation(ppropertyDescription, useField, constraint)) {
                    final String constraintDescriptorVar = this
                            .constraintDescriptorVar(ppropertyDescription.getPropertyName(), count);
                    if (includedAnnotations.contains(annotation)) {
                        // The annotation has been looked at once already during this validate property call
                        // so we know the field and the getter are both annotated with the same constraint.
                        if (!useField) {
                            this.writeValidateConstraint(sw, ppropertyDescription, elementClass, constraint,
                                    constraintDescriptorVar);
                        }
                    } else {
                        if (useField) {
                            this.writeValidateConstraint(sw, ppropertyDescription, elementClass, constraint,
                                    constraintDescriptorVar);
                        } else {
                            // The annotation hasn't been looked at twice (yet) and we are validating a getter
                            // Write the call if only the getter has this constraint applied to it
                            final boolean hasField = this.beanHelper.hasField(ppropertyDescription);
                            if (!hasField || hasField
                                    && !this.hasMatchingAnnotation(ppropertyDescription, true, constraint)) {
                                this.writeValidateConstraint(sw, ppropertyDescription, elementClass, constraint,
                                        constraintDescriptorVar);
                            }
                        }
                    }
                    includedAnnotations.add(annotation);
                }
                count++;
            }
        }
        // }
        sw.outdent();
        sw.println("}");
    }
    sw.outdent();
    sw.println("}");
}

From source file:de.knightsoftnet.validators.rebind.GwtSpecificValidatorCreator.java

License:Apache License

private void writeValidateValueGroups(final SourceWriter sw) throws UnableToCompleteException {
    // public <T> void validateValueGroups(
    sw.println("public <T> void validateValueGroups(");

    // GwtValidationContext<T> context, Class<Author> beanType, String propertyName,
    // Object value, Set<ConstraintViolation<T>> violations, Class<?>... groups) {
    sw.indent();//from w ww  .  j  ava  2s  .  c  o  m
    sw.indent();
    sw.println("GwtValidationContext<T> context,");
    sw.println("Class<" + this.beanHelper.getTypeCanonicalName() + "> beanType,");
    sw.println("String propertyName,");
    sw.println("Object value,");
    sw.println("Set<ConstraintViolation<T>> violations,");
    sw.println("Class<?>... groups) {");
    sw.outdent();

    for (final PropertyDescriptor property : this.beanHelper.getBeanDescriptor().getConstrainedProperties()) {
        // if (propertyName.equals(myPropety)) {
        sw.print("if (propertyName.equals(\"");
        sw.print(property.getPropertyName());
        sw.println("\")) {");
        sw.indent();

        if (!isIterableOrMap(property.getElementClass())) {
            this.writeValidatePropertyCall(sw, property, true, false);
        }

        // validate all super classes and interfaces
        this.writeValidateInheritance(sw, this.beanHelper.getClazz(), Stage.VALUE, property);

        // }
        sw.outdent();
        sw.print("} else ");
    }

    this.writeIfPropertyNameNotFound(sw);

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

From source file:de.knightsoftnet.validators.rebind.GwtSpecificValidatorCreator.java

License:Apache License

/**
 * @param ppropertyDescription Only used if writing a call to validate a property - otherwise can
 *        be null./*from w  w  w .ja  v  a 2 s  .c o  m*/
 * @param expandDefaultGroupSequence Only used if writing a call to validate a bean.
 * @param groupsVarName The name of the variable containing the groups.
 */
private void writeValidatorCall(final SourceWriter sw, final Class<?> type, final Stage stage,
        final PropertyDescriptor ppropertyDescription, final boolean expandDefaultGroupSequence,
        final String groupsVarName) throws UnableToCompleteException {
    if (this.cache.isClassConstrained(type) && !isIterableOrMap(type)) {
        final BeanHelper helper = this.createBeanHelper(type);
        this.beansToValidate.add(helper);
        switch (stage) {
        case OBJECT:
            // myValidator
            sw.print(helper.getValidatorInstanceName());
            if (expandDefaultGroupSequence) {
                // .expandDefaultAndValidateClassGroups(context,object,violations,groups);
                sw.println(".expandDefaultAndValidateClassGroups(context, object, violations, " + groupsVarName
                        + ");");
            } else {
                // .validateClassGroups(context,object,violations,groups);
                sw.println(".validateClassGroups(context, object, violations, " + groupsVarName + ");");
            }
            break;
        case PROPERTY:
            if (this.isPropertyConstrained(helper, ppropertyDescription)) {
                // myValidator.validatePropertyGroups(context,object
                // ,propertyName, violations, groups);
                sw.print(helper.getValidatorInstanceName());
                sw.print(".validatePropertyGroups(context, object,");
                sw.println(" propertyName, violations, " + groupsVarName + ");");
            }
            break;
        case VALUE:
            if (this.isPropertyConstrained(helper, ppropertyDescription)) {
                // myValidator.validateValueGroups(context,beanType
                // ,propertyName, value, violations, groups);
                sw.print(helper.getValidatorInstanceName());
                sw.print(".validateValueGroups(context, ");
                sw.print(helper.getTypeCanonicalName());
                sw.println(".class, propertyName, value, violations, " + groupsVarName + ");");
            }
            break;
        default:
            throw new IllegalStateException();
        }
    }
}

From source file:de.knightsoftnet.validators.rebind.ValidatorCreator.java

License:Apache License

private void writeContext(final SourceWriter sw, final BeanHelper bean, final String objectName) {
    // GwtValidationContext<MyBean> context = new GwtValidationContext<MyBean>(
    sw.print(GwtValidationContext.class.getSimpleName());
    sw.print("<T> context = new ");
    sw.print(GwtValidationContext.class.getSimpleName());
    sw.println("<T>(");
    sw.indent();//w ww  . j  ava2 s .  c o  m
    sw.indent();

    // (Class<T>) MyBean.class,
    sw.print("(Class<T>) ");
    sw.println(bean.getTypeCanonicalName() + ".class, ");

    // object,
    sw.println(objectName + ", ");

    // MyBeanValidator.INSTANCE.getConstraints(getValidationGroupsMetadata()),
    sw.print(bean.getFullyQualifiedValidatorName());
    sw.println(".INSTANCE.getConstraints(getValidationGroupsMetadata()), ");

    // getMessageInterpolator(),
    sw.println("getMessageInterpolator(), ");

    // getTraversableResolver(),
    sw.println("getTraversableResolver(), ");

    // this);
    sw.println("this);");
    sw.outdent();
    sw.outdent();
}

From source file:de.knightsoftnet.validators.rebind.ValidatorCreator.java

License:Apache License

private void writeCreateValidationGroupsMetadata(final SourceWriter sw) {
    // private static ValidationGroupsMetadata createValidationGroupsMetadata() {
    sw.println("private static ValidationGroupsMetadata createValidationGroupsMetadata() {");
    sw.indent();//from  w  w  w  .jav a  2s  .  c o  m

    // return ValidationGroupsMetadata.builder()
    sw.println("return ValidationGroupsMetadata.builder()");
    sw.indent();
    sw.indent();
    for (final Class<?> group : this.gwtValidation.groups()) {
        final GroupSequence sequenceAnnotation = group.getAnnotation(GroupSequence.class);
        Class<?>[] groups;
        if (sequenceAnnotation == null) {
            // .addGroup(<<group>>
            sw.print(".addGroup(");
            sw.print(group.getCanonicalName() + ".class");
            groups = group.getInterfaces();
        } else {
            // .addSequence(<<sequence>>
            sw.print(".addSequence(");
            sw.print(group.getCanonicalName() + ".class");
            groups = sequenceAnnotation.value();
        }
        for (final Class<?> clazz : groups) {
            // , <<group class>>
            sw.print(", ");
            sw.print(clazz.getCanonicalName() + ".class");
        }
        // )
        sw.println(")");
    }

    // .build();
    sw.println(".build();");
    sw.outdent();
    sw.outdent();

    // }
    sw.outdent();
    sw.println("}");
}