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>
protected AbstractDerivedStringValidator(final DerivedStringSupport<R> representationSupport,
final Class<T> validatedClass) {
this.representationSupport = requireNonNull(representationSupport);
protected AbstractDerivedStringValidator(final DerivedStringSupport<R> representationSupport,
final Class<T> validatedClass) {
this.representationSupport = requireNonNull(representationSupport);
- this.validatedClass = DerivedString.validateRepresentationClass(validatedClass);
+ this.validatedClass = DerivedString.validateValidationClass(validatedClass);
import java.lang.reflect.Modifier;
import java.util.Arrays;
import javax.annotation.concurrent.ThreadSafe;
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;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.opendaylight.yangtools.concepts.Immutable;
@NonNullByDefault
@ThreadSafe
public abstract class DerivedString<R extends DerivedString<R>> implements Comparable<R>, Immutable, Serializable {
@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);
- 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);
// Every DerivedString representation class must:
checkArgument(DerivedString.class.isAssignableFrom(type), "%s is not a DerivedString", type);
checkFinalMethod(type, "support");
checkFinalMethod(type, "hashCode");
checkFinalMethod(type, "equals", Object.class);
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;
} catch (SecurityException e) {
LOG.warn("Cannot completely validate {}", type, e);
return Boolean.FALSE;
+ abstract void checkCompareTo(Class<?> type);
+
private static void checkFinalMethod(final Class<?> type, final String name) {
try {
checkFinalMethod(type.getMethod(name).getModifiers(), type, name, "");
private static void checkFinalMethod(final Class<?> type, final String name) {
try {
checkFinalMethod(type.getMethod(name).getModifiers(), type, name, "");
- 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);
final String argName = arg.getSimpleName();
try {
checkFinalMethod(type.getMethod(name, arg).getModifiers(), type, name, argName);
- 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;
/**
private static final long serialVersionUID = 1L;
/**
VALIDATED_REPRESENTATIONS.get(representationClass);
return representationClass;
}
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;
+ }