CPD Results

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

Duplications

File Line
info/magnolia/config/ByteBuddyMutableWrapperHelper.java 292
info/magnolia/config/MutableWrapper.java 355
        }

        private void invokeSetter(String propertyName, Object value) {
            // track modified property
            modifiedPropertyNames.add(propertyName);
            // Remember the new explicit property value
            propertyValueCache.put(propertyName, value);
        }

        private Object invokeGetter(String propertyName, Method getterMethod) {
            if (propertyValueCache.containsKey(propertyName)) {
                return propertyValueCache.get(propertyName);
            }

            final Object fallbackValue;
            try {
                fallbackValue = getterMethod.invoke(getTarget());
            } catch (IllegalAccessException | InvocationTargetException e) {
                log.warn("Failed to invoke a fallback {} call due to a reflection operation problem: {}, returning null", getterMethod.getName(), e.getMessage(), e);
                return null;
            }

            if (fallbackValue == null) {
                return null;
            }

            Object wrappedValue;

            if (fallbackValue instanceof Collection) {
                wrappedValue = wrapCollection((Collection) fallbackValue);
            } else if (fallbackValue instanceof Map) {
                wrappedValue = wrapMap((Map) fallbackValue);
            } else {
                wrappedValue = MutableWrapper.wrap(fallbackValue);
            }

            propertyValueCache.put(propertyName, wrappedValue);
            return wrappedValue;
        }

        private Map<?, ?> wrapMap(Map<?, ?> sourceMap) {
            final Map<Object, Object> mapCopy = new LinkedHashMap<>();
            for (final Map.Entry<?, ?> entry : sourceMap.entrySet()) {
                mapCopy.put(entry.getKey(), MutableWrapper.wrap(entry.getValue()));
            }
            return mapCopy;
        }

        private Collection<?> wrapCollection(Collection<?> sourceCollection) {
            final Collection<Object> collectionCopy = sourceCollection instanceof List ? new ArrayList<>() : new LinkedHashSet<>();
            for (final Object element : sourceCollection) {
                collectionCopy.add(MutableWrapper.wrap(element));
            }
            return collectionCopy;
        }
    }
}
File Line
info/magnolia/config/ByteBuddyMutableWrapperHelper.java 249
info/magnolia/config/MutableWrapper.java 259
            }

            final Matcher setterInvocationMatcher = setter.matcher(method.getName());
            if (setterInvocationMatcher.matches()) {
                if (args.length < 1) {
                    log.debug("Encountered [{}] setter invocation without arguments, related type: [{}]", method.getName(), proxy.getClass().getName());
                } else {
                    invokeSetter(StringUtils.uncapitalize(setterInvocationMatcher.group(1)), args[0]);
                }
                return null;
            }

            final Matcher getterInvocationMatcher = getter.matcher(method.getName());
            if (getterInvocationMatcher.matches()) {
                return invokeGetter(StringUtils.uncapitalize(getterInvocationMatcher.group(1)), method);
            }

            final Map<String, Object> currentValues = applyFieldValues(this.propertyValueCache);
            try {
                return method.invoke(getTarget(), args);
File Line
info/magnolia/config/ByteBuddyMutableWrapperHelper.java 268
info/magnolia/config/MutableWrapper.java 278
                return method.invoke(getTarget(), args);
            } finally {
                applyFieldValues(currentValues);
            }
        }

        private Map<String, Object> applyFieldValues(Map<String, Object> valuesToApply) {
            final Map<String, Object> oldValues = new HashMap<>();
            valuesToApply.forEach((propertyName, value) -> {
                try {
                    final Field property = getTarget().getClass().getDeclaredField(propertyName);
                    property.setAccessible(true);
                    oldValues.put(propertyName, property.get(getTarget()));
                    property.set(getTarget(), value);
                } catch (NoSuchFieldException | IllegalAccessException e) {
                    log.debug("Reflective setting of the property: '{}' has failed.", propertyName, e);
                }
            });

            return oldValues;
        }