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 --- .../compile/singleclass/SingleClassBuilder.java | 28 ++++++++++++---------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to 'core/core-infrastructure/src') 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