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 013import java.util.Objects; 014import java.util.Collections; 015import java.util.List; 016 017public final class ThrowStatement extends GoloStatement<ThrowStatement> { 018 019 private GoloStatement<?> expressionStatement; 020 021 private ThrowStatement(GoloStatement<?> expressionStatement) { 022 super(); 023 setExpressionStatement(expressionStatement); 024 } 025 026 /** 027 * Creates a {@code throw} statement. 028 * 029 * @param expression an object interpreted as an {@link ExpressionStatement} that will evaluate to an exception. 030 * @see ExpressionStatement#of(Object) 031 */ 032 public static ThrowStatement of(Object expression) { 033 return new ThrowStatement(ExpressionStatement.of(expression)); 034 } 035 036 protected ThrowStatement self() { return this; } 037 038 /** 039 * {@inheritDoc} 040 */ 041 @Override 042 public void accept(GoloIrVisitor visitor) { 043 visitor.visitThrowStatement(this); 044 } 045 046 /** 047 * {@inheritDoc} 048 */ 049 @Override 050 public List<GoloElement<?>> children() { 051 return Collections.singletonList(expressionStatement); 052 } 053 054 /** 055 * {@inheritDoc} 056 */ 057 @Override 058 protected void replaceElement(GoloElement<?> original, GoloElement<?> newElement) { 059 if (Objects.equals(original, expressionStatement) && newElement instanceof ExpressionStatement) { 060 setExpressionStatement(ExpressionStatement.of(newElement)); 061 } else { 062 throw cantReplace(original, newElement); 063 } 064 } 065 066 public GoloStatement<?> expression() { 067 return expressionStatement; 068 } 069 070 private void setExpressionStatement(GoloStatement<?> stat) { 071 this.expressionStatement = makeParentOf(stat); 072 } 073}