diff --git a/src/changes/changes.xml b/src/changes/changes.xml index efa2c8de0..a837d17ba 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -27,6 +27,9 @@ Link to Javadoc API broken. + + Replace use of deprecated Class#newInstance() #49. + diff --git a/src/main/java/org/apache/commons/collections4/list/SetUniqueList.java b/src/main/java/org/apache/commons/collections4/list/SetUniqueList.java index 8aa081516..3227e4392 100644 --- a/src/main/java/org/apache/commons/collections4/list/SetUniqueList.java +++ b/src/main/java/org/apache/commons/collections4/list/SetUniqueList.java @@ -16,6 +16,8 @@ */ package org.apache.commons.collections4.list; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; @@ -327,21 +329,20 @@ public class SetUniqueList extends AbstractSerializableListDecorator { * @return a new {@link Set} populated with all elements of the provided * {@link List} */ - @SuppressWarnings("unchecked") protected Set createSetBasedOnList(final Set set, final List list) { Set subSet; if (set.getClass().equals(HashSet.class)) { subSet = new HashSet<>(list.size()); } else { try { - subSet = set.getClass().newInstance(); - } catch (final InstantiationException ie) { - subSet = new HashSet<>(); - } catch (final IllegalAccessException iae) { + subSet = set.getClass().getDeclaredConstructor(set.getClass()).newInstance(set); + } catch (final InstantiationException + | IllegalAccessException + | InvocationTargetException + | NoSuchMethodException ie) { subSet = new HashSet<>(); } } - subSet.addAll(list); return subSet; } diff --git a/src/main/java/org/apache/commons/collections4/map/MultiValueMap.java b/src/main/java/org/apache/commons/collections4/map/MultiValueMap.java index 2480a5fb5..81f497116 100644 --- a/src/main/java/org/apache/commons/collections4/map/MultiValueMap.java +++ b/src/main/java/org/apache/commons/collections4/map/MultiValueMap.java @@ -560,7 +560,7 @@ public class MultiValueMap extends AbstractMapDecorator impleme @Override public T create() { try { - return clazz.newInstance(); + return clazz.getDeclaredConstructor().newInstance(); } catch (final Exception ex) { throw new FunctorException("Cannot instantiate class: " + clazz, ex); }