]> source.dussan.org Git - poi.git/commitdiff
#64036 - Replace reflection calls in factories for Java 9+
authorAndreas Beeker <kiwiwings@apache.org>
Mon, 17 Aug 2020 11:49:02 +0000 (11:49 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Mon, 17 Aug 2020 11:49:02 +0000 (11:49 +0000)
provide EvalutionWorkbook in HSSF/XSSF/SXSSF

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1880925 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluator.java
src/java/org/apache/poi/ss/usermodel/Workbook.java
src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java

index 6ce74d1206ee0a74da5333b803bb361774d38dd9..7f9692ecdc834518a7e43a974a209a579f8806ee 100644 (file)
@@ -2251,4 +2251,10 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
             }
         }
     }
+
+
+    @Override
+    public HSSFEvaluationWorkbook createEvaluationWorkbook() {
+        return HSSFEvaluationWorkbook.create(this);
+    }
 }
index 9a46ac44a87054e97b2f49986f339afa1652a975..b85cc15ea9731873df6862fcb76d90ca40706913 100644 (file)
 
 package org.apache.poi.ss.formula.eval.forked;
 
-import org.apache.poi.ss.formula.eval.BoolEval;
-import org.apache.poi.ss.formula.eval.ErrorEval;
-import org.apache.poi.ss.formula.eval.NumberEval;
-import org.apache.poi.ss.formula.eval.StringEval;
-import org.apache.poi.ss.formula.eval.ValueEval;
-import org.apache.poi.ss.formula.udf.UDFFinder;
+import java.util.stream.Stream;
 
-import java.lang.reflect.Method;
-
-import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook;
 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 import org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment;
 import org.apache.poi.ss.formula.EvaluationCell;
 import org.apache.poi.ss.formula.EvaluationWorkbook;
 import org.apache.poi.ss.formula.IStabilityClassifier;
 import org.apache.poi.ss.formula.WorkbookEvaluator;
+import org.apache.poi.ss.formula.eval.BoolEval;
+import org.apache.poi.ss.formula.eval.ErrorEval;
+import org.apache.poi.ss.formula.eval.NumberEval;
+import org.apache.poi.ss.formula.eval.StringEval;
+import org.apache.poi.ss.formula.eval.ValueEval;
+import org.apache.poi.ss.formula.udf.UDFFinder;
 import org.apache.poi.ss.usermodel.Workbook;
 
 /**
@@ -45,34 +43,19 @@ import org.apache.poi.ss.usermodel.Workbook;
  */
 public final class ForkedEvaluator {
 
-       private WorkbookEvaluator _evaluator;
-       private ForkedEvaluationWorkbook _sewb;
+       private final WorkbookEvaluator _evaluator;
+       private final ForkedEvaluationWorkbook _sewb;
 
        private ForkedEvaluator(EvaluationWorkbook masterWorkbook, IStabilityClassifier stabilityClassifier, UDFFinder udfFinder) {
                _sewb = new ForkedEvaluationWorkbook(masterWorkbook);
                _evaluator = new WorkbookEvaluator(_sewb, stabilityClassifier, udfFinder);
        }
-       private static EvaluationWorkbook createEvaluationWorkbook(Workbook wb) {
-               if (wb instanceof HSSFWorkbook) {
-                       return HSSFEvaluationWorkbook.create((HSSFWorkbook) wb);
-               } else {
-                   try {
-                       // TODO: check if this is Java 9 compatible ...
-                       Class<?> evalWB = Class.forName("org.apache.poi.xssf.usermodel.XSSFEvaluationWorkbook");
-                       Class<?> xssfWB = Class.forName("org.apache.poi.xssf.usermodel.XSSFWorkbook");
-                       Method createM = evalWB.getDeclaredMethod("create", xssfWB);
-                       return (EvaluationWorkbook)createM.invoke(null, wb);
-                   } catch (Exception e) {
-                       throw new IllegalArgumentException("Unexpected workbook type (" + wb.getClass().getName() + ") - check for poi-ooxml and poi-ooxml schemas jar in the classpath", e);
-                   }
-               }
-       }
 
        /**
         * @param udfFinder pass <code>null</code> for default (AnalysisToolPak only)
         */
        public static ForkedEvaluator create(Workbook wb, IStabilityClassifier stabilityClassifier, UDFFinder udfFinder) {
-               return new ForkedEvaluator(createEvaluationWorkbook(wb), stabilityClassifier, udfFinder);
+               return new ForkedEvaluator(wb.createEvaluationWorkbook(), stabilityClassifier, udfFinder);
        }
 
        /**
@@ -137,10 +120,7 @@ public final class ForkedEvaluator {
         * @param evaluators all evaluators for the full set of workbooks required by the formulas.
         */
        public static void setupEnvironment(String[] workbookNames, ForkedEvaluator[] evaluators) {
-               WorkbookEvaluator[] wbEvals = new WorkbookEvaluator[evaluators.length];
-               for (int i = 0; i < wbEvals.length; i++) {
-                       wbEvals[i] = evaluators[i]._evaluator;
-               }
+               WorkbookEvaluator[] wbEvals = Stream.of(evaluators).map(e -> e._evaluator).toArray(WorkbookEvaluator[]::new);
                CollaboratingWorkbooksEnvironment.setup(workbookNames, wbEvals);
        }
 }
index 77cce0b226780e1a4fc06938917b96605b709e8c..7ef1bdbf3d115364fa9beea72cad080080e8ab5a 100644 (file)
@@ -24,6 +24,7 @@ import java.util.Iterator;
 import java.util.List;
 
 import org.apache.poi.ss.SpreadsheetVersion;
+import org.apache.poi.ss.formula.EvaluationWorkbook;
 import org.apache.poi.ss.formula.udf.UDFFinder;
 import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
 import org.apache.poi.util.Removal;
@@ -206,7 +207,7 @@ public interface Workbook extends Closeable, Iterable<Sheet> {
      */
     Sheet cloneSheet(int sheetNum);
 
-    
+
     /**
      *  Returns an iterator of the sheets in the workbook
      *  in sheet order. Includes hidden and very hidden sheets.
@@ -253,7 +254,7 @@ public interface Workbook extends Closeable, Iterable<Sheet> {
      * @return new font object
      */
     Font createFont();
-    
+
     /**
      * Finds a font that matches the one with the supplied attributes
      *
@@ -513,9 +514,9 @@ public interface Workbook extends Closeable, Iterable<Sheet> {
 
     /**
      * Hide or unhide a sheet.
-     * 
-     * Please note that the sheet currently set as active sheet (sheet 0 in a newly 
-     * created workbook or the one set via setActiveSheet()) cannot be hidden. 
+     *
+     * Please note that the sheet currently set as active sheet (sheet 0 in a newly
+     * created workbook or the one set via setActiveSheet()) cannot be hidden.
      *
      * @param sheetIx the sheet index (0-based)
      * @param hidden True to mark the sheet as hidden, false otherwise
@@ -535,9 +536,9 @@ public interface Workbook extends Closeable, Iterable<Sheet> {
     /**
      * Hide or unhide a sheet.
      *
-     * Please note that the sheet currently set as active sheet (sheet 0 in a newly 
+     * Please note that the sheet currently set as active sheet (sheet 0 in a newly
      * created workbook or the one set via setActiveSheet()) cannot be hidden.
-     *  
+     *
      * @param sheetIx     the sheet index (0-based)
      * @param visibility  the sheet visibility to set
      * @since POI 3.16 beta 2
@@ -576,10 +577,10 @@ public interface Workbook extends Closeable, Iterable<Sheet> {
      * @since 3.8
      */
     boolean getForceFormulaRecalculation();
-    
+
     /**
      * Returns the spreadsheet version of this workbook
-     * 
+     *
      * @return SpreadsheetVersion enum
      * @since 3.14 beta 2
      */
@@ -592,10 +593,15 @@ public interface Workbook extends Closeable, Iterable<Sheet> {
      * @param label the label of the payload
      * @param fileName the original filename
      * @param command the command to open the payload
-     * 
+     *
      * @return the index of the added ole object, i.e. the storage id
-     * 
+     *
      * @throws IOException if the object can't be embedded
      */
     int addOlePackage(byte[] oleData, String label, String fileName, String command) throws IOException;
+
+    /**
+     * @return an evaluation workbook
+     */
+    EvaluationWorkbook createEvaluationWorkbook();
 }
index 8e6445ffebb292b823811c316c7496c2d4bda25a..da784c7d04034aa0e523d35138feeacaf66df1d3 100644 (file)
@@ -42,6 +42,7 @@ import org.apache.poi.openxml4j.util.ZipEntrySource;
 import org.apache.poi.openxml4j.util.ZipFileZipEntrySource;
 import org.apache.poi.openxml4j.util.ZipSecureFile;
 import org.apache.poi.ss.SpreadsheetVersion;
+import org.apache.poi.ss.formula.EvaluationWorkbook;
 import org.apache.poi.ss.formula.udf.UDFFinder;
 import org.apache.poi.ss.usermodel.CellStyle;
 import org.apache.poi.ss.usermodel.CreationHelper;
@@ -53,7 +54,14 @@ import org.apache.poi.ss.usermodel.Row.MissingCellPolicy;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.SheetVisibility;
 import org.apache.poi.ss.usermodel.Workbook;
-import org.apache.poi.util.*;
+import org.apache.poi.util.Beta;
+import org.apache.poi.util.IOUtils;
+import org.apache.poi.util.Internal;
+import org.apache.poi.util.NotImplemented;
+import org.apache.poi.util.POILogFactory;
+import org.apache.poi.util.POILogger;
+import org.apache.poi.util.Removal;
+import org.apache.poi.util.TempFile;
 import org.apache.poi.xssf.model.SharedStringsTable;
 import org.apache.poi.xssf.usermodel.XSSFChartSheet;
 import org.apache.poi.xssf.usermodel.XSSFSheet;
@@ -1311,5 +1319,9 @@ public class SXSSFWorkbook implements Workbook {
         return _wb.addOlePackage(oleData, label, fileName, command);
     }
 
-//end of interface implementation
+
+    @Override
+    public EvaluationWorkbook createEvaluationWorkbook() {
+        return SXSSFEvaluationWorkbook.create(this);
+    }
 }
index e0f5c7fc08961760dbd7a2a7c0f5ef7f0295c46e..3375ea52bb6ef736a5bf8f7cf5913c05b68d1fe1 100644 (file)
@@ -2390,4 +2390,9 @@ public class XSSFWorkbook extends POIXMLDocument implements Workbook, Date1904Su
     public boolean getCellFormulaValidation() {
         return this.cellFormulaValidation;
     }
+
+    @Override
+    public XSSFEvaluationWorkbook createEvaluationWorkbook() {
+        return XSSFEvaluationWorkbook.create(this);
+    }
 }