1 package org.apache.velocity.exception; 2 3 import org.apache.velocity.util.StringUtils; 4 5 /* 6 * Licensed to the Apache Software Foundation (ASF) under one 7 * or more contributor license agreements. See the NOTICE file 8 * distributed with this work for additional information 9 * regarding copyright ownership. The ASF licenses this file 10 * to you under the Apache License, Version 2.0 (the 11 * "License"); you may not use this file except in compliance 12 * with the License. You may obtain a copy of the License at 13 * 14 * http://www.apache.org/licenses/LICENSE-2.0 15 * 16 * Unless required by applicable law or agreed to in writing, 17 * software distributed under the License is distributed on an 18 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 19 * KIND, either express or implied. See the License for the 20 * specific language governing permissions and limitations 21 * under the License. 22 */ 23 24 /** 25 * Application-level exception thrown when a reference method is 26 * invoked and an exception is thrown. 27 * <br> 28 * When this exception is thrown, a best effort will be made to have 29 * useful information in the exception's message. For complete 30 * information, consult the runtime log. 31 * 32 * @author <a href="mailto:geirm@optonline.net">Geir Magnusson Jr.</a> 33 * @version $Id$ 34 */ 35 public class MethodInvocationException extends VelocityException implements ExtendedParseException 36 { 37 /** 38 * Version Id for serializable 39 */ 40 private static final long serialVersionUID = 7305685093478106342L; 41 42 private String referenceName = ""; 43 44 private final String methodName; 45 46 private final int lineNumber; 47 private final int columnNumber; 48 private final String templateName; 49 50 /** 51 * CTOR - wraps the passed in exception for 52 * examination later 53 * 54 * @param message 55 * @param e Throwable that we are wrapping 56 * @param methodName name of method that threw the exception 57 * @param templateName The name of the template where the exception occurred 58 * @param lineNumber line number 59 * @param columnNumber column number 60 */ MethodInvocationException(final String message, final Throwable e, final String methodName, final String templateName, final int lineNumber, final int columnNumber)61 public MethodInvocationException(final String message, final Throwable e, final String methodName, final String templateName, final int lineNumber, final int columnNumber) 62 { 63 super(message, e); 64 65 this.methodName = methodName; 66 this.templateName = templateName; 67 this.lineNumber = lineNumber; 68 this.columnNumber = columnNumber; 69 } 70 71 /** 72 * CTOR - wraps the passed in exception for 73 * examination later 74 * 75 * @param message 76 * @param e Throwable that we are wrapping 77 * @param stacktrace VTL stacktrace 78 * @param methodName name of method that threw the exception 79 * @param templateName The name of the template where the exception occurred 80 * @param lineNumber line number 81 * @param columnNumber column number 82 */ MethodInvocationException(final String message, final Throwable e, final String[] stacktrace, final String methodName, final String templateName, final int lineNumber, final int columnNumber)83 public MethodInvocationException(final String message, final Throwable e, final String[] stacktrace, final String methodName, final String templateName, final int lineNumber, final int columnNumber) 84 { 85 super(message, e, stacktrace); 86 87 this.methodName = methodName; 88 this.templateName = templateName; 89 this.lineNumber = lineNumber; 90 this.columnNumber = columnNumber; 91 } 92 93 /** 94 * Returns the name of the method that threw the 95 * exception. 96 * 97 * @return String name of method 98 */ getMethodName()99 public String getMethodName() 100 { 101 return methodName; 102 } 103 104 /** 105 * Sets the reference name that threw this exception. 106 * 107 * @param ref name of reference 108 */ setReferenceName(String ref)109 public void setReferenceName(String ref) 110 { 111 referenceName = ref; 112 } 113 114 /** 115 * Retrieves the name of the reference that caused the 116 * exception. 117 * 118 * @return name of reference. 119 */ getReferenceName()120 public String getReferenceName() 121 { 122 return referenceName; 123 } 124 125 /** 126 * @see ExtendedParseException#getColumnNumber() 127 * @since 1.5 128 */ 129 @Override getColumnNumber()130 public int getColumnNumber() 131 { 132 return columnNumber; 133 } 134 135 /** 136 * @see ExtendedParseException#getLineNumber() 137 * @since 1.5 138 */ 139 @Override getLineNumber()140 public int getLineNumber() 141 { 142 return lineNumber; 143 } 144 145 /** 146 * @see ExtendedParseException#getTemplateName() 147 * @since 1.5 148 */ 149 @Override getTemplateName()150 public String getTemplateName() 151 { 152 return templateName; 153 } 154 155 /** 156 * @see Exception#getMessage() 157 * @since 1.5 158 */ 159 @Override getMessage()160 public String getMessage() 161 { 162 StringBuilder message = new StringBuilder(); 163 message.append(super.getMessage()); 164 message.append(" at "); 165 message.append(StringUtils.formatFileString(templateName, lineNumber, columnNumber)); 166 return message.toString(); 167 } 168 } 169