CPD Results

The following document contains the results of PMD's CPD 5.3.5.

Duplications

File Line
net\sf\oval\constraint\EqualToFieldCheck.java 43
net\sf\oval\constraint\NotEqualToFieldCheck.java 43
    public void configure(final EqualToField constraintAnnotation) {
        super.configure(constraintAnnotation);
        setFieldName(constraintAnnotation.value());
        setDeclaringClass(constraintAnnotation.declaringClass());
        setUseGetter(constraintAnnotation.useGetter());
    }

    @Override
    protected Map<String, String> createMessageVariables() {
        final Map<String, String> messageVariables = getCollectionFactory().createMap(2);
        messageVariables.put("fieldName", fieldName);
        messageVariables.put("declaringClass", declaringClass == null || declaringClass == Void.class ? null : declaringClass.getName());
        messageVariables.put("useGetter", Boolean.toString(useGetter));
        return messageVariables;
    }

    public Class<?> getDeclaringClass() {
        return declaringClass;
    }

    public String getFieldName() {
        return fieldName;
    }

    public boolean isSatisfied(final Object validatedObject, final Object valueToValidate, final OValContext context, final Validator validator) {
        if (valueToValidate == null)
            return true;

        final Class<?> clazz = validatedObject.getClass();

        final Object valueToCompare;
        if (useGetter) {
            final Method getter = ReflectionUtils.getGetterRecursive(clazz, fieldName);
            if (getter == null)
                throw new MethodNotFoundException("Getter for field <" + fieldName + "> not found in class <" + clazz + "> or it's super classes.");

            try {
                valueToCompare = getter.invoke(validatedObject);
            } catch (final Exception ex) {
                throw new InvokingMethodFailedException(getter.getName(), validatedObject, ContextCache.getMethodReturnValueContext(getter), ex);
            }
        } else {
            final Field field = ReflectionUtils.getFieldRecursive(clazz, fieldName);

            if (field == null)
                throw new FieldNotFoundException("Field <" + fieldName + "> not found in class <" + clazz + "> or it's super classes.");

            valueToCompare = ReflectionUtils.getFieldValue(field, validatedObject);
        }

        if (valueToCompare == null)
            return false;
File Line
net\sf\oval\guard\Guard.java 550
net\sf\oval\guard\Guard.java 712
        final ClassChecks cc = getClassChecks(method.getDeclaringClass());

        final boolean checkInvariants = isInvariantsEnabled && cc.isCheckInvariants && !ReflectionUtils.isPrivate(method) && !ReflectionUtils.isProtected(
            method);

        // if static method use the declaring class as guardedObject
        if (guardedObject == null && ReflectionUtils.isStatic(method)) {
            guardedObject = method.getDeclaringClass();
        }

        final List<ConstraintViolation> violations = getCollectionFactory().createList();
        currentViolations.get().add(violations);

        try {
            // check invariants
            if (checkInvariants || cc.methodsWithCheckInvariantsPre.contains(method)) {
                validateInvariants(guardedObject, violations, null);
            }

            if (isPreConditionsEnabled) {
                // method parameter validation
                if (violations.size() == 0 && args.length > 0) {
                    validateMethodParameters(guardedObject, method, args, violations);
                }

                // @Pre validation
                if (violations.size() == 0) {
                    validateMethodPre(guardedObject, method, args, violations);
                }
            }
        } catch (final ValidationFailedException ex) {
            throw translateException(ex);
        } finally {
            currentViolations.get().removeLast();
        }

        final ProbeModeListener pml = isProbeModeFeatureUsed ? objectsInProbeMode.get().get(guardedObject) : null;
        if (pml != null) {
            pml.onMethodCall(method, args);
        }

        if (violations.size() > 0) {
            final ConstraintsViolatedException violationException = new ConstraintsViolatedException(violations);
            if (isListenersFeatureUsed) {
                notifyListeners(guardedObject, violationException);
            }

            // don't throw an exception if the method is a setter and suppressing for precondition is enabled
            if (pml != null) {
                pml.onConstraintsViolatedException(violationException);
                return null;
File Line
net\sf\oval\constraint\MemberOfCheck.java 38
net\sf\oval\constraint\NotMemberOfCheck.java 38
    public void configure(final MemberOf constraintAnnotation) {
        super.configure(constraintAnnotation);
        setIgnoreCase(constraintAnnotation.ignoreCase());
        setMembers(constraintAnnotation.value());
    }

    @Override
    protected Map<String, String> createMessageVariables() {
        final Map<String, String> messageVariables = getCollectionFactory().createMap(2);
        messageVariables.put("ignoreCase", Boolean.toString(ignoreCase));
        messageVariables.put("members", StringUtils.implode(members, ","));
        return messageVariables;
    }

    @Override
    protected ConstraintTarget[] getAppliesToDefault() {
        return new ConstraintTarget[] { ConstraintTarget.VALUES };
    }

    public List<String> getMembers() {
        final List<String> v = getCollectionFactory().createList();
        v.addAll(members);
        return v;
    }

    private List<String> getMembersLowerCase() {
        if (membersLowerCase == null) {
            membersLowerCase = getCollectionFactory().createList(members.size());
            for (final String val : members) {
                membersLowerCase.add(val.toLowerCase(Validator.getLocaleProvider().getLocale()));
            }
        }
        return membersLowerCase;
    }

    public boolean isIgnoreCase() {
        return ignoreCase;
    }

    public boolean isSatisfied(final Object validatedObject, final Object valueToValidate, final OValContext context, final Validator validator) {
        if (valueToValidate == null)
            return true;

        if (ignoreCase)
            return getMembersLowerCase().contains(valueToValidate.toString().toLowerCase(Validator.getLocaleProvider().getLocale()));
File Line
net\sf\oval\constraint\MatchPatternCheck.java 41
net\sf\oval\constraint\NotMatchPatternCheck.java 38
        setMatchAll(constraintAnnotation.matchAll());

        synchronized (patterns) {
            patterns.clear();
            final String[] stringPatterns = constraintAnnotation.pattern();
            final int[] f = constraintAnnotation.flags();
            for (int i = 0, l = stringPatterns.length; i < l; i++) {
                final int flag = f.length > i ? f[i] : 0;
                final Pattern p = Pattern.compile(stringPatterns[i], flag);
                patterns.add(p);
            }
            requireMessageVariablesRecreation();
        }
    }

    @Override
    protected Map<String, String> createMessageVariables() {
        final Map<String, String> messageVariables = getCollectionFactory().createMap(2);
        messageVariables.put("pattern", patterns.size() == 1 ? patterns.get(0).toString() : patterns.toString());
        return messageVariables;
    }

    @Override
    protected ConstraintTarget[] getAppliesToDefault() {
        return new ConstraintTarget[] { ConstraintTarget.VALUES };
    }

    public Pattern[] getPatterns() {
        synchronized (patterns) {
            return patterns.toArray(new Pattern[patterns.size()]);
        }
    }

    public boolean isMatchAll() {
File Line
net\sf\oval\constraint\InstanceOfAnyCheck.java 33
net\sf\oval\constraint\InstanceOfCheck.java 33
    public void configure(final InstanceOfAny constraintAnnotation) {
        super.configure(constraintAnnotation);
        setTypes(constraintAnnotation.value());
    }

    @Override
    protected Map<String, String> createMessageVariables() {
        final Map<String, String> messageVariables = getCollectionFactory().createMap(2);
        if (types.length == 1) {
            messageVariables.put("types", types[0].getName());
        } else {
            final String[] classNames = new String[types.length];
            for (int i = 0, l = classNames.length; i < l; i++) {
                classNames[i] = types[i].getName();
            }
            messageVariables.put("types", StringUtils.implode(classNames, ","));
        }
        return messageVariables;
    }

    public Class<?>[] getTypes() {
        return types;
    }

    public boolean isSatisfied(final Object validatedObject, final Object valueToValidate, final OValContext context, final Validator validator) {
        if (valueToValidate == null)
            return true;

        for (final Class<?> type : types)
            if (type.isInstance(valueToValidate))
File Line
net\sf\oval\guard\Guard.java 1205
net\sf\oval\guard\Guard.java 1263
                values.put("_old", oldValues.get(check));
                if (hasParameters) {
                    values.put("_args", args);
                    for (int i = 0; i < args.length; i++) {
                        values.put(parameterNames[i], args[i]);
                    }
                } else {
                    values.put("_args", ArrayUtils.EMPTY_OBJECT_ARRAY);
                }

                if (!eng.evaluateAsBoolean(check.getExpression(), values)) {
                    final Map<String, String> messageVariables = getCollectionFactory().createMap(2);
                    messageVariables.put("expression", check.getExpression());
                    final String errorMessage = renderMessage(context, null, check.getMessage(), messageVariables);

                    violations.add(new ConstraintViolation(check, errorMessage, validatedObject, null, context));
                }
            }
        } catch (final OValException ex) {
            throw new ValidationFailedException("Method post conditions validation failed. Method: " + method + " Validated object: " + validatedObject, ex);
File Line
net\sf\oval\constraint\MatchPatternCheck.java 97
net\sf\oval\constraint\NotMatchPatternCheck.java 79
    }

    public void setPattern(final Pattern pattern) {
        synchronized (patterns) {
            patterns.clear();
            patterns.add(pattern);
        }
        requireMessageVariablesRecreation();
    }

    public void setPattern(final String pattern, final int flags) {
        synchronized (patterns) {
            patterns.clear();
            patterns.add(Pattern.compile(pattern, flags));
        }
        requireMessageVariablesRecreation();
    }

    public void setPatterns(final Collection<Pattern> patterns) {
        synchronized (this.patterns) {
            this.patterns.clear();
            this.patterns.addAll(patterns);
        }
        requireMessageVariablesRecreation();
    }

    public void setPatterns(final Pattern... patterns) {
        synchronized (this.patterns) {
            this.patterns.clear();
            ArrayUtils.addAll(this.patterns, patterns);
        }
        requireMessageVariablesRecreation();
    }
}
File Line
net\sf\oval\configuration\annotation\AnnotationsConfigurer.java 245
net\sf\oval\configuration\annotation\BeanValidationAnnotationsConfigurer.java 196
        }
    }

    public ClassConfiguration getClassConfiguration(final Class<?> clazz) {
        final ClassConfiguration classCfg = new ClassConfiguration();
        classCfg.type = clazz;

        final Guarded guarded = clazz.getAnnotation(Guarded.class);

        if (guarded == null) {
            classCfg.applyFieldConstraintsToConstructors = false;
            classCfg.applyFieldConstraintsToSetters = false;
            classCfg.assertParametersNotNull = false;
            classCfg.checkInvariants = false;
            classCfg.inspectInterfaces = false;
        } else {
            classCfg.applyFieldConstraintsToConstructors = guarded.applyFieldConstraintsToConstructors();
            classCfg.applyFieldConstraintsToSetters = guarded.applyFieldConstraintsToSetters();
            classCfg.assertParametersNotNull = guarded.assertParametersNotNull();
            classCfg.checkInvariants = guarded.checkInvariants();
            classCfg.inspectInterfaces = guarded.inspectInterfaces();
        }
File Line
net\sf\oval\configuration\annotation\AnnotationsConfigurer.java 137
net\sf\oval\configuration\annotation\BeanValidationAnnotationsConfigurer.java 141
                } else if (annotation.annotationType().isAnnotationPresent(Constraints.class)) {
                    initializeChecks(annotation, checks);
                }

            if (checks.size() > 0) {
                if (classCfg.fieldConfigurations == null) {
                    classCfg.fieldConfigurations = cf.createSet(2);
                }

                final FieldConfiguration fc = new FieldConfiguration();
                fc.name = field.getName();
                fc.checks = checks;
                classCfg.fieldConfigurations.add(fc);
                checks = cf.createList(2); // create a new list for the next field with checks
            }
        }
    }

    /**
     * configure method return value and parameter checks
     */
    protected void configureMethodChecks(final ClassConfiguration classCfg) {
        final CollectionFactory cf = getCollectionFactory();

        List<Check> returnValueChecks = cf.createList(2);
File Line
net\sf\oval\constraint\FutureCheck.java 48
net\sf\oval\constraint\PastCheck.java 48
    public void configure(final Future constraintAnnotation) {
        super.configure(constraintAnnotation);
        setTolerance(constraintAnnotation.tolerance());
    }

    @Override
    protected ConstraintTarget[] getAppliesToDefault() {
        return new ConstraintTarget[] { ConstraintTarget.VALUES };
    }

    public long getTolerance() {
        return tolerance;
    }

    private Boolean isJavaTimeSatisfied(final long now, final Object valueToValidate) {
        // check if the value is a LocalDate
        if (valueToValidate instanceof java.time.LocalDate)
            return ((java.time.LocalDate) valueToValidate).atStartOfDay().atZone(java.time.ZoneId.systemDefault()).toInstant().toEpochMilli() > now;
File Line
net\sf\oval\constraint\MemberOfCheck.java 84
net\sf\oval\constraint\NotMemberOfCheck.java 84
        return members.contains(valueToValidate.toString());
    }

    public void setIgnoreCase(final boolean ignoreCase) {
        this.ignoreCase = ignoreCase;
        requireMessageVariablesRecreation();
    }

    public void setMembers(final List<String> members) {
        this.members = getCollectionFactory().createList();
        this.members.addAll(members);
        membersLowerCase = null;
        requireMessageVariablesRecreation();
    }

    public void setMembers(final String... members) {
        this.members = getCollectionFactory().createList();
        ArrayUtils.addAll(this.members, members);
        membersLowerCase = null;
        requireMessageVariablesRecreation();
    }
}