From b62cc549fe24f223788716128fb2e218f62025e0 Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Wed, 11 Nov 2015 07:41:54 +0000 Subject: [PATCH] #58597: Remove setAccessible (more coming soon) git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1713770 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hssf/dev/BiffDrawingToXml.java | 33 ++----------------- .../poi/hssf/usermodel/HSSFComment.java | 4 +-- .../poi/hssf/usermodel/HSSFObjectData.java | 2 +- .../poi/hssf/usermodel/HSSFPatriarch.java | 3 +- .../poi/hssf/usermodel/HSSFPicture.java | 2 +- .../poi/hssf/usermodel/HSSFPolygon.java | 2 +- .../poi/hssf/usermodel/HSSFShapeGroup.java | 4 +-- .../apache/poi/hssf/usermodel/HSSFSheet.java | 2 +- .../poi/hssf/usermodel/HSSFSimpleShape.java | 8 ++--- .../poi/hssf/usermodel/HSSFTextbox.java | 6 ++-- .../poi/hssf/usermodel/HSSFWorkbook.java | 6 ++++ .../org/apache/poi/poifs/dev/POIFSDump.java | 20 ++--------- .../poi/poifs/dev/POIFSHeaderDumper.java | 11 ++----- .../poifs/filesystem/NPOIFSFileSystem.java | 11 +++++++ .../storage/BlockAllocationTableReader.java | 5 +++ .../poi/poifs/storage/BlockListImpl.java | 7 ++-- .../org/apache/poi/POIXMLDocumentPart.java | 12 +++++++ .../poi/xwpf/usermodel/XWPFDocument.java | 13 +++----- .../poi/hssf/usermodel/HSSFTestHelper.java | 2 +- .../poi/hssf/usermodel/TestCloneSheet.java | 4 +-- .../poi/hssf/usermodel/TestHSSFSheet.java | 4 +-- .../poi/hssf/usermodel/TestPatriarch.java | 4 +-- 22 files changed, 74 insertions(+), 91 deletions(-) diff --git a/src/java/org/apache/poi/hssf/dev/BiffDrawingToXml.java b/src/java/org/apache/poi/hssf/dev/BiffDrawingToXml.java index 94ba90bed..4d6acf962 100644 --- a/src/java/org/apache/poi/hssf/dev/BiffDrawingToXml.java +++ b/src/java/org/apache/poi/hssf/dev/BiffDrawingToXml.java @@ -24,14 +24,12 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; import org.apache.poi.ddf.EscherRecord; import org.apache.poi.hssf.model.InternalWorkbook; import org.apache.poi.hssf.record.DrawingGroupRecord; -import org.apache.poi.hssf.record.EscherAggregate; import org.apache.poi.hssf.usermodel.HSSFPatriarch; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; @@ -137,7 +135,7 @@ public class BiffDrawingToXml { public static void writeToFile(OutputStream fos, InputStream xlsWorkbook, boolean excludeWorkbookRecords, String[] params) throws IOException { NPOIFSFileSystem fs = new NPOIFSFileSystem(xlsWorkbook); HSSFWorkbook workbook = new HSSFWorkbook(fs); - InternalWorkbook internalWorkbook = getInternalWorkbook(workbook); + InternalWorkbook internalWorkbook = workbook.getInternalWorkbook(); DrawingGroupRecord r = (DrawingGroupRecord) internalWorkbook.findFirstRecordBySid(DrawingGroupRecord.sid); r.decode(); @@ -155,7 +153,7 @@ public class BiffDrawingToXml { HSSFPatriarch p = workbook.getSheetAt(i).getDrawingPatriarch(); if(p != null ) { builder.append(tab).append("\n"); - builder.append(getHSSFPatriarchBoundAggregate(p).toXml(tab + "\t")); + builder.append(p.getBoundAggregate().toXml(tab + "\t")); builder.append(tab).append("\n"); } } @@ -164,31 +162,4 @@ public class BiffDrawingToXml { fos.close(); } - private static EscherAggregate getHSSFPatriarchBoundAggregate(HSSFPatriarch patriarch) { - Field boundAggregateField = null; - try { - boundAggregateField = patriarch.getClass().getDeclaredField("_boundAggregate"); - boundAggregateField.setAccessible(true); - return (EscherAggregate) boundAggregateField.get(patriarch); - } catch (NoSuchFieldException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - return null; - } - - private static InternalWorkbook getInternalWorkbook(HSSFWorkbook workbook) { - Field internalSheetField = null; - try { - internalSheetField = workbook.getClass().getDeclaredField("workbook"); - internalSheetField.setAccessible(true); - return (InternalWorkbook) internalSheetField.get(workbook); - } catch (NoSuchFieldException e) { - e.printStackTrace(); - } catch (IllegalAccessException e) { - e.printStackTrace(); - } - return null; - } } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFComment.java b/src/java/org/apache/poi/hssf/usermodel/HSSFComment.java index 4e71ac2b8..1b8176b3c 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFComment.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFComment.java @@ -87,7 +87,7 @@ public class HSSFComment extends HSSFTextbox implements Comment { @Override void afterInsert(HSSFPatriarch patriarch) { super.afterInsert(patriarch); - patriarch._getBoundAggregate().addTailRecord(getNoteRecord()); + patriarch.getBoundAggregate().addTailRecord(getNoteRecord()); } @Override @@ -253,7 +253,7 @@ public class HSSFComment extends HSSFTextbox implements Comment { public void afterRemove(HSSFPatriarch patriarch){ super.afterRemove(patriarch); - patriarch._getBoundAggregate().removeTailRecord(getNoteRecord()); + patriarch.getBoundAggregate().removeTailRecord(getNoteRecord()); } @Override diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFObjectData.java b/src/java/org/apache/poi/hssf/usermodel/HSSFObjectData.java index ef1f2d9f7..d92f216ab 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFObjectData.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFObjectData.java @@ -126,7 +126,7 @@ public final class HSSFObjectData extends HSSFPicture { @Override void afterInsert(HSSFPatriarch patriarch) { - EscherAggregate agg = patriarch._getBoundAggregate(); + EscherAggregate agg = patriarch.getBoundAggregate(); agg.associateShapeToObjRecord(getEscherContainer().getChildById(EscherClientDataRecord.RECORD_ID), getObjRecord()); EscherBSERecord bse = patriarch.getSheet().getWorkbook().getWorkbook().getBSERecord(getPictureIndex()); diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFPatriarch.java b/src/java/org/apache/poi/hssf/usermodel/HSSFPatriarch.java index 04f57b493..b43168745 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFPatriarch.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFPatriarch.java @@ -495,7 +495,8 @@ public final class HSSFPatriarch implements HSSFShapeContainer, Drawing { * Returns the aggregate escher record we're bound to * @return - low level representation of sheet drawing data */ - protected EscherAggregate _getBoundAggregate() { + @Internal + public EscherAggregate getBoundAggregate() { return _boundAggregate; } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFPicture.java b/src/java/org/apache/poi/hssf/usermodel/HSSFPicture.java index 4de4a40f2..7ee9a0693 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFPicture.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFPicture.java @@ -211,7 +211,7 @@ public class HSSFPicture extends HSSFSimpleShape implements Picture { @Override void afterInsert(HSSFPatriarch patriarch) { - EscherAggregate agg = patriarch._getBoundAggregate(); + EscherAggregate agg = patriarch.getBoundAggregate(); agg.associateShapeToObjRecord(getEscherContainer().getChildById(EscherClientDataRecord.RECORD_ID), getObjRecord()); EscherBSERecord bse = patriarch.getSheet().getWorkbook().getWorkbook().getBSERecord(getPictureIndex()); diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFPolygon.java b/src/java/org/apache/poi/hssf/usermodel/HSSFPolygon.java index df1b765c7..8403a81c4 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFPolygon.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFPolygon.java @@ -116,7 +116,7 @@ public class HSSFPolygon extends HSSFSimpleShape { @Override protected void afterRemove(HSSFPatriarch patriarch) { - patriarch._getBoundAggregate().removeShapeToObjRecord(getEscherContainer().getChildById(EscherClientDataRecord.RECORD_ID)); + patriarch.getBoundAggregate().removeShapeToObjRecord(getEscherContainer().getChildById(EscherClientDataRecord.RECORD_ID)); } /** diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFShapeGroup.java b/src/java/org/apache/poi/hssf/usermodel/HSSFShapeGroup.java index 66440005b..f97b23a8c 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFShapeGroup.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFShapeGroup.java @@ -122,7 +122,7 @@ public class HSSFShapeGroup extends HSSFShape implements HSSFShapeContainer { @Override protected void afterRemove(HSSFPatriarch patriarch) { - patriarch._getBoundAggregate().removeShapeToObjRecord(getEscherContainer().getChildContainers().get(0) + patriarch.getBoundAggregate().removeShapeToObjRecord(getEscherContainer().getChildContainers().get(0) .getChildById(EscherClientDataRecord.RECORD_ID)); for ( int i=0; i it = root.getEntries(); it.hasNext();){ Entry entry = it.next(); diff --git a/src/java/org/apache/poi/poifs/dev/POIFSHeaderDumper.java b/src/java/org/apache/poi/poifs/dev/POIFSHeaderDumper.java index e321c8b9b..642ffa0f9 100644 --- a/src/java/org/apache/poi/poifs/dev/POIFSHeaderDumper.java +++ b/src/java/org/apache/poi/poifs/dev/POIFSHeaderDumper.java @@ -19,8 +19,6 @@ package org.apache.poi.poifs.dev; import java.io.FileInputStream; import java.io.InputStream; -import java.lang.reflect.Field; -import java.lang.reflect.Method; import org.apache.poi.poifs.common.POIFSBigBlockSize; import org.apache.poi.poifs.common.POIFSConstants; @@ -118,11 +116,8 @@ public class POIFSHeaderDumper { System.out.println("Raw Blocks Details:"); System.out.println(" Number of blocks: " + data_blocks.blockCount()); - Method gbm = data_blocks.getClass().getSuperclass().getDeclaredMethod("get", int.class); - gbm.setAccessible(true); - for(int i=0; i + * This method only exists to allow access to protected {@link POIXMLDocumentPart#onDocumentRead()} + * from {@link org.apache.poi.xwpf.usermodel.XWPFDocument} without reflection. It should be removed. + */ + @Internal @Deprecated + public static void _invokeOnDocumentRead(POIXMLDocumentPart part) throws IOException { + part.onDocumentRead(); + } } diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java index 9855e2fda..da9999721 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java @@ -20,7 +20,6 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; -import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -48,7 +47,6 @@ import org.apache.poi.openxml4j.opc.PackageRelationshipTypes; import org.apache.poi.openxml4j.opc.PackagingURIHelper; import org.apache.poi.openxml4j.opc.TargetMode; import org.apache.poi.poifs.crypt.HashAlgorithm; -import org.apache.poi.util.DocumentHelper; import org.apache.poi.util.IOUtils; import org.apache.poi.util.IdentifierManager; import org.apache.poi.util.Internal; @@ -153,6 +151,7 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody { } } + @SuppressWarnings("deprecation") @Override protected void onDocumentRead() throws IOException { try { @@ -224,13 +223,9 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody { for (POIXMLDocumentPart gp : p.getRelations()) { // Trigger the onDocumentRead for all the child parts // Otherwise we'll hit issues on Styles, Settings etc on save - try { - Method onDocumentRead = gp.getClass().getDeclaredMethod("onDocumentRead"); - onDocumentRead.setAccessible(true); - onDocumentRead.invoke(gp); - } catch (Exception e) { - throw new POIXMLException(e); - } + // TODO: Refactor this to not need to access protected method + // from other package! Remove the static helper method once fixed!!! + POIXMLDocumentPart._invokeOnDocumentRead(gp); } } } diff --git a/src/testcases/org/apache/poi/hssf/usermodel/HSSFTestHelper.java b/src/testcases/org/apache/poi/hssf/usermodel/HSSFTestHelper.java index d6b265785..4abebc866 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/HSSFTestHelper.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/HSSFTestHelper.java @@ -72,7 +72,7 @@ public class HSSFTestHelper { } public static EscherAggregate getEscherAggregate(HSSFPatriarch patriarch){ - return patriarch._getBoundAggregate(); + return patriarch.getBoundAggregate(); } public static int allocateNewShapeId(HSSFPatriarch patriarch){ diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestCloneSheet.java b/src/testcases/org/apache/poi/hssf/usermodel/TestCloneSheet.java index 6813baaa9..3bcd66bd4 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestCloneSheet.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestCloneSheet.java @@ -85,13 +85,13 @@ public final class TestCloneSheet extends TestCase { HSSFSheet s = b.createSheet("Test"); HSSFPatriarch patriarch = s.createDrawingPatriarch(); - EscherAggregate agg1 = patriarch._getBoundAggregate(); + EscherAggregate agg1 = patriarch.getBoundAggregate(); HSSFSheet s2 = s.cloneSheet(b); patriarch = s2.getDrawingPatriarch(); - EscherAggregate agg2 = patriarch._getBoundAggregate(); + EscherAggregate agg2 = patriarch.getBoundAggregate(); EscherSpRecord sp1 = (EscherSpRecord) agg1.getEscherContainer().getChild(1).getChild(0).getChild(1); EscherSpRecord sp2 = (EscherSpRecord) agg2.getEscherContainer().getChild(1).getChild(0).getChild(1); diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java index 4fa9865a6..c60823ee8 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFSheet.java @@ -940,8 +940,8 @@ public final class TestHSSFSheet extends BaseTestSheet { HSSFSheet sheet2 = wb2.getSheetAt(1); //check that id of the drawing group was updated - EscherDgRecord dg1 = (EscherDgRecord)sheet1.getDrawingPatriarch()._getBoundAggregate().findFirstWithId(EscherDgRecord.RECORD_ID); - EscherDgRecord dg2 = (EscherDgRecord)sheet2.getDrawingPatriarch()._getBoundAggregate().findFirstWithId(EscherDgRecord.RECORD_ID); + EscherDgRecord dg1 = (EscherDgRecord)sheet1.getDrawingPatriarch().getBoundAggregate().findFirstWithId(EscherDgRecord.RECORD_ID); + EscherDgRecord dg2 = (EscherDgRecord)sheet2.getDrawingPatriarch().getBoundAggregate().findFirstWithId(EscherDgRecord.RECORD_ID); int dg_id_1 = dg1.getOptions() >> 4; int dg_id_2 = dg2.getOptions() >> 4; assertEquals(dg_id_1 + 1, dg_id_2); diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestPatriarch.java b/src/testcases/org/apache/poi/hssf/usermodel/TestPatriarch.java index d9eba84bd..49cbaeec0 100644 --- a/src/testcases/org/apache/poi/hssf/usermodel/TestPatriarch.java +++ b/src/testcases/org/apache/poi/hssf/usermodel/TestPatriarch.java @@ -40,7 +40,7 @@ public class TestPatriarch extends TestCase { assertSame(patriarch, sh.getDrawingPatriarch()); - EscherAggregate agg = patriarch._getBoundAggregate(); + EscherAggregate agg = patriarch.getBoundAggregate(); EscherDgRecord dg = agg.getEscherContainer().getChildById(EscherDgRecord.RECORD_ID); int lastId = dg.getLastMSOSPID(); @@ -50,7 +50,7 @@ public class TestPatriarch extends TestCase { wb = HSSFTestDataSamples.writeOutAndReadBack(wb); sh = wb.getSheetAt(0); patriarch = sh.createDrawingPatriarch(); - dg = patriarch._getBoundAggregate().getEscherContainer().getChildById(EscherDgRecord.RECORD_ID); + dg = patriarch.getBoundAggregate().getEscherContainer().getChildById(EscherDgRecord.RECORD_ID); assertEquals(lastId, dg.getLastMSOSPID()); }