Package org.jboss.errai.codegen

Examples of org.jboss.errai.codegen.Statement


      MethodBodyCallback methodBodyCallback = new JpaMetamodelMethodBodyCallback(classBuilder, et);

      // now, snapshot all the EntityType's attributes, adding them as we go
      List<Statement> attributes = new ArrayList<Statement>();
      for (SingularAttribute<?, ?> attrib : et.getSingularAttributes()) {
        Statement attribSnapshot = SnapshotMaker.makeSnapshotAsSubclass(
                attrib, SingularAttribute.class, ErraiSingularAttribute.class, methodBodyCallback,
                EntityType.class, ManagedType.class, Type.class);
        pmm.append(Stmt.loadVariable(entityTypeVarName).invoke("addAttribute", attribSnapshot));
      }
      for (PluralAttribute<?, ?, ?> attrib : et.getPluralAttributes()) {
        Statement attribSnapshot = SnapshotMaker.makeSnapshotAsSubclass(
                attrib, PluralAttribute.class, ErraiPluralAttribute.class, methodBodyCallback,
                EntityType.class, ManagedType.class, Type.class);
        pmm.append(Stmt.loadVariable(entityTypeVarName).invoke("addAttribute", attribSnapshot));
      }
View Full Code Here


                    throw new MarshallingException("no marshaller for type: " + toMap);
                  }
                }
                else {
                  if (context.canMarshal(type.getFullyQualifiedName())) {
                    Statement s = maybeAddAssumedTypes(tryBuilder, "c" + constructorParameters.size(),
                        mapping, fieldDemarshall(mapping, EJObject.class));

                    constructorParameters.add(s);
                  }
                  else {
                    throw new MarshallingException("no marshaller for type: " + type);
                  }
                }
            }

            if (instantiationMapping instanceof ConstructorMapping) {
              final ConstructorMapping mapping = (ConstructorMapping) instantiationMapping;
              final MetaConstructor constructor = mapping.getMember();

              if (constructor.isPublic()) {
                tryBuilder.append(Stmt.declareVariable(toMap).named("entity")
                    .initializeWith(Stmt.newObject(toMap, constructorParameters.toArray(new Object[constructorParameters.size()]))));
              }
              else {
                PrivateAccessUtil.addPrivateAccessStubs(gwtTarget ? "jsni" : "reflection", context.getClassStructureBuilder(), constructor);
                tryBuilder.append(Stmt.declareVariable(toMap).named("entity")
                    .initializeWith(
                        Stmt.invokeStatic(
                            context.getClassStructureBuilder().getClassDefinition(),
                            PrivateAccessUtil.getPrivateMethodName(constructor),
                            constructorParameters.toArray(new Object[constructorParameters.size()]))));
              }
            }
            else if (instantiationMapping instanceof FactoryMapping) {
              tryBuilder.append(Stmt.declareVariable(toMap).named("entity")
                      .initializeWith(
                          Stmt.invokeStatic(toMap, ((FactoryMapping) instantiationMapping).getMember().getName(),
                              constructorParameters.toArray(new Object[constructorParameters.size()]))));
            }
          }
          else {
            // use default constructor

            tryBuilder._(
                Stmt.declareVariable(toMap).named("entity").initializeWith(
                Stmt.nestedCall(Stmt.newObject(toMap))));
          }

          tryBuilder._(loadVariable("a1").invoke("recordObject",
                  loadVariable("objId"), loadVariable("entity")));
        }

        /**
         *
         * FIELD BINDINGS
         *
         */
        for (final MemberMapping memberMapping : mappingDefinition.getMemberMappings()) {
          if (!memberMapping.canWrite()) continue;

          if (memberMapping.getTargetType().isConcrete() && !context.isRendered(memberMapping.getTargetType())) {
             context.getMarshallerGeneratorFactory().addMarshaller(memberMapping.getTargetType());
          }

          final Statement bindingStatement;
          final Statement val;
          if (memberMapping.getType().isArray()) {
            val =
                context.getArrayMarshallerCallback()
                    .demarshall(memberMapping.getType(),
                        extractJSONObjectProperty(memberMapping.getKey(), EJObject.class));
View Full Code Here

  public Statement fieldDemarshall(final Mapping mapping, final Class<?> fromType) {
    return fieldDemarshall(mapping, MetaClassFactory.get(fromType));
  }

  public Statement fieldDemarshall(final Mapping mapping, final MetaClass fromType) {
    final Statement statement = unwrapJSON(extractJSONObjectProperty(mapping.getKey(), fromType), mapping.getType(), mapping.getTargetType());
    return Cast.to(mapping.getTargetType(), statement);
  }
View Full Code Here

      if (!(compType.isAbstract() || compType.isInterface() || compType.isEnum()) && !context.canMarshal(compType.getFullyQualifiedName())) {
        throw new NoAvailableMarshallerException(compType.getFullyQualifiedName());
      }

      Statement valueStatement = valueAccessorFor(mapping.getReadingMember());
      if (targetType.isArray()) {
        valueStatement = context.getArrayMarshallerCallback().marshal(targetType, valueStatement);
      }
      sb.append("\"" + mapping.getKey() + "\" : ");
View Full Code Here

  public Statement demarshallEnum(final Statement objStatement,
                                  final Statement valStatement,
                                  final MetaClass toType) {

    final Statement trueStatement = Stmt.invokeStatic(Enum.class, "valueOf", toType,
            Stmt.nestedCall(objStatement)
                    .invoke("get", SerializationParts.ENUM_STRING_VALUE).invoke("isString").invoke("stringValue"));

    final Statement falseStatement =
      (valStatement != null) ?
        new TernaryStatement(Bool.isNotNull(Stmt.nestedCall(valStatement).invoke("isString")),
            Stmt.invokeStatic(Enum.class, "valueOf", toType,
                Stmt.nestedCall(valStatement).invoke("isString").invoke("stringValue")),
            Stmt.load(null))
View Full Code Here

      if (prevPageWithThisName != null) {
        throw new GenerationException(
                "Page names must be unique, but " + prevPageWithThisName + " and " + pageClass +
                " are both named [" + pageName + "]");
      }
      Statement pageImplStmt = generateNewInstanceOfPageImpl(pageClass, pageName);
      if (annotation.startingPage() == true) {
        defaultPages.add(pageClass);

        // need to assign the page impl to a variable and add it to the map twice
        ctor.append(Stmt.declareFinalVariable("defaultPage", PageNode.class, pageImplStmt));
View Full Code Here

          .initializeWith(
              Stmt.invokeStatic(TemplateUtil.class, "getRootTemplateElement", Stmt
                  .loadVariable(templateVarName).invoke("getContents").invoke("getText"),
                  getTemplateFragmentName(declaringClass))));

      Statement rootTemplateElement = Stmt.loadVariable(rootTemplateElementVarName);

      /*
       * Get a reference to the actual Composite component being created
       */
      Statement component = Refs.get(ctx.getInjector().getInstanceVarName());

      /*
       * Get all of the data-field Elements from the Template
       */
      String dataFieldElementsVarName = InjectUtil.getUniqueVarName();
      builder.append(Stmt.declareVariable(dataFieldElementsVarName, new TypeLiteral<Map<String, Element>>() {
      },
          Stmt.invokeStatic(TemplateUtil.class, "getDataFieldElements", rootTemplateElement)));

      /*
       * Attach Widget field children Elements to the Template DOM
       */

      String fieldsMapVarName = InjectUtil.getUniqueVarName();

      /*
       * The Map<String, Widget> to store actual component field references.
       */
      builder.append(Stmt.declareVariable(fieldsMapVarName, new TypeLiteral<Map<String, Widget>>() {
      },
          Stmt.newObject(new TypeLiteral<LinkedHashMap<String, Widget>>() {
          })));
      Statement fieldsMap = Stmt.loadVariable(fieldsMapVarName);

      generateComponentCompositions(ctx, builder, component, rootTemplateElement,
          Stmt.loadVariable(dataFieldElementsVarName), fieldsMap);

      generateEventHandlerMethodClasses(ctx, builder, component, dataFieldElementsVarName, fieldsMap);
View Full Code Here

          }

          processedEventHandlers.add(name);

          // Where will the event come from? It could be a @DataField member, or it could be the templated widget itself!
          Statement eventSource;
          if ("this".equals(name)) {
            eventSource = Stmt.loadVariable("obj");
          }
          else {
            eventSource = Stmt.nestedCall(fieldsMap).invoke("get", name);
          }

          if (dataFieldType.isAssignableTo(Element.class)) {
            builder.append(Stmt.invokeStatic(TemplateUtil.class, "setupWrappedElementEventHandler", component,
                eventSource, listenerInstance,
                Stmt.invokeStatic(eventType, "getType")));
          }
          else if (dataFieldType.isAssignableTo(hasHandlerType)) {
            Statement widget = Cast.to(hasHandlerType, eventSource);
            builder.append(Stmt.nestedCall(widget).invoke("add" + handlerType.getName(),
                Cast.to(handlerType, listenerInstance)));
          }
          else if (dataFieldType.isAssignableTo(Widget.class)) {
            Statement widget = Cast.to(Widget.class, eventSource);
            builder.append(Stmt.nestedCall(widget).invoke("addDomHandler",
                listenerInstance, Stmt.invokeStatic(eventType, "getType")));
          }
          else {
            throw new GenerationException("@DataField [" + name + "] of type [" + dataFieldType.getName()
View Full Code Here

                                             Statement dataFieldElements, Statement fieldsMap) {

    /*
     * In case of constructor injection, search for the data binder parameter annotated with @AutoBound
     */
    Statement dataBinderRef = null;
    MetaClass dataModelType = null;
    BeanMetric beanMetric = InjectUtil.analyzeBean(ctx.getInjectionContext(), ctx.getEnclosingType());
    MetaConstructor mc = beanMetric.getInjectorConstructor();
    if (mc != null) {
      for (MetaParameter mp : mc.getParameters()) {
        if (mp.getType().getErased().isAssignableTo(MetaClassFactory.get(DataBinder.class))
            && mp.isAnnotationPresent(AutoBound.class)) {
          dataModelType = (MetaClass) mp.getType().getParameterizedType().getTypeParameters()[0];
          if (dataBinderRef != null) {
            throw new GenerationException("Multiple @AutoBound data binders found in constructor of " +
                mc.getDeclaringClass());
          }
          dataBinderRef = ctx.getInjectionContext().getInlineBeanReference(mp);
        }
      }
    }

    /*
     * Search for data binder fields annotated with @AutoBound
     */
    MetaField dataBinderField = null;
    for (MetaField field : ctx.getInjector().getInjectedType().getFields()) {
      if (field.getType().getErased().equals(MetaClassFactory.get(DataBinder.class))
          && field.isAnnotationPresent(AutoBound.class)) {
        if (dataBinderField != null) {
          throw new GenerationException("Multiple @AutoBound data binder fields found in class "
              + ctx.getInjector().getInjectedType());
        }
        if (dataBinderRef != null) {
          throw new GenerationException(
              "Multiple @AutoBound data binders found (check constructors and fields) in class "
                  + ctx.getInjector().getInjectedType());
        }
        dataModelType = (MetaClass) field.getType().getParameterizedType().getTypeParameters()[0];
        dataBinderField = field;
        dataBinderRef = Stmt.invokeStatic(ctx.getInjectionContext().getProcessingContext().getBootstrapClass(),
            PrivateAccessUtil.getPrivateFieldInjectorName(dataBinderField),
            Variable.get(ctx.getInjector().getInstanceVarName()));
      }
    }

    /*
     * Create a reference to the composite's data binder
     */
    if (dataBinderRef != null) {
      builder.append(Stmt.declareVariable("binder", DataBinder.class, dataBinderRef));
    }

    /*
     * Merge each field's Widget Element into the DOM in place of the
     * corresponding data-field
     */
    Map<String, Statement> dataFields = DecoratorDataField.aggregateDataFieldMap(ctx, ctx.getType());
    for (Entry<String, Statement> field : dataFields.entrySet()) {
      builder.append(Stmt.invokeStatic(TemplateUtil.class, "compositeComponentReplace", ctx.getType()
          .getFullyQualifiedName(), getTemplateFileName(ctx.getType()), Cast.to(Widget.class, field.getValue()),
          dataFieldElements, field.getKey()));
    }

    /*
     * Add each field to the Collection of children of the new Composite
     * Template
     */
    for (Entry<String, Statement> field : dataFields.entrySet()) {
      builder.append(Stmt.nestedCall(fieldsMap).invoke("put", field.getKey(), field.getValue()));
    }

    /*
     * Bind each bound data field if data binder is found and has been initialized.
     */
    Map<String, BoundDataField> boundDataFields = DecoratorDataField.aggregateDataFieldBoundMap(ctx, ctx.getType());
    BlockBuilder<ElseBlockBuilder> binderBlock = If.isNotNull(Variable.get("binder"));
    for (Entry<String, BoundDataField> boundDataField : boundDataFields.entrySet()) {
      Bound bound = boundDataField.getValue().getBound();
      if (dataBinderRef != null) {
        String property = bound.property().equals("") ? boundDataField.getKey() : bound.property();
        // Check if bound property exists in data model type
        if (!DataBindingValidator.isValidPropertyChain(dataModelType, property)) {
          throw new GenerationException("Invalid binding of DataField " + boundDataField.getValue().getName()
              + " in class " + ctx.getInjector().getInjectedType() + "! Property " + property
              + " not resolvable from class " + dataModelType +
              ". Hint: All types in a property chain must be @Bindable!");
        }

        Statement converter =
            bound.converter().equals(Bound.NO_CONVERTER.class) ? null : Stmt.newObject(bound.converter());
        binderBlock.append(Stmt.loadVariable("binder")
            .invoke("bind", boundDataField.getValue().getWidgetStatement(), property, converter));
      }
      else {
View Full Code Here

  }

  @Override
  public List<? extends Statement> generateDecorator(InjectableInstance<DataField> ctx) {
    ctx.ensureMemberExposed();
    Statement instance = ctx.getValueStatement();
    String name = getTemplateDataFieldName(ctx.getAnnotation(), ctx.getMemberName());
    if (ctx.getType().isAssignableTo(Element.class)) {
      if (ctx.isAnnotationPresent(Inject.class)) {
        throw new GenerationException("@DataField [" + name + "] in class ["
                + ctx.getEnclosingType().getFullyQualifiedName() + "] is of type ["
View Full Code Here

TOP

Related Classes of org.jboss.errai.codegen.Statement

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.