private String generateByCustomGenerators(
      TreeLogger logger,
      GeneratorContext context,
      String typeName,
      String superClass,
      String actualTypeName,
      List<AbstractGenerator> awaitingGeneratos)
      throws UnableToCompleteException {

    //		AbstractGenerator processedGenerator = null;

    for (AbstractGenerator generator : awaitingGeneratos) {
      if (superClass != null && !("".equals(superClass))) {
        generator.setSuperclassName(superClass);
      } else {
        generator.setSuperclassName(actualTypeName);
      }

      if (generator.isGenerate(logger, context, actualTypeName, generator.getConditions())) {
        superClass = generator.doGenerate(logger, context, actualTypeName);
        //				processedGenerator = generator;
        //				break;
      }
    }

    //		if (processedGenerator != null) {
    //			awaitingGeneratos.remove(processedGenerator);
    //			superClass = generateByCustomGenerators(logger, context, typeName, superClass,
    // actualTypeName, awaitingGeneratos);
    //		}

    return superClass;
  }
  /**
   * gets generator chain for typeName, calls every generator from chain with superclass name set to
   * previously generated class
   *
   * @param logger
   * @param context
   * @param typeName
   * @return name of generated class
   * @throws UnableToCompleteException
   */
  public String generate(TreeLogger logger, GeneratorContext context, String typeName)
      throws UnableToCompleteException {
    if (GeneratorChain.customGenerators == null || GeneratorChain.context != context) {
      fillUpGeneratorChainMap(logger, context);
      GeneratorChain.context = context;

      //			new RemoteServiceGinInjectorGenerator().generate(logger, context,
      // RemoteServicePath.class.getCanonicalName());
    }

    //		if (!BeansBindingInit.isInitialized()) {
    //			BeansBindingInit.setInitialized(true);
    //			BeansBindingInitGenerator beansBindingInitGenerator = new BeansBindingInitGenerator();
    //			beansBindingInitGenerator.generate(logger, context,
    // BeansBindingInit.class.getCanonicalName());
    //			//GWT.create(BeansBindingInit.class);
    //		}

    // can be changed by generator
    String superClass = "";
    // can be changed by replacer
    String actualTypeName = typeName;

    // Replace-with rules
    List<AbstractGenerator> replacers = GeneratorChain.getReplaceByChain(context, typeName);
    for (AbstractGenerator replacer : replacers) {
      if (replacer.isGenerate(logger, context, typeName, replacer.getConditions())) {
        actualTypeName = replacer.doGenerate(logger, context, actualTypeName);
      }
    }

    // generators from third party - can get only one (last) generator
    LinkedHashMap<Generator, AbstractGenerator> thirdPartyGens =
        GeneratorChain.getThirdPartyGenerators();
    for (Entry<Generator, AbstractGenerator> entry : thirdPartyGens.entrySet()) {
      Generator gen = (Generator) entry.getKey();
      AbstractGenerator aGen = (AbstractGenerator) entry.getValue();
      if (superClass != null && !("".equals(superClass))) {
        aGen.setSuperclassName(superClass);
      } else {
        aGen.setSuperclassName(actualTypeName);
      }
      if (aGen.isGenerate(logger, context, actualTypeName, aGen.getConditions())) {
        superClass = gen.generate(logger, context, actualTypeName);
      }
    }

    // custom generators
    List<AbstractGenerator> generators = GeneratorChain.getGeneratorChain(context, typeName);

    //		List<AbstractGenerator> awaitingGeneratos = new ArrayList<AbstractGenerator>();
    //		for (AbstractGenerator abstractGenerator : generators) {
    //			awaitingGeneratos.add(abstractGenerator);
    //		}
    //
    superClass =
        generateByCustomGenerators(
            logger, context, typeName, superClass, actualTypeName, generators);

    if (superClass.length() == 0) {
      return actualTypeName;
    }

    return superClass;
  }
  /**
   * according to .gwt.xml files generates a LinkedMap which has interfaces as keys array of
   * generators as values keys are sorted according order of <generate-with> elements in .gwt.xml
   * files
   *
   * @param context
   * @throws UnableToCompleteException
   */
  private void fillUpGeneratorChainMap(TreeLogger logger, GeneratorContext context)
      throws UnableToCompleteException {

    GeneratorChain.customGenerators = new LinkedList<AbstractGenerator>();
    GeneratorChain.replacers = new LinkedList<AbstractGenerator>();
    GeneratorChain.thirdPartyGenerators = new LinkedHashMap<Generator, AbstractGenerator>();
    ModuleDef moduleDef =
        ((CompilerContext) getPrivateField(context, "compilerContext")).getModule();
    Rules rules = moduleDef.getRules();
    Iterator<Rule> rulesIter = rules.iterator();
    while (rulesIter.hasNext()) {
      Rule rul = rulesIter.next();
      Generator gen = null;

      // =================replace with
      if (rul instanceof RuleReplaceWith) {
        String replaceClass = (String) getPrivateField(rul, "replacementTypeName");
        gen = new ReplaceByGenerator(replaceClass);
        // gen = null;

        // =================generate with
      } else if (rul instanceof RuleGenerateWith) {
        Class<? extends Generator> generatorClass =
            (Class<? extends Generator>) getPrivateField(rul, "generatorClass");
        Constructor<?> constructor;
        try {
          constructor = generatorClass.getDeclaredConstructor();
        } catch (Exception e) {
          logger.log(
              Type.ERROR, "Unable to obtain default constructor of generator " + generatorClass);
          throw new UnableToCompleteException();
        }
        constructor.setAccessible(true);
        try {
          gen = (Generator) constructor.newInstance();
        } catch (Exception e) {
          logger.log(Type.ERROR, "Unable to create instance of generator " + generatorClass);
          throw new UnableToCompleteException();
        }
      }

      if (gen != null) {
        if (gen instanceof AbstractGenerator) {
          GenPredicGroup newGroup = null;
          AbstractGenerator myGen = (AbstractGenerator) gen;
          if (GeneratorChain.customGenerators.contains(gen)
              || GeneratorChain.replacers.contains(gen)) {
            newGroup = addPredicsToExisting(rul, myGen.getConditions());
            myGen.setConditions(newGroup);
          } else {
            newGroup = getGroupConditions(rul.getRootCondition().getConditions(), null);
            myGen.setConditions(newGroup);
            if (gen instanceof ReplaceByGenerator) {
              GeneratorChain.replacers.addFirst(myGen);
            } else {
              GeneratorChain.customGenerators.addFirst(myGen);
            }
          }

        } else {
          if (GeneratorChain.thirdPartyGenerators.containsKey(gen)) {
            AbstractGenerator myGen = GeneratorChain.thirdPartyGenerators.get(gen);
            GenPredicGroup newGroup = addPredicsToExisting(rul, myGen.getConditions());
            myGen.setConditions(newGroup);
            GeneratorChain.thirdPartyGenerators.put(gen, myGen);
          } else {
            AbstractGenerator myGen =
                new AbstractGenerator() {

                  @Override
                  public String doGenerate(
                      TreeLogger logger, GeneratorContext context, String typeName)
                      throws UnableToCompleteException {
                    return null;
                  }
                };
            myGen.setConditions(getGroupConditions(rul.getRootCondition().getConditions(), null));
            GeneratorChain.thirdPartyGenerators.put(gen, myGen);
          }
        }
      }
    }
  }