]> source.dussan.org Git - poi.git/commitdiff
add forbidden-apis plugin to gradle builds
authorAndreas Beeker <kiwiwings@apache.org>
Fri, 21 May 2021 22:32:09 +0000 (22:32 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Fri, 21 May 2021 22:32:09 +0000 (22:32 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1890090 13f79535-47bb-0310-9956-ffa450edef68

build.gradle
poi-examples/src/main/java/org/apache/poi/examples/hssf/usermodel/InCellLists.java
poi-examples/src/main/java/org/apache/poi/examples/ss/ToCSV.java
poi-examples/src/test/java/org/apache/poi/integration/TestXLSX2CSV.java

index bb5580639faceb844f7c7441c6f1b7c64d3f2ed2..ff0d3e7d935d417d38bdfa13d00881ade0431031 100644 (file)
 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
index cfec4222d80a2bb48d1f6378ab1808d820827749..77282d980d5736e3dad10c068916c3027fb6b004 100644 (file)
 
 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<String> lowerLevelItems;
+        private final String itemText;
+        private final List<String> lowerLevelItems;
 
         /**
          * Create a new instance of the MultiLevelListItem class using the
index c38709bbfcf55c294613b6d407402b844a767eee..75ea1c34990de92e318bcb90f178c81418f87ed0 100644 (file)
@@ -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<ArrayList<String>> 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;
         }
 
index f21f512284e544abd884d06e394b09df099add67..139b9115d3aa73294df3d8a52eda3b7bebef5ed4 100644 (file)
@@ -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)) {