Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Remove reflections: Misc and Summary #1137

Merged
merged 25 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions OpenXmlFormats/OOXMLFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace NPOI.OpenXmlFormats
{
public class OOXMLFactory<T>
public class OOXMLFactory<T> where T : new()
{
XmlSerializer serializerObj = null;
public OOXMLFactory()
Expand All @@ -22,7 +22,7 @@ public T Parse(Stream stream)
}
public T Create()
{
return (T)Activator.CreateInstance(typeof(T));
return new T();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although new T() uses Activator.CreateInstance internally, this semantic is slightly different for AOT analyzers I believe.

}
}
}
2 changes: 1 addition & 1 deletion OpenXmlFormats/Wordprocessing/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5784,7 +5784,7 @@ public CT_Row Copy()
ctRow.textIdField = this.textIdField?.ToArray();
ctRow.trPrField = this.trPrField?.Copy();
ctRow.tblPrExField = this.tblPrExField?.Copy();
ctRow.itemsElementNameField = this.itemsElementNameField?.Copy();
ctRow.itemsElementNameField = this.itemsElementNameField?.ReflectionlessDeepCopy();
ctRow.itemsField = this.itemsField?.Copy();
return ctRow;
}
Expand Down
2 changes: 1 addition & 1 deletion main/HPSF/MutableSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ public override Property[] Properties
/// </summary>
public void EnsureProperties()
{
properties = (Property[])preprops.ToArray(typeof(Property));
properties = preprops.ToArray<Property>();
}


Expand Down
9 changes: 3 additions & 6 deletions main/HSSF/EventUserModel/EventWorkbookBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace NPOI.HSSF.EventUserModel
using NPOI.HSSF.Record;
using NPOI.HSSF.UserModel;
using System.Collections.Generic;
using NPOI.Util;

/// <summary>
/// When working with the EventUserModel, if you want to
Expand Down Expand Up @@ -134,19 +135,15 @@ public SheetRecordCollectingListener(IHSSFListener childListener)
/// <returns></returns>
public BoundSheetRecord[] GetBoundSheetRecords()
{
return (BoundSheetRecord[])boundSheetRecords.ToArray(
typeof(BoundSheetRecord)
);
return boundSheetRecords.ToArray<BoundSheetRecord>();
}
/// <summary>
/// Gets the extern sheet records.
/// </summary>
/// <returns></returns>
public ExternSheetRecord[] GetExternSheetRecords()
{
return (ExternSheetRecord[])externSheetRecords.ToArray(
typeof(ExternSheetRecord)
);
return externSheetRecords.ToArray<ExternSheetRecord>();
}
/// <summary>
/// Gets the SST record.
Expand Down
6 changes: 3 additions & 3 deletions main/HSSF/Model/LinkTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,15 @@ public ExternalBookBlock(RecordStream rs)
{
temp.Add(rs.GetNext());
}
_externalNameRecords = (ExternalNameRecord[])temp.ToArray(typeof(ExternalNameRecord));
_externalNameRecords = temp.ToArray<ExternalNameRecord>();

temp.Clear();

while (rs.PeekNextClass() == typeof(CRNCountRecord))
{
temp.Add(new CRNBlock(rs));
}
_crnBlocks = (CRNBlock[])temp.ToArray(typeof(CRNBlock));
_crnBlocks = temp.ToArray<CRNBlock>();
}

/**
Expand Down Expand Up @@ -235,7 +235,7 @@ public LinkTable(List<Record> inputList, int startIndex, WorkbookRecordList work
}

//_externalBookBlocks = new ExternalBookBlock[temp.Count];
_externalBookBlocks = (ExternalBookBlock[])temp.ToArray(typeof(ExternalBookBlock));
_externalBookBlocks = temp.ToArray<ExternalBookBlock>();
temp.Clear();

if (_externalBookBlocks.Length > 0)
Expand Down
20 changes: 10 additions & 10 deletions main/HSSF/Model/RowBlocksReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace NPOI.HSSF.Model
using NPOI.HSSF.Record;
using NPOI.HSSF.Record.Aggregates;
using System.Collections;
using NPOI.Util;

/**
* Segregates the 'Row Blocks' section of a single sheet into plain row/cell records and
Expand Down Expand Up @@ -92,20 +93,19 @@ public RowBlocksReader(RecordStream rs)
dest.Add(rec);
prevRec = rec;
}
SharedFormulaRecord[] sharedFormulaRecs = new SharedFormulaRecord[shFrmRecords.Count];
List<ArrayRecord> arrayRecs = new List<ArrayRecord>(arrayRecords.Count);
List<TableRecord> tableRecs = new List<TableRecord>(tableRecords.Count);
sharedFormulaRecs = (SharedFormulaRecord[])shFrmRecords.ToArray(typeof(SharedFormulaRecord));
SharedFormulaRecord[] sharedFormulaRecs;
List<ArrayRecord> arrayRecs;
List<TableRecord> tableRecs;
sharedFormulaRecs = shFrmRecords.ToArray<SharedFormulaRecord>();

CellReference[] firstCells = new CellReference[firstCellRefs.Count];
firstCells=firstCellRefs.ToArray();
arrayRecs = new List<ArrayRecord>((ArrayRecord[])arrayRecords.ToArray(typeof(ArrayRecord)));
tableRecs = new List<TableRecord>((TableRecord[])tableRecords.ToArray(typeof(TableRecord)));
CellReference[] firstCells;
firstCells = firstCellRefs.ToArray();
arrayRecs = new List<ArrayRecord>(arrayRecords.ToArray<ArrayRecord>());
tableRecs = new List<TableRecord>(tableRecords.ToArray<TableRecord>());

_plainRecords = plainRecords;
_sfm = SharedValueManager.Create(sharedFormulaRecs,firstCells, arrayRecs, tableRecs);
_mergedCellsRecords = new MergeCellsRecord[mergeCellRecords.Count];
_mergedCellsRecords = (MergeCellsRecord[])mergeCellRecords.ToArray(typeof(MergeCellsRecord));
_mergedCellsRecords = mergeCellRecords.ToArray<MergeCellsRecord>();
}

/**
Expand Down
1 change: 0 additions & 1 deletion main/HSSF/UserModel/HSSFWorkbook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ namespace NPOI.HSSF.UserModel
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using NPOI.DDF;
Expand Down
3 changes: 2 additions & 1 deletion main/SS/Formula/CellEvaluationFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace NPOI.SS.Formula
using System.Collections;
using System.Text;
using NPOI.SS.Formula.Eval;
using NPOI.Util;

/**
* Stores details about the current evaluation of a cell.<br/>
Expand Down Expand Up @@ -69,7 +70,7 @@ private CellCacheEntry[] GetSensitiveInputCells()
return CellCacheEntry.EMPTY_ARRAY;
}
CellCacheEntry[] result = new CellCacheEntry[nItems];
result = (CellCacheEntry[])_sensitiveInputCells.ToArray(typeof(CellCacheEntry));
result = _sensitiveInputCells.ToArray<CellCacheEntry>();
return result;
}
public void AddUsedBlankCell(int bookIndex, int sheetIndex, int rowIndex, int columnIndex)
Expand Down
3 changes: 2 additions & 1 deletion main/SS/Formula/CollaboratingWorkbooksEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace NPOI.SS.Formula
using System.Collections;
using System.Collections.Generic;
using NPOI.SS.UserModel;
using NPOI.Util;

[Serializable]
public class WorkbookNotFoundException : Exception
Expand Down Expand Up @@ -169,7 +170,7 @@ private void UnhookOldEnvironments(WorkbookEvaluator[] evaluators)
oldEnvs.Add(evaluators[i].GetEnvironment());
}
CollaboratingWorkbooksEnvironment[] oldCWEs = new CollaboratingWorkbooksEnvironment[oldEnvs.Count];
oldCWEs = (CollaboratingWorkbooksEnvironment[])oldEnvs.ToArray(typeof(CollaboratingWorkbooksEnvironment));
oldCWEs = oldEnvs.ToArray<CollaboratingWorkbooksEnvironment>();
for (int i = 0; i < oldCWEs.Length; i++)
{
oldCWEs[i].Unhook();
Expand Down
3 changes: 1 addition & 2 deletions main/SS/Formula/EvaluationConditionalFormatRule.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using EnumsNET;
using NPOI.SS.Formula.Eval;
using NPOI.SS.Formula.Eval;
using NPOI.SS.Formula.Functions;
using NPOI.SS.UserModel;
using NPOI.SS.Util;
Expand Down
2 changes: 1 addition & 1 deletion main/SS/Formula/FormulaParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1805,7 +1805,7 @@ private ParseNode[] Arguments()
throw expected("',' or ')'");
}
}
ParseNode[] result = (ParseNode[])temp.ToArray(typeof(ParseNode));
ParseNode[] result = temp.ToArray<ParseNode>();
return result;
}

Expand Down
7 changes: 4 additions & 3 deletions main/SS/Formula/OperationEvaluatorFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ namespace NPOI.SS.Formula

using System;
using System.Collections;
using System.Reflection;

using NPOI.SS.Formula.Eval;
using NPOI.SS.Formula.Functions;
Expand Down Expand Up @@ -69,13 +68,15 @@ private static Hashtable InitialiseInstancesMap()

private static void Add(Hashtable m, OperationPtg ptgKey, Functions.Function instance)
{
// REMOVE-REFLECTION: Reflection here is only to ensure singleton mode. Safe to remove since we are sure.

// make sure ptg has single private constructor because map lookups assume singleton keys
ConstructorInfo[] cc = ptgKey.GetType().GetConstructors();
/*ConstructorInfo[] cc = ptgKey.GetType().GetConstructors();
if (cc.Length > 1 || (cc.Length > 0 && !cc[0].IsPrivate))
{
throw new Exception("Failed to verify instance ("
+ ptgKey.GetType().Name + ") is a singleton.");
}
}*/
m[ptgKey] = instance;
}

Expand Down
4 changes: 2 additions & 2 deletions main/SS/Formula/PTG/AttrPtg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ public override String ToFormulaString()
return "UNKNOWN ATTRIBUTE";
}

public override Object Clone()
/*public override Object Clone()
{
int[] jt;
if (_jumpTable == null)
Expand All @@ -349,6 +349,6 @@ public override Object Clone()
jt = (int[])_jumpTable.Clone();
}
return new AttrPtg(field_1_options, field_2_data, jt, _chooseFuncOffset);
}
}*/
}
}
3 changes: 2 additions & 1 deletion main/SS/Formula/PTG/OperandPtg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public OperandPtg Copy()
{
try
{
return (OperandPtg)Clone();
// REMOVE-REFLECTION: After careful inspection, MemberwiseClone() should be enough for all built-in OpreandPtgs.
return (OperandPtg)MemberwiseClone();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All derived classes of OperandPtg holds only value types (some unused members are not, but that's fine).

}
catch (NotSupportedException e)
{
Expand Down
16 changes: 8 additions & 8 deletions main/SS/Formula/PTG/Ptg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace NPOI.SS.Formula.PTG
* @author Jason Height (jheight at chariot dot net dot au)
*/
[Serializable]
public abstract class Ptg : ICloneable
public abstract class Ptg //: ICloneable
{
public static Ptg[] EMPTY_PTG_ARRAY = { };

Expand Down Expand Up @@ -187,7 +187,7 @@ private static Ptg[] ToPtgArray(ArrayList l)
return EMPTY_PTG_ARRAY;
}

Ptg[] result = (Ptg[])l.ToArray(typeof(Ptg));
Ptg[] result = l.ToArray<Ptg>();
return result;
}
/**
Expand All @@ -208,10 +208,10 @@ private static Ptg[] ToPtgArray(ArrayList l)
// }
// return (Ptg)Clone();
//}
public virtual object Clone()
{
return this.Copy();
}
// private virtual object Clone()
//{
//return this.Copy();
//}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not used elsewhere. And IClonable.Clone throws NotImplementedException, see below.


/**
* This method will return the same result as {@link #getEncodedSizeWithoutArrayData(Ptg[])}
Expand Down Expand Up @@ -358,14 +358,14 @@ public char RVAType
}
}

#region ICloneable Members
/*#region ICloneable Members

object ICloneable.Clone()
{
throw new NotImplementedException();
}

#endregion
#endregion*/

public static bool DoesFormulaReferToDeletedCell(Ptg[] ptgs)
{
Expand Down
4 changes: 2 additions & 2 deletions main/SS/Formula/PTG/UnknownPtg.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public override byte DefaultOperandClass
get { return Ptg.CLASS_VALUE; }
}

public override Object Clone()
/*public override Object Clone()
{
return new UnknownPtg();
}
}*/
}
}
5 changes: 3 additions & 2 deletions main/SS/Util/AreaReference.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace NPOI.SS.Util
using System;
using System.Text;
using System.Collections;
using NPOI.Util;

public class AreaReference
{
Expand Down Expand Up @@ -315,7 +316,7 @@ public static AreaReference[] GenerateContiguous(String reference)
new AreaReference(t)
);
}
return (AreaReference[])refs.ToArray(typeof(AreaReference));
return refs.ToArray<AreaReference>();
}

/**
Expand Down Expand Up @@ -373,7 +374,7 @@ public CellReference[] GetAllReferencedCells()
refs.Add(ref1);
}
}
return (CellReference[])refs.ToArray(typeof(CellReference));
return refs.ToArray<CellReference>();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion main/SS/Util/CellRangeAddressList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public CellRangeAddress[] CellRangeAddresses
get
{
CellRangeAddress[] result =
(CellRangeAddress[])_list.ToArray(typeof(CellRangeAddress));
_list.ToArray<CellRangeAddress>();
return result;
}
}
Expand Down
3 changes: 2 additions & 1 deletion main/SS/Util/CellRangeUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace NPOI.SS.Util
using System.Collections;
using NPOI.SS.Util;
using System.Collections.Generic;
using NPOI.Util;

/**
* Utility class that builds on {@link CellRangeAddress}
Expand Down Expand Up @@ -297,7 +298,7 @@ private static CellRangeAddress[] MergeRanges(CellRangeAddress range1, CellRange
private static CellRangeAddress[] ToArray(ArrayList temp)
{
CellRangeAddress[] result = new CellRangeAddress[temp.Count];
result = (CellRangeAddress[])temp.ToArray(typeof(CellRangeAddress));
result = temp.ToArray<CellRangeAddress>();
return result;
}

Expand Down
9 changes: 3 additions & 6 deletions main/SS/Util/SSCellRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ public static SSCellRange<K> Create(int firstRow, int firstColumn, int height, i
throw new ArgumentException("Array size mismatch.");
}

K[] flattenedArray = (K[])Array.CreateInstance(cellClass, nItems);
flattenedArray=flattenedList.ToArray();
K[] flattenedArray = flattenedList.ToArray();
return new SSCellRange<K>(firstRow, firstColumn, height, width, flattenedArray);
}

Expand Down Expand Up @@ -155,12 +154,10 @@ public K[] FlattenedCells
public K[][] Cells
{
get {
Type itemCls = _flattenedArray.GetType();
K[][] result = (K[][])Array.CreateInstance(itemCls, _height);
itemCls = itemCls.GetElementType();
K[][] result = new K[_height][];
for (int r = _height - 1; r >= 0; r--)
{
K[] row = (K[])Array.CreateInstance(itemCls, _width);
K[] row = new K[_width];
int flatIndex = _width * r;
Array.Copy(_flattenedArray, flatIndex, row, 0, _width);
}
Expand Down
Loading