Skip to content

Commit f5acb42

Browse files
dmitry.radchukiText-CI
authored andcommitted
Replaced reflection instances creation with functional interfaces
DEVSIX-5739 Autoported commit. Original commit hash: [2ff83064]
1 parent c11db32 commit f5acb42

File tree

8 files changed

+348
-326
lines changed

8 files changed

+348
-326
lines changed

itext.tests/itext.html2pdf.tests/itext/html2pdf/attach/impl/DefaultTagWorkerFactoryTest.cs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,44 @@ You should have received a copy of the GNU Affero General Public License
2121
along with this program. If not, see <https://www.gnu.org/licenses/>.
2222
*/
2323
using System;
24-
using iText.Commons.Utils;
2524
using iText.Html2pdf;
2625
using iText.Html2pdf.Attach;
27-
using iText.Html2pdf.Exceptions;
26+
using iText.Layout;
27+
using iText.StyledXmlParser.Node;
2828
using iText.StyledXmlParser.Node.Impl.Jsoup.Node;
2929
using iText.Test;
3030

3131
namespace iText.Html2pdf.Attach.Impl {
3232
public class DefaultTagWorkerFactoryTest : ExtendedITextTest {
3333
[NUnit.Framework.Test]
3434
public virtual void CannotGetTagWorkerForCustomTagViaReflection() {
35-
String tag = "custom-tag";
36-
String className = "iText.Html2pdf.Attach.Impl.TestClass";
37-
NUnit.Framework.Assert.That(() => {
38-
new TestTagWorkerFactory().GetTagWorker(new JsoupElementNode(new iText.StyledXmlParser.Jsoup.Nodes.Element
39-
(iText.StyledXmlParser.Jsoup.Parser.Tag.ValueOf("custom-tag"), "")), new ProcessorContext(new ConverterProperties
40-
()));
41-
}
42-
, NUnit.Framework.Throws.InstanceOf<TagWorkerInitializationException>().With.Message.EqualTo(MessageFormatUtil.Format(TagWorkerInitializationException.REFLECTION_IN_TAG_WORKER_FACTORY_IMPLEMENTATION_FAILED, className, tag)))
43-
;
35+
ITagWorker tagWorker = new TestTagWorkerFactory().GetTagWorker(new JsoupElementNode(new iText.StyledXmlParser.Jsoup.Nodes.Element
36+
(iText.StyledXmlParser.Jsoup.Parser.Tag.ValueOf("custom-tag"), "")), new ProcessorContext(new ConverterProperties
37+
()));
38+
NUnit.Framework.Assert.AreEqual(typeof(TestClass), tagWorker.GetType());
4439
}
4540
}
4641

4742
internal class TestTagWorkerFactory : DefaultTagWorkerFactory {
4843
public TestTagWorkerFactory() {
49-
GetDefaultMapping().PutMapping("custom-tag", typeof(TestClass));
44+
GetDefaultMapping().PutMapping("custom-tag", (lhs, rhs) => new TestClass());
5045
}
5146
}
5247

53-
internal class TestClass {
48+
internal class TestClass : ITagWorker {
49+
public virtual void ProcessEnd(IElementNode element, ProcessorContext context) {
50+
}
51+
52+
public virtual bool ProcessContent(String content, ProcessorContext context) {
53+
return false;
54+
}
55+
56+
public virtual bool ProcessTagChild(ITagWorker childTagWorker, ProcessorContext context) {
57+
return false;
58+
}
59+
60+
public virtual IPropertyContainer GetElementResult() {
61+
return null;
62+
}
5463
}
5564
}

itext.tests/itext.html2pdf.tests/itext/html2pdf/css/apply/impl/DefaultCssApplierFactoryTest.cs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,31 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2020
You should have received a copy of the GNU Affero General Public License
2121
along with this program. If not, see <https://www.gnu.org/licenses/>.
2222
*/
23-
using System;
24-
using iText.Commons.Utils;
25-
using iText.Html2pdf.Exceptions;
23+
using iText.Html2pdf.Attach;
24+
using iText.Html2pdf.Css.Apply;
25+
using iText.StyledXmlParser.Node;
2626
using iText.StyledXmlParser.Node.Impl.Jsoup.Node;
2727
using iText.Test;
2828

2929
namespace iText.Html2pdf.Css.Apply.Impl {
3030
public class DefaultCssApplierFactoryTest : ExtendedITextTest {
3131
[NUnit.Framework.Test]
3232
public virtual void CannotGetCssApplierForCustomTagViaReflection() {
33-
String tag = "custom-tag";
34-
String className = "iText.Html2pdf.Css.Apply.Impl.TestClass";
35-
NUnit.Framework.Assert.That(() => {
36-
new TestCssApplierFactory().GetCssApplier(new JsoupElementNode(new iText.StyledXmlParser.Jsoup.Nodes.Element
37-
(iText.StyledXmlParser.Jsoup.Parser.Tag.ValueOf("custom-tag"), "")));
38-
}
39-
, NUnit.Framework.Throws.InstanceOf<CssApplierInitializationException>().With.Message.EqualTo(MessageFormatUtil.Format(CssApplierInitializationException.REFLECTION_FAILED, className, tag)))
40-
;
33+
ICssApplier cssApplier = new TestCssApplierFactory().GetCssApplier(new JsoupElementNode(new iText.StyledXmlParser.Jsoup.Nodes.Element
34+
(iText.StyledXmlParser.Jsoup.Parser.Tag.ValueOf("custom-tag"), "")));
35+
NUnit.Framework.Assert.AreEqual(typeof(TestClass), cssApplier.GetType());
4136
}
4237
}
4338

4439
internal class TestCssApplierFactory : DefaultCssApplierFactory {
4540
public TestCssApplierFactory() {
46-
GetDefaultMapping().PutMapping("custom-tag", typeof(TestClass));
41+
GetDefaultMapping().PutMapping("custom-tag", () => new TestClass());
4742
}
4843
}
4944

50-
internal class TestClass {
45+
internal class TestClass : ICssApplier {
46+
public virtual void Apply(ProcessorContext context, IStylesContainer stylesContainer, ITagWorker tagWorker
47+
) {
48+
}
5149
}
5250
}

itext/itext.html2pdf/itext/html2pdf/attach/impl/DefaultTagWorkerFactory.cs

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,8 @@ source product.
4141
4242
*/
4343
using System;
44-
using System.Reflection;
4544
using iText.Html2pdf.Attach;
4645
using iText.Html2pdf.Css;
47-
using iText.Html2pdf.Exceptions;
4846
using iText.Html2pdf.Util;
4947
using iText.StyledXmlParser.Node;
5048

@@ -58,11 +56,11 @@ public class DefaultTagWorkerFactory : ITagWorkerFactory {
5856
();
5957

6058
/// <summary>The default mapping.</summary>
61-
private readonly TagProcessorMapping defaultMapping;
59+
private readonly TagProcessorMapping<DefaultTagWorkerMapping.ITagWorkerCreator> defaultMapping;
6260

6361
/// <summary>Instantiates a new default tag worker factory.</summary>
6462
public DefaultTagWorkerFactory() {
65-
this.defaultMapping = DefaultTagWorkerMapping.GetDefaultTagWorkerMapping();
63+
this.defaultMapping = new DefaultTagWorkerMapping().GetDefaultTagWorkerMapping();
6664
}
6765

6866
/// <summary>
@@ -81,43 +79,34 @@ public static ITagWorkerFactory GetInstance() {
8179
public ITagWorker GetTagWorker(IElementNode tag, ProcessorContext context) {
8280
ITagWorker tagWorker = GetCustomTagWorker(tag, context);
8381
if (tagWorker == null) {
84-
Type tagWorkerClass = GetTagWorkerClass(this.defaultMapping, tag);
85-
if (tagWorkerClass == null) {
82+
DefaultTagWorkerMapping.ITagWorkerCreator tagWorkerCreator = GetTagWorkerCreator(this.defaultMapping, tag);
83+
if (tagWorkerCreator == null) {
8684
return null;
8785
}
88-
// Use reflection to create an instance
89-
try {
90-
ConstructorInfo ctor = tagWorkerClass.GetConstructor(new Type[] { typeof(IElementNode), typeof(ProcessorContext
91-
) });
92-
ITagWorker res = (ITagWorker)ctor.Invoke(new Object[] { tag, context });
93-
return res;
94-
}
95-
catch (Exception e) {
96-
throw new TagWorkerInitializationException(TagWorkerInitializationException.REFLECTION_IN_TAG_WORKER_FACTORY_IMPLEMENTATION_FAILED
97-
, tagWorkerClass.FullName, tag.Name(), e);
98-
}
86+
return tagWorkerCreator(tag, context);
9987
}
10088
return tagWorker;
10189
}
10290

103-
internal virtual TagProcessorMapping GetDefaultMapping() {
91+
internal virtual TagProcessorMapping<DefaultTagWorkerMapping.ITagWorkerCreator> GetDefaultMapping() {
10492
return defaultMapping;
10593
}
10694

107-
/// <summary>Gets the tag worker class for a specific element node.</summary>
95+
/// <summary>Gets the tag worker creator for a specific element node.</summary>
10896
/// <param name="mapping">the mapping</param>
10997
/// <param name="tag">the element node</param>
110-
/// <returns>the tag worker class</returns>
111-
private Type GetTagWorkerClass(TagProcessorMapping mapping, IElementNode tag) {
112-
Type tagWorkerClass = null;
98+
/// <returns>the tag worker class creator</returns>
99+
private static DefaultTagWorkerMapping.ITagWorkerCreator GetTagWorkerCreator(TagProcessorMapping<DefaultTagWorkerMapping.ITagWorkerCreator
100+
> mapping, IElementNode tag) {
101+
DefaultTagWorkerMapping.ITagWorkerCreator tagWorkerCreator = null;
113102
String display = tag.GetStyles() != null ? tag.GetStyles().Get(CssConstants.DISPLAY) : null;
114103
if (display != null) {
115-
tagWorkerClass = mapping.GetMapping(tag.Name(), display);
104+
tagWorkerCreator = (DefaultTagWorkerMapping.ITagWorkerCreator)mapping.GetMapping(tag.Name(), display);
116105
}
117-
if (tagWorkerClass == null) {
118-
tagWorkerClass = mapping.GetMapping(tag.Name());
106+
if (tagWorkerCreator == null) {
107+
tagWorkerCreator = (DefaultTagWorkerMapping.ITagWorkerCreator)mapping.GetMapping(tag.Name());
119108
}
120-
return tagWorkerClass;
109+
return tagWorkerCreator;
121110
}
122111

123112
/// <summary>This is a hook method.</summary>

0 commit comments

Comments
 (0)