Skip to content

Commit

Permalink
0.6.0: bug fix to allow safe types as a root message type
Browse files Browse the repository at this point in the history
  • Loading branch information
jacek99 committed Jul 7, 2015
1 parent 327a373 commit ea68605
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# speed up Gradle
org.gradle.daemon=true

version = 0.5.0
version = 0.6.0
group = org.immutizer4j
20 changes: 12 additions & 8 deletions src/main/java/org/immutizer4j/Immutizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@
* @author Jacek Furmankiewicz
*/
@Slf4j
@Value
public class Immutizer {

// may allow arrays to pass or not (no by default)
private boolean strict;
private final boolean strict;

// additional types that we were told are immutable
private ImmutableSet<Class<?>> safeTypes;
private final ImmutableSet<Class<?>> safeTypes;

private ConcurrentMap<Class<?>,ValidationResult> validationCache =
private final ConcurrentMap<Class<?>,ValidationResult> validationCache =
new MapMaker()
.concurrencyLevel(Runtime.getRuntime().availableProcessors())
.initialCapacity(100)
Expand Down Expand Up @@ -127,13 +126,18 @@ public ValidationResult getValidationResult(Class<?> clazz) {
}

// performs actual walk down the graph hierarchy starting from the root object
private ValidationResult validateType(Class<?> entity) {
return validateType(entity, new ValidationResult(ImmutableSet.<ValidationError>of()));
private ValidationResult validateType(Class<?> type) {
if (!isSafeType(type)) {
return validateType(type, new ValidationResult(ImmutableSet.<ValidationError>of()));
} else {
// safe types do not need to be tested
return new ValidationResult(ImmutableSet.<ValidationError>of());
}
}

// performs actual walk down the graph hierarchy within an existing validation process
private ValidationResult validateType(Class<?> entity, ValidationResult result) {
Class<?> current = entity;
private ValidationResult validateType(Class<?> type, ValidationResult result) {
Class<?> current = type;
while (current != null && !current.equals(Object.class)) {

Field[] fields = current.getDeclaredFields();
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/org/immutizer4j/test/BaseTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,14 @@ public void testRegexPatternReference() {
assertTrue(result.toString(),
result.toString().contains("java.util.regex.Pattern$Node.next : NON_FINAL_FIELD"));
}

/**
* Ensure the base types can be sent as a root message themselves
*/
@Test
public void testSafeTypes() {
assertEquals(true, defaultImmutizer.getValidationResult(String.class).isValid());
assertEquals(true, defaultImmutizer.getValidationResult(Integer.class).isValid());
assertEquals(true, defaultImmutizer.getValidationResult(int.class).isValid());
}
}

0 comments on commit ea68605

Please sign in to comment.