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

core.main.ScriptRio Maven / Gradle / Ivy

The newest version!
/*
 * Copyright 2014 Objectos, Fábrica de Software LTDA.
 *
 * 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 br.com.objectos.rio.core.main;

import static com.google.common.collect.Lists.newArrayList;

import java.lang.reflect.Method;
import java.util.List;

import br.com.objectos.rio.RioParser.StringLiteralContext;

import br.com.objectos.way.core.util.WayIterables;

import com.google.common.base.Optional;
import com.google.common.base.Throwables;

/**
 * @author [email protected] (Marcio Endo)
 */
class ScriptRio {

  private final List expressionList = newArrayList();

  private final ReferenceMap referenceMap;
  private final VariableMap variableMap;

  public ScriptRio(ReferenceMap referenceMap, VariableMap variableMap) {
    this.referenceMap = referenceMap;
    this.variableMap = variableMap;
  }

  public List getExpressiontList() {
    return expressionList;
  }

  public String toString(StringLiteralContext ctx) {
    String text = ctx.getText();
    int length = text.length();
    return text.substring(1, length - 1);
  }

  public void add(Expression expression) {
    expressionList.add(expression);

    expression.addTo(variableMap);
  }

  public Method parseMethod(Reference reference, String methodName, List methodArgumentList) {
    Class type = reference.type();
    return parseMethod(type, methodName, methodArgumentList);
  }

  public Method parseMethod(Class type, String methodName, List methodArgumentList) {
    try {
      Class[] argumentClassArray = argumentClassArray(methodArgumentList);
      return type.getMethod(methodName, argumentClassArray);
    } catch (SecurityException e) {
      throw Throwables.propagate(e);
    } catch (NoSuchMethodException e) {
      throw Throwables.propagate(e);
    }
  }

  public Reference parseReference(String referenceName) {
    return referenceMap.fromReferenceName(referenceName);
  }

  public Reference parseReference(String methodName, List argumentList) {
    return referenceMap.fromMethodName(methodName, argumentList);
  }

  public Optional popExpression() {
    Optional res = Optional.absent();

    int size = expressionList.size();
    if (size > 0) {
      Expression expression = expressionList.get(size - 1);
      res = Optional.of(expression);
    }

    return res;
  }

  private Class[] argumentClassArray(List methodArgumentList) {
    return WayIterables.from(methodArgumentList)
        .transform(MethodArgumentGetType.get())
        .toImmutableList()
        .toArray(new Class[] {});
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy