diff --git a/build.gradle b/build.gradle index bb5580639f..ff0d3e7d93 100644 --- a/build.gradle +++ b/build.gradle @@ -18,10 +18,12 @@ buildscript { repositories { maven { url "https://plugins.gradle.org/m2/" } + mavenCentral() } dependencies { classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:3.1.1" + classpath "de.thetaphi:forbiddenapis:3.1" } } @@ -94,7 +96,7 @@ subprojects { apply plugin: 'jacoco' apply plugin: 'maven-publish' apply plugin: 'signing' - + apply plugin: 'de.thetaphi.forbiddenapis' version = '5.0.1-SNAPSHOT' ext { @@ -304,6 +306,19 @@ subprojects { } } + forbiddenApis { + bundledSignatures = [ 'jdk-unsafe', 'jdk-deprecated', 'jdk-internal', 'jdk-non-portable', 'jdk-reflection' ] + signaturesFiles = files('../src/resources/devtools/forbidden-signatures.txt') + ignoreFailures = false + suppressAnnotations = [ 'org.apache.poi.util.SuppressForbidden' ] + // forbiddenapis bundled signatures max supported version is 14 + targetCompatibility = (JavaVersion.VERSION_14.isCompatibleWith(JavaVersion.current()) ? JavaVersion.current() : JavaVersion.VERSION_14) + } + + forbiddenApisMain { + signaturesFiles = files('../src/resources/devtools/forbidden-signatures-prod.txt') + } + task jenkins jenkins.dependsOn build jenkins.dependsOn check diff --git a/poi-examples/src/main/java/org/apache/poi/examples/hssf/usermodel/InCellLists.java b/poi-examples/src/main/java/org/apache/poi/examples/hssf/usermodel/InCellLists.java index cfec4222d8..77282d980d 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/hssf/usermodel/InCellLists.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/hssf/usermodel/InCellLists.java @@ -18,12 +18,13 @@ package org.apache.poi.examples.hssf.usermodel; -import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFDataFormat; @@ -49,6 +50,8 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook; */ @SuppressWarnings({"java:S106","java:S4823"}) public class InCellLists { + private static final Logger LOG = LogManager.getLogger(InCellLists.class); + // This character looks like a solid, black, loser case letter 'o' // positioned up from the base line of the text. @@ -65,7 +68,7 @@ public class InCellLists { * @param outputFilename A String that encapsulates the name of and path to * the Excel spreadsheet file this code will create. */ - public void demonstrateMethodCalls(String outputFilename) throws IOException { + public void demonstrateMethodCalls(String outputFilename) { try (HSSFWorkbook workbook = new HSSFWorkbook()) { HSSFSheet sheet = workbook.createSheet("In Cell Lists"); HSSFRow row = sheet.createRow(0); @@ -161,14 +164,11 @@ public class InCellLists { row.setHeight((short) 2800); // Save the completed workbook - try (FileOutputStream fos = new FileOutputStream(new File(outputFilename))) { + try (FileOutputStream fos = new FileOutputStream(outputFilename)) { workbook.write(fos); } } catch (IOException ioEx) { - System.out.println("Caught a: " + ioEx.getClass().getName()); - System.out.println("Message: " + ioEx.getMessage()); - System.out.println("Stacktrace follows..........."); - ioEx.printStackTrace(System.out); + LOG.atWarn().withThrowable(ioEx).log("Unexpected IOException"); } } @@ -496,10 +496,10 @@ public class InCellLists { * complex list structures descending through two, three or even more * levels. */ - public final class MultiLevelListItem { + public static final class MultiLevelListItem { - private String itemText; - private List lowerLevelItems; + private final String itemText; + private final List lowerLevelItems; /** * Create a new instance of the MultiLevelListItem class using the diff --git a/poi-examples/src/main/java/org/apache/poi/examples/ss/ToCSV.java b/poi-examples/src/main/java/org/apache/poi/examples/ss/ToCSV.java index c38709bbfc..75ea1c3499 100644 --- a/poi-examples/src/main/java/org/apache/poi/examples/ss/ToCSV.java +++ b/poi-examples/src/main/java/org/apache/poi/examples/ss/ToCSV.java @@ -28,6 +28,8 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.util.ArrayList; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellType; import org.apache.poi.ss.usermodel.DataFormatter; @@ -132,6 +134,7 @@ import org.apache.poi.ss.usermodel.WorkbookFactory; */ @SuppressWarnings({"java:S106","java:S4823","java:S1192"}) public class ToCSV { + private static final Logger LOG = LogManager.getLogger(ToCSV.class); private Workbook workbook; private ArrayList> csvData; @@ -691,10 +694,7 @@ public class ToCSV { // program. It should however, ideally be replaced with one or more // catch clauses optimised to handle more specific problems. catch(Exception ex) { - System.out.println("Caught an: " + ex.getClass().getName()); - System.out.println("Message: " + ex.getMessage()); - System.out.println("Stacktrace follows:....."); - ex.printStackTrace(System.out); + LOG.atWarn().withThrowable(ex).log("Unexpected exception"); converted = false; } diff --git a/poi-examples/src/test/java/org/apache/poi/integration/TestXLSX2CSV.java b/poi-examples/src/test/java/org/apache/poi/integration/TestXLSX2CSV.java index f21f512284..139b9115d3 100644 --- a/poi-examples/src/test/java/org/apache/poi/integration/TestXLSX2CSV.java +++ b/poi-examples/src/test/java/org/apache/poi/integration/TestXLSX2CSV.java @@ -21,6 +21,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.PrintStream; +import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream; @@ -37,11 +38,11 @@ public class TestXLSX2CSV { private final UnsynchronizedByteArrayOutputStream errorBytes = new UnsynchronizedByteArrayOutputStream(); @BeforeEach - public void setUp() { + public void setUp() throws UnsupportedEncodingException { // remember and replace default error streams err = System.err; - PrintStream error = new PrintStream(errorBytes); + PrintStream error = new PrintStream(errorBytes, true, "UTF-8"); System.setErr(error); } @@ -77,7 +78,7 @@ public class TestXLSX2CSV { @Test public void testSampleFile() throws Exception { final UnsynchronizedByteArrayOutputStream outputBytes = new UnsynchronizedByteArrayOutputStream(); - PrintStream out = new PrintStream(outputBytes); + PrintStream out = new PrintStream(outputBytes, true, "UTF-8"); // The package open is instantaneous, as it should be. try (OPCPackage p = OPCPackage.open(XSSFTestDataSamples.getSampleFile("sample.xlsx").getAbsolutePath(), PackageAccess.READ)) { @@ -96,7 +97,7 @@ public class TestXLSX2CSV { @Test public void testMinColumns() throws Exception { final UnsynchronizedByteArrayOutputStream outputBytes = new UnsynchronizedByteArrayOutputStream(); - PrintStream out = new PrintStream(outputBytes); + PrintStream out = new PrintStream(outputBytes, true, "UTF-8"); // The package open is instantaneous, as it should be. try (OPCPackage p = OPCPackage.open(XSSFTestDataSamples.getSampleFile("sample.xlsx").getAbsolutePath(), PackageAccess.READ)) {