From: Andreas Beeker Date: Mon, 17 Aug 2020 11:49:02 +0000 (+0000) Subject: #64036 - Replace reflection calls in factories for Java 9+ X-Git-Tag: before_ooxml_3rd_edition~137 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f8040e5f8c0e74b064db13c93ec9c5025542848c;p=poi.git #64036 - Replace reflection calls in factories for Java 9+ provide EvalutionWorkbook in HSSF/XSSF/SXSSF git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1880925 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index 6ce74d1206..7f9692ecdc 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -2251,4 +2251,10 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss } } } + + + @Override + public HSSFEvaluationWorkbook createEvaluationWorkbook() { + return HSSFEvaluationWorkbook.create(this); + } } diff --git a/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluator.java b/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluator.java index 9a46ac44a8..b85cc15ea9 100644 --- a/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluator.java +++ b/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluator.java @@ -17,22 +17,20 @@ 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 null 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); } } diff --git a/src/java/org/apache/poi/ss/usermodel/Workbook.java b/src/java/org/apache/poi/ss/usermodel/Workbook.java index 77cce0b226..7ef1bdbf3d 100644 --- a/src/java/org/apache/poi/ss/usermodel/Workbook.java +++ b/src/java/org/apache/poi/ss/usermodel/Workbook.java @@ -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 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 { * @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 { /** * 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 { /** * 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 { * @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 { * @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(); } diff --git a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java index 8e6445ffeb..da784c7d04 100644 --- a/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/streaming/SXSSFWorkbook.java @@ -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); + } } diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java index e0f5c7fc08..3375ea52bb 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -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); + } }