| 1 | /* |
| 2 | * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package net.sf.antcontrib.logic; |
| 17 | |
| 18 | import org.apache.tools.ant.BuildException; |
| 19 | import org.apache.tools.ant.taskdefs.Exit; |
| 20 | import org.apache.tools.ant.types.Reference; |
| 21 | |
| 22 | /** |
| 23 | * Extension of <code><fail></code> that can throw an exception |
| 24 | * that is a reference in the project. |
| 25 | * |
| 26 | * <p>This may be useful inside the <code><catch></code> block |
| 27 | * of a <code><trycatch></code> task if you want to rethrow the |
| 28 | * exception just caught.</p> |
| 29 | */ |
| 30 | public class Throw extends Exit { |
| 31 | |
| 32 | private Reference ref; |
| 33 | |
| 34 | /** |
| 35 | * The reference that points to a BuildException. |
| 36 | */ |
| 37 | public void setRefid(Reference ref) { |
| 38 | this.ref = ref; |
| 39 | } |
| 40 | |
| 41 | public void execute() throws BuildException { |
| 42 | Object reffed = ref != null |
| 43 | ? ref.getReferencedObject(getProject()) |
| 44 | : null; |
| 45 | if (reffed != null && reffed instanceof BuildException) { |
| 46 | throw (BuildException) reffed; |
| 47 | } |
| 48 | super.execute(); |
| 49 | } |
| 50 | } |