New issue

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

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

Already on GitHub? Sign in to your account

Failed to instantiate type:ca.uhn.fhir.model.primitive.BoundCodeDt #120

Closed
baopingle opened this Issue Mar 6, 2015 · 4 comments

Comments

Projects
None yet
2 participants
@baopingle

baopingle commented Mar 6, 2015

When I use the sub-class to extend the Resource, i.e, extend DiagnosticReport, and introduce an extension with type is BoundCodeDt for this sub-class. This BoundCodeDt bounded with my own enum. The failed to instantiate type issue happened when invoking the fhirContext.newXmlParser().parseResource

@jamesagnew

This comment has been minimized.

Owner

jamesagnew commented Mar 6, 2015

Hello,

Oh, that's interesting, thanks for reporting! You're probably the first person to try adding your own bound code in a custom type, actually.. but it should work.

Any chance you'd be able to attach a sample to the bug? I.e. if you take your custom class and cut ourt everything except the one custom field, as well as the enum type you're using, those would be useful to help create a unit test for fixing this.

Cheers,
James

@baopingle

This comment has been minimized.

baopingle commented Mar 11, 2015

ca.uhn.fhir.context.ConfigurationException: Failed to instantiate type:ca.uhn.fhir.model.primitive.BoundCodeDt
at ca.uhn.fhir.context.BaseRuntimeElementDefinition.newInstance(BaseRuntimeElementDefinition.java:110)
at ca.uhn.fhir.context.BaseRuntimeElementDefinition.newInstance(BaseRuntimeElementDefinition.java:99)
at ca.uhn.fhir.parser.ParserState$DeclaredExtensionState.enteringNewElement(ParserState.java:878)
at ca.uhn.fhir.parser.ParserState.enteringNewElement(ParserState.java:93)
at ca.uhn.fhir.parser.XmlParser.doXmlLoop(XmlParser.java:386)
at ca.uhn.fhir.parser.XmlParser.parseResource(XmlParser.java:756)
at ca.uhn.fhir.parser.XmlParser.parseResource(XmlParser.java:311)
at ca.uhn.fhir.parser.BaseParser.parseResource(BaseParser.java:164)
at com.agfa.ris.server.fhir.resource.ReportTest.testReport(ReportTest.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.InstantiationException: ca.uhn.fhir.model.primitive.BoundCodeDt
at java.lang.Class.newInstance(Unknown Source)
at ca.uhn.fhir.context.BaseRuntimeElementDefinition.newInstance(BaseRuntimeElementDefinition.java:105)
... 32 more

import java.util.HashMap;
import java.util.Map;

import ca.uhn.fhir.model.api.IValueSetEnumBinder;

public enum WorkflowActionEnum {

/**
 * Code Value: <b>sign-off</b>
 *
 * Sign off workflow action.
 */
SIGNOFF("sign-off","#workflow-action"),

/**
 * Code Value: <b>sign-off-later</b>
 *
 * Sign off later workflow action.
 */
SIGNOFFLATER("sign-off-later","#workflow-action"),

/**
 * Code Value: <b>correctionist</b>
 *
 * Correctionist workflow action.
 */
CORRECTIONIST("correctionist","#workflow-action");

/**
 * Identifier for this Value Set:
 * local/workflow-action
 */
public static final String VALUESET_IDENTIFIER = "#workflow-action";

/**
 * Name for this Value Set:
 * WorkflowAction
 */
public static final String VALUESET_NAME = "WorkflowAction";

private static Map<String, WorkflowActionEnum> CODE_TO_ENUM = new HashMap<String, WorkflowActionEnum>();
private static Map<String, Map<String, WorkflowActionEnum>> SYSTEM_TO_CODE_TO_ENUM = new HashMap<String, Map<String, WorkflowActionEnum>>();

private final String myCode;
private final String mySystem;

static {
    for (WorkflowActionEnum next : WorkflowActionEnum.values()) {
        CODE_TO_ENUM.put(next.getCode(), next);

        if (!SYSTEM_TO_CODE_TO_ENUM.containsKey(next.getSystem())) {
            SYSTEM_TO_CODE_TO_ENUM.put(next.getSystem(), new HashMap<String, WorkflowActionEnum>());
        }
        SYSTEM_TO_CODE_TO_ENUM.get(next.getSystem()).put(next.getCode(), next);         
    }
}

/**
 * Returns the code associated with this enumerated value
 */
public String getCode() {
    return myCode;
}

/**
 * Returns the code system associated with this enumerated value
 */
public String getSystem() {
    return mySystem;
}

/**
 * Returns the enumerated value associated with this code
 */
public WorkflowActionEnum forCode(String theCode) {
    WorkflowActionEnum retVal = CODE_TO_ENUM.get(theCode);
    return retVal;
}

/**
 * Converts codes to their respective enumerated values
 */
public static final IValueSetEnumBinder<WorkflowActionEnum> VALUESET_BINDER = new IValueSetEnumBinder<WorkflowActionEnum>() {
    @Override
    public String toCodeString(WorkflowActionEnum theEnum) {
        return theEnum.getCode();
    }

    @Override
    public String toSystemString(WorkflowActionEnum theEnum) {
        return theEnum.getSystem();
    }

    @Override
    public WorkflowActionEnum fromCodeString(String theCodeString) {
        return CODE_TO_ENUM.get(theCodeString);
    }

    @Override
    public WorkflowActionEnum fromCodeString(String theCodeString, String theSystemString) {
        Map<String, WorkflowActionEnum> map = SYSTEM_TO_CODE_TO_ENUM.get(theSystemString);
        if (map == null) {
            return null;
        }
        return map.get(theCodeString);
    }

};

/** 
 * Constructor
 */
WorkflowActionEnum(String theCode, String theSystem){
    myCode = theCode;
    mySystem = theSystem;
}

}

@ResourceDef(name="DiagnosticReport")
public class MyDiagnosticReport extends DiagnosticReport {

public static final String SP_IMAGING_STUDY = "ImagingStudy";

/**
 * Each extension is defined in a field. Any valid HAPI Data Type
 * can be used for the field type. Note that the [name=""] attribute
 * in the @Child annotation needs to match the name for the bean accessor
 * and mutator methods.
 */
@Child(name="taskId", order=16)  
@Extension(url="http://agfa.com/extensions#taskId", definedLocally=true, isModifier=false)
@Description(shortDefinition="The task id")
private IntegerDt taskId;

@Child(name="workflowAction", type=CodeDt.class, order=17)  
@Extension(url="http://agfa.com/extensions#workflowAction", definedLocally=true, isModifier=false)
@Description(shortDefinition="sign-off |sign-off-later |correctionist +",
             formalDefinition="The work-flow action")
private BoundCodeDt<WorkflowActionEnum> workflowAction;

@Child(name="workflowComments", order=18)  
@Extension(url="http://agfa.com/extensions#workflowComments", definedLocally=true, isModifier=false)
@Description(shortDefinition="The work-flow comments")
private StringDt workflowComments;


/**
 * It is important to override the isEmpty() method, adding a check for any
 * newly added fields.
 */
@Override
public boolean isEmpty() {
    return super.isEmpty() && ElementUtil.isEmpty(taskId, workflowAction,workflowComments);
}

/********
 * Accessors and mutators follow
 *
 * IMPORTANT:
 * Each extension is required to have an getter/accessor and a stter/mutator.
 * You are highly recommended to create getters which create instances if they
 * do not already exist, since this is how the rest of the HAPI FHIR API works.
 ********/

/** Getter for mandatory */

public IntegerDt getTaskId() {
    if(taskId == null){
        taskId = new IntegerDt();
    }
    return taskId;
}

public MyDiagnosticReport setTaskId(int taskId) {
    this.taskId = new IntegerDt(taskId);
    return this;
}

public MyDiagnosticReport setTaskId(IntegerDt taskId){
    this.taskId = taskId;
    return this;
}

public BoundCodeDt<WorkflowActionEnum> getWorkflowAction() {  
    if (workflowAction== null) {
        workflowAction = new BoundCodeDt<WorkflowActionEnum>(WorkflowActionEnum.VALUESET_BINDER);
    }

    return workflowAction;
}

public MyDiagnosticReport setWorkflowAction(BoundCodeDt<WorkflowActionEnum> workflowAction) {
    this.workflowAction = workflowAction;

    return this;
}


public MyDiagnosticReport setWorkflowAction(WorkflowActionEnum workflowAction){
    getWorkflowAction().setValueAsEnum(workflowAction);
    return this;
}

public StringDt getWorkflowComments() {
    if(workflowComments == null){
        workflowComments = new StringDt();
    }
    return workflowComments;
}

public MyDiagnosticReport setWorkflowComments(StringDt workflowComments) {
    this.workflowComments = workflowComments;
    return this;
}

public MyDiagnosticReport setWorkflowComments(String workflowComments){
    this.workflowComments = new StringDt(workflowComments);
    return this;
}

}

@jamesagnew

This comment has been minimized.

Owner

jamesagnew commented Mar 11, 2015

Perfect, that was exactly what was needed. Thanks for providing.
I'll check in a fix shortly, and deploy a new snapshot build to maven later today.

This fix will be in the 0.9 release which is due this week.

jamesagnew added a commit that referenced this issue Mar 11, 2015

@jamesagnew

This comment has been minimized.

Owner

jamesagnew commented Mar 11, 2015

A new snapshot has been deployed with this fix. Closing.

@jamesagnew jamesagnew closed this Mar 11, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment