[COLLECTIONS-692] Replace use of deprecated Class#newInstance() #49.

This commit is contained in:
Gary Gregory 2018-07-27 08:32:13 -06:00
parent 4bcd8c12ed
commit b1c45ac691
3 changed files with 11 additions and 7 deletions

View File

@ -27,6 +27,9 @@
<action issue="COLLECTIONS-689" dev="ggregory" type="update" due-to="Richard Walker"> <action issue="COLLECTIONS-689" dev="ggregory" type="update" due-to="Richard Walker">
Link to Javadoc API broken. Link to Javadoc API broken.
</action> </action>
<action issue="COLLECTIONS-692" dev="ggregory" type="update" due-to="Gary Gregory, ">
Replace use of deprecated Class#newInstance() #49.
</action>
</release> </release>
<release version="4.2" date="2018-07-11" description="Update from Java 6 to Java 7, bug fixes, and small changes."> <release version="4.2" date="2018-07-11" description="Update from Java 6 to Java 7, bug fixes, and small changes.">
<action issue="COLLECTIONS-681" dev="kinow" type="add" due-to="Stephan Fuhrmann"> <action issue="COLLECTIONS-681" dev="kinow" type="add" due-to="Stephan Fuhrmann">

View File

@ -16,6 +16,8 @@
*/ */
package org.apache.commons.collections4.list; package org.apache.commons.collections4.list;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet; import java.util.HashSet;
@ -327,21 +329,20 @@ public class SetUniqueList<E> extends AbstractSerializableListDecorator<E> {
* @return a new {@link Set} populated with all elements of the provided * @return a new {@link Set} populated with all elements of the provided
* {@link List} * {@link List}
*/ */
@SuppressWarnings("unchecked")
protected Set<E> createSetBasedOnList(final Set<E> set, final List<E> list) { protected Set<E> createSetBasedOnList(final Set<E> set, final List<E> list) {
Set<E> subSet; Set<E> subSet;
if (set.getClass().equals(HashSet.class)) { if (set.getClass().equals(HashSet.class)) {
subSet = new HashSet<>(list.size()); subSet = new HashSet<>(list.size());
} else { } else {
try { try {
subSet = set.getClass().newInstance(); subSet = set.getClass().getDeclaredConstructor(set.getClass()).newInstance(set);
} catch (final InstantiationException ie) { } catch (final InstantiationException
subSet = new HashSet<>(); | IllegalAccessException
} catch (final IllegalAccessException iae) { | InvocationTargetException
| NoSuchMethodException ie) {
subSet = new HashSet<>(); subSet = new HashSet<>();
} }
} }
subSet.addAll(list);
return subSet; return subSet;
} }

View File

@ -560,7 +560,7 @@ public class MultiValueMap<K, V> extends AbstractMapDecorator<K, Object> impleme
@Override @Override
public T create() { public T create() {
try { try {
return clazz.newInstance(); return clazz.getDeclaredConstructor().newInstance();
} catch (final Exception ex) { } catch (final Exception ex) {
throw new FunctorException("Cannot instantiate class: " + clazz, ex); throw new FunctorException("Cannot instantiate class: " + clazz, ex);
} }