Example usage for com.google.gwt.core.ext RebindMode USE_ALL_CACHED

List of usage examples for com.google.gwt.core.ext RebindMode USE_ALL_CACHED

Introduction

In this page you can find the example usage for com.google.gwt.core.ext RebindMode USE_ALL_CACHED.

Prototype

RebindMode USE_ALL_CACHED

To view the source code for com.google.gwt.core.ext RebindMode USE_ALL_CACHED.

Click Source Link

Document

Indicates nothing new was generated, only cached output previously generated should be used.

Usage

From source file:com.guit.rebind.appcontroller.AppControllerGenerator.java

License:Apache License

@Override
protected RebindMode rebindMode() {
    CachedGeneratorResult last = context.getCachedGeneratorResult();
    if (last != null) {
        return RebindMode.USE_ALL_CACHED;
    }/*from ww w . java  2 s  .  com*/
    return RebindMode.USE_ALL_NEW;
}

From source file:com.guit.rebind.common.AbstractGenerator.java

License:Apache License

@Override
public RebindResult generateIncrementally(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    saveVariables(logger, context, typeName);

    baseClass = getType(typeName);/* w ww .ja v a 2  s.  c  om*/
    generatedPackage = baseClass.getPackage().getName();
    implName = baseClass.getSimpleSourceName() + implementationPostfix;
    implName = processImplName(implName);
    JClassType enclosingType = baseClass.getEnclosingType();
    if (enclosingType != null) {
        implName = enclosingType.getSimpleSourceName() + implName;
    }

    RebindMode rebindMode = context.isGeneratorResultCachingEnabled() ? rebindMode()
            : RebindMode.USE_ALL_NEW_WITH_NO_CACHING;
    if (rebindMode.equals(RebindMode.USE_ALL_CACHED) || rebindMode.equals(RebindMode.USE_EXISTING)) {
        return new RebindResult(rebindMode, generatedPackage + "." + implName);
    }

    ClassSourceFileComposerFactory composer = createComposer();
    processComposer(composer);
    createdClassName = composer.getCreatedClassName();
    PrintWriter printWriter = createPrintWriter();
    if (printWriter != null) {
        SourceWriter writer = composer.createSourceWriter(context, printWriter);
        generate(writer);

        writer.commit(logger);
    }

    RebindResult result = new RebindResult(rebindMode, composer.getCreatedClassName());
    saveClientData(result);
    return result;
}

From source file:com.gwtplatform.dispatch.rest.rebind.DispatchRestIncrementalGenerator.java

License:Apache License

@Override
public RebindResult generateIncrementally(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    if (lastGeneration != null) {
        // Prevents unnecessary calls to the generator for every permutations.
        // TODO: Try to really optimize for incremental generators (if possible at all)
        return new RebindResult(RebindMode.USE_ALL_CACHED, lastGeneration.getQualifiedName());
    }//  w  w w .  j  av  a2s.  c om

    this.logger = logger;
    this.context = context;
    this.typeName = typeName;

    Injector injector = createInjector();
    DispatchRestGenerator generator = injector.getInstance(DispatchRestGenerator.class);
    lastGeneration = generate(generator);

    return new RebindResult(RebindMode.USE_ALL_NEW, lastGeneration.getQualifiedName());
}

From source file:net.sf.mmm.util.gwt.base.rebind.AbstractIncrementalGenerator.java

License:Apache License

/**
 * {@inheritDoc}//w w  w. ja v  a 2  s.co m
 */
@Override
public RebindResult generateIncrementally(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {

    CachedGeneratorResult cachedGeneratorResult = context.getCachedGeneratorResult();
    if (cachedGeneratorResult != null) {
        boolean obsolete = isCachedResultObsolete(cachedGeneratorResult, typeName);
        if (!obsolete) {
            return new RebindResult(RebindMode.USE_ALL_CACHED, cachedGeneratorResult.getResultTypeName());
        }
    }
    TypeOracle typeOracle = context.getTypeOracle();
    JClassType inputType = typeOracle.findType(typeName);
    String resultType = generate(inputType, logger, context);
    return new RebindResult(RebindMode.USE_ALL_NEW, resultType);
}

From source file:sk.turn.gwtmvp.gen.ViewGenerator.java

License:Apache License

@Override
public RebindResult generateIncrementally(TreeLogger logger, GeneratorContext context, String typeName)
        throws UnableToCompleteException {
    try {/*w  w  w.j  a v a 2 s .  co  m*/
        TypeOracle typeOracle = context.getTypeOracle();
        JClassType viewType = typeOracle.getType(typeName);
        boolean asyncView = (viewType.getAnnotation(AsyncView.class) != null);
        ViewHtml viewHtml = viewType.getAnnotation(ViewHtml.class);
        String packageName = viewType.getPackage().getName();
        String generatedClassName = viewType.getName().replace('.', '_') + "Impl";
        PrintWriter w = context.tryCreate(logger, packageName, generatedClassName);
        if (w == null) {
            return new RebindResult(RebindMode.USE_ALL_CACHED, packageName + "." + generatedClassName);
        }
        JParameterizedType superType = null;
        String handlerType = null;
        for (JClassType intfc : viewType.getImplementedInterfaces()) {
            if (intfc.getQualifiedSourceName().equals(View.class.getName())
                    || intfc.getQualifiedSourceName().equals(HandlerView.class.getName())) {
                superType = intfc.isParameterized();
                if (intfc.getQualifiedSourceName().equals(HandlerView.class.getName())) {
                    handlerType = superType.getTypeArgs()[1].getQualifiedSourceName();
                }
                break;
            }
        }
        if (superType == null) {
            throw new Exception(typeName + " does not inherit from " + View.class.getName());
        }
        // If the view HTML is not inline, make sure the view HTML file exists
        Resource htmlResource = null;
        if (viewHtml == null) {
            htmlResource = context.getResourcesOracle().getResource(
                    packageName.replace('.', '/') + "/" + viewType.getSimpleSourceName() + ".html");
            if (htmlResource == null) {
                throw new Exception("Cannot find " + viewType.getSimpleSourceName() + ".html");
            }
        }
        String rootElementType = superType.getTypeArgs()[0].getQualifiedSourceName();
        // Map all annotated methods
        Map<String, JMethod> fieldsMap = new LinkedHashMap<>();
        // elemId -> { com.google.gwt.event.dom.client.???Handler -> JMethod, ... }, ...
        Map<String, Map<String, JMethod>> handlersMap = new LinkedHashMap<>();
        for (JMethod method : viewType.getMethods()) {
            // Check element mapping
            HtmlElement elemAnn = method.getAnnotation(HtmlElement.class);
            if (elemAnn != null) {
                if (method.getParameters().length > 0) {
                    throw new Exception("Method " + typeName + "." + method.getName()
                            + "() must have zero parameters (has " + method.getParameters().length + ").");
                }
                String id = elemAnn.value();
                if (id.equals("")) {
                    id = method.getName();
                    if (id.startsWith("get") && id.length() > 3) {
                        id = id.substring(3, 4).toLowerCase() + id.substring(4);
                    }
                }
                fieldsMap.put(id, method);
            }
            // Check handler mapping
            HtmlHandler handlerAnn = method.getAnnotation(HtmlHandler.class);
            if (handlerAnn != null) {
                JParameter param = (method.getParameters().length == 1 ? method.getParameters()[0] : null);
                JClassType paramType = (param != null ? param.getType().isInterface() : null);
                if (paramType == null
                        || !paramType.isAssignableTo(typeOracle.getType(EventHandler.class.getName()))) {
                    throw new Exception("Method " + typeName + "." + method.getName()
                            + "() must have exactly one parameter of an EventHandler subinterface (has "
                            + (paramType == null ? "none" : paramType.getQualifiedSourceName()) + ").");
                }
                for (String val : handlerAnn.value()) {
                    Map<String, JMethod> methods = handlersMap.get(val);
                    if (methods == null) {
                        methods = new LinkedHashMap<>();
                        handlersMap.put(val, methods);
                    }
                    if (methods.containsKey(paramType.getQualifiedSourceName())) {
                        throw new Exception(
                                "Element \"" + val + "\" already has a " + paramType.getName() + " defined.");
                    }
                    methods.put(paramType.getQualifiedSourceName(), method);
                }
            }
        }
        // Check whether dictionary class is defined
        String html = (viewHtml != null ? viewHtml.value()
                : Util.readStreamAsString(htmlResource.openContents()));
        Matcher dictMatcher = Pattern.compile("^<[^<]+data-mvp-dict=\"(.+)\"").matcher(html);
        String dictClassName = null;
        JClassType dictClass = null;
        if (dictMatcher.find()) {
            dictClassName = dictMatcher.group(1);
            if ((dictClass = typeOracle.getType(dictClassName)) == null) {
                throw new Exception("Localization classs " + dictClassName + " does not exist");
            }
        }
        // Check whether data-gwtid is still being used
        if (html.indexOf("data-gwtid=\"") != -1) {
            logger.log(TreeLogger.Type.WARN,
                    "The use of \"data-gwtid\" attribute is deprecated and its support will be removed in future versions, please use \"data-mvp-id\" instead.");
        }
        w.println("package " + packageName + ";");
        w.println();
        w.println("import com.google.gwt.core.client.GWT;");
        w.println("import com.google.gwt.dom.client.Element;");
        w.println("import com.google.gwt.dom.client.Document;");
        w.println("import com.google.gwt.dom.client.NodeList;");
        w.println("import com.google.gwt.event.dom.client.DomEvent;");
        w.println("import com.google.gwt.event.shared.HandlerManager;");
        w.println("import com.google.gwt.resources.client.ClientBundle;");
        w.println("import com.google.gwt.resources.client.ExternalTextResource;");
        w.println("import com.google.gwt.resources.client.ResourceCallback;");
        w.println("import com.google.gwt.resources.client.ResourceException;");
        w.println("import com.google.gwt.resources.client.TextResource;");
        w.println("import com.google.gwt.user.client.Event;");
        w.println("import com.google.gwt.user.client.EventListener;");
        w.println("import java.util.HashMap;");
        w.println("import java.util.Map;");
        w.println("import java.util.logging.Logger;");
        w.println();
        w.println("public class " + generatedClassName + " implements " + viewType.getQualifiedSourceName()
                + " {");
        if (htmlResource != null) {
            w.println("  interface Resources extends ClientBundle {");
            w.println("    Resources INSTANCE = GWT.create(Resources.class);");
            w.println("    @Source(\"" + viewType.getSimpleSourceName() + ".html\") "
                    + (asyncView ? "External" : "") + "TextResource htmlContent();");
            w.println("  }");
            w.println();
        }
        w.println("  private static final Logger LOG = Logger.getLogger(\"" + packageName + "."
                + generatedClassName + "\");");
        if (asyncView) {
            w.println("  private static String sHtml = null;");
        }
        w.println();
        w.println("  private " + rootElementType + " rootElement = null;");
        w.println("  private final Map<String, Element> elementsMap = new HashMap<>();");
        if (handlerType != null) {
            w.println("  private " + handlerType + " handler;");
        }
        w.println();
        w.println("  @Override");
        w.println(
                "  public void loadView(final ViewLoadedHandler<" + rootElementType + "> viewLoadedHandler) {");
        w.println("    if (rootElement != null) {");
        w.println("      viewLoadedHandler.onViewLoaded(rootElement);");
        w.println("    }");
        if (asyncView) {
            w.println("    if (sHtml != null) {");
            w.println("      loadView(new String(sHtml), viewLoadedHandler);");
            w.println("    } else {");
            w.println("      try {");
            w.println(
                    "        Resources.INSTANCE.htmlContent().getText(new ResourceCallback<TextResource>() {");
            w.println("          public void onSuccess(TextResource r) {");
            w.println("            sHtml = r.getText();");
            w.println("            loadView(new String(sHtml), viewLoadedHandler);");
            w.println("          }");
            w.println("          public void onError(ResourceException e) {");
            w.println("            LOG.severe(\"Failed to load " + viewType.getSimpleSourceName()
                    + ".html: \" + e);");
            w.println("            viewLoadedHandler.onViewLoaded(null);");
            w.println("          }");
            w.println("        });");
            w.println("      } catch (ResourceException e) {");
            w.println("        LOG.severe(\"Failed to load " + viewType.getSimpleSourceName()
                    + ".html: \" + e);");
            w.println("        viewLoadedHandler.onViewLoaded(null);");
            w.println("      }");
            w.println("    }");
        } else if (viewHtml != null) {
            w.println("    loadView(\"" + escapeJavaString(viewHtml.value()) + "\", viewLoadedHandler);");
        } else {
            w.println("    loadView(Resources.INSTANCE.htmlContent().getText(), viewLoadedHandler);");
        }
        w.println("  }");
        w.println();
        w.println("  private void loadView(String html, ViewLoadedHandler<" + rootElementType
                + "> viewLoadedHandler) {");
        // Replace any dictionary entries
        new SafeHtmlBuilder();
        if (dictClassName != null) {
            w.println("    " + dictClassName + " dict = GWT.create(" + dictClassName + ".class);");
            w.println("    Object dictEntry;");
            dictMatcher = Pattern.compile("\\{mvpDict\\.([^}]+)\\}").matcher(html);
            Set<String> replacedEntries = new HashSet<>();
            while (dictMatcher.find()) {
                String dictEntry = dictMatcher.group(1);
                if (!replacedEntries.contains(dictEntry)) {
                    try {
                        dictClass.getMethod(dictEntry, new JType[] {});
                    } catch (NotFoundException e) {
                        throw new Exception("Localization method " + dictClassName + "." + dictEntry
                                + "() does not exist.");
                    }
                    w.println("    dictEntry = dict." + dictEntry + "();");
                    w.println("    html = html.replace(\"" + dictMatcher.group(0)
                            + "\", dictEntry instanceof com.google.gwt.safehtml.shared.SafeHtml ? ((com.google.gwt.safehtml.shared.SafeHtml)dictEntry).asString() : dictEntry.toString());");
                    replacedEntries.add(dictEntry);
                }
            }
        }
        w.println("    Element tempElem = Document.get().create"
                + (rootElementType.equals("com.google.gwt.dom.client.TableRowElement") ? "TBody"
                        : rootElementType.equals("com.google.gwt.dom.client.TableCellElement") ? "TR" : "Div")
                + "Element();");
        w.println("    tempElem.setInnerHTML(html);");
        w.println("    rootElement = (" + rootElementType + ") tempElem.getFirstChild();");
        if (dictClassName != null) {
            w.println("    rootElement.removeAttribute(\"data-mvp-dict\");");
        }
        w.println("    addElementToMap(rootElement, elementsMap);");
        w.println("    NodeList<Element> elements = rootElement.getElementsByTagName(\"*\");");
        w.println("    for (int i = 0; i < elements.getLength(); i++) {");
        w.println("      addElementToMap(elements.getItem(i), elementsMap);");
        w.println("    }");
        for (Map.Entry<String, JMethod> entry : fieldsMap.entrySet()) {
            w.println("    if (elementsMap.get(\"" + entry.getKey() + "\") == null) {");
            w.println("      LOG.severe(\"Could not find element with data-mvp-id=\\\"" + entry.getKey()
                    + "\\\" in " + viewType.getSimpleSourceName() + ".html.\");");
            w.println("    }");
        }
        for (Map.Entry<String, Map<String, JMethod>> entry : handlersMap.entrySet()) {
            w.println("    if (elementsMap.get(\"" + entry.getKey() + "\") == null) {");
            w.println("      LOG.severe(\"Could not find element with data-mvp-id=\\\"" + entry.getKey()
                    + "\\\" in " + viewType.getSimpleSourceName() + ".html.\");");
            w.println("    }");
        }
        if (handlerType != null) {
            // Map @HtmlHandlers of enclosing class
            JClassType enclosingType = viewType.getEnclosingType();
            JMethod[] methods = (enclosingType != null ? enclosingType.getMethods() : new JMethod[] {});
            for (JMethod method : methods) {
                HtmlHandler handlerAnnotation = method.getAnnotation(HtmlHandler.class);
                if (handlerAnnotation == null) {
                    continue;
                }
                String eventType = method.getParameters()[0].getType().getQualifiedSourceName();
                String eventHandlerType = eventType.substring(0, eventType.length() - 5) + "Handler";
                String handlerMethod = "on" + eventType.substring(0, eventType.length() - 5)
                        .substring(eventType.lastIndexOf('.') + 1);
                for (String elemId : handlerAnnotation.value()) {
                    w.println("    sk.turn.gwtmvp.client.EventManager.setEventHandler(getElement(\"" + elemId
                            + "\"), " + eventType + ".getType(), new " + eventHandlerType + "() {");
                    w.println("      @Override");
                    w.println("      public void " + handlerMethod + "(" + eventType + " event) {");
                    w.println("        if (handler != null) {");
                    w.println("          try { handler." + method.getName() + "(event); }");
                    w.println("          catch (Exception e) { LOG.severe(\"Invoke of "
                            + enclosingType.getName() + "." + method.getName() + " failed: \" + e); }");
                    w.println("        } else {");
                    w.println("          LOG.severe(\"Ignoring " + enclosingType.getName() + "."
                            + method.getName() + " - no HandlerView.handler set\");");
                    w.println("        }");
                    w.println("      }");
                    w.println("    });");
                }
            }
        }
        w.println("    viewLoadedHandler.onViewLoaded(rootElement);");
        w.println("  }");
        w.println();
        w.println("  @Override");
        w.println("  public " + rootElementType + " getRootElement() {");
        w.println("    return rootElement;");
        w.println("  }");
        w.println();
        w.println("  @Override");
        w.println("  public <E2 extends Element> E2 getElement(String mvpId) {");
        w.println("    return (E2) elementsMap.get(mvpId);");
        w.println("  }");
        if (handlerType != null) {
            w.println();
            w.println("  @Override");
            w.println("  public void setHandler(" + handlerType + " handler) {");
            w.println("    this.handler = handler;");
            w.println("  }");
        }
        for (Map.Entry<String, JMethod> entry : fieldsMap.entrySet()) {
            w.println();
            w.println("  @Override");
            w.println("  public " + entry.getValue().getReturnType().getQualifiedSourceName() + " "
                    + entry.getValue().getName() + "() {");
            w.println("    return elementsMap.get(\"" + entry.getKey() + "\").<"
                    + entry.getValue().getReturnType().getQualifiedSourceName() + ">cast();");
            w.println("  }");
        }
        for (JMethod method : viewType.getMethods()) {
            HtmlHandler handlerAnn = method.getAnnotation(HtmlHandler.class);
            if (handlerAnn == null) {
                continue;
            }
            String paramType = method.getParameters()[0].getType().getQualifiedSourceName();
            String eventType = paramType.substring(0, paramType.length() - 7) + "Event.getType()";
            w.println();
            w.println("  @Override");
            w.println("  public void " + method.getName() + "(" + paramType + " handler) {");
            for (String id : handlerAnn.value()) {
                w.println("    sk.turn.gwtmvp.client.EventManager.setEventHandler(getElement(\"" + id + "\"), "
                        + eventType + ", handler);");
            }
            w.println("  }");
        }
        w.println();
        w.println("  private void addElementToMap(Element element, Map<String, Element> elementsMap) {");
        w.println(
                "    String attrName = (element.hasAttribute(\"data-mvp-id\") ? \"data-mvp-id\" : element.hasAttribute(\"data-gwtid\") ? \"data-gwtid\" : null);");
        w.println("    if (attrName == null) {");
        w.println("      return;");
        w.println("    }");
        w.println("    String mvpId = element.getAttribute(attrName);");
        w.println("    if (!mvpId.equals(\"\")) {");
        w.println("      element.removeAttribute(attrName);");
        w.println("      elementsMap.put(mvpId, element);");
        w.println("    }");
        w.println("  }");
        w.println("}");
        context.commit(logger, w);
        return new RebindResult(RebindMode.USE_ALL_NEW, packageName + "." + generatedClassName);
    } catch (Exception e) {
        logger.log(TreeLogger.Type.ERROR,
                "Failed generating wrapper for class " + typeName + ": " + e.getMessage());
        throw new UnableToCompleteException();
    }
}