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 writeFields(final SourceWriter sw) throws UnableToCompleteException {

    // Create a static array of all valid property names.
    BeanInfo beanInfo;//w  ww. j a  v a2s  . co  m
    try {
        beanInfo = Introspector.getBeanInfo(this.beanHelper.getClazz());
    } catch (final IntrospectionException e) {
        throw error(this.logger, e);
    }

    // private static final java.util.List<String> ALL_PROPERTY_NAMES =
    sw.println("private static final java.util.List<String> ALL_PROPERTY_NAMES = ");
    sw.indent();
    sw.indent();

    // Collections.<String>unmodifiableList(
    sw.println("java.util.Collections.<String>unmodifiableList(");
    sw.indent();
    sw.indent();

    // java.util.Arrays.<String>asList(
    sw.print("java.util.Arrays.<String>asList(");

    // "foo","bar" );
    sw.print(Joiner.on(",").join(Iterables.transform(ImmutableList.copyOf(beanInfo.getPropertyDescriptors()),
            Functions.compose(TO_LITERAL, PROPERTY_DESCRIPTOR_TO_NAME))));
    sw.println("));");
    sw.outdent();
    sw.outdent();
    sw.outdent();
    sw.outdent();

    // Write the metadata for the bean
    this.writeBeanMetadata(sw);
    sw.println();

    // Create a variable for each constraint of each property
    for (final PropertyDescriptor p : this.beanHelper.getBeanDescriptor().getConstrainedProperties()) {
        int count = 0;
        for (final ConstraintDescriptor<?> constraint : p.getConstraintDescriptors()) {
            final org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl<?> constraintHibernate = (org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl<?>) constraint;
            if (this.areConstraintDescriptorGroupsValid(constraint)) {
                this.writeConstraintDescriptor(sw, constraint, constraintHibernate.getElementType(),
                        convertConstraintOriginEnum(constraintHibernate.getDefinedOn()),
                        this.constraintDescriptorVar(p.getPropertyName(), count++));
            }
        }
        this.writePropertyDescriptor(sw, p);
        if (p.isCascaded()) {
            this.beansToValidate.add(isIterableOrMap(p.getElementClass())
                    ? this.createBeanHelper(this.beanHelper.getAssociationType(p, true))
                    : this.createBeanHelper(p.getElementClass()));
        }
    }

    // Create a variable for each constraint of this class.
    int count = 0;
    for (final ConstraintDescriptor<?> constraint : this.beanHelper.getBeanDescriptor()
            .getConstraintDescriptors()) {
        final org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl<?> constraintHibernate = (org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl<?>) constraint;
        if (this.areConstraintDescriptorGroupsValid(constraint)) {
            this.writeConstraintDescriptor(sw, constraint, ElementType.TYPE,
                    convertConstraintOriginEnum(constraintHibernate.getDefinedOn()),
                    this.constraintDescriptorVar("this", count++));
        }
    }

    // Now write the BeanDescriptor after we already have the
    // PropertyDescriptors and class constraints
    this.writeBeanDescriptor(sw);
    sw.println();
}

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

License:Apache License

private void writeFieldWrapperMethod(final SourceWriter sw, final JField field) {
    this.writeUnsafeNativeLongIfNeeded(sw, field.getType());

    // private native fieldType _fieldName(com.example.Bean object) /*-{
    sw.print("private native ");

    sw.print(field.getType().getQualifiedSourceName());
    sw.print(" ");/*from w  w w  .ja va2  s  .  c o m*/
    sw.print(this.toWrapperName(field));
    sw.print("(");
    sw.print(field.getEnclosingType().getQualifiedSourceName());
    sw.println(" object) /*-{");
    sw.indent();

    // return object.@com.examples.Bean::myMethod();
    sw.print("return object.@");
    sw.print(field.getEnclosingType().getQualifiedSourceName());
    sw.print("::" + field.getName());
    sw.println(";");

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

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

License:Apache License

private void writeGetDescriptor(final SourceWriter sw) {
    // public GwtBeanDescriptor<beanType>
    // getConstraints(ValidationGroupsMetadata validationGroupsMetadata) {
    sw.print("public ");
    sw.print("GwtBeanDescriptor<" + this.beanHelper.getTypeCanonicalName() + "> ");
    sw.println("getConstraints(ValidationGroupsMetadata validationGroupsMetadata) {");
    sw.indent();//from  www.  j  a  v a2  s . c o m

    // beanDescriptor.setValidationGroupsMetadata(validationGroupsMetadata);
    sw.println("beanDescriptor.setValidationGroupsMetadata(validationGroupsMetadata);");

    // return beanDescriptor;
    sw.println("return beanDescriptor;");

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

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

License:Apache License

private void writeGetterWrapperMethod(final SourceWriter sw, final JMethod method) {
    this.writeUnsafeNativeLongIfNeeded(sw, method.getReturnType());

    // private native fieldType _getter(Bean object) /*={
    sw.print("private native ");
    sw.print(method.getReturnType().getQualifiedSourceName());
    sw.print(" ");// w w  w. j a  v a 2 s  .c o  m
    sw.print(this.toWrapperName(method));
    sw.print("(");
    sw.print(this.beanType.getName());
    sw.println(" object) /*-{");
    sw.indent();

    // return object.@com.examples.Bean::myMethod()();
    sw.print("return object.");
    sw.print(method.getJsniSignature());
    sw.println("();");

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

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

License:Apache License

private void writeIfPropertyNameNotFound(final SourceWriter sw) {
    // if (!ALL_PROPERTY_NAMES.contains(propertyName)) {
    sw.println(" if (!ALL_PROPERTY_NAMES.contains(propertyName)) {");
    sw.indent();//from   w ww. ja v  a 2 s.  c  o m

    // throw new IllegalArgumentException(propertyName
    // +"is not a valid property of myClass");
    sw.print("throw new ");
    sw.print(IllegalArgumentException.class.getCanonicalName());
    sw.print("( propertyName +\" is not a valid property of ");
    sw.print(this.beanType.getQualifiedSourceName());
    sw.println("\");");

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

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

License:Apache License

private void writeNewAnnotation(final SourceWriter sw,
        final ConstraintDescriptor<? extends Annotation> constraint) throws UnableToCompleteException {
    final Annotation annotation = constraint.getAnnotation();
    final Class<? extends Annotation> annotationType = annotation.annotationType();

    // new MyAnnotation () {
    sw.print("new ");
    sw.print(annotationType.getCanonicalName());
    sw.println("(){");
    sw.indent();//  w  w w.ja va2  s.  c om
    sw.indent();

    // public Class<? extends Annotation> annotationType() { return
    // MyAnnotation.class; }
    sw.print("public Class<? extends Annotation> annotationType() {  return ");
    sw.print(annotationType.getCanonicalName());
    sw.println(".class; }");

    for (final Method method : annotationType.getMethods()) {
        // method.isAbstract would be better
        if (method.getDeclaringClass().equals(annotation.annotationType())) {
            // public returnType method() { return value ;}
            sw.print("public ");
            sw.print(method.getReturnType().getCanonicalName()); // TODO handle
                                                                 // generics
            sw.print(" ");
            sw.print(method.getName());
            sw.print("() { return ");

            try {
                final Object value = method.invoke(annotation);
                sw.print(asLiteral(value));
            } catch (final IllegalArgumentException e) {
                throw error(this.logger, e);
            } catch (final IllegalAccessException e) {
                throw error(this.logger, e);
            } catch (final InvocationTargetException e) {
                throw error(this.logger, e);
            }
            sw.println(";}");
        }
    }

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

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);/*ww  w. j  av a  2 s .c om*/
    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  w w w .j  a  v  a2 s  .com*/
    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

/**
 * Writes the call to actually validate a constraint, including its composite constraints.
 * <p>/*  ww  w  .j  av  a2 s.  co  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 writeValidateFieldCall(final SourceWriter sw, final PropertyDescriptor ppropertyDescription,
        final boolean useValue, final boolean honorValid) {
    final String propertyName = ppropertyDescription.getPropertyName();

    // validateProperty_<<field>>(context,
    sw.print(this.validateMethodFieldName(ppropertyDescription));
    sw.print("(context, ");
    sw.print("violations, ");

    // null, (MyType) value,
    // or/*w w  w.j  a va 2 s  .com*/
    // object, object.getLastName(),
    if (useValue) {
        sw.print("null, ");
        sw.print("(");
        sw.print(this.getQualifiedSourceNonPrimitiveType(
                this.beanHelper.getElementType(ppropertyDescription, true)));
        sw.print(") value");
    } else {
        sw.print("object, ");
        final JField field = this.beanType.getField(propertyName);
        if (field.isPublic()) {
            sw.print("object.");
            sw.print(propertyName);
        } else {
            this.fieldsToWrap.add(field);
            sw.print(this.toWrapperName(field) + "(object)");
        }
    }
    sw.print(", ");

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