From 35840d835f581c2d61de1c57fe9963e36eb15c9f Mon Sep 17 00:00:00 2001 From: liamfallon Date: Tue, 25 Feb 2020 16:10:08 +0000 Subject: Fix Java 11/Checkstyle/Sonar warnings A number of Java 11, checkstyle, and SONAR warnings have crept into the Apex codebase over the last number of reviews. This change fixes those issues. Issue-ID: POLICY-1913 Change-Id: I2afd607e80f48323355380fb2fe5e048e18879f9 Signed-off-by: liamfallon --- .../apex/model/basicmodel/concepts/AxKeyUse.java | 4 +- .../apex/model/basicmodel/dao/ApexDaoFactory.java | 14 +-- .../model/basicmodel/dao/impl/DefaultApexDao.java | 108 +++++++++------------ .../model/basicmodel/concepts/DummyEntity.java | 15 ++- 4 files changed, 63 insertions(+), 78 deletions(-) (limited to 'model/basic-model/src') diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyUse.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyUse.java index aff688bf8..8eed845ad 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyUse.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/concepts/AxKeyUse.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -161,7 +161,7 @@ public class AxKeyUse extends AxKey { final AxKeyUse copy = ((AxKeyUse) copyObject); try { - copy.usedKey = usedKey.getClass().newInstance(); + copy.usedKey = usedKey.getClass().getDeclaredConstructor().newInstance(); } catch (final Exception e) { throw new ApexRuntimeException("error copying concept key: " + e.getMessage(), e); } diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/ApexDaoFactory.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/ApexDaoFactory.java index b374ff40a..fad3b08b9 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/ApexDaoFactory.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/ApexDaoFactory.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,24 +45,24 @@ public class ApexDaoFactory { */ public ApexDao createApexDao(final DaoParameters daoParameters) throws ApexException { Assertions.argumentOfClassNotNull(daoParameters, ApexException.class, - "Parameter \"daoParameters\" may not be null"); + "Parameter \"daoParameters\" may not be null"); // Get the class for the DAO using reflection Object apexDaoObject = null; try { - apexDaoObject = Class.forName(daoParameters.getPluginClass()).newInstance(); - } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) { + apexDaoObject = Class.forName(daoParameters.getPluginClass()).getDeclaredConstructor().newInstance(); + } catch (final Exception e) { LOGGER.error("Apex DAO class not found for DAO plugin \"" + daoParameters.getPluginClass() + "\"", e); throw new ApexException( - "Apex DAO class not found for DAO plugin \"" + daoParameters.getPluginClass() + "\"", e); + "Apex DAO class not found for DAO plugin \"" + daoParameters.getPluginClass() + "\"", e); } // Check the class is an Apex DAO if (!(apexDaoObject instanceof ApexDao)) { LOGGER.error("Specified Apex DAO plugin class \"" + daoParameters.getPluginClass() - + "\" does not implement the ApexDao interface"); + + "\" does not implement the ApexDao interface"); throw new ApexException("Specified Apex DAO plugin class \"" + daoParameters.getPluginClass() - + "\" does not implement the ApexDao interface"); + + "\" does not implement the ApexDao interface"); } return (ApexDao) apexDaoObject; diff --git a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/impl/DefaultApexDao.java b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/impl/DefaultApexDao.java index 375d7f0bf..b8a97928c 100644 --- a/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/impl/DefaultApexDao.java +++ b/model/basic-model/src/main/java/org/onap/policy/apex/model/basicmodel/dao/impl/DefaultApexDao.java @@ -1,7 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2019 Nordix Foundation. + * Modifications Copyright (C) 2019-2020 Nordix Foundation. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -73,12 +73,11 @@ public class DefaultApexDao implements ApexDao { LOGGER.debug("Creating Apex persistence unit \"" + daoParameters.getPersistenceUnit() + "\" . . ."); try { emf = Persistence.createEntityManagerFactory(daoParameters.getPersistenceUnit(), - daoParameters.getJdbcProperties()); + daoParameters.getJdbcProperties()); } catch (final Exception e) { LOGGER.warn("Creation of Apex persistence unit \"" + daoParameters.getPersistenceUnit() + "\" failed", e); throw new ApexException( - "Creation of Apex persistence unit \"" + daoParameters.getPersistenceUnit() + "\" failed", - e); + "Creation of Apex persistence unit \"" + daoParameters.getPersistenceUnit() + "\" failed", e); } LOGGER.debug("Created Apex persistence unit \"" + daoParameters.getPersistenceUnit() + "\""); } @@ -120,8 +119,7 @@ public class DefaultApexDao implements ApexDao { mg.getTransaction().begin(); mg.merge(obj); mg.getTransaction().commit(); - } - finally { + } finally { mg.close(); } } @@ -139,8 +137,7 @@ public class DefaultApexDao implements ApexDao { mg.getTransaction().begin(); mg.remove(mg.contains(obj) ? obj : mg.merge(obj)); mg.getTransaction().commit(); - } - finally { + } finally { mg.close(); } } @@ -157,10 +154,9 @@ public class DefaultApexDao implements ApexDao { try { mg.getTransaction().begin(); mg.createQuery(DELETE_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_NAME + key.getName() - + AND_C_KEY_VERSION + key.getVersion() + "'", someClass).executeUpdate(); + + AND_C_KEY_VERSION + key.getVersion() + "'", someClass).executeUpdate(); mg.getTransaction().commit(); - } - finally { + } finally { mg.close(); } } @@ -177,11 +173,10 @@ public class DefaultApexDao implements ApexDao { try { mg.getTransaction().begin(); mg.createQuery(DELETE_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_PARENT_KEY_NAME - + key.getParentKeyName() + AND_C_KEY_PARENT_KEY_VERSION + key.getParentKeyVersion() - + AND_C_KEY_LOCAL_NAME + key.getLocalName() + "'", someClass).executeUpdate(); + + key.getParentKeyName() + AND_C_KEY_PARENT_KEY_VERSION + key.getParentKeyVersion() + + AND_C_KEY_LOCAL_NAME + key.getLocalName() + "'", someClass).executeUpdate(); mg.getTransaction().commit(); - } - finally { + } finally { mg.close(); } } @@ -201,8 +196,7 @@ public class DefaultApexDao implements ApexDao { mg.merge(t); } mg.getTransaction().commit(); - } - finally { + } finally { mg.close(); } } @@ -222,8 +216,7 @@ public class DefaultApexDao implements ApexDao { mg.remove(mg.contains(t) ? t : mg.merge(t)); } mg.getTransaction().commit(); - } - finally { + } finally { mg.close(); } } @@ -233,7 +226,7 @@ public class DefaultApexDao implements ApexDao { */ @Override public int deleteByArtifactKey(final Class someClass, - final Collection keys) { + final Collection keys) { if (keys == null || keys.isEmpty()) { return 0; } @@ -242,14 +235,11 @@ public class DefaultApexDao implements ApexDao { try { mg.getTransaction().begin(); for (final AxArtifactKey key : keys) { - deletedCount += mg - .createQuery(DELETE_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_NAME - + key.getName() + AND_C_KEY_VERSION + key.getVersion() + "'", someClass) - .executeUpdate(); + deletedCount += mg.createQuery(DELETE_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_NAME + + key.getName() + AND_C_KEY_VERSION + key.getVersion() + "'", someClass).executeUpdate(); } mg.getTransaction().commit(); - } - finally { + } finally { mg.close(); } return deletedCount; @@ -260,7 +250,7 @@ public class DefaultApexDao implements ApexDao { */ @Override public int deleteByReferenceKey(final Class someClass, - final Collection keys) { + final Collection keys) { if (keys == null || keys.isEmpty()) { return 0; } @@ -269,13 +259,15 @@ public class DefaultApexDao implements ApexDao { try { mg.getTransaction().begin(); for (final AxReferenceKey key : keys) { - deletedCount += mg.createQuery(DELETE_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_PARENT_KEY_NAME - + key.getParentKeyName() + AND_C_KEY_PARENT_KEY_VERSION + key.getParentKeyVersion() - + AND_C_KEY_LOCAL_NAME + key.getLocalName() + "'", someClass).executeUpdate(); + deletedCount += + mg.createQuery( + DELETE_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_PARENT_KEY_NAME + + key.getParentKeyName() + AND_C_KEY_PARENT_KEY_VERSION + + key.getParentKeyVersion() + AND_C_KEY_LOCAL_NAME + key.getLocalName() + "'", + someClass).executeUpdate(); } mg.getTransaction().commit(); - } - finally { + } finally { mg.close(); } return deletedCount; @@ -291,8 +283,7 @@ public class DefaultApexDao implements ApexDao { mg.getTransaction().begin(); mg.createQuery(DELETE_FROM + someClass.getSimpleName() + " c ", someClass).executeUpdate(); mg.getTransaction().commit(); - } - finally { + } finally { mg.close(); } } @@ -311,7 +302,7 @@ public class DefaultApexDao implements ApexDao { if (t != null) { // This clone is created to force the JPA DAO to recurse down through the object try { - final T clonedT = someClass.newInstance(); + final T clonedT = someClass.getDeclaredConstructor().newInstance(); t.copyTo(clonedT); return clonedT; } catch (final Exception e) { @@ -321,8 +312,7 @@ public class DefaultApexDao implements ApexDao { } else { return null; } - } - finally { + } finally { mg.close(); } } @@ -340,7 +330,7 @@ public class DefaultApexDao implements ApexDao { final T t = mg.find(someClass, key); if (t != null) { try { - final T clonedT = someClass.newInstance(); + final T clonedT = someClass.getDeclaredConstructor().newInstance(); t.copyTo(clonedT); return clonedT; } catch (final Exception e) { @@ -350,8 +340,7 @@ public class DefaultApexDao implements ApexDao { } else { return null; } - } - finally { + } finally { mg.close(); } } @@ -367,8 +356,7 @@ public class DefaultApexDao implements ApexDao { final EntityManager mg = getEntityManager(); try { return mg.createQuery(SELECT_C_FROM + someClass.getSimpleName() + " c", someClass).getResultList(); - } - finally { + } finally { mg.close(); } } @@ -383,11 +371,13 @@ public class DefaultApexDao implements ApexDao { } final EntityManager mg = getEntityManager(); try { - return mg.createQuery(SELECT_C_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_PARENT_KEY_NAME - + parentKey.getName() + AND_C_KEY_PARENT_KEY_VERSION + parentKey.getVersion() + "'", - someClass).getResultList(); - } - finally { + return mg + .createQuery( + SELECT_C_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_PARENT_KEY_NAME + + parentKey.getName() + AND_C_KEY_PARENT_KEY_VERSION + parentKey.getVersion() + "'", + someClass) + .getResultList(); + } finally { mg.close(); } } @@ -404,9 +394,8 @@ public class DefaultApexDao implements ApexDao { List ret; try { ret = mg.createQuery(SELECT_C_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_NAME + key.getName() - + AND_C_KEY_VERSION + key.getVersion() + "'", someClass).getResultList(); - } - finally { + + AND_C_KEY_VERSION + key.getVersion() + "'", someClass).getResultList(); + } finally { mg.close(); } if (ret == null || ret.isEmpty()) { @@ -414,7 +403,7 @@ public class DefaultApexDao implements ApexDao { } if (ret.size() > 1) { throw new IllegalArgumentException("More than one result was returned for search for " + someClass - + " with key " + key.getId() + ": " + ret); + + " with key " + key.getId() + ": " + ret); } return ret.get(0); } @@ -431,10 +420,9 @@ public class DefaultApexDao implements ApexDao { List ret; try { ret = mg.createQuery(SELECT_C_FROM + someClass.getSimpleName() + C_WHERE_C_KEY_PARENT_KEY_NAME - + key.getParentKeyName() + AND_C_KEY_PARENT_KEY_VERSION + key.getParentKeyVersion() - + AND_C_KEY_LOCAL_NAME + key.getLocalName() + "'", someClass).getResultList(); - } - finally { + + key.getParentKeyName() + AND_C_KEY_PARENT_KEY_VERSION + key.getParentKeyVersion() + + AND_C_KEY_LOCAL_NAME + key.getLocalName() + "'", someClass).getResultList(); + } finally { mg.close(); } if (ret == null || ret.isEmpty()) { @@ -442,7 +430,7 @@ public class DefaultApexDao implements ApexDao { } if (ret.size() > 1) { throw new IllegalArgumentException("More than one result was returned for search for " + someClass - + " with key " + key.getId() + ": " + ret); + + " with key " + key.getId() + ": " + ret); } return ret.get(0); } @@ -459,8 +447,7 @@ public class DefaultApexDao implements ApexDao { ret = mg.merge(obj); mg.flush(); mg.getTransaction().commit(); - } - finally { + } finally { mg.close(); } return ret; @@ -478,9 +465,8 @@ public class DefaultApexDao implements ApexDao { long size = 0; try { size = mg.createQuery("SELECT COUNT(c) FROM " + someClass.getSimpleName() + " c", Long.class) - .getSingleResult(); - } - finally { + .getSingleResult(); + } finally { mg.close(); } return size; diff --git a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/DummyEntity.java b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/DummyEntity.java index 02174488e..ff2bf9354 100644 --- a/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/DummyEntity.java +++ b/model/basic-model/src/test/java/org/onap/policy/apex/model/basicmodel/concepts/DummyEntity.java @@ -1,19 +1,20 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2020 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========================================================= */ @@ -29,10 +30,6 @@ import javax.persistence.Table; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; -import org.onap.policy.apex.model.basicmodel.concepts.AxConcept; -import org.onap.policy.apex.model.basicmodel.concepts.AxKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; -import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult; import org.onap.policy.apex.model.basicmodel.xml.AxReferenceKeyAdapter; @Entity @@ -63,10 +60,12 @@ public class DummyEntity extends AxConcept { this.doubleValue = doubleValue; } + @Override public AxReferenceKey getKey() { return key; } + @Override public List getKeys() { return Arrays.asList((AxKey) getKey()); } @@ -172,7 +171,7 @@ public class DummyEntity extends AxConcept { return key.compareTo(other.key); } if (doubleValue != other.doubleValue) { - return new Double(doubleValue).compareTo(other.doubleValue); + return Double.valueOf(doubleValue).compareTo(other.doubleValue); } return 0; -- cgit 1.2.3-korg