Package org.jboss.errai.ioc.rebind.ioc.bootstrapper

Examples of org.jboss.errai.ioc.rebind.ioc.bootstrapper.IOCProcessingContext


        return loadVariable(creationalCallbackVarName).invoke("getInstance", Refs.get("context"));
      }
    }

    final InjectionContext injectContext = injectableInstance.getInjectionContext();
    final IOCProcessingContext ctx = injectContext.getProcessingContext();

    /*
    get a parameterized version of the CreationalCallback class, parameterized with the type of
    bean it produces.
    */
    final MetaClass creationCallbackRef = parameterizedAs(CreationalCallback.class, typeParametersOf(type));

    /*
    begin building the creational callback, implement the "getInstance" method from the interface
    and assign its BlockBuilder to a callbackBuilder so we can work with it.
    */
    final BlockBuilder<AnonymousClassStructureBuilder> callbackBuilder
            = newInstanceOf(creationCallbackRef).extend()
            .publicOverridesMethod("getInstance", Parameter.of(CreationalContext.class, "context", true));

    /*
    render local variables Class::beanType and Annotation[]::qualifiers at the beginning of the getInstance()
    method so we can easily refer to them later on.
    */
    callbackBuilder
            ._(declareVariable(Class.class).named("beanType").initializeWith(load(type)))
            ._(declareVariable(Annotation[].class).named("qualifiers")
                    .initializeWith(load(qualifyingMetadata.getQualifiers())));


    /* push the method block builder onto the stack, so injection tasks are rendered appropriately. */
    ctx.pushBlockBuilder(callbackBuilder);

    /* get a new unique variable for the creational callback */
    creationalCallbackVarName = InjectUtil.getNewInjectorName() + "_" + type.getName() + "_creationalCallback";

    /* get the construction strategy and execute it to wire the bean */
    InjectUtil.getConstructionStrategy(this, injectContext).generateConstructor(new ConstructionStatusCallback() {
      @Override
      public void beanConstructed() {
        /* the bean has been constructed, so get a reference to the BeanRef and set it to the 'beanRef' variable. */

        callbackBuilder.append(declareVariable(BeanRef.class).named("beanRef")
                .initializeWith(loadVariable("context").invoke("getBeanReference", Refs.get("beanType"),
                        Refs.get("qualifiers"))));

        /* add the bean to CreationalContext */
        callbackBuilder.append(loadVariable("context").invoke("addBean", Refs.get("beanRef"), Refs.get(varName)));

        /* mark this injector as injected so we don't go into a loop if there is a cycle. */
        setCreated(true);
      }
    });

    /*
    return the instance of the bean from the creational callback.
    */
    callbackBuilder.append(loadVariable(varName).returnValue());

    /* pop the block builder of the stack now that we're done wiring. */
    ctx.popBlockBuilder();

    /*
    declare a final variable for the CreationalCallback and initialize it with the anonymous class we just
    built.
    */
//    ctx.globalAppend(declareVariable(creationCallbackRef).asFinal().named(creationalCallbackVarName)
//            .initializeWith(callbackBuilder.finish().finish()));

    ctx.getBootstrapBuilder().privateField(creationalCallbackVarName, creationCallbackRef).modifiers(Modifier.Final)
            .initializesWith(callbackBuilder.finish().finish()).finish();

    Statement retVal;

    if (isSingleton()) {
      /*
       if the injector is for a singleton, we create a variable to hold the singleton reference in the bootstrapper
       method and assign it with CreationalContext.getInstance().
       */
//      ctx.globalAppend(declareVariable(type).asFinal().named(varName)
//              .initializeWith(loadVariable(creationalCallbackVarName).invoke("getInstance",
//                      Refs.get("context"))));

      ctx.getBootstrapBuilder().privateField(varName, type).modifiers(Modifier.Final)
              .initializesWith(loadVariable(creationalCallbackVarName).invoke("getInstance", Refs.get("context"))).finish();

      /*
       use the variable we just assigned as the return value for this injector.
       */
 
View Full Code Here


          Statement[] parameterStatements
                  = resolveInjectionDependencies(constructor.getParameters(), ctx, constructor);

          if (injector.isSingleton() && injector.isCreated()) return;

          IOCProcessingContext processingContext = ctx.getProcessingContext();

          processingContext.append(
                  Stmt.declareVariable(type)
                          .asFinal()
                          .named(injector.getVarName())
                          .initializeWith(Stmt
                                  .newObject(type)
                                  .withParameters(parameterStatements))
          );
          callback.beanConstructed();

          handleInjectionTasks(ctx, injectionTasks);

          doPostConstruct(ctx, injector, postConstructTasks);
          doPreDestroy(ctx, injector, preDestroyTasks);
        }

      };
    }
    else {
      // field injection
      if (!hasDefaultConstructor(type))
        throw new InjectionFailure("there is no public default constructor or suitable injection constructor for type: "
                + type.getFullyQualifiedName());

      return new ConstructionStrategy() {
        @Override
        public void generateConstructor(ConstructionStatusCallback callback) {
          if (injector.isSingleton() && injector.isCreated()) return;

          IOCProcessingContext processingContext = ctx.getProcessingContext();

          processingContext.append(
                  Stmt.declareVariable(type)
                          .asFinal()
                          .named(injector.getVarName())
                          .initializeWith(Stmt.newObject(type))
View Full Code Here

    renderLifeCycleEvents(PostConstruct.class, injector, ctx, initMeth, postConstructTasks);

    final AnonymousClassStructureBuilder classStructureBuilder = initMeth.finish();

    IOCProcessingContext pc = ctx.getProcessingContext();

//    pc.globalInsertBefore(Stmt.declareVariable(initializationCallbackType).asFinal().named(varName)
//            .initializeWith(classStructureBuilder.finish()));

    pc.getBootstrapBuilder()
            .privateField(varName, initializationCallbackType)
            .initializesWith(classStructureBuilder.finish()).finish();

    pc.append(Stmt.loadVariable("context").invoke("addInitializationCallback",
            Refs.get(injector.getVarName()), Refs.get(varName)));
  }
View Full Code Here

    renderLifeCycleEvents(PreDestroy.class, injector, ctx, initMeth, preDestroyTasks);

    AnonymousClassStructureBuilder classStructureBuilder = initMeth.finish();

    IOCProcessingContext pc = ctx.getProcessingContext();

//    pc.globalInsertBefore(Stmt.declareVariable(destructionCallbackType).asFinal().named(varName)
//            .initializeWith(classStructureBuilder.finish()));

    pc.getBootstrapBuilder().privateField(varName, destructionCallbackType)
            .initializesWith(classStructureBuilder.finish()).finish();

    pc.append(Stmt.loadVariable("context").invoke("addDestructionCallback",
            Refs.get(injector.getVarName()), Refs.get(varName)));
  }
View Full Code Here

          final Statement[] parameterStatements
              = resolveInjectionDependencies(constructor.getParameters(), ctx, constructor);

          if (injector.isSingleton() && injector.isCreated()) return;

          final IOCProcessingContext processingContext = ctx.getProcessingContext();

          processingContext.append(
              Stmt.declareFinalVariable(injector.getInstanceVarName(), type, Stmt.newObject(type, parameterStatements))

          );
          callback.beanConstructed();

          handleInjectionTasks(ctx, injectionTasks);

          doPostConstruct(ctx, injector, postConstructTasks);
          doPreDestroy(ctx, injector, preDestroyTasks);
        }

      };
    }
    else {
      // field injection
      if (!hasDefaultConstructor(type))
        throw new InjectionFailure("there is no public default constructor or suitable injection constructor for type: "
            + type.getFullyQualifiedName());

      return new ConstructionStrategy() {
        @Override
        public void generateConstructor(ConstructionStatusCallback callback) {
          if (injector.isSingleton() && injector.isCreated()) return;

          final IOCProcessingContext processingContext = ctx.getProcessingContext();

          processingContext.append(
              Stmt.declareVariable(type)
                  .asFinal()
                  .named(injector.getInstanceVarName())
                  .initializeWith(Stmt.newObject(type))
View Full Code Here

    renderLifeCycleEvents(PostConstruct.class, injector, ctx, initMeth, postConstructTasks);

    final AnonymousClassStructureBuilder classStructureBuilder = initMeth.finish();

    final IOCProcessingContext pc = ctx.getProcessingContext();

//    pc.globalInsertBefore(Stmt.declareVariable(initializationCallbackType).asFinal().named(varName)
//            .initializeWith(classStructureBuilder.finish()));

    pc.getBootstrapBuilder()
        .privateField(varName, initializationCallbackType)
        .initializesWith(classStructureBuilder.finish()).finish();

    pc.append(Stmt.loadVariable("context").invoke("addInitializationCallback",
        Refs.get(injector.getInstanceVarName()), Refs.get(varName)));
  }
View Full Code Here

    renderLifeCycleEvents(PreDestroy.class, injector, ctx, initMeth, preDestroyTasks);

    final AnonymousClassStructureBuilder classStructureBuilder = initMeth.finish();

    final IOCProcessingContext pc = ctx.getProcessingContext();

//    pc.globalInsertBefore(Stmt.declareVariable(destructionCallbackType).asFinal().named(varName)
//            .initializeWith(classStructureBuilder.finish()));

    pc.getBootstrapBuilder().privateField(varName, destructionCallbackType)
        .initializesWith(classStructureBuilder.finish()).finish();

    pc.append(Stmt.loadVariable("context").invoke("addDestructionCallback",
        Refs.get(injector.getInstanceVarName()), Refs.get(varName)));
  }
View Full Code Here

            .addInnerClass(new InnerClass(proxyClass));
  }

  @Override
  public Statement getBeanInstance(InjectableInstance injectableInstance) {
    final IOCProcessingContext pCtx = injectableInstance.getInjectionContext().getProcessingContext();

    pCtx.append(Stmt.declareFinalVariable(varName, proxyClass, newObject(proxyClass)));

    final MetaClass proxyResolverRef = parameterizedAs(ProxyResolver.class, typeParametersOf(proxiedType));

    final BlockBuilder<AnonymousClassStructureBuilder> proxyResolverBody = newObject(proxyResolverRef)
            .extend().publicOverridesMethod("resolve", Parameter.of(proxiedType, "obj"));

    final Statement proxyResolver = proxyResolverBody._(loadVariable(varName)
            .invoke(ProxyMaker.PROXY_BIND_METHOD, Refs.get("obj"))).finish().finish();

    proxyResolverBody._(Stmt.loadVariable("context").invoke("addProxyReference", Refs.get(varName), Refs.get("obj")));

    pCtx.append(loadVariable("context").invoke("addUnresolvedProxy", proxyResolver,
            proxiedType, qualifyingMetadata.getQualifiers()));

    for (Statement statement : closeStatements) {
      proxyResolverBody.append(statement);
    }
View Full Code Here

    this.parm = null;
  }

  @SuppressWarnings({"unchecked"})
  public boolean doTask(final InjectionContext ctx) {
    final IOCProcessingContext processingContext = ctx.getProcessingContext();

    final InjectableInstance injectableInstance = getInjectableInstance(ctx);

    final QualifyingMetadata qualifyingMetadata = processingContext.getQualifyingMetadataFactory()
            .createFrom(injectableInstance.getQualifiers());
    final Statement val;

    ctx.allowProxyCapture();

    switch (taskType) {
      case Type:
        ctx.getQualifiedInjector(type, qualifyingMetadata);
        break;

      case PrivateField:
        ctx.addExposedField(field, PrivateAccessType.Write);

      case Field:
        try {
           val = InjectUtil.getInjectorOrProxy(ctx, getInjectableInstance(ctx), field.getType(), qualifyingMetadata);
         }
         catch (InjectionFailure e) {
           throw UnsatisfiedDependenciesException.createWithSingleFieldFailure(field, field.getDeclaringClass(),
                   field.getType(), e.getMessage());
         }
         catch (UnproxyableClassException e) {
           final String err = "your object graph may have cyclical dependencies and the cycle could not be proxied. " +
               "use of the @Dependent scope and @New qualifier may not " +
                   "produce properly initalized objects for: " + getInjector().getInjectedType().getFullyQualifiedName() + "\n" +
                   "\t Offending node: " + toString() + "\n" +
                   "\t Note          : this issue can be resolved by making "
                   + e.getUnproxyableClass().getFullyQualifiedName() + " proxyable. Introduce a default" +
               " no-arg constructor and make sure the class is non-final.";

           throw UnsatisfiedDependenciesException.createWithSingleFieldFailure(field, field.getDeclaringClass(),
                   field.getType(), err);
         }

         final Statement fieldAccessStmt;

         if (field.isStatic()) {
           throw new InjectionFailure("attempt to inject bean into a static field: "
                   + field.getDeclaringClass().getFullyQualifiedName() + "." + field.getName());
         }
         else {
           fieldAccessStmt = InjectUtil.setPublicOrPrivateFieldValue(processingContext, Refs.get(injector.getInstanceVarName()), field, val);
         }

         processingContext.append(fieldAccessStmt);

        break;

      case PrivateMethod:
        ctx.addExposedMethod(method);

      case Method:
        for (final MetaParameter parm : method.getParameters()) {
          ctx.getProcessingContext().handleDiscoveryOfType(
                  new InjectableInstance(null, TaskType.Parameter, null, method, null, parm.getType(), parm, injector, ctx));
        }

        final Statement[] args = InjectUtil.resolveInjectionDependencies(method.getParameters(), ctx, method);

        processingContext.append(
                InjectUtil.invokePublicOrPrivateMethod(processingContext,
                        Refs.get(injector.getInstanceVarName()),
                        method,
                        args)
        );
View Full Code Here

        return loadVariable(creationalCallbackVarName).invoke("getInstance", Refs.get("context"));
      }
    }

    final InjectionContext injectContext = injectableInstance.getInjectionContext();
    final IOCProcessingContext ctx = injectContext.getProcessingContext();

    /*
    get a parameterized version of the CreationalCallback class, parameterized with the type of
    bean it produces.
    */
    final MetaClass creationCallbackRef = parameterizedAs(CreationalCallback.class, typeParametersOf(type));

    /*
    begin building the creational callback, implement the "getInstance" method from the interface
    and assign its BlockBuilder to a callbackBuilder so we can work with it.
    */
    final BlockBuilder<AnonymousClassStructureBuilder> callbackBuilder
        = newInstanceOf(creationCallbackRef).extend()
        .publicOverridesMethod("getInstance", Parameter.of(CreationalContext.class, "context", true));

    /*
    render local variables Class::beanType and Annotation[]::qualifiers at the beginning of the getInstance()
    method so we can easily refer to them later on.
    */
    callbackBuilder
        ._(declareVariable(Class.class).named("beanType").initializeWith(load(type)))
        ._(declareVariable(Annotation[].class).named("qualifiers")
            .initializeWith(load(qualifyingMetadata.getQualifiers())));


    /* push the method block builder onto the stack, so injection tasks are rendered appropriately. */
    ctx.pushBlockBuilder(callbackBuilder);

    /* get a new unique variable for the creational callback */
    creationalCallbackVarName = InjectUtil.getNewInjectorName() + "_" + type.getName() + "_creationalCallback";

    /* get the construction strategy and execute it to wire the bean */
    InjectUtil.getConstructionStrategy(this, injectContext).generateConstructor(new ConstructionStatusCallback() {
      @Override
      public void beanConstructed() {
        /* the bean has been constructed, so get a reference to the BeanRef and set it to the 'beanRef' variable. */

        callbackBuilder.append(declareVariable(BeanRef.class).named("beanRef")
            .initializeWith(loadVariable("context").invoke("getBeanReference", Refs.get("beanType"),
                Refs.get("qualifiers"))));

        /* add the bean to CreationalContext */
        callbackBuilder.append(loadVariable("context").invoke("addBean", Refs.get("beanRef"), Refs.get(instanceVarName)));

        /* mark this injector as injected so we don't go into a loop if there is a cycle. */
        setCreated(true);
      }
    });

    /*
    return the instance of the bean from the creational callback.
    */
    callbackBuilder.append(loadVariable(instanceVarName).returnValue());

    /* pop the block builder of the stack now that we're done wiring. */
    ctx.popBlockBuilder();

    /*
    declare a final variable for the CreationalCallback and initialize it with the anonymous class we just
    built.
    */
    ctx.getBootstrapBuilder().privateField(creationalCallbackVarName, creationCallbackRef).modifiers(Modifier.Final)
        .initializesWith(callbackBuilder.finish().finish()).finish();

    final Statement retVal;

    if (isSingleton()) {
      /*
       if the injector is for a singleton, we create a variable to hold the singleton reference in the bootstrapper
       method and assign it with CreationalContext.getInstance().
       */
      ctx.getBootstrapBuilder().privateField(instanceVarName, type).modifiers(Modifier.Final)
          .initializesWith(loadVariable(creationalCallbackVarName).invoke("getInstance", Refs.get("context"))).finish();

      /*
       use the variable we just assigned as the return value for this injector.
       */
 
View Full Code Here

TOP

Related Classes of org.jboss.errai.ioc.rebind.ioc.bootstrapper.IOCProcessingContext

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.