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

com.google.gwt.dev.jjs.impl.JavaToJavaScriptMapImpl Maven / Gradle / Ivy

There is a newer version: 2.10.0
Show newest version
/*
 * Copyright 2014 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.dev.jjs.impl;

import com.google.gwt.dev.jjs.ast.HasName;
import com.google.gwt.dev.jjs.ast.JClassType;
import com.google.gwt.dev.jjs.ast.JDeclaredType;
import com.google.gwt.dev.jjs.ast.JField;
import com.google.gwt.dev.jjs.ast.JMethod;
import com.google.gwt.dev.js.ast.JsExprStmt;
import com.google.gwt.dev.js.ast.JsFunction;
import com.google.gwt.dev.js.ast.JsName;
import com.google.gwt.dev.js.ast.JsStatement;
import com.google.gwt.thirdparty.guava.common.collect.Maps;

import java.util.List;
import java.util.Map;

/**
 * The default implementation of a JavaToJavaScriptMap.
 */
public class JavaToJavaScriptMapImpl implements JavaToJavaScriptMap {

  private final Map names;
  private final Map fieldForName;
  private final Map methodForName;
  private final Map typeForConstructorName;

  private final Map typeForStatement;
  private final Map methodForStatement;

  public JavaToJavaScriptMapImpl(List types,
      Map names,
      Map typeForStatement,
      Map methodForStatement) {

    // Generate reverse indexes for names.
    Map methodsByJsName = Maps.newHashMap();
    Map staticFieldsByJsName = Maps.newHashMap();
    Map typesByContructorJsName = Maps.newHashMap();
    for (JDeclaredType type : types) {
      JsName typeName = names.get(type);
      if (type instanceof JClassType && typeName != null) {
        typesByContructorJsName.put(typeName, (JClassType) type);
      }
      for (JField field : type.getFields()) {
        if (field.isStatic()) {
          JsName fieldName = names.get(field);
          if (fieldName != null) {
            staticFieldsByJsName.put(fieldName, field);
          }
        }
      }
      for (JMethod method : type.getMethods()) {
        JsName methodName = names.get(method);
        if (methodName != null) {
          methodsByJsName.put(methodName, method);
        }
      }
    }

    this.names = names;
    this.fieldForName = staticFieldsByJsName;
    this.methodForName = methodsByJsName;
    this.typeForConstructorName = typesByContructorJsName;

    this.typeForStatement = typeForStatement;
    this.methodForStatement = methodForStatement;
  }

  @Override
  public JsName nameForField(JField field) {
    return names.get(field);
  }

  @Override
  public JsName nameForMethod(JMethod method) {
    return names.get(method);
  }

  @Override
  public JsName nameForType(JDeclaredType type) {
    return names.get(type);
  }

  @Override
  public JField nameToField(JsName name) {
    return fieldForName.get(name);
  }

  @Override
  public JMethod nameToMethod(JsName name) {
    return methodForName.get(name);
  }

  @Override
  public JClassType nameToType(JsName name) {
    return typeForConstructorName.get(name);
  }

  @Override
  public JDeclaredType typeForStatement(JsStatement statement) {
    return typeForStatement.get(statement);
  }

  @Override
  public JMethod methodForStatement(JsStatement statement) {
    JMethod method = methodForStatement.get(statement);
    if (JavaToJavaScriptMapImpl.class.desiredAssertionStatus()
        && method == null && statement instanceof JsExprStmt
        && ((JsExprStmt) statement).getExpression() instanceof JsFunction) {
      JsFunction function = (JsFunction) ((JsExprStmt) statement).getExpression();
      assert nameToMethod(function.getName()) == null;
    }
    return method;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy