Fixup DerivedStringValidator representation checking 89/70789/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 11 Apr 2018 15:32:35 +0000 (17:32 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 11 Apr 2018 15:33:57 +0000 (17:33 +0200)
Validated DerviedStrings need to define compareTo() on the base
representation, disable the check for validators.

JIRA: YANGTOOLS-418
Change-Id: I8413674d15fc4caf7492402ddfb92e4de05d9c3f
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/AbstractDerivedStringValidator.java
yang/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/DerivedString.java

index 40c98f979703fe4caaf995144d5c8b33c6738e85..fcac4cb68ef57e09515806dee68e161b6f178a38 100644 (file)
@@ -32,7 +32,7 @@ public abstract class AbstractDerivedStringValidator<R extends DerivedString<R>,
     protected AbstractDerivedStringValidator(final DerivedStringSupport<R> representationSupport,
             final Class<T> validatedClass) {
         this.representationSupport = requireNonNull(representationSupport);
-        this.validatedClass = DerivedString.validateRepresentationClass(validatedClass);
+        this.validatedClass = DerivedString.validateValidationClass(validatedClass);
     }
 
     @Override
index 3dc15224394ec1068eee246130e3dabe438af0d2..edfcc6046d60f8fa2ddb7365253e675d84f14325 100644 (file)
@@ -15,6 +15,7 @@ import java.lang.reflect.Constructor;
 import java.lang.reflect.Modifier;
 import java.util.Arrays;
 import javax.annotation.concurrent.ThreadSafe;
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 import org.opendaylight.yangtools.concepts.Immutable;
@@ -80,11 +81,11 @@ import org.slf4j.LoggerFactory;
 @NonNullByDefault
 @ThreadSafe
 public abstract class DerivedString<R extends DerivedString<R>> implements Comparable<R>, Immutable, Serializable {
-    private static final class Validator extends ClassValue<Boolean> {
-        private static final Logger LOG = LoggerFactory.getLogger(Validator.class);
+    private abstract static class AbstractValidator extends ClassValue<Boolean> {
+        private static final Logger LOG = LoggerFactory.getLogger(AbstractValidator.class);
 
         @Override
-        protected Boolean computeValue(final @Nullable Class<?> type) {
+        protected final Boolean computeValue(final @Nullable Class<?> type) {
             // Every DerivedString representation class must:
             checkArgument(DerivedString.class.isAssignableFrom(type), "%s is not a DerivedString", type);
 
@@ -114,7 +115,6 @@ public abstract class DerivedString<R extends DerivedString<R>> implements Compa
                 checkFinalMethod(type, "support");
                 checkFinalMethod(type, "hashCode");
                 checkFinalMethod(type, "equals", Object.class);
-                checkFinalMethod(type, "compareTo", type);
             } catch (SecurityException e) {
                 LOG.warn("Cannot completely validate {}", type, e);
                 return Boolean.FALSE;
@@ -123,6 +123,8 @@ public abstract class DerivedString<R extends DerivedString<R>> implements Compa
             return Boolean.TRUE;
         }
 
+        abstract void checkCompareTo(Class<?> type);
+
         private static void checkFinalMethod(final Class<?> type, final String name) {
             try {
                 checkFinalMethod(type.getMethod(name).getModifiers(), type, name, "");
@@ -131,7 +133,7 @@ public abstract class DerivedString<R extends DerivedString<R>> implements Compa
             }
         }
 
-        private static void checkFinalMethod(final Class<?> type, final String name, final Class<?> arg) {
+        static void checkFinalMethod(final Class<?> type, final String name, final Class<?> arg) {
             final String argName = arg.getSimpleName();
             try {
                 checkFinalMethod(type.getMethod(name, arg).getModifiers(), type, name, argName);
@@ -146,7 +148,18 @@ public abstract class DerivedString<R extends DerivedString<R>> implements Compa
         }
     }
 
-    private static final ClassValue<Boolean> VALIDATED_REPRESENTATIONS = new Validator();
+    private static final ClassValue<Boolean> VALIDATED_REPRESENTATIONS = new AbstractValidator() {
+        @Override
+        void checkCompareTo(@NonNull final Class<?> type) {
+            checkFinalMethod(type, "compareTo", type);
+        }
+    };
+    private static final ClassValue<Boolean> VALIDATED_VALIDATIONS = new AbstractValidator() {
+        @Override
+        void checkCompareTo(@NonNull final Class<?> type) {
+            // Intentional no-op, as we'd need a type capture of the representation
+        }
+    };
     private static final long serialVersionUID = 1L;
 
     /**
@@ -189,4 +202,10 @@ public abstract class DerivedString<R extends DerivedString<R>> implements Compa
         VALIDATED_REPRESENTATIONS.get(representationClass);
         return representationClass;
     }
+
+    static <T extends DerivedString<?>> Class<T> validateValidationClass(final Class<T> representationClass) {
+        // Validation is reflective, cache its result
+        VALIDATED_VALIDATIONS.get(representationClass);
+        return representationClass;
+    }
 }