From f902d1d273ef12d6d1b1693b7f9a6ea0bf19544e Mon Sep 17 00:00:00 2001 From: jamesagnew Date: Sun, 16 Jan 2022 13:50:10 -0500 Subject: [PATCH] Avoid reflection --- .../hl7/fhir/r5/formats/FormatUtilities.java | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/FormatUtilities.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/FormatUtilities.java index 95a092f05..f5018231d 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/FormatUtilities.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/formats/FormatUtilities.java @@ -123,27 +123,26 @@ public abstract class FormatUtilities { public static ParserBase makeParser(String format) { /* - * Note: In this class we're instantiating the parsers using reflection. This is because the + * Note: Use fully qualified references to the parsers here in order to avoid adding + * a class-level import statement for them. This is because the * XmlParser and JsonParser are huuuuuge classes and classloading them is quite expensive * in cases where they won't actually ever be instantiated (such as when using the - * validator in HAPI FHIR) + * validator in HAPI FHIR). + * + * See https://github.com/hapifhir/hapi-fhir/issues/3268 */ - try { - if ("XML".equalsIgnoreCase(format)) - return (ParserBase) Class.forName("org.hl7.fhir.r5.formats.XmlParser").getConstructor().newInstance(); - if ("JSON".equalsIgnoreCase(format)) - return (ParserBase) Class.forName("org.hl7.fhir.r5.formats.JsonParser").getConstructor().newInstance(); - if ("TURTLE".equalsIgnoreCase(format)) - throw new Error("unsupported Format " + format.toString()); // return new TurtleParser(); - if ("JSONLD".equalsIgnoreCase(format)) - throw new Error("unsupported Format " + format.toString()); // return new JsonLdParser(); - if ("VBAR".equalsIgnoreCase(format)) throw new Error("unsupported Format " + format.toString()); // - if ("TEXT".equalsIgnoreCase(format)) throw new Error("unsupported Format " + format.toString()); // - throw new Error("unsupported Format " + format); - } catch (ClassNotFoundException | NoSuchMethodException | InstantiationException | IllegalAccessException | InvocationTargetException e) { - throw new IllegalStateException("Failed to create parser", e); - } - } + if ("XML".equalsIgnoreCase(format)) + return new org.hl7.fhir.r5.formats.XmlParser(); + if ("JSON".equalsIgnoreCase(format)) + return new org.hl7.fhir.r5.formats.JsonParser(); + if ("TURTLE".equalsIgnoreCase(format)) + throw new Error("unsupported Format " + format.toString()); // return new TurtleParser(); + if ("JSONLD".equalsIgnoreCase(format)) + throw new Error("unsupported Format " + format.toString()); // return new JsonLdParser(); + if ("VBAR".equalsIgnoreCase(format)) throw new Error("unsupported Format " + format.toString()); // + if ("TEXT".equalsIgnoreCase(format)) throw new Error("unsupported Format " + format.toString()); // + throw new Error("unsupported Format " + format); + } public static FhirFormat determineFormat(byte[] source) throws FHIRException { return determineFormat(source, MAX_SCAN_LENGTH);