From ea68605e26558c3d6cd4224d50b98ee4ad211449 Mon Sep 17 00:00:00 2001 From: Jacek Furmankiewicz Date: Tue, 7 Jul 2015 11:48:58 -0500 Subject: [PATCH] 0.6.0: bug fix to allow safe types as a root message type --- gradle.properties | 2 +- src/main/java/org/immutizer4j/Immutizer.java | 20 +++++++++++-------- .../java/org/immutizer4j/test/BaseTests.java | 10 ++++++++++ 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/gradle.properties b/gradle.properties index f73f294..82850b7 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ # speed up Gradle org.gradle.daemon=true -version = 0.5.0 +version = 0.6.0 group = org.immutizer4j diff --git a/src/main/java/org/immutizer4j/Immutizer.java b/src/main/java/org/immutizer4j/Immutizer.java index a52584b..f8849ee 100644 --- a/src/main/java/org/immutizer4j/Immutizer.java +++ b/src/main/java/org/immutizer4j/Immutizer.java @@ -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> safeTypes; + private final ImmutableSet> safeTypes; - private ConcurrentMap,ValidationResult> validationCache = + private final ConcurrentMap,ValidationResult> validationCache = new MapMaker() .concurrencyLevel(Runtime.getRuntime().availableProcessors()) .initialCapacity(100) @@ -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.of())); + private ValidationResult validateType(Class type) { + if (!isSafeType(type)) { + return validateType(type, new ValidationResult(ImmutableSet.of())); + } else { + // safe types do not need to be tested + return new ValidationResult(ImmutableSet.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(); diff --git a/src/test/java/org/immutizer4j/test/BaseTests.java b/src/test/java/org/immutizer4j/test/BaseTests.java index 2b4cfb9..615bec1 100644 --- a/src/test/java/org/immutizer4j/test/BaseTests.java +++ b/src/test/java/org/immutizer4j/test/BaseTests.java @@ -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()); + } }