From b6f2b57ed9761b89f24dca74371c535f8a244fea Mon Sep 17 00:00:00 2001 From: jamesagnew Date: Fri, 10 Apr 2015 08:02:05 -0400 Subject: [PATCH] Work on HL7 structs --- .../ca/uhn/fhir/context/ModelScanner.java | 53 +++++++++++-------- .../ca/uhn/fhir/parser/XmlParserTest.java | 4 +- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/ModelScanner.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/ModelScanner.java index 9f08d39687b..a0a783f1bf3 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/ModelScanner.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/context/ModelScanner.java @@ -27,8 +27,11 @@ import java.io.InputStream; import java.lang.annotation.Annotation; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Field; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Proxy; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Collection; @@ -225,31 +228,39 @@ class ModelScanner { private T pullAnnotation(AnnotatedElement theTarget, Class theAnnotationType) { T retVal = theTarget.getAnnotation(theAnnotationType); - if (true) { + if (myContext.getVersion().getVersion().equals(FhirVersionEnum.DSTU2_HL7ORG) == false) { return retVal; } - // Below disabled for now due to performance issues + if (retVal == null) { + String sourceClassName = theAnnotationType.getName(); + String candidateAltClassName = sourceClassName.replace("ca.uhn.fhir.model.api.annotation", "org.hl7.fhir.instance.model.annotations"); - /* - * if (retVal == null) { String sourceClassName = theAnnotationType.getName(); String candidateAltClassName = - * sourceClassName.replace("ca.uhn.fhir.model.api.annotation", "org.hl7.fhir.instance.model.annotations"); - * - * if (!sourceClassName.equals(candidateAltClassName)) { try { final Class - * altAnnotationClass = (Class) Class.forName(candidateAltClassName); final Annotation - * altAnnotation = theTarget.getAnnotation(altAnnotationClass); if (altAnnotation == null) { return null; } - * - * ourLog.debug("Forwarding annotation request for [{}] to class [{}]", sourceClassName, candidateAltClassName); - * - * InvocationHandler h = new InvocationHandler() { - * - * @Override public Object invoke(Object theProxy, Method theMethod, Object[] theArgs) throws Throwable { Method - * altMethod = altAnnotationClass.getMethod(theMethod.getName(), theMethod.getParameterTypes()); return - * altMethod.invoke(altAnnotation, theArgs); } }; retVal = (T) - * Proxy.newProxyInstance(theAnnotationType.getClassLoader(), new Class[] { theAnnotationType }, h); - * - * } catch (ClassNotFoundException e) { return null; } } } - */ + if (!sourceClassName.equals(candidateAltClassName)) { + try { + final Class altAnnotationClass = (Class) Class.forName(candidateAltClassName); + final Annotation altAnnotation = theTarget.getAnnotation(altAnnotationClass); + if (altAnnotation == null) { + return null; + } + + ourLog.debug("Forwarding annotation request for [{}] to class [{}]", sourceClassName, candidateAltClassName); + + InvocationHandler h = new InvocationHandler() { + + @Override + public Object invoke(Object theProxy, Method theMethod, Object[] theArgs) throws Throwable { + Method altMethod = altAnnotationClass.getMethod(theMethod.getName(), theMethod.getParameterTypes()); + return altMethod.invoke(altAnnotation, theArgs); + } + }; + retVal = (T) Proxy.newProxyInstance(theAnnotationType.getClassLoader(), new Class[] { theAnnotationType }, h); + + } catch (ClassNotFoundException e) { + return null; + } + } + } return retVal; } diff --git a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserTest.java b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserTest.java index 0c4c80d95be..b34a4c45c7f 100644 --- a/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserTest.java +++ b/hapi-fhir-structures-hl7org-dstu2/src/test/java/ca/uhn/fhir/parser/XmlParserTest.java @@ -94,7 +94,7 @@ public class XmlParserTest { String output = ourCtx.newXmlParser().encodeResourceToString(b); ourLog.info(output); - assertEquals("AQIDBA==", output); + assertEquals("", output); } @@ -126,7 +126,9 @@ public class XmlParserTest { String encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(b); ourLog.info(encoded); assertThat(encoded, not(containsString(""))); + assertThat(encoded, stringContainsInOrder("", "")); assertThat(encoded, containsString("")); + assertThat(encoded, stringContainsInOrder("", "")); encoded = ourCtx.newXmlParser().setPrettyPrint(true).encodeResourceToString(patient); ourLog.info(encoded);