All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.gwt.core.ext.linker.impl.StandardCompilationResult Maven / Gradle / Ivy

Go to download

Vaadin is a web application framework for Rich Internet Applications (RIA). Vaadin enables easy development and maintenance of fast and secure rich web applications with a stunning look and feel and a wide browser support. It features a server-side architecture with the majority of the logic running on the server. Ajax technology is used at the browser-side to ensure a rich and interactive user experience.

There is a newer version: 8.25.2
Show newest version
/*
 * Copyright 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.gwt.core.ext.linker.impl;

import com.google.gwt.core.ext.linker.CompilationResult;
import com.google.gwt.core.ext.linker.SelectionProperty;
import com.google.gwt.core.ext.linker.SoftPermutation;
import com.google.gwt.core.ext.linker.StatementRanges;
import com.google.gwt.core.ext.linker.SymbolData;
import com.google.gwt.dev.jjs.PermutationResult;
import com.google.gwt.dev.util.DiskCache;
import com.google.gwt.dev.util.Util;
import com.google.gwt.dev.util.collect.Lists;
import com.google.gwt.thirdparty.guava.common.collect.Sets;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;

/**
 * The standard implementation of {@link CompilationResult}.
 */
public class StandardCompilationResult extends CompilationResult {

  private static final class MapComparator implements
      Comparator>, Serializable {
    @Override
    public int compare(SortedMap arg0,
        SortedMap arg1) {
      int diff = arg0.size() - arg1.size();
      if (diff != 0) {
        return diff;
      }

      Iterator i0 = arg0.values().iterator();
      Iterator i1 = arg1.values().iterator();

      StringBuilder sb0 = new StringBuilder();
      StringBuilder sb1 = new StringBuilder();

      while (i0.hasNext()) {
        assert i1.hasNext();
        sb0.append(i0.next());
        sb1.append(i1.next());
      }
      assert !i1.hasNext();

      return sb0.toString().compareTo(sb1.toString());
    }
  }

  /**
   * Smaller maps come before larger maps, then we compare the concatenation of
   * every value.
   */
  public static final Comparator> MAP_COMPARATOR = new MapComparator();

  private static final DiskCache diskCache = DiskCache.INSTANCE;

  private final SortedSet> propertyValues = new TreeSet>(
      MAP_COMPARATOR);

  private List softPermutations = Lists.create();

  private final StatementRanges[] applicationStatementRanges;

  private final String strongName;

  private final long symbolToken;

  private final int permutationId;

  private Set libraryPermutationResults;

  private PermutationResult applicationPermutationResult;

  public StandardCompilationResult(PermutationResult permutationResult) {
    this(permutationResult, Sets.newHashSet());
  }

  public StandardCompilationResult(PermutationResult applicationPermutationResult,
      Set libraryPermutationResults) {
    super(StandardLinkerContext.class);
    this.applicationPermutationResult = applicationPermutationResult;
    this.libraryPermutationResults = libraryPermutationResults;
    this.strongName = computeStrongName();
    this.applicationStatementRanges = applicationPermutationResult.getStatementRanges();
    this.permutationId = applicationPermutationResult.getPermutation().getId();
    this.symbolToken =
        diskCache.writeByteArray(applicationPermutationResult.getSerializedSymbolMap());
  }

  private String computeStrongName() {
    // If there are no library permutations
    if (libraryPermutationResults.isEmpty()) {
      // then just reuse the precalculated root application permutation strong name.
      return applicationPermutationResult.getJsStrongName();
    }

    // Otherwise stick all the different strong names together
    StringBuilder strongNames = new StringBuilder();
    strongNames.append(applicationPermutationResult.getJsStrongName());
    for (PermutationResult libraryPermutationResult : libraryPermutationResults) {
      strongNames.append(libraryPermutationResult.getJsStrongName());
    }
    // And hash that.
    return Util.computeStrongName(strongNames.toString().getBytes());
  }

  /**
   * Record a particular permutation of SelectionProperty values that resulted
   * in the compilation.
   */
  public void addSelectionPermutation(Map values) {
    SortedMap map = new TreeMap(
        StandardLinkerContext.SELECTION_PROPERTY_COMPARATOR);
    map.putAll(values);
    propertyValues.add(Collections.unmodifiableSortedMap(map));
  }

  public void addSoftPermutation(Map propertyMap) {
    softPermutations = Lists.add(softPermutations, new StandardSoftPermutation(
        softPermutations.size(), propertyMap));
  }

  @Override
  public String[] getJavaScript() {
    byte[][] applicationJs = applicationPermutationResult.getJs();
    int applicationFragmentCount = applicationJs.length;

    // If there are no libraries
    if (libraryPermutationResults.isEmpty()) {
      // then return just the application JavaScript.
      String[] jsStrings = new String[applicationFragmentCount];
      for (int fragmentIndex = 0; fragmentIndex < applicationFragmentCount; fragmentIndex++) {
        jsStrings[fragmentIndex] = Util.toString(applicationJs[fragmentIndex]);
      }
      return jsStrings;
    }

    // Otherwise if there are multiple libraries.
    assert applicationFragmentCount == 1 : "Libraries can only have one fragment.";

    StringBuilder jsBuffer = new StringBuilder();

    // Concatenate the libraries and application JavaScript.
    for (PermutationResult libraryPermutationResult : libraryPermutationResults) {
      byte[][] libraryJs = libraryPermutationResult.getJs();
      int libraryFragmentCount = libraryJs.length;

      assert libraryFragmentCount == 1 : "Libraries can only have one fragment.";

      jsBuffer.append(Util.toString(libraryJs[0]));
    }

    jsBuffer.append(Util.toString(applicationJs[0]));

    return new String[] {jsBuffer.toString()};
  }

  @Override
  public int getPermutationId() {
    return permutationId;
  }

  @Override
  public SortedSet> getPropertyMap() {
    return Collections.unmodifiableSortedSet(propertyValues);
  }

  @Override
  public SoftPermutation[] getSoftPermutations() {
    return softPermutations.toArray(new SoftPermutation[softPermutations.size()]);
  }

  @Override
  public StatementRanges[] getStatementRanges() {
    byte[][] applicationJs = applicationPermutationResult.getJs();
    int applicationFragmentCount = applicationJs.length;

    // If there are no libraries
    if (libraryPermutationResults.isEmpty()) {
      // then return just the application statement ranges.
      return applicationStatementRanges;
    }

    // Otherwise if there are multiple libraries.
    assert applicationFragmentCount == 1 : "Libraries can only have one fragment.";

    // Concatenate the libraries and application JavaScript.
    List statementRangesList = new ArrayList();
    for (PermutationResult libraryPermutationResult : libraryPermutationResults) {
      StatementRanges[] libraryStatementRanges = libraryPermutationResult.getStatementRanges();
      int libraryFragmentCount = libraryStatementRanges.length;

      assert libraryFragmentCount == 1 : "Libraries can only have one fragment.";

      statementRangesList.add(libraryStatementRanges[0]);
    }

    statementRangesList.add(applicationStatementRanges[0]);
    // Some library might not have contained any source and thus have a null statementRange.
    statementRangesList.removeAll(Collections.singleton(null));

    return new StatementRanges[] {StandardStatementRanges.combine(statementRangesList)};
  }

  @Override
  public String getStrongName() {
    return strongName;
  }

  @Override
  public SymbolData[] getSymbolMap() {
    return diskCache.readObject(symbolToken, SymbolData[].class);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy