Skip to content

Commit

Permalink
PrismContext-aware constructors for prism objects and containers.
Browse files Browse the repository at this point in the history
  • Loading branch information
mederly committed Jul 10, 2014
1 parent f557768 commit e42ad83
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 7 deletions.
Expand Up @@ -76,7 +76,13 @@ public PrismContainer(QName name, Class<V> compileTimeClass) {
}
this.compileTimeClass = compileTimeClass;
}


public PrismContainer(QName name, Class<V> compileTimeClass, PrismContext prismContext) {
this(name, compileTimeClass);
this.prismContext = prismContext;
}


protected PrismContainer(QName name, PrismContainerDefinition<V> definition, PrismContext prismContext) {
super(name, definition, prismContext);
}
Expand Down
Expand Up @@ -61,7 +61,11 @@ public PrismObject(QName name, Class<T> compileTimeClass) {
super(name, compileTimeClass);
}

public PrismObject(QName name, PrismObjectDefinition<T> definition, PrismContext prismContext) {
public PrismObject(QName name, Class<T> compileTimeClass, PrismContext prismContext) {
super(name, compileTimeClass, prismContext);
}

public PrismObject(QName name, PrismObjectDefinition<T> definition, PrismContext prismContext) {
super(name, definition, prismContext);
}

Expand Down
Expand Up @@ -83,7 +83,7 @@ private void removeConstructors(ClassOutline classOutline) {
while (constructors.hasNext()) {
JMethod constructor = constructors.next();
if (constructor.hasSignature(new JType[]{impl})
|| constructor.hasSignature(new JType[]{})) {
/* || constructor.hasSignature(new JType[]{}) */) { // default constructor has to be kept there!
constructors.remove();
}
}
Expand Down
Expand Up @@ -21,6 +21,7 @@
import com.evolveum.midpoint.prism.PrismContainer;
import com.evolveum.midpoint.prism.PrismContainerDefinition;
import com.evolveum.midpoint.prism.PrismContainerValue;
import com.evolveum.midpoint.prism.PrismContext;
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.PrismReference;
import com.evolveum.midpoint.prism.PrismReferenceValue;
Expand Down Expand Up @@ -258,6 +259,9 @@ private void updateObjectReferenceType(Outline outline) {

JDefinedClass definedClass = objectReferenceOutline.implClass;
definedClass._implements(CLASS_MAP.get(Referencable.class));

createDefaultConstructor(definedClass);

//add prism reference and get/set method for it
JVar reference = definedClass.field(JMod.PRIVATE, PrismReferenceValue.class, REFERENCE_VALUE_FIELD_NAME);
JMethod getReference = definedClass.method(JMod.PUBLIC, PrismReferenceValue.class, METHOD_AS_REFERENCE_VALUE);
Expand Down Expand Up @@ -377,6 +381,7 @@ private Set<JDefinedClass> updatePrismContainer(Outline outline) {
Set<Map.Entry<NClass, CClassInfo>> set = outline.getModel().beans().entrySet();
for (Map.Entry<NClass, CClassInfo> entry : set) {
ClassOutline classOutline = outline.getClazz(entry.getValue());

QName qname = getCClassInfoQName(entry.getValue());
if (qname == null || !hasAnnotation(classOutline, A_PRISM_CONTAINER)) {
continue;
Expand All @@ -392,11 +397,19 @@ private Set<JDefinedClass> updatePrismContainer(Outline outline) {

//inserting MidPointObject field into ObjectType class
JVar containerValue = definedClass.field(JMod.PRIVATE, PrismContainerValue.class, CONTAINER_VALUE_FIELD_NAME);

// default constructor
createDefaultConstructor(definedClass);

//create asPrismContainer
// createAsPrismContainer(classOutline, containerValue);
createAsPrismContainerValue(definedClass, containerValue);

//create setContainer
createSetContainerValueMethod(definedClass, containerValue);
JMethod setupContainerMethod = createSetContainerValueMethod(definedClass, containerValue);

// constructor with prismContext
createPrismContextContainerableConstructor(definedClass, setupContainerMethod);

// System.out.println("Creating toString, equals, hashCode methods.");
//create toString, equals, hashCode
Expand All @@ -416,6 +429,41 @@ private Set<JDefinedClass> updatePrismContainer(Outline outline) {
return containers;
}

private JMethod createDefaultConstructor(JDefinedClass definedClass) {
JMethod constructor = definedClass.constructor(JMod.PUBLIC);
constructor.body().invoke("super").invoke("aaa");
return constructor;
}

private JMethod createPrismContextContainerableConstructor(JDefinedClass definedClass, JMethod setupContainerMethod) {
JMethod constructor = definedClass.constructor(JMod.PUBLIC);
constructor.param(PrismContext.class, "prismContext");

JBlock body = constructor.body();
body.invoke(setupContainerMethod).arg(JExpr._new(CLASS_MAP.get(PrismContainerValue.class)).arg(constructor.params().get(0)));
return constructor;
}

/*
public UserType(PrismContext prismContext) {
setupContainer(new PrismObject(_getContainerName(), this.getClass(), prismContext));
}
*/

private JMethod createPrismContextObjectableConstructor(JDefinedClass definedClass) {
JMethod constructor = definedClass.constructor(JMod.PUBLIC);
constructor.param(PrismContext.class, "prismContext");

JBlock body = constructor.body();
body.invoke("setupContainer")
.arg(JExpr._new(CLASS_MAP.get(PrismObject.class))
.arg(JExpr.invoke("_getContainerName"))
.arg(JExpr.invoke("getClass"))
.arg(constructor.params().get(0)));
return constructor;
}


// private void createAsPrismContainer(JDefinedClass definedClass) {
// JMethod getContainer = definedClass.method(JMod.PUBLIC, CLASS_MAP.get(PrismContainer.class),
// METHOD_AS_PRISM_CONTAINER);
Expand Down Expand Up @@ -454,11 +502,27 @@ private Set<JDefinedClass> updatePrismObject(Outline outline) {
for (Map.Entry<NClass, CClassInfo> entry : set) {
ClassOutline classOutline = outline.getClazz(entry.getValue());
QName qname = getCClassInfoQName(entry.getValue());
if (qname == null || !hasAnnotation(classOutline, A_PRISM_OBJECT)) {

if (qname == null) {
continue;
}

boolean isDirectPrismObject = hasAnnotation(classOutline, A_PRISM_OBJECT);
boolean isIndirectPrismObject = hasParentAnnotation(classOutline, A_PRISM_OBJECT);

if (!isIndirectPrismObject) {
continue;
}

JDefinedClass definedClass = classOutline.implClass;

createDefaultConstructor(definedClass);
createPrismContextObjectableConstructor(definedClass);

if (!isDirectPrismObject) {
continue;
}

definedClass._implements(CLASS_MAP.get(Objectable.class));
containers.add(definedClass);

Expand All @@ -469,6 +533,7 @@ private Set<JDefinedClass> updatePrismObject(Outline outline) {
// createGetContainerMethod(classOutline, container);
//create setContainer
createSetContainerMethod(definedClass, container);

//create asPrismObject()
createAsPrismObject(definedClass);
createAsPrismContainer(classOutline, container);
Expand Down Expand Up @@ -770,7 +835,7 @@ private void createToStringMethod(JDefinedClass definedClass, String baseMethod)
body._return(invocation);
}

private void createSetContainerValueMethod(JDefinedClass definedClass, JVar container) {
private JMethod createSetContainerValueMethod(JDefinedClass definedClass, JVar container) {
JMethod setContainer = definedClass.method(JMod.PUBLIC, void.class, METHOD_SETUP_CONTAINER_VALUE);
JVar methodContainer = setContainer.param(PrismContainerValue.class, "containerValue");
//create method body
Expand Down Expand Up @@ -798,6 +863,7 @@ private void createSetContainerValueMethod(JDefinedClass definedClass, JVar cont
// then._throw(exception);

body.assign(JExpr._this().ref(container), methodContainer);
return setContainer;
}

private void createSetContainerValueMethodInObject(JDefinedClass definedClass, JVar container) {
Expand Down Expand Up @@ -830,7 +896,7 @@ private void createAsPrismContainer(ClassOutline classOutline, JVar container) {
body._return(container);
}

private void createSetContainerMethod(JDefinedClass definedClass, JVar container) {
private JMethod createSetContainerMethod(JDefinedClass definedClass, JVar container) {
JMethod setContainer = definedClass.method(JMod.PUBLIC, void.class, METHOD_SETUP_CONTAINER);
JVar methodContainer = setContainer.param(PrismObject.class, "container");
//create method body
Expand All @@ -854,6 +920,7 @@ private void createSetContainerMethod(JDefinedClass definedClass, JVar container
// then._throw(exception);

body.assign(JExpr._this().ref(container), methodContainer);
return setContainer;
}

private QName getCClassInfoQName(CClassInfo info) {
Expand Down

0 comments on commit e42ad83

Please sign in to comment.