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

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

Introduction

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

Prototype

void outdent();

Source Link

Usage

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

License:Apache License

private void writeNewViolations(final SourceWriter sw, final String violationName) {
    // Set<ConstraintViolation<T>> violations =
    sw.print("Set<ConstraintViolation<T>> ");
    sw.print(violationName);/*from  ww w  . ja  v a 2s.com*/
    sw.println(" = ");
    sw.indent();
    sw.indent();

    // new HashSet<ConstraintViolation<T>>();
    sw.println("new HashSet<ConstraintViolation<T>>();");
    sw.outdent();
    sw.outdent();
}

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

License:Apache License

private void writePropertyDescriptor(final SourceWriter sw, final PropertyDescriptor ppropertyDescription) {
    // private final PropertyDescriptor myProperty_pd =
    sw.print("private final ");
    sw.print(PropertyDescriptorImpl.class.getCanonicalName());
    sw.print(" ");
    sw.print(ppropertyDescription.getPropertyName());
    sw.println("_pd =");
    sw.indent();//from ww  w  .j ava 2 s.co  m
    sw.indent();

    // new PropertyDescriptorImpl(
    sw.println("new " + PropertyDescriptorImpl.class.getCanonicalName() + "(");
    sw.indent();
    sw.indent();

    // "myProperty",
    sw.println("\"" + ppropertyDescription.getPropertyName() + "\",");

    // MyType.class,
    sw.println(ppropertyDescription.getElementClass().getCanonicalName() + ".class,");

    // isCascaded,
    sw.print(Boolean.toString(ppropertyDescription.isCascaded()) + ",");

    // beanMetadata,
    sw.print("beanMetadata");

    // myProperty_c0,
    // myProperty_c1 );
    int count = 0;
    for (final ConstraintDescriptor<?> constraint : ppropertyDescription.getConstraintDescriptors()) {
        if (this.areConstraintDescriptorGroupsValid(constraint)) {
            sw.println(","); // Print the , for the previous line
            sw.print(this.constraintDescriptorVar(ppropertyDescription.getPropertyName(), count));
            count++;
        }
    }
    sw.println(");");

    sw.outdent();
    sw.outdent();
    sw.outdent();
    sw.outdent();
}

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

License:Apache License

private void writeValidateAllNonInheritedProperties(final SourceWriter sw) {
    // private <T> void validateAllNonInheritedProperties(
    sw.println("private <T> void validateAllNonInheritedProperties(");
    sw.indent();/*from w w  w  .ja va 2 s  . c  o  m*/
    sw.indent();

    // GwtValidationContext<T> context, BeanType object,
    // Set<ConstraintViolation<T>> violations, Class<?>... groups) {
    sw.println("GwtValidationContext<T> context,");
    sw.println(this.beanHelper.getTypeCanonicalName() + " object,");
    sw.println("Set<ConstraintViolation<T>> violations,");
    sw.println("Class<?>... groups) {");
    sw.outdent();

    for (final PropertyDescriptor p : this.beanHelper.getBeanDescriptor().getConstrainedProperties()) {
        this.writeValidatePropertyCall(sw, p, false, true);
    }

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

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

License:Apache License

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

    // GwtValidationContext<T> context, BeanType object,
    // Set<ConstraintViolation<T>> violations, Group... groups) {
    sw.indent();/* w ww .ja v  a  2  s .c  o m*/
    sw.indent();
    sw.println("GwtValidationContext<T> context,");
    sw.println(this.beanHelper.getTypeCanonicalName() + " object,");
    sw.println("Set<ConstraintViolation<T>> violations,");
    sw.println("Class<?>... groups) {");
    sw.outdent();

    // /// For each group

    // TODO(nchalko) handle the sequence in the AbstractValidator

    // See JSR 303 section 3.5
    // all reachable fields
    // all reachable getters (both) at once
    // including all reachable and cascadable associations

    sw.println("validateAllNonInheritedProperties(context, object, violations, groups);");

    // validate super classes and super interfaces
    this.writeValidateInheritance(sw, this.beanHelper.getClazz(), Stage.OBJECT, null, false, "groups");

    this.writeClassLevelConstraintsValidation(sw, "groups");

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

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

License:Apache License

/**
 * Writes the call to actually validate a constraint, including its composite constraints.
 * <p>// ww  w.  j a  va 2  s. c o m
 * If the constraint is annotated as {@link javax.validation.ReportAsSingleViolation
 * ReportAsSingleViolation}, then is called recursively and the {@code violationsVar} is changed
 * to match the {@code constraintDescriptorVar}.
 * </p>
 *
 * @param sw the Source Writer
 * @param ppropertyDescription the property
 * @param elementClass The class of the Element
 * @param constraint the constraint to validate.
 * @param constraintDescriptorVar the name of the constraintDescriptor variable.
 * @param violationsVar the name of the variable to hold violations
 * @throws UnableToCompleteException when validation can not be completed
 */
private void writeValidateConstraint(final SourceWriter sw, final PropertyDescriptor ppropertyDescription,
        final Class<?> elementClass, final ConstraintDescriptor<?> constraint,
        final String constraintDescriptorVar, final String violationsVar) throws UnableToCompleteException {
    final boolean isComposite = !constraint.getComposingConstraints().isEmpty();
    final boolean firstReportAsSingleViolation = constraint.isReportAsSingleViolation()
            && violationsVar.equals(DEFAULT_VIOLATION_VAR) && isComposite;
    final boolean reportAsSingleViolation = firstReportAsSingleViolation
            || !violationsVar.equals(DEFAULT_VIOLATION_VAR);
    final boolean hasValidator = !constraint.getConstraintValidatorClasses().isEmpty();
    final String compositeViolationsVar = constraintDescriptorVar + "_violations";

    // Only do this the first time in a constraint composition.
    if (firstReportAsSingleViolation) {
        // Report myConstraint as Single Violation
        sw.print("// Report ");
        sw.print(constraint.getAnnotation().annotationType().getCanonicalName());
        sw.println(" as Single Violation");
        this.writeNewViolations(sw, compositeViolationsVar);
    }

    if (hasValidator) {
        Class<? extends ConstraintValidator<? extends Annotation, ?>> validatorClass;
        try {
            validatorClass = getValidatorForType(constraint, elementClass);
        } catch (final UnexpectedTypeException e) {
            throw error(this.logger, e);
        }

        if (firstReportAsSingleViolation) {
            // if (!
            sw.println("if (!");
            sw.indent();
            sw.indent();
        }

        // validate(myContext, violations object, value, new MyValidator(),
        // constraintDescriptor, groups));
        sw.print("validate(myContext, ");
        sw.print(violationsVar);
        sw.print(", object, value, ");
        sw.print("new ");
        sw.print(validatorClass.getCanonicalName());
        sw.print("(), ");
        sw.print(constraintDescriptorVar);
        sw.print(", groups)");
        if (firstReportAsSingleViolation) {
            // ) {
            sw.println(") {");
            sw.outdent();

        } else if (reportAsSingleViolation) {
            if (isComposite) {
                // ||
                sw.println(" ||");
            }
        } else {
            // ;
            sw.println(";");
        }
    } else if (!isComposite) {
        // TODO(nchalko) What does the spec say to do here.
        this.logger.log(TreeLogger.WARN, "No ConstraintValidator of " + constraint + " for "
                + ppropertyDescription.getPropertyName() + " of type " + elementClass);
    }

    if (firstReportAsSingleViolation) {
        // if (
        sw.print("if (");
        sw.indent();
        sw.indent();
    }
    int count = 0;

    for (final ConstraintDescriptor<?> compositeConstraint : constraint.getComposingConstraints()) {
        final String compositeVar = constraintDescriptorVar + "_" + count++;
        this.writeValidateConstraint(sw, ppropertyDescription, elementClass, compositeConstraint, compositeVar,
                firstReportAsSingleViolation ? compositeViolationsVar : violationsVar);
        if (reportAsSingleViolation) {
            // ||
            sw.println(" ||");
        } else {
            // ;
            sw.println(";");
        }
    }
    if (isComposite && reportAsSingleViolation) {
        // false
        sw.print("false");
    }
    if (firstReportAsSingleViolation) {
        // ) {
        sw.println(" ) {");
        sw.outdent();

        // addSingleViolation(myContext, violations, object, value,
        // constraintDescriptor);
        sw.print("addSingleViolation(myContext, violations, object, value, ");
        sw.print(constraintDescriptorVar);
        sw.println(");");

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

        if (hasValidator) {
            // }
            sw.outdent();
            sw.println("}");
        }
    }
}

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 w w  .  j  a  v  a2 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  w  w .  j a v  a2  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 w  w w  .j a  va2 s.  c o 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();//from  w w  w .ja  v a 2  s. c om
    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 {/*  w  w  w . j  a  va2s.  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("}");
}