001/*
002 * Copyright (c) 2012-2018 Institut National des Sciences Appliquées de Lyon (INSA Lyon) and others
003 *
004 * This program and the accompanying materials are made available under the
005 * terms of the Eclipse Public License 2.0 which is available at
006 * http://www.eclipse.org/legal/epl-2.0.
007 *
008 * SPDX-License-Identifier: EPL-2.0
009 */
010
011package gololang.ir;
012
013/**
014 * Represent a method invocation on an expression.
015 * <p>
016 * For instance golo code as
017 * <pre class="listing"><code class="lang-golo" data-lang="golo">
018 * foo()?: bar(42)
019 * </code></pre>
020 * can be generated with:
021   * <pre class="listing"><code class="lang-java" data-lang="java">
022 * invoke("bar").nullSafe().withArgs(42).on(call("foo"))
023 * </code></pre>
024 */
025public final class MethodInvocation extends AbstractInvocation<MethodInvocation> {
026
027  private boolean nullSafeGuarded = false;
028
029  private MethodInvocation(String name) {
030    super(name);
031  }
032
033  public static MethodInvocation invoke(String name) {
034    return new MethodInvocation(name);
035  }
036
037  protected MethodInvocation self() { return this; }
038
039  /**
040   * Checks if this invocation is null-safe.
041   * <p>
042   * I.e. if called with an elvis operator syntax ({@code obj?: method()})
043   */
044  public boolean isNullSafeGuarded() {
045    return nullSafeGuarded;
046  }
047
048  /**
049   * Defines if the invocation is null-safe or not.
050   */
051  public MethodInvocation nullSafe(boolean v) {
052    this.nullSafeGuarded = v;
053    return this;
054  }
055
056  /**
057   * Defines the invocation as null-safe.
058   */
059  public MethodInvocation nullSafe() {
060    return nullSafe(true);
061  }
062
063  /**
064   * Defines the receiver of the invocation.
065   */
066  public BinaryOperation on(Object target) {
067    return BinaryOperation.of(nullSafeGuarded ? OperatorType.ELVIS_METHOD_CALL : OperatorType.METHOD_CALL)
068      .left(ExpressionStatement.of(target))
069      .right(this);
070  }
071
072  /**
073   * {@inheritDoc}
074   */
075  @Override
076  public MethodInvocation withArgs(Object... args) {
077    super.withArgs(args);
078    return this;
079  }
080
081  /**
082   * {@inheritDoc}
083   */
084  @Override
085  public void accept(GoloIrVisitor visitor) {
086    visitor.visitMethodInvocation(this);
087  }
088}