#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
This commit is contained in:
Andreas Beeker 2020-08-17 11:49:02 +00:00
parent 506089a9f1
commit f8040e5f8c
5 changed files with 53 additions and 44 deletions

View File

@ -2251,4 +2251,10 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss
}
}
}
@Override
public HSSFEvaluationWorkbook createEvaluationWorkbook() {
return HSSFEvaluationWorkbook.create(this);
}
}

View File

@ -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 <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);
}
}

View 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();
}

View 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);
}
}

View 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);
}
}