Examples of IOCProcessingContext


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

    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(ctx, 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(ctx,
                        Refs.get(injector.getInstanceVarName()),
                        method,
                        args)
        );
View Full Code Here

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));

    /* 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().concat("_")
        .concat(type.getName()).concat("_creational");

    /* get the construction strategy and execute it to wire the bean */
    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. */

        /* add the bean to CreationalContext */
        callbackBuilder.append(
            loadVariable("context").invoke("addBean", loadVariable("context").invoke("getBeanReference", load(type),
                load(qualifyingMetadata.getQualifiers())), 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

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

  public void renderProvider(InjectableInstance injectableInstance) {
  }

  @Override
    public Statement getBeanInstance(final 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 (final Statement statement : closeStatements) {
        proxyResolverBody.append(statement);
      }

      setRendered(true);


      final String var = InjectUtil.getVarNameFromType(proxiedType, injectableInstance);

      pCtx.append(Stmt.loadVariable(var).invoke("callback", Refs.get(varName)));

      return null;
    }
View Full Code Here

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

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

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

          final IOCProcessingContext processingContext = ctx.getProcessingContext();

          final Statement objectInstantiate = Stmt.newObject(type, parameterStatements);

          processingContext.append(
              Stmt.declareFinalVariable(injector.getInstanceVarName(), type, objectInstantiate)
          );
          callback.beanConstructed(ConstructionType.CONSTRUCTOR);

          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(final 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

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

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

    final Statement initCallback = createInitializationCallback(injector.getInjectedType(), "obj", initStatements);

    final IOCProcessingContext pc = ctx.getProcessingContext();

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

    pc.append(Stmt.loadVariable("context").invoke("addInitializationCallback",
        Refs.get(injector.getInstanceVarName()), Refs.get(varName)));

    return true;
  }
View Full Code Here

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

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

    Statement destructionCallback = createDestructionCallback(injector.getInjectedType(), "obj", initMeth);

    final IOCProcessingContext pc = ctx.getProcessingContext();

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

    pc.append(Stmt.loadVariable("context").invoke("addDestructionCallback",
        Refs.get(injector.getInstanceVarName()), Refs.get(varName)));

    return true;
  }
View Full Code Here

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

  @Override
  public void renderProvider(final InjectableInstance injectableInstance) {
    if (isRendered()) return;

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

    /*
    get a parameterized version of the BeanProvider class, parameterized with the type of
    bean it produces.
    */
    final MetaClass beanProviderClassRef = parameterizedAs(AsyncBeanProvider.class, typeParametersOf(type));
    final MetaClass creationalCallbackClassRef = 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(beanProviderClassRef).extend()
        .publicOverridesMethod("getInstance", Parameter.of(creationalCallbackClassRef, "callback", true),
            Parameter.of(AsyncCreationalContext.class, "context", true));


    final boolean loadAsync = type.isAnnotationPresent(LoadAsync.class);

    final BlockBuilder<AnonymousClassStructureBuilder> targetBlock;

    if (loadAsync) {
      final BlockBuilder<AnonymousClassStructureBuilder> asyncBuilder = ObjectBuilder.newInstanceOf(RunAsyncCallback.class).extend()
          .publicOverridesMethod("onFailure", Parameter.of(Throwable.class, "throwable"))
          .append(Stmt.throw_(RuntimeException.class, "failed to run asynchronously", Refs.get("throwable")))
          .finish()
          .publicOverridesMethod("onSuccess");

      targetBlock = asyncBuilder;
    }
    else {
      targetBlock = callbackBuilder;
    }
        /* push the method block builder onto the stack, so injection tasks are rendered appropriately. */
    ctx.pushBlockBuilder(targetBlock);

    targetBlock.append(
        Stmt.create().declareFinalVariable("beanRef", BeanRef.class,
            loadVariable("context").invoke("getBeanReference", load(type),
                load(qualifyingMetadata.getQualifiers()))
        ));

    targetBlock.append(
        Stmt.create().declareFinalVariable("async", AsyncBeanContext.class,
            Stmt.create().newObject(AsyncBeanContext.class))
    );

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

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

        injectContext.getProcessingContext().append(
            loadVariable("context").invoke("addBean", Refs.get("beanRef"), Refs.get(instanceVarName))
        );

        /* add the bean to SimpleCreationalContext */

        final ObjectBuilder objectBuilder = Stmt.create().newObject(Runnable.class);
        final BlockBuilder<AnonymousClassStructureBuilder> blockBuilder = objectBuilder
            .extend().publicOverridesMethod("run");

        final BlockBuilderUpdater updater
            = new BlockBuilderUpdater(injectContext, AsyncTypeInjector.this, constructionType, targetBlock, blockBuilder);

        setAttribute("BlockBuilderUpdater", updater);
        final Statement beanRef = updater.run();

        injectContext.addBeanReference(type, beanRef);
        addStatementToEndOfInjector(Stmt.loadVariable("async").invoke("runOnFinish", blockBuilder.finish().finish()));

        /* 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.
    */

    targetBlock.appendAll(getAddToEndStatements());

    targetBlock._(Stmt.loadVariable("async").invoke("finish"));

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

    if (loadAsync) {
      final ObjectBuilder objectBuilder = targetBlock.finish().finish();

      final String frameworkOrSystemProperty
          = EnvUtil.getEnvironmentConfig().getFrameworkOrSystemProperty("errai.ioc.testing.simulated_loadasync_latency");
      if (Boolean.parseBoolean(frameworkOrSystemProperty)) {
        callbackBuilder.append(Stmt.invokeStatic(FakeGWT.class, "runAsync", objectBuilder));
      }
      else {
        callbackBuilder.append(Stmt.invokeStatic(GWT.class, "runAsync", objectBuilder));
      }
    }
    /*
      declare a final variable for the BeanProvider and initialize it with the anonymous class we just
      built.
    */
    ctx.getBootstrapBuilder().privateField(creationalCallbackVarName, beanProviderClassRef).modifiers(Modifier.Final)
        .initializesWith(callbackBuilder.finish().finish()).finish();

    if (isSingleton()) {
      registerWithBeanManager(injectContext, Stmt.load(true));
    }
View Full Code Here

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

    this.parm = null;
  }

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

    InjectableInstance injectableInstance = getInjectableInstance(ctx);

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

    ctx.allowProxyCapture();

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

      case PrivateField: {
        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) {
          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 = Stmt.invokeStatic(processingContext.getBootstrapClass(),
                  getPrivateFieldInjectorName(field), Refs.get(injector.getVarName()), val);
        }

        processingContext.append(fieldAccessStmt);

        ctx.addExposedField(field, PrivateAccessType.Write);
        break;
      }

      case Field:
        try {
          val = InjectUtil.getInjectorOrProxy(ctx, getInjectableInstance(ctx), field.getType(), qualifyingMetadata);
        }
        catch (UnproxyableClassException e) {
          return false;
        }
        processingContext.append(
                Stmt.loadVariable(injector.getVarName()).loadField(field.getName()).assignValue(val)
        );

        break;

      case PrivateMethod: {
        for (MetaParameter parm : method.getParameters()) {
          ctx.getProcessingContext().handleDiscoveryOfType(
                  new InjectableInstance(null, TaskType.Parameter, null, method, null, parm.getType(), parm, injector, ctx));
          if (!ctx.isInjectableQualified(parm.getType(), qualifyingMetadata)) {
            return false;
          }
        }

        Statement[] stmts = InjectUtil.resolveInjectionDependencies(method.getParameters(), ctx, method);
        Statement[] parms;

        parms = new Statement[stmts.length + 1];
        parms[0] = Refs.get(injector.getVarName());
        System.arraycopy(stmts, 0, parms, 1, stmts.length);

        injectableInstance.getInjectionContext().addExposedMethod(method);

        processingContext.append(
                Stmt.invokeStatic(processingContext.getBootstrapClass(), getPrivateMethodName(method),
                        parms)
        );

        break;
      }

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

          if (!ctx.isInjectableQualified(parm.getType(), qualifyingMetadata)) {
            return false;
          }
        }

        processingContext.append(
                Stmt.loadVariable(injector.getVarName()).invoke(method,
                        InjectUtil.resolveInjectionDependencies(method.getParameters(), ctx, method))
        );

        break;
View Full Code Here

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

    if (classStatementMap.containsKey(type)) {
      throw new RuntimeException("transient value already exists: " + name + "::" + type.getFullyQualifiedName());
    }

    final IOCProcessingContext pCtx = getInjectionContext().getProcessingContext();
    if (hasUnsatisfiedTransientValue(name, type)) {
      final Statement unsatisfiedTransientValue = getUnsatisfiedTransientValue(name, type);
      pCtx.append(Stmt.nestedCall(unsatisfiedTransientValue).invoke("set", valueRef));
      classStatementMap.put(type, Stmt.nestedCall(unsatisfiedTransientValue).invoke("get"));
      markSatisfied(name, type);
    }
    else {
      final String varName = InjectUtil.getUniqueVarName();
      pCtx.append(Stmt.declareFinalVariable(varName, type, valueRef));
      classStatementMap.put(type, Stmt.loadVariable(varName));
    }
  }
View Full Code Here

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

    }

    final String holderVar = InjectUtil.getUniqueVarName();
    final MetaClass holderType = MetaClassFactory.parameterizedAs(RefHolder.class, MetaClassFactory.typeParametersOf(type));

    final IOCProcessingContext pCtx = getInjectionContext().getProcessingContext();
    pCtx.append(Stmt.declareFinalVariable(holderVar, holderType, Stmt.newObject(holderType)));

    addUnsatisifiedTransientValue(name, type, Stmt.loadVariable(holderVar));

    return Stmt.loadVariable(holderVar).invoke("get");
  }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. 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.