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 --- .../policy/apex/core/engine/TaskParameters.java | 8 ++++ .../engine/executor/impl/ExecutorFactoryImpl.java | 52 +++++++++++----------- .../compile/singleclass/SingleClassBuilder.java | 28 ++++++------ 3 files changed, 49 insertions(+), 39 deletions(-) (limited to 'core') diff --git a/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/TaskParameters.java b/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/TaskParameters.java index 8a0f6db9c..64474f0e8 100644 --- a/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/TaskParameters.java +++ b/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/TaskParameters.java @@ -22,6 +22,7 @@ package org.onap.policy.apex.core.engine; import lombok.Getter; import lombok.Setter; + import org.onap.policy.common.parameters.BeanValidator; import org.onap.policy.common.parameters.ValidationResult; import org.onap.policy.common.parameters.annotations.NotBlank; @@ -51,6 +52,13 @@ public class TaskParameters { this.name = "taskParameters"; } + /** + * Full constructor. + * + * @param key the task parameter key + * @param value the task parameter value + * @param taskId the task ID of this task parameter + */ public TaskParameters(String key, String value, String taskId) { this(); this.key = key; diff --git a/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/impl/ExecutorFactoryImpl.java b/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/impl/ExecutorFactoryImpl.java index 231f6bc39..809dc737e 100644 --- a/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/impl/ExecutorFactoryImpl.java +++ b/core/core-engine/src/main/java/org/onap/policy/apex/core/engine/executor/impl/ExecutorFactoryImpl.java @@ -24,6 +24,7 @@ package org.onap.policy.apex.core.engine.executor.impl; import java.util.Map; import java.util.Map.Entry; import java.util.TreeMap; + import org.onap.policy.apex.core.engine.EngineParameterConstants; import org.onap.policy.apex.core.engine.EngineParameters; import org.onap.policy.apex.core.engine.ExecutorParameters; @@ -71,18 +72,18 @@ public class ExecutorFactoryImpl implements ExecutorFactory { engineParameters = ParameterService.get(EngineParameterConstants.MAIN_GROUP_NAME); Assertions.argumentOfClassNotNull(engineParameters, StateMachineException.class, - "Parameter \"engineParameters\" may not be null"); + "Parameter \"engineParameters\" may not be null"); // Instantiate each executor class map entry for (final Entry executorParameterEntry : engineParameters.getExecutorParameterMap() - .entrySet()) { + .entrySet()) { // Get classes for all types of executors for this logic type taskExecutorPluginClassMap.put(executorParameterEntry.getKey(), - getExecutorPluginClass(executorParameterEntry.getValue().getTaskExecutorPluginClass())); - taskSelectionExecutorPluginClassMap.put(executorParameterEntry.getKey(), getExecutorPluginClass( - executorParameterEntry.getValue().getTaskSelectionExecutorPluginClass())); - stateFinalizerExecutorPluginClassMap.put(executorParameterEntry.getKey(), getExecutorPluginClass( - executorParameterEntry.getValue().getStateFinalizerExecutorPluginClass())); + getExecutorPluginClass(executorParameterEntry.getValue().getTaskExecutorPluginClass())); + taskSelectionExecutorPluginClassMap.put(executorParameterEntry.getKey(), + getExecutorPluginClass(executorParameterEntry.getValue().getTaskSelectionExecutorPluginClass())); + stateFinalizerExecutorPluginClassMap.put(executorParameterEntry.getKey(), + getExecutorPluginClass(executorParameterEntry.getValue().getStateFinalizerExecutorPluginClass())); // Save the executor implementation parameters implementationParameterMap.put(executorParameterEntry.getKey(), executorParameterEntry.getValue()); @@ -94,14 +95,14 @@ public class ExecutorFactoryImpl implements ExecutorFactory { */ @Override public TaskSelectExecutor getTaskSelectionExecutor(final Executor parentExecutor, final AxState state, - final ApexInternalContext context) { + final ApexInternalContext context) { if (!state.checkSetTaskSelectionLogic()) { return null; } // Create task selection executor - final TaskSelectExecutor tsExecutor = (TaskSelectExecutor) createExecutor( - state.getTaskSelectionLogic().getLogicFlavour(), + final TaskSelectExecutor tsExecutor = + (TaskSelectExecutor) createExecutor(state.getTaskSelectionLogic().getLogicFlavour(), taskSelectionExecutorPluginClassMap.get(state.getTaskSelectionLogic().getLogicFlavour()), TaskSelectExecutor.class); tsExecutor.setParameters(implementationParameterMap.get(state.getTaskSelectionLogic().getLogicFlavour())); @@ -115,10 +116,10 @@ public class ExecutorFactoryImpl implements ExecutorFactory { */ @Override public TaskExecutor getTaskExecutor(final Executor parentExecutor, final AxTask task, - final ApexInternalContext context) { + final ApexInternalContext context) { // Create task executor final TaskExecutor taskExecutor = (TaskExecutor) createExecutor(task.getTaskLogic().getLogicFlavour(), - taskExecutorPluginClassMap.get(task.getTaskLogic().getLogicFlavour()), TaskExecutor.class); + taskExecutorPluginClassMap.get(task.getTaskLogic().getLogicFlavour()), TaskExecutor.class); taskExecutor.setParameters(implementationParameterMap.get(task.getTaskLogic().getLogicFlavour())); taskExecutor.setContext(parentExecutor, task, context); taskExecutor.updateTaskParameters(engineParameters.getTaskParameters()); @@ -130,11 +131,10 @@ public class ExecutorFactoryImpl implements ExecutorFactory { */ @Override public StateFinalizerExecutor getStateFinalizerExecutor(final Executor parentExecutor, - final AxStateFinalizerLogic logic, final ApexInternalContext context) { + final AxStateFinalizerLogic logic, final ApexInternalContext context) { // Create state finalizer executor final StateFinalizerExecutor sfExecutor = (StateFinalizerExecutor) createExecutor(logic.getLogicFlavour(), - stateFinalizerExecutorPluginClassMap.get(logic.getLogicFlavour()), - StateFinalizerExecutor.class); + stateFinalizerExecutorPluginClassMap.get(logic.getLogicFlavour()), StateFinalizerExecutor.class); sfExecutor.setParameters(implementationParameterMap.get(logic.getLogicFlavour())); sfExecutor.setContext(parentExecutor, logic, context); @@ -150,7 +150,7 @@ public class ExecutorFactoryImpl implements ExecutorFactory { */ @SuppressWarnings("unchecked") private Class> getExecutorPluginClass(final String executorClassName) - throws StateMachineException { + throws StateMachineException { // It's OK for an executor class not to be defined as long as it's not called if (executorClassName == null) { return null; @@ -163,15 +163,15 @@ public class ExecutorFactoryImpl implements ExecutorFactory { } catch (final ClassNotFoundException e) { LOGGER.error("Apex executor class not found for executor plugin \"" + executorClassName + "\"", e); throw new StateMachineException( - "Apex executor class not found for executor plugin \"" + executorClassName + "\"", e); + "Apex executor class not found for executor plugin \"" + executorClassName + "\"", e); } // Check the class is an executor if (!Executor.class.isAssignableFrom(executorPluginClass)) { LOGGER.error("Specified Apex executor plugin class \"{}\" does not implment the Executor interface", - executorClassName); + executorClassName); throw new StateMachineException("Specified Apex executor plugin class \"" + executorClassName - + "\" does not implment the Executor interface"); + + "\" does not implment the Executor interface"); } return (Class>) executorPluginClass; @@ -186,14 +186,14 @@ public class ExecutorFactoryImpl implements ExecutorFactory { * @return The instantiated class */ private Executor createExecutor(final String logicFlavour, - final Class> executorClass, - final Class> executorSuperClass) { + final Class> executorClass, + final Class> executorSuperClass) { // It's OK for an executor class not to be defined but it's not all right to try and create // a non-defined // executor class if (executorClass == null) { final String errorMessage = "Executor plugin class not defined for \"" + logicFlavour - + "\" executor of type \"" + executorSuperClass.getName() + "\""; + + "\" executor of type \"" + executorSuperClass.getName() + "\""; LOGGER.error(errorMessage); throw new StateMachineRuntimeException(errorMessage); } @@ -201,10 +201,10 @@ public class ExecutorFactoryImpl implements ExecutorFactory { // Create an executor for the specified logic flavour Object executorObject = null; try { - executorObject = executorClass.newInstance(); - } catch (InstantiationException | IllegalAccessException e) { + executorObject = executorClass.getDeclaredConstructor().newInstance(); + } catch (final Exception e) { final String errorMessage = "Instantiation error on \"" + logicFlavour + "\" executor of type \"" - + executorClass.getName() + "\""; + + executorClass.getName() + "\""; LOGGER.error(errorMessage, e); throw new StateMachineRuntimeException(errorMessage, e); } @@ -212,7 +212,7 @@ public class ExecutorFactoryImpl implements ExecutorFactory { // Check the class is the correct type of executor if (!(executorSuperClass.isAssignableFrom(executorObject.getClass()))) { final String errorMessage = "Executor on \"" + logicFlavour + "\" of type \"" + executorClass - + "\" is not an instance of \"" + executorSuperClass.getName() + "\""; + + "\" is not an instance of \"" + executorSuperClass.getName() + "\""; LOGGER.error(errorMessage); throw new StateMachineRuntimeException(errorMessage); diff --git a/core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/java/compile/singleclass/SingleClassBuilder.java b/core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/java/compile/singleclass/SingleClassBuilder.java index 464a601e1..7695e29fd 100644 --- a/core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/java/compile/singleclass/SingleClassBuilder.java +++ b/core/core-infrastructure/src/main/java/org/onap/policy/apex/core/infrastructure/java/compile/singleclass/SingleClassBuilder.java @@ -1,6 +1,7 @@ /*- * ============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. @@ -69,8 +70,8 @@ public class SingleClassBuilder { */ public void compile() throws JavaHandlingException { // Get the list of compilation units, there is only one here - final List compilationUnits = Arrays - .asList(new SingleClassCompilationUnit(className, sourceCode)); + final List compilationUnits = + Arrays.asList(new SingleClassCompilationUnit(className, sourceCode)); // Allows us to get diagnostics from the compilation final DiagnosticCollector diagnosticListener = new DiagnosticCollector<>(); @@ -80,11 +81,11 @@ public class SingleClassBuilder { // Set up the target file manager and call the compiler singleFileManager = new SingleFileManager(compiler, new SingleClassByteCodeFileObject(className)); - final JavaCompiler.CompilationTask task = compiler.getTask(null, singleFileManager, diagnosticListener, null, - null, compilationUnits); + final JavaCompiler.CompilationTask task = + compiler.getTask(null, singleFileManager, diagnosticListener, null, null, compilationUnits); // Check if the compilation worked - if (!task.call()) { + if (Boolean.FALSE.equals(task.call())) { final StringBuilder builder = new StringBuilder(); for (final Diagnostic diagnostic : diagnosticListener.getDiagnostics()) { builder.append("code:"); @@ -114,20 +115,21 @@ public class SingleClassBuilder { * Create a new instance of the Java class using its byte code definition. * * @return A new instance of the object - * @throws InstantiationException if an instance of the object cannot be created, for example if the class has no - * default constructor - * @throws IllegalAccessException the caller does not have permission to call the class - * @throws ClassNotFoundException the byte code for the class is not found in the class loader - * @throws JavaHandlingException the java handling exception if the Java class source code is not compiled + * @throws JavaHandlingException on errors creating the object */ - public Object createObject() throws InstantiationException, IllegalAccessException, ClassNotFoundException, - JavaHandlingException { + public Object createObject() + throws InstantiationException, IllegalAccessException, ClassNotFoundException, JavaHandlingException { if (singleFileManager == null) { String message = "error instantiating instance for class \"" + className + "\": code may not be compiled"; LOGGER.warn(message); throw new JavaHandlingException(message); } - return singleFileManager.getClassLoader(null).findClass(className).newInstance(); + try { + return singleFileManager.getClassLoader(null).findClass(className).getDeclaredConstructor().newInstance(); + } catch (Exception e) { + return new JavaHandlingException("could not create java class", e); + } + } } -- cgit 1.2.3-korg