Example usage for com.google.gwt.core.ext.linker.impl StandardCompilationResult addSelectionPermutation

List of usage examples for com.google.gwt.core.ext.linker.impl StandardCompilationResult addSelectionPermutation

Introduction

In this page you can find the example usage for com.google.gwt.core.ext.linker.impl StandardCompilationResult addSelectionPermutation.

Prototype

public void addSelectionPermutation(Map<SelectionProperty, String> values) 

Source Link

Document

Record a particular permutation of SelectionProperty values that resulted in the compilation.

Usage

From source file:gwt.ns.webworker.linker.WorkerCompilationLinker.java

License:Apache License

/**
 * Searches for all instances of a placeholder String in a
 * CompilationResult. If found, they are replaced as specified and a
 * new CompilationResult, with a newly generated strong name, is returned.
 * If no occurrences were found, the original CompilationResult is
 * returned.//from w  ww  .  ja v  a2 s .co m
 * 
 * @param logger
 * @param result CompilationResult to process
 * @param placeholder String to be replaced
 * @param replacement String to insert in place of placeholder
 * 
 * @return A CompilationResult suitable for emitting
 */
public CompilationResult replacePlaceholder(TreeLogger logger, CompilationResult result, String placeholder,
        String replacement) {

    boolean needsMod = false;

    String[] js = result.getJavaScript();
    StringBuffer[] jsbuf = new StringBuffer[js.length];
    for (int i = 0; i < jsbuf.length; i++) {
        jsbuf[i] = new StringBuffer(js[i]);
    }

    // search and replace in all fragments
    for (StringBuffer fragment : jsbuf) {
        needsMod |= replaceAll(fragment, placeholder, replacement);
    }

    // by default, returning unaltered result
    CompilationResult toReturn = result;

    // code has been altered, need to create new CompilationResult
    if (needsMod) {
        logger.log(TreeLogger.SPAM,
                "Compilation permutation " + result.getPermutationId() + " being modified.");

        // new js for compilation result
        byte[][] newcode = new byte[jsbuf.length][];
        for (int i = 0; i < jsbuf.length; i++) {
            newcode[i] = Util.getBytes(jsbuf[i].toString());
        }

        // new strong name
        String strongName = Util.computeStrongName(newcode);

        // same symbolMap copied over since none altered
        // symbolMap a little more complicated
        // can only get deserialized version, need to reserialize
        // code from com.google.gwt.dev.jjs.JavaToJavaScriptCompiler
        // TODO: turns out this can get pretty bad. Workaround?
        SymbolData[] symbolMap = result.getSymbolMap();
        byte[] serializedSymbolMap;
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            Util.writeObjectToStream(baos, (Object) symbolMap);
            serializedSymbolMap = baos.toByteArray();
        } catch (IOException e) {
            // should still never happen
            logger.log(TreeLogger.ERROR, "IOException while reserializing " + "SymbolMap.");
            throw new RuntimeException("Should never happen with in-memory stream", e);
        }

        StandardCompilationResult altered = new StandardCompilationResult(strongName, newcode,
                serializedSymbolMap, result.getStatementRanges(), result.getPermutationId());

        // need to copy permutation properties to new result
        for (Map<SelectionProperty, String> propertyMap : result.getPropertyMap()) {
            altered.addSelectionPermutation(propertyMap);
        }

        toReturn = altered;

        logger.log(TreeLogger.SPAM, "Compilation permuation " + toReturn.getPermutationId()
                + " altered to include path to worker script(s).");
    }

    return toReturn;
}