Package org.stjs.generator.writer.expression

Source Code of org.stjs.generator.writer.expression.TypeCastWriter

package org.stjs.generator.writer.expression;

import java.util.Collections;

import javax.lang.model.type.TypeMirror;

import org.stjs.generator.GenerationContext;
import org.stjs.generator.javac.TypesUtils;
import org.stjs.generator.writer.WriterContributor;
import org.stjs.generator.writer.WriterVisitor;

import com.sun.source.tree.TypeCastTree;
import com.sun.source.util.TreePath;

public class TypeCastWriter<JS> implements WriterContributor<TypeCastTree, JS> {

  @Override
  public JS visit(WriterVisitor<JS> visitor, TypeCastTree tree, GenerationContext<JS> context) {
    TypeMirror type = context.getTrees().getTypeMirror(new TreePath(context.getCurrentPath(), tree.getType()));
    JS expr = visitor.scan(tree.getExpression(), context);
    if (TypesUtils.isIntegral(type)) {
      // add explicit cast in this case
      JS target = context.js().property(context.js().name("stjs"), "trunc");
      expr = context.js().functionCall(target, Collections.singleton(expr));
    }
    // otherwise skip to cast type - continue with the expression
    return expr;
  }
}
TOP

Related Classes of org.stjs.generator.writer.expression.TypeCastWriter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.