Java Code Examples for com.sun.codemodel.JBlock#invoke()

The following examples show how to use com.sun.codemodel.JBlock#invoke() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
private void processArrayElementLoop(final Schema arraySchema, final JClass arrayClass, JExpression arrayExpr, JBlock body, String getMethodName) {
  final JForLoop forLoop = body._for();
  final JVar counter = forLoop.init(codeModel.INT, getUniqueName("counter"), JExpr.lit(0));
  forLoop.test(counter.lt(JExpr.invoke(arrayExpr, "size")));
  forLoop.update(counter.incr());
  final JBlock forBody = forLoop.body();
  forBody.invoke(JExpr.direct(ENCODER), "startItem");

  final Schema elementSchema = arraySchema.getElementType();
  if (SchemaAssistant.isComplexType(elementSchema)) {
    JVar containerVar = declareValueVar(elementSchema.getName(), elementSchema, forBody);
    forBody.assign(containerVar, JExpr.invoke(JExpr.cast(arrayClass, arrayExpr), getMethodName).arg(counter));
    processComplexType(elementSchema, containerVar, forBody);
  } else {
    processSimpleType(elementSchema, arrayExpr.invoke(getMethodName).arg(counter), forBody, false);
  }
}
 
Example 2
private void processArray(final Schema arraySchema, JExpression arrayExpr, JBlock body) {
  final JClass arrayClass = schemaAssistant.classFromSchema(arraySchema);
  body.invoke(JExpr.direct(ENCODER), "writeArrayStart");

  final JExpression emptyArrayCondition = arrayExpr.eq(JExpr._null()).cor(JExpr.invoke(arrayExpr, "isEmpty"));

  ifCodeGen(body, emptyArrayCondition, then1 -> {
    then1.invoke(JExpr.direct(ENCODER), "setItemCount").arg(JExpr.lit(0));
  }, else1 -> {
    else1.invoke(JExpr.direct(ENCODER), "setItemCount").arg(JExpr.invoke(arrayExpr, "size"));

    if (SchemaAssistant.isPrimitive(arraySchema.getElementType())) {
      JClass primitiveListInterface = schemaAssistant.classFromSchema(arraySchema, true, false, true);
      final JExpression primitiveListCondition = arrayExpr._instanceof(primitiveListInterface);
      ifCodeGen(else1, primitiveListCondition, then2 -> {
        final JVar primitiveList = declareValueVar("primitiveList", arraySchema, then2, true, false, true);
        then2.assign(primitiveList, JExpr.cast(primitiveListInterface, arrayExpr));
        processArrayElementLoop(arraySchema, arrayClass, primitiveList, then2, "getPrimitive");
      }, else2 -> {
        processArrayElementLoop(arraySchema, arrayClass, arrayExpr, else2, "get");
      });
    } else {
      processArrayElementLoop(arraySchema, arrayClass, arrayExpr, else1, "get");
    }
  });
  body.invoke(JExpr.direct(ENCODER), "writeArrayEnd");
}
 
Example 3
private void processArray(final Schema arraySchema, JExpression arrayExpr, JBlock body) {
    final JClass arrayClass = schemaAssistant.classFromSchema(arraySchema);
    body.invoke(JExpr.direct(ENCODER), "writeArrayStart");

    final JExpression emptyArrayCondition = arrayExpr.eq(JExpr._null())
            .cor(JExpr.invoke(arrayExpr, "isEmpty"));

    final JConditional emptyArrayIf = body._if(emptyArrayCondition);
    final JBlock emptyArrayBlock = emptyArrayIf._then();
    emptyArrayBlock.invoke(JExpr.direct(ENCODER), "setItemCount").arg(JExpr.lit(0));

    final JBlock nonEmptyArrayBlock = emptyArrayIf._else();
    nonEmptyArrayBlock.invoke(JExpr.direct(ENCODER), "setItemCount")
            .arg(JExpr.invoke(arrayExpr, "size"));
    final JForLoop forLoop = nonEmptyArrayBlock._for();
    final JVar counter = forLoop.init(codeModel.INT, getVariableName("counter"), JExpr.lit(0));
    forLoop.test(counter.lt(JExpr.invoke(JExpr.cast(arrayClass, arrayExpr), "size")));
    forLoop.update(counter.incr());
    final JBlock forBody = forLoop.body();
    forBody.invoke(JExpr.direct(ENCODER), "startItem");

    final Schema elementSchema = arraySchema.getElementType();
    if (SchemaAssistant.isComplexType(elementSchema)) {
        JVar containerVar = declareValueVar(elementSchema.getName(), elementSchema, forBody);
        forBody.assign(containerVar, JExpr.invoke(JExpr.cast(arrayClass, arrayExpr), "get").arg(counter));
        processComplexType(elementSchema, containerVar, forBody);
    } else {
        processSimpleType(elementSchema, arrayExpr.invoke("get").arg(counter), forBody);
    }

    body.invoke(JExpr.direct(ENCODER), "writeArrayEnd");
}
 
Example 4
Source Project: dremio-oss   File: ClassGenerator.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
ClassGenerator(CodeGenerator<T> codeGenerator, MappingSet mappingSet, SignatureHolder signature, EvaluationVisitor eval, JDefinedClass clazz, JCodeModel model) throws JClassAlreadyExistsException {
  this.codeGenerator = codeGenerator;
  this.clazz = clazz;
  this.mappings = mappingSet;
  this.sig = signature;
  this.evaluationVisitor = eval;
  this.model = Preconditions.checkNotNull(model, "Code model object cannot be null.");
  blocks = new LinkedList[sig.size()];

  for (int i =0; i < sig.size(); i++) {
    blocks[i] = Lists.newLinkedList();
  }
  rotateBlock();

  for (SignatureHolder child : signature.getChildHolders()) {
    final String innerClassName = child.getSignatureClass().getSimpleName();
    final JDefinedClass innerClazz;
    // we need to extend the template class and avoid using static inner classes.
    innerClazz = clazz._class(JMod.FINAL, innerClassName)._extends(child.getSignatureClass());

    // we also need to delegate any inner class constructors.
    for(Constructor<?> c : child.getSignatureClass().getDeclaredConstructors()){
      final Class<?>[] params = c.getParameterTypes();
      JMethod constructor = innerClazz.constructor(JMod.PUBLIC);
      JBlock block = constructor.body();
      JInvocation invoke = block.invoke("super");
      block.invoke(SignatureHolder.INIT_METHOD);

      // start at 1 since first parameter is the parent class
      for (int i = 1; i < params.length; i++) {
        constructor.param(params[i], "arg" + i);
        invoke.arg(JExpr.direct("arg" + i));
      }
    }

    innerClasses.put(innerClassName, new ClassGenerator<>(codeGenerator, mappingSet, child, eval, innerClazz, model));
  }
}
 
Example 5
private void withDefaultConstructor(String className) {
	// Create default constructor
	JMethod constructor = this.pojo.constructor(JMod.PUBLIC);
	constructor.javadoc().add("Creates a new " + className + ".");
	JBlock defaultConstructorBody = constructor.body();
	defaultConstructorBody.invoke("super");
}
 
Example 6
private void addSuperConstructorInvocation(JMethod constructor, Map<String, JVar> superParametersToAdd) {
	JBlock constructorBody = constructor.body();
	JInvocation invocation = constructorBody.invoke("super");
	for (JVar arg : superParametersToAdd.values()) {
		JVar param = constructor.param(arg.type(), arg.name());
		invocation.arg(param);
	}

}
 
Example 7
public void unsetValues(JBlock body) {
	if (constantField != null) {

	} else if (unSetter != null) {
		body.invoke(targetObject, unSetter);
	} else {

		fieldAccessor.unsetValues(body);
	}
}
 
Example 8
private JInvocation invokeListener(final JBlock block, final JFieldVar field, final JVar oldValueVar, final JVar setterArg, final String aspectName) {
	final String aspectNameCap = aspectName.substring(0, 1).toUpperCase() + aspectName.substring(1);
	final JInvocation fvcInvoke = block.invoke(JExpr._this().ref(aspectName + BoundPropertiesPlugin.SUPPORT_FIELD_SUFFIX), "fire" + aspectNameCap);
	fvcInvoke.arg(JExpr.lit(field.name()));
	fvcInvoke.arg(oldValueVar);
	fvcInvoke.arg(setterArg);
	return fvcInvoke;
}
 
Example 9
Source Project: immutable-xjc   File: PluginImpl.java    License: MIT License 5 votes vote down vote up
private void generateSuperCall(JMethod method) {
    method.annotate(Override.class);
    JBlock block = method.body();
    JInvocation superInvocation = block.invoke(JExpr._super(), method);
    for (JVar param : method.params()) {
        superInvocation.arg(param);
    }
}
 
Example 10
private void processMap(final Schema mapSchema, JExpression mapExpr, JBlock body) {
  final JClass mapClass = schemaAssistant.classFromSchema(mapSchema);
  JClass keyClass = schemaAssistant.findStringClass(mapSchema);

  body.invoke(JExpr.direct(ENCODER), "writeMapStart");

  final JExpression emptyMapCondition = mapExpr.eq(JExpr._null()).cor(JExpr.invoke(mapExpr, "isEmpty"));
  final JConditional emptyMapIf = body._if(emptyMapCondition);
  final JBlock emptyMapBlock = emptyMapIf._then();
  emptyMapBlock.invoke(JExpr.direct(ENCODER), "setItemCount").arg(JExpr.lit(0));

  final JBlock nonEmptyMapBlock = emptyMapIf._else();
  nonEmptyMapBlock.invoke(JExpr.direct(ENCODER), "setItemCount").arg(JExpr.invoke(mapExpr, "size"));

  final JForEach mapKeysLoop = nonEmptyMapBlock.forEach(keyClass, getUniqueName("key"),
      JExpr.invoke(JExpr.cast(mapClass, mapExpr), "keySet"));

  final JBlock forBody = mapKeysLoop.body();
  forBody.invoke(JExpr.direct(ENCODER), "startItem");

  JVar keyStringVar;
  if (SchemaAssistant.hasStringableKey(mapSchema)) {
    keyStringVar =
        forBody.decl(codeModel.ref(String.class), getUniqueName("keyString"), mapKeysLoop.var().invoke("toString"));
  } else {
    keyStringVar = mapKeysLoop.var();
  }

  final Schema valueSchema = mapSchema.getValueType();

  forBody.invoke(JExpr.direct(ENCODER), "writeString").arg(keyStringVar);

  JVar containerVar;
  if (SchemaAssistant.isComplexType(valueSchema)) {
    containerVar = declareValueVar(valueSchema.getName(), valueSchema, forBody);
    forBody.assign(containerVar, JExpr.invoke(JExpr.cast(mapClass, mapExpr), "get").arg(mapKeysLoop.var()));

    processComplexType(valueSchema, containerVar, forBody);
  } else {
    processSimpleType(valueSchema, mapExpr.invoke("get").arg(mapKeysLoop.var()), forBody);
  }
  body.invoke(JExpr.direct(ENCODER), "writeMapEnd");
}
 
Example 11
private void processMap(final Schema mapSchema, JExpression mapExpr, JBlock body) {

        final JClass mapClass = schemaAssistant.classFromSchema(mapSchema);
        JClass keyClass = schemaAssistant.keyClassFromMapSchema(mapSchema);

        body.invoke(JExpr.direct(ENCODER), "writeMapStart");

        final JExpression emptyMapCondition = mapExpr.eq(JExpr._null())
                .cor(JExpr.invoke(mapExpr, "isEmpty"));
        final JConditional emptyMapIf = body._if(emptyMapCondition);
        final JBlock emptyMapBlock = emptyMapIf._then();
        emptyMapBlock.invoke(JExpr.direct(ENCODER), "setItemCount").arg(JExpr.lit(0));

        final JBlock nonEmptyMapBlock = emptyMapIf._else();
        nonEmptyMapBlock.invoke(JExpr.direct(ENCODER), "setItemCount")
                .arg(JExpr.invoke(mapExpr, "size"));

        final JForEach mapKeysLoop = nonEmptyMapBlock.forEach(keyClass, getVariableName("key"),
                JExpr.invoke(JExpr.cast(mapClass, mapExpr), "keySet"));

        final JBlock forBody = mapKeysLoop.body();
        forBody.invoke(JExpr.direct(ENCODER), "startItem");

        JVar keyStringVar;
        if (SchemaAssistant.hasStringableKey(mapSchema)) {
            keyStringVar = forBody.decl(string, getVariableName("keyString"),
                    mapKeysLoop.var().invoke("toString"));
        } else {
            keyStringVar = mapKeysLoop.var();
        }

        final Schema valueSchema = mapSchema.getValueType();

        forBody.invoke(JExpr.direct(ENCODER), "writeString").arg(keyStringVar);

        JVar containerVar;
        if (SchemaAssistant.isComplexType(valueSchema)) {
            containerVar = declareValueVar(valueSchema.getName(), valueSchema, forBody);
            forBody.assign(containerVar, JExpr.invoke(JExpr.cast(mapClass, mapExpr), "get").arg(mapKeysLoop.var()));

            processComplexType(valueSchema, containerVar, forBody);
        } else {
            processSimpleType(valueSchema, mapExpr.invoke("get").arg(mapKeysLoop.var()), forBody);
        }
        body.invoke(JExpr.direct(ENCODER), "writeMapEnd");
    }