From d9052b3d3c09087ada45dadd8efbd750b6195c94 Mon Sep 17 00:00:00 2001 From: Gan Keyu Date: Sat, 12 Aug 2023 13:55:05 +0800 Subject: [PATCH] Remove reflections in unused classes --- .../UserModel/OperationEvaluatorFactory.cs | 28 +++++++-------- main/POIFS/NIO/FileBackedDataSource.cs | 34 +------------------ .../SS/Formula/Eval/Forked/ForkedEvaluator.cs | 17 +++++++--- openxml4Net/Util/ZipSecureFile.cs | 10 +++--- 4 files changed, 29 insertions(+), 60 deletions(-) diff --git a/main/HSSF/UserModel/OperationEvaluatorFactory.cs b/main/HSSF/UserModel/OperationEvaluatorFactory.cs index 4f6aef695..1e0846416 100644 --- a/main/HSSF/UserModel/OperationEvaluatorFactory.cs +++ b/main/HSSF/UserModel/OperationEvaluatorFactory.cs @@ -21,10 +21,11 @@ namespace NPOI.HSSF.UserModel using NPOI.SS.Formula.Eval; using System.Collections; - using System.Reflection; using NPOI.SS.Formula.PTG; + // REMOVE-REFLECTION: This class won't work as intended, and not used elsewhere in NPOI. Obsolete and remove. + /** * This class Creates OperationEval instances to help evaluate OperationPtg * formula tokens. @@ -85,44 +86,39 @@ private static void Add(Hashtable m, Type ptgClass, Type evalClass) throw new Exception("Eval class must not be abstract"); } - ConstructorInfo constructor; + /*ConstructorInfo constructor; constructor = evalClass.GetConstructor(OPERATION_CONSTRUCTOR_CLASS_ARRAY); if (!constructor.IsPublic) { throw new Exception("Eval constructor must be public"); } - m[ptgClass] = constructor; + m[ptgClass] = constructor;*/ } /** * returns the OperationEval concrete impl instance corresponding * to the supplied operationPtg */ + [Obsolete] public static OperationEval Create(OperationPtg ptg) { + throw new NotSupportedException(); + if (ptg == null) { throw new ArgumentException("ptg must not be null"); } - Type ptgClass = ptg.GetType(); + // (original comment) + // ExpPtg Is used for array formulas and shared formulas. + // it Is currently Unsupported, and may not even Get implemented here - ConstructorInfo constructor = (ConstructorInfo)_constructorsByPtgClass[ptgClass]; - if (constructor == null) - { - if (ptgClass == typeof(ExpPtg)) - { - // ExpPtg Is used for array formulas and shared formulas. - // it Is currently Unsupported, and may not even Get implemented here - throw new Exception("ExpPtg currently not supported"); - } - throw new Exception("Unexpected operation ptg class (" + ptgClass.Name + ")"); - } + /*Type ptgClass = ptg.GetType(); Object result; Object[] initargs = { ptg }; result = constructor.Invoke(initargs); - return (OperationEval)result; + return (OperationEval)result;*/ } } } \ No newline at end of file diff --git a/main/POIFS/NIO/FileBackedDataSource.cs b/main/POIFS/NIO/FileBackedDataSource.cs index 92fc61971..8cb894f25 100644 --- a/main/POIFS/NIO/FileBackedDataSource.cs +++ b/main/POIFS/NIO/FileBackedDataSource.cs @@ -20,7 +20,6 @@ limitations under the License. using NPOI.Util; using System.Collections.Generic; using System.Security; -using System.Reflection; //using System.IO.MemoryMappedFiles; namespace NPOI.POIFS.NIO { @@ -209,38 +208,7 @@ public override void Close() // but we at least have unit-tests which will indicate this when run on Windows private static void unmap(ByteBuffer bb) { - //TODO: try add clean method for ByteBuffer class. - //Type fcClass = bb.GetType(); - //try - //{ - // // invoke bb.cleaner().clean(), but do not depend on sun.nio - // // interfaces - // MethodInfo cleanerMethod = fcClass.GetMethod("cleaner"); - // //cleanerMethod.setAccessible(true); - // Object cleaner = cleanerMethod.Invoke(bb, null); - // MethodInfo cleanMethod = cleaner.GetType().GetMethod("clean"); - // cleanMethod.Invoke(cleaner, null); - //} - //catch (NotSupportedException e) - //{ - // // e.printStackTrace(); - //} - //catch (SecurityException e) - //{ - // // e.printStackTrace(); - //} - //catch (MethodAccessException e) - //{ - // // e.printStackTrace(); - //} - //catch (ArgumentException e) - //{ - // // e.printStackTrace(); - //} - //catch (TargetException e) - //{ - // // e.printStackTrace(); - //} + // REMOVE-REFLECTION: This method is Java-specific. } } diff --git a/main/SS/Formula/Eval/Forked/ForkedEvaluator.cs b/main/SS/Formula/Eval/Forked/ForkedEvaluator.cs index a1b8e41c0..e5a7ff90a 100644 --- a/main/SS/Formula/Eval/Forked/ForkedEvaluator.cs +++ b/main/SS/Formula/Eval/Forked/ForkedEvaluator.cs @@ -16,7 +16,6 @@ limitations under the License. ==================================================================== */ using System; -using System.Reflection; using NPOI.HSSF.UserModel; using NPOI.SS.Formula; using NPOI.SS.Formula.Eval; @@ -36,6 +35,9 @@ namespace NPOI.SS.Formula.Eval.Forked * * @author Josh Micich */ + + // REMOVE-REFLECTION: This class doesn't work from the beginning because of typo. + // And no elsewhere in NPOI uses this class. Consider removing it. public class ForkedEvaluator { @@ -58,10 +60,15 @@ private static IEvaluationWorkbook CreateEvaluationWorkbook(IWorkbook wb) try { // TODO: check if this is Java 9 compatible ... - Type evalWB = Type.GetType("NPOI.XSSF.UserModel.XSSFEvaluationWorkbook"); - Type xssfWB = Type.GetType("NPOI.XSSF.UserMode.XSSFWorkbook"); - MethodInfo createM = evalWB.GetMethod("create", new Type[] { xssfWB }); - return (IEvaluationWorkbook)createM.Invoke(null, new object[] { wb }); + /*Type evalWB = Type.GetType("NPOI.XSSF.UserModel.XSSFEvaluationWorkbook"); + Type xssfWB = Type.GetType("NPOI.XSSF.UserMode.XSSFWorkbook");*/ + + // REMOVE-REFLECTION: The code is Java-specific and different from the current NPOI object model + + /*var createM = evalWB.GetMethod("create", new Type[] { xssfWB }); + return (IEvaluationWorkbook)createM.Invoke(null, new object[] { wb });*/ + + throw new NotSupportedException(); } catch (Exception e) { diff --git a/openxml4Net/Util/ZipSecureFile.cs b/openxml4Net/Util/ZipSecureFile.cs index 53edb5915..5ccb27bd3 100644 --- a/openxml4Net/Util/ZipSecureFile.cs +++ b/openxml4Net/Util/ZipSecureFile.cs @@ -5,7 +5,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using System.Reflection; using System.Text; namespace NPOI.OpenXml4Net.Util @@ -110,6 +109,9 @@ public ZipSecureFile(String name) public static ThresholdInputStream AddThreshold(Stream zipIS) { + // REMOVE-REFLCETION: Meaningless reflection is used here as the type FilterInputStream doesn't have a field named "in". + // And obviously this stub does nothing. + // There needs to be another way of handling ZIP bombs. ThresholdInputStream newInner = null; if (zipIS is InflaterInputStream) @@ -117,11 +119,7 @@ public static ThresholdInputStream AddThreshold(Stream zipIS) //replace inner stream of zipIS by using a ThresholdInputStream instance?? try { - FieldInfo f = typeof(FilterInputStream).GetField("in"); - //f.SetAccessible(true); - //InputStream oldInner = (InputStream)f.Get(zipIS); - //newInner = new ThresholdInputStream(oldInner, null); - //f.Set(zipIS, newInner); + // REMOVE-REFLECTION: Java-specific code } catch (Exception ex) { //logger.Log(POILogger.WARN, "SecurityManager doesn't allow manipulation via reflection for zipbomb detection - continue with original input stream", ex); newInner = null;