diff options
Diffstat (limited to 'openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib')
2 files changed, 38 insertions, 4 deletions
diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/CommonMethods.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/CommonMethods.java index b2cc289676..0bcdf8ebbf 100644 --- a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/CommonMethods.java +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/CommonMethods.java @@ -17,6 +17,7 @@ package org.openecomp.core.utilities; import java.lang.reflect.Array; +import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -30,6 +31,7 @@ import java.util.UUID; import org.apache.commons.collections4.MapUtils; import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.StringUtils; +import org.openecomp.core.utilities.exception.NewInstanceRuntimeException; /** * This class provides auxiliary static methods. @@ -160,11 +162,12 @@ public class CommonMethods { * @param cls the cls * @return the t */ - public static <T> T newInstance(Class<T> cls) { + public static <T> T newInstance(final Class<T> cls) { try { - return cls.newInstance(); - } catch (InstantiationException | IllegalAccessException exception) { - throw new RuntimeException(exception); + return cls.getDeclaredConstructor().newInstance(); + } catch (final InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException ex) { + throw new NewInstanceRuntimeException(String.format("Could not create instance for '%s'", cls.getName()) + , ex); } } diff --git a/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/exception/NewInstanceRuntimeException.java b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/exception/NewInstanceRuntimeException.java new file mode 100644 index 0000000000..386486ff9e --- /dev/null +++ b/openecomp-be/lib/openecomp-core-lib/openecomp-utilities-lib/src/main/java/org/openecomp/core/utilities/exception/NewInstanceRuntimeException.java @@ -0,0 +1,31 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2019 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.openecomp.core.utilities.exception; + +public class NewInstanceRuntimeException extends RuntimeException { + + public NewInstanceRuntimeException(String s) { + super(s); + } + + public NewInstanceRuntimeException(String s, Throwable throwable) { + super(s, throwable); + } +} |