@Test public void testValidateJavaClassName() { Object source = new Object(); String attribute = "xyz"; assertNull(BeanValidationFailure.validateJavaClassName(source, attribute, "a")); assertNull(BeanValidationFailure.validateJavaClassName(source, attribute, "a.b")); assertNull(BeanValidationFailure.validateJavaClassName(source, attribute, "XYZ._AA_A")); assertNull(BeanValidationFailure.validateJavaClassName(source, attribute, "abc[]")); assertNotNull(BeanValidationFailure.validateJavaClassName(source, attribute, "")); assertNotNull(BeanValidationFailure.validateJavaClassName(source, attribute, ".XYZ._AA_A")); assertNotNull(BeanValidationFailure.validateJavaClassName(source, attribute, "a..b")); assertNotNull(BeanValidationFailure.validateJavaClassName(source, attribute, "a?c")); assertNotNull(BeanValidationFailure.validateJavaClassName(source, attribute, "a.")); assertNotNull(BeanValidationFailure.validateJavaClassName(source, attribute, "ab]c[")); assertNotNull(BeanValidationFailure.validateJavaClassName(source, attribute, "abc[[]")); }
public static ValidationFailure validateJavaClassName( Object bean, String attribute, String identifier) { ValidationFailure emptyFailure = validateNotEmpty(bean, attribute, identifier); if (emptyFailure != null) { return emptyFailure; } char c = identifier.charAt(0); if (!Character.isJavaIdentifierStart(c)) { return new BeanValidationFailure(bean, attribute, validationMessage( attribute, " starts with invalid character: " + c)); } if (identifier.endsWith("[]")) { identifier = identifier.substring(0, identifier.length() - 2); } boolean wasDot = false; for (int i = 1; i < identifier.length(); i++) { c = identifier.charAt(i); if (c == '.') { if (wasDot || i + 1 == identifier.length()) { return new BeanValidationFailure(bean, attribute, validationMessage( attribute, " is not a valid Java Class Name: " + identifier)); } wasDot = true; continue; } if (!Character.isJavaIdentifierPart(c)) { return new BeanValidationFailure(bean, attribute, validationMessage( attribute, " contains invalid character: " + c)); } wasDot = false; } return null; }
BeanValidationFailure extends SimpleValidationFailure { public static ValidationFailure validateJavaClassName( Object bean, String attribute, String identifier) { ValidationFailure emptyFailure = validateNotEmpty(bean, attribute, identifier); if (emptyFailure != null) { return emptyFailure; } char c = identifier.charAt(0); if (!Character.isJavaIdentifierStart(c)) { return new BeanValidationFailure(bean, attribute, validationMessage( attribute, " starts with invalid character: " + c)); } if (identifier.endsWith("[]")) { identifier = identifier.substring(0, identifier.length() - 2); } boolean wasDot = false; for (int i = 1; i < identifier.length(); i++) { c = identifier.charAt(i); if (c == '.') { if (wasDot || i + 1 == identifier.length()) { return new BeanValidationFailure(bean, attribute, validationMessage( attribute, " is not a valid Java Class Name: " + identifier)); } wasDot = true; continue; } if (!Character.isJavaIdentifierPart(c)) { return new BeanValidationFailure(bean, attribute, validationMessage( attribute, " contains invalid character: " + c)); } wasDot = false; } return null; } }
BeanValidationFailure extends SimpleValidationFailure { public static ValidationFailure validateJavaClassName( Object bean, String attribute, String identifier) { ValidationFailure emptyFailure = validateNotEmpty(bean, attribute, identifier); if (emptyFailure != null) { return emptyFailure; } char c = identifier.charAt(0); if (!Character.isJavaIdentifierStart(c)) { return new BeanValidationFailure(bean, attribute, validationMessage( attribute, " starts with invalid character: " + c)); } if (identifier.endsWith("[]")) { identifier = identifier.substring(0, identifier.length() - 2); } boolean wasDot = false; for (int i = 1; i < identifier.length(); i++) { c = identifier.charAt(i); if (c == '.') { if (wasDot || i + 1 == identifier.length()) { return new BeanValidationFailure(bean, attribute, validationMessage( attribute, " is not a valid Java Class Name: " + identifier)); } wasDot = true; continue; } if (!Character.isJavaIdentifierPart(c)) { return new BeanValidationFailure(bean, attribute, validationMessage( attribute, " contains invalid character: " + c)); } wasDot = false; } return null; } BeanValidationFailure(Object source, String property, Object error); }
BeanValidationFailure extends SimpleValidationFailure { public static ValidationFailure validateJavaClassName( Object bean, String attribute, String identifier) { ValidationFailure emptyFailure = validateNotEmpty(bean, attribute, identifier); if (emptyFailure != null) { return emptyFailure; } char c = identifier.charAt(0); if (!Character.isJavaIdentifierStart(c)) { return new BeanValidationFailure(bean, attribute, validationMessage( attribute, " starts with invalid character: " + c)); } if (identifier.endsWith("[]")) { identifier = identifier.substring(0, identifier.length() - 2); } boolean wasDot = false; for (int i = 1; i < identifier.length(); i++) { c = identifier.charAt(i); if (c == '.') { if (wasDot || i + 1 == identifier.length()) { return new BeanValidationFailure(bean, attribute, validationMessage( attribute, " is not a valid Java Class Name: " + identifier)); } wasDot = true; continue; } if (!Character.isJavaIdentifierPart(c)) { return new BeanValidationFailure(bean, attribute, validationMessage( attribute, " contains invalid character: " + c)); } wasDot = false; } return null; } BeanValidationFailure(Object source, String property, Object error); static ValidationFailure validateNotEmpty(
            Object bean,
            String attribute,
            Collection<?> value); static ValidationFailure validateMandatory(
            Object bean,
            String attribute,
            Object value); static ValidationFailure validateMandatory(Object bean, String attribute); static ValidationFailure validateNotNull(
            Object bean,
            String attribute,
            Object value); static ValidationFailure validateNotEmpty(
            Object bean,
            String attribute,
            String value); static ValidationFailure validateJavaClassName(
            Object bean,
            String attribute,
            String identifier); String getProperty(); @Override String toString(); }
BeanValidationFailure extends SimpleValidationFailure { public static ValidationFailure validateJavaClassName( Object bean, String attribute, String identifier) { ValidationFailure emptyFailure = validateNotEmpty(bean, attribute, identifier); if (emptyFailure != null) { return emptyFailure; } char c = identifier.charAt(0); if (!Character.isJavaIdentifierStart(c)) { return new BeanValidationFailure(bean, attribute, validationMessage( attribute, " starts with invalid character: " + c)); } if (identifier.endsWith("[]")) { identifier = identifier.substring(0, identifier.length() - 2); } boolean wasDot = false; for (int i = 1; i < identifier.length(); i++) { c = identifier.charAt(i); if (c == '.') { if (wasDot || i + 1 == identifier.length()) { return new BeanValidationFailure(bean, attribute, validationMessage( attribute, " is not a valid Java Class Name: " + identifier)); } wasDot = true; continue; } if (!Character.isJavaIdentifierPart(c)) { return new BeanValidationFailure(bean, attribute, validationMessage( attribute, " contains invalid character: " + c)); } wasDot = false; } return null; } BeanValidationFailure(Object source, String property, Object error); static ValidationFailure validateNotEmpty(
            Object bean,
            String attribute,
            Collection<?> value); static ValidationFailure validateMandatory(
            Object bean,
            String attribute,
            Object value); static ValidationFailure validateMandatory(Object bean, String attribute); static ValidationFailure validateNotNull(
            Object bean,
            String attribute,
            Object value); static ValidationFailure validateNotEmpty(
            Object bean,
            String attribute,
            String value); static ValidationFailure validateJavaClassName(
            Object bean,
            String attribute,
            String identifier); String getProperty(); @Override String toString();  }