Package rocket.generator.rebind

Examples of rocket.generator.rebind.GeneratorContext


    newFactoryMethod.setNative(false);

    final ConstructorTemplatedFile body = new ConstructorTemplatedFile();
    newFactoryMethod.setBody(body);

    final GeneratorContext context = this.getGeneratorContext();
    context.delayedBranch();
    context.debug("Constructor parameter values.");

    final List<Value> arguments = bean.getConstructorValues();
    final Iterator<Value> argumentsIterator = arguments.iterator();
    while (argumentsIterator.hasNext()) {
      final Value value = argumentsIterator.next();
      body.addArgument(value);

      if (context.isDebugEnabled()) {
        context.debug(value.toString());
      }
    }

    context.unbranch();

    context.branch();
    context.debug("Matching constructors.");

    final List<Constructor> matchingConstructors = new ArrayList<Constructor>();
    final TypeConstructorsVisitor visitor = new TypeConstructorsVisitor() {

      protected boolean visit(final Constructor constructor) {
        boolean match = false;

        // skip non public constructors
        if (constructor.getVisibility() == Visibility.PUBLIC) {
          final List<ConstructorParameter> constructorParameters = constructor.getParameters();

          // skip the given constructor with different numbers of
          // parameters.
          if (constructorParameters.size() == arguments.size()) {

            match = true;
            final Iterator<Value> valuesIterator = arguments.iterator();
            final Iterator<ConstructorParameter> parametersIterator = constructorParameters.iterator();

            while (valuesIterator.hasNext()) {
              final ConstructorParameter parameter = parametersIterator.next();
              final Value value0 = valuesIterator.next();
              if (false == value0.isCompatibleWith(parameter.getType())) {
                match = false;
              }
            }
          }
        }

        if (match) {
          matchingConstructors.add(constructor);
          context.debug("" + constructor);
        }
        return false;
      }
    };

    final Type beanType = bean.getType();
    visitor.start(beanType);

    if (matchingConstructors.size() == 0) {
      this.throwUnableToFindConstructor(bean);
    }
    if (matchingConstructors.size() > 1) {
      this.throwTooManyConstructors(bean, matchingConstructors);
    }
    context.unbranch();

    final Constructor constructor = (Constructor) matchingConstructors.get(0);
    body.setBean(constructor);

    final Iterator<ConstructorParameter> constructorParameters = constructor.getParameters().iterator();
    final Iterator<Value> valuesIterator = arguments.iterator();
    while (constructorParameters.hasNext()) {
      final ConstructorParameter constructorParameter = constructorParameters.next();
      final Value value = valuesIterator.next();
      value.setPropertyType(constructorParameter.getType());

      this.prepareValue(value);
    }

    if (context.isDebugEnabled()) {
      context.debug(constructor.toString());
    }
  }
View Full Code Here


   *            The beans
   */
  protected void overrideAllFactoryBeanSatisfyInits(final Set<Bean> beans) {
    Checker.notNull("parameter:beans", beans);

    final GeneratorContext context = this.getGeneratorContext();
    context.delayedBranch();
    context.info("Overriding satisfyInit methods for beans with custom initMethods.");

    final Iterator<Bean> iterator = beans.iterator();
    while (iterator.hasNext()) {
      final Bean bean = iterator.next();
      final String initMethodName = bean.getInitMethod();

      if (false == Tester.isNullOrEmpty(initMethodName)) {
        context.branch();
        context.debug(bean.getId());

        this.overrideFactoryBeanSatisfyInit(bean);

        context.unbranch();
      }
    }

    context.unbranch();
  }
View Full Code Here

    final Method initMethod = beanType.findMethod(initMethodName, Collections.<Type>emptyList());
    if (null == initMethod || initMethod.isStatic() || initMethod.getVisibility() != Visibility.PUBLIC) {
      throwInitMethodNotFound(bean, initMethodName);
    }

    final GeneratorContext context = this.getGeneratorContext();
    context.debug("Overriding satisfyInit to call " + initMethod);

    final NewType beanFactory = bean.getFactoryBean();
    final Method beanFactoryInitMethod = beanFactory.getMostDerivedMethod(Constants.SATISFY_INIT, this
        .getParameterListWithOnlyObject());
    final NewMethod newMethod = beanFactoryInitMethod.copy(beanFactory);
View Full Code Here

   *            The beans
   */
  protected void overrideAllFactoryBeanSatisfyProperties(final Set<Bean> beans) {
    Checker.notNull("parameter:beans", beans);

    final GeneratorContext context = this.getGeneratorContext();
    context.branch();
    context.info("Overriding satisfyProperties method for bean(s) with 1 or more properties.");

    final Iterator<Bean> iterator = beans.iterator();
    while (iterator.hasNext()) {

      context.delayedBranch();

      final Bean bean = iterator.next();
      final String id = bean.getId();
      context.debug(id);

      while (true) {
        if (bean instanceof Rpc) {
          this.overrideFactoryBeanSatisfyPropertiesWithSettingServiceEntryPoint((Rpc) bean);
          break;
        }
        this.overrideFactoryBeanSatisfyProperties(bean);
        break;
      }

      context.unbranch();
    }

    context.unbranch();
  }
View Full Code Here

    instanceParameter.setName(Constants.SATISFY_PROPERTIES_INSTANCE_PARAMETER);

    final Type serviceDefTarget = this.getServiceDefTarget();
    body.setBean(serviceDefTarget);

    final GeneratorContext context = this.getGeneratorContext();

    final Type string = context.getString();
    final Method setter = serviceDefTarget.getMostDerivedMethod(Constants.SET_SERVICE_ENTRY_POINT, Collections.nCopies(1, string));
    final StringValue addressValue = new StringValue();
    addressValue.setGeneratorContext(context);
    addressValue.setType(string);

    final String serviceEntryPoint = rpc.getServiceEntryPoint();
    addressValue.setValue(serviceEntryPoint);

    body.addProperty(setter, addressValue);

    context.debug("serviceEntryPoint: \"" + serviceEntryPoint + "\"");
  }
View Full Code Here

   *            The bean being processed.
   */
  protected void overrideFactoryBeanSatisfyProperties(final Bean bean) {
    Checker.notNull("parameter:bean", bean);

    final GeneratorContext context = this.getGeneratorContext();
    final Type voidType = this.getGeneratorContext().getVoid();
    final Type beanType = bean.getType();
    context.debug(beanType.getName());

    final SetPropertiesTemplatedFile body = new SetPropertiesTemplatedFile();
    body.setBean(beanType);

    final NewType factoryBean = bean.getFactoryBean();
    final Method method = factoryBean.getMostDerivedMethod(Constants.SATISFY_PROPERTIES, this.getParameterListWithOnlyObject());

    final NewMethod newMethod = method.copy(factoryBean);
    newMethod.setAbstract(false);
    newMethod.setFinal(true);
    newMethod.setNative(false);

    final NewMethodParameter instanceParameter = (NewMethodParameter) newMethod.getParameters().get(0);
    instanceParameter.setFinal(true);
    instanceParameter.setName(Constants.SATISFY_PROPERTIES_INSTANCE_PARAMETER);

    newMethod.setBody(body);

    // loop thru all properties
    final Set<Property> properties = bean.getProperties();
    final Iterator<Property> propertyIterator = properties.iterator();
    while (propertyIterator.hasNext()) {
      final Property property = propertyIterator.next();
      final String propertyName = property.getName();
      final String setterName = GeneratorHelper.buildSetterName(propertyName);

      final Value value = property.getValue();
      this.prepareValue(value);

      final List<Method> matchingSetters = new ArrayList<Method>();

      final VirtualMethodVisitor visitor = new VirtualMethodVisitor() {
        protected boolean visit(final Method method) {

          while (true) {
            // names dont match
            if (false == method.getName().equals(setterName)) {
              break;
            }
            // return type must be void
            if (false == method.getReturnType().equals(voidType)) {
              break;
            }
            // parameter types must be compatible...
            final List<MethodParameter> parameters = method.getParameters();
            if (parameters.size() != 1) {
              break;
            }

            final MethodParameter parameter = (MethodParameter) parameters.get(0);
            final Type propertyType = parameter.getType();
            if (false == value.isCompatibleWith(propertyType)) {
              break;
            }

            value.setPropertyType(propertyType);
            matchingSetters.add(method);
            break;
          }

          return false;
        }

        protected boolean skipJavaLangObjectMethods() {
          return true;
        }
      };

      visitor.start(beanType);
      if (matchingSetters.isEmpty()) {
        this.throwUnableToFindSetter(bean, property);
      }
      if (matchingSetters.size() != 1) {
        this.throwTooManySettersFound(bean, property);
      }

      final Method setter = (Method) matchingSetters.get(0);
      body.addProperty(setter, value);

      context.debug(propertyName + "=" + value);
    }
  }
View Full Code Here

  /**
   * Creates an inner class belonging to the BeanFactory being generated with
   * getters for each image property.
   */
  protected void createImageFactoryIfNecessary() {
    final GeneratorContext context = this.getGeneratorContext();
    context.delayedBranch();
    context.info("Creating an ImageFactory which will supply all images.");

    final Iterator<ImageValue> i = this.getImageValues().iterator();
    NewType imageFactory = null;
    while (i.hasNext()) {
      if (null == imageFactory) {
        imageFactory = this.createImageFactory();

        context.branch();
        context.debug("Creating abstract getters on image factory");
      }

      final ImageValue imageValue = i.next();

      // create abstract getter
      this.addImageFactoryAbstractImageGetter(imageValue, imageFactory);
    }

    if (null != imageFactory) {
      context.unbranch();
    }
    context.unbranch();
  }
View Full Code Here

   * generated.
   *
   * @return
   */
  protected NewType createImageFactory() {
    final GeneratorContext context = this.getGeneratorContext();
    context.branch();
    context.info("Creating Image Factory");

    final NewType beanFactory = this.getBeanFactory();

    final NewNestedInterfaceType imageFactory = beanFactory.newNestedInterfaceType();
    imageFactory.setNestedName(Constants.IMAGE_FACTORY_NESTED_CLASS_NAME);
    imageFactory.setStatic(true);
    imageFactory.setSuperType(this.getImageFactory());
    imageFactory.setVisibility(Visibility.PACKAGE_PRIVATE);

    this.addImageFactoryField(imageFactory);
    this.addImageFactoryGetter(imageFactory);
    this.addImageFactorySetter(imageFactory);

    context.unbranch();

    return imageFactory;
  }
View Full Code Here

   *            The beans
   */
  protected void overrideAllSingletonFactoryBeanToInvokeCustomDestroy(final Set<Bean> beans) {
    Checker.notNull("parameter:beans", beans);

    final GeneratorContext context = this.getGeneratorContext();
    context.branch();
    context.info("Visiting singleton beans.");

    final Iterator<Bean> iterator = beans.iterator();
    int singletonCount = 0;
    int customDestroyMethodCount = 0;

    while (iterator.hasNext()) {
      final Bean bean = (Bean) iterator.next();
      if (false == bean.isSingleton()) {
        continue;
      }

      final String destroyMethodName = bean.getDestroyMethod();

      if (false == Tester.isNullOrEmpty(destroyMethodName)) {
        context.branch();
        context.debug(bean.getId());

        this.overrideSingletonFactoryBeanDestroy(bean);

        context.unbranch();
      }
    }
    context.debug(singletonCount == 0 ? "No singletons" : customDestroyMethodCount + " singletons have custom destroy methods.");

    context.unbranch();
  }
View Full Code Here

    final Method destroyMethod = beanType.findMethod(destroyMethodName, Collections.<Type>emptyList());
    if (null == destroyMethod || destroyMethod.isStatic() || destroyMethod.getVisibility() != Visibility.PUBLIC) {
      throwCustomMethodNotFound(bean, destroyMethodName);
    }

    final GeneratorContext context = this.getGeneratorContext();
    context.debug("Overriding destroy to call " + destroyMethod + " for bean: " + bean);

    final NewType beanFactory = bean.getFactoryBean();
    final Method beanFactoryInitMethod = beanFactory.getMostDerivedMethod(Constants.DESTROY, this.getParameterListWithOnlyObject());
    final NewMethod newMethod = beanFactoryInitMethod.copy(beanFactory);
    newMethod.setAbstract(false);
View Full Code Here

TOP

Related Classes of rocket.generator.rebind.GeneratorContext

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.