Bug 54625 - User defined functions added to static variable
Summary: User defined functions added to static variable
Alias: None
Product: POI
Classification: Unclassified
Component: HSSF (show other bugs)
Version: 3.9-FINAL
Hardware: PC All
: P2 normal (vote)
Target Milestone: ---
Assignee: POI Developers List
Depends on:
Reported: 2013-03-01 03:18 UTC by stuart
Modified: 2013-03-03 16:16 UTC (History)
0 users


Note You need to log in before you can comment on or make changes to this bug.
Description stuart 2013-03-01 03:18:12 UTC
When registering a new User defined function as described here:


In the case of the HSSF workbook

doing this:


adds the udfToolpack to a static variable. 

At the very least this could cause a bit of a memory leak, and in my case when dealing with multiple workbooks in a single process, that have different implementations for a particular UDF name, this statefulness means that the first one registered is the one that always gets used.

I suggest changing this

private UDFFinder _udfFinder = UDFFinder.DEFAULT;

to this

private IndexedUDFFinder _udfFinder = new IndexedUDFFinder(UDFFinder.DEFAULT);

in org.apache.poi.hssf.usermodel.HSSFWorkbook

In the meantime, a suitable workaround for this issue is to create your own implementation of a workbook factory, and when you create a HSSFWorkbook, use reflection to set the appropriate value to the _udfFinder private variable.
Comment 1 stuart 2013-03-01 03:29:29 UTC
If anybody needs it, here is the workaround that I use

public class POIWorkbookFactory {

	public static Workbook create(InputStream inp) throws Exception {
		Workbook workbook = WorkbookFactory.create(inp);
		if (workbook instanceof HSSFWorkbook) {
			Field field = HSSFWorkbook.class.getDeclaredField("_udfFinder");
			field.set(workbook, new IndexedUDFFinder(UDFFinder.DEFAULT));
		return workbook;

Comment 2 Yegor Kozlov 2013-03-03 16:16:36 UTC
Fix applied in r1452060

I had to move IndexedUDFFinder to the common package org.apache.poi.ss.formula.udf, otherwise HSSF did not compile.