|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ReflectExceptionVisitor.java | - | 90% | 90% | 90% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK* | |
| 2 | ||
| 3 | PLT Utilities BSD License | |
| 4 | ||
| 5 | Copyright (c) 2007-2010 JavaPLT group at Rice University | |
| 6 | All rights reserved. | |
| 7 | ||
| 8 | Developed by: Java Programming Languages Team | |
| 9 | Rice University | |
| 10 | http://www.cs.rice.edu/~javaplt/ | |
| 11 | ||
| 12 | Redistribution and use in source and binary forms, with or without modification, are permitted | |
| 13 | provided that the following conditions are met: | |
| 14 | ||
| 15 | - Redistributions of source code must retain the above copyright notice, this list of conditions | |
| 16 | and the following disclaimer. | |
| 17 | - Redistributions in binary form must reproduce the above copyright notice, this list of | |
| 18 | conditions and the following disclaimer in the documentation and/or other materials provided | |
| 19 | with the distribution. | |
| 20 | - Neither the name of the JavaPLT group, Rice University, nor the names of the library's | |
| 21 | contributors may be used to endorse or promote products derived from this software without | |
| 22 | specific prior written permission. | |
| 23 | ||
| 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR | |
| 25 | IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 26 | FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND | |
| 27 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
| 28 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 29 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER | |
| 30 | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
| 31 | OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 32 | ||
| 33 | *END_COPYRIGHT_BLOCK*/ | |
| 34 | ||
| 35 | package edu.rice.cs.plt.reflect; | |
| 36 | ||
| 37 | import java.lang.reflect.InvocationTargetException; | |
| 38 | ||
| 39 | /** | |
| 40 | * <p>A visitor for {@link ReflectException}s. By default, each method simply defers to the {@link #defaultCase} | |
| 41 | * method. An implementation should define {@code defaultCase} and override any methods corresponding to | |
| 42 | * exceptions that deserve special treatment.</p> | |
| 43 | * | |
| 44 | * <p>If, as is often the case in exception handling, no return value is required, the return type can be | |
| 45 | * specified as {@link Void}, and all methods should simply return {@code null}.</p> | |
| 46 | */ | |
| 47 | public abstract class ReflectExceptionVisitor<T> { | |
| 48 | ||
| 49 | protected abstract T defaultCase(Exception e); | |
| 50 | ||
| 51 | /** | |
| 52 | * Handle a {@code ClassNotFoundException}, which can occur if a class name does not refer to | |
| 53 | * any available classes. | |
| 54 | */ | |
| 55 | 5 | public T forClassNotFound(ClassNotFoundException e) { return defaultCase(e); } |
| 56 | ||
| 57 | /** | |
| 58 | * Handle a {@code NoSuchFieldException}, which can occur when a field name does not refer to | |
| 59 | * a publicly-accessible field of a class. | |
| 60 | */ | |
| 61 | 3 | public T forNoSuchField(NoSuchFieldException e) { return defaultCase(e); } |
| 62 | ||
| 63 | /** | |
| 64 | * Handle a {@code NoSuchMethodException}, which can occur when a method or constructor name | |
| 65 | * and signature do not refer to a publicly-accessible method of a class. | |
| 66 | */ | |
| 67 | 11 | public T forNoSuchMethod(NoSuchMethodException e) { return defaultCase(e); } |
| 68 | ||
| 69 | /** | |
| 70 | * Handle an {@code IllegalArgumentException}, which can occur if an object whose member is | |
| 71 | * being accessed does not have the required type, or if the arguments to a method or constructor | |
| 72 | * do not match the required signature. (It can also occur in the rare circumstance that an | |
| 73 | * accessible enum constructor reflectively is invoked.) | |
| 74 | */ | |
| 75 | 3 | public T forIllegalArgument(IllegalArgumentException e) { return defaultCase(e); } |
| 76 | ||
| 77 | /** | |
| 78 | * Handle a {@code NullPointerException}, which can occur when statically accessing a non-static | |
| 79 | * member, or when attempting to access a member of the object {@code null}. | |
| 80 | */ | |
| 81 | 4 | public T forNullPointer(NullPointerException e) { return defaultCase(e); } |
| 82 | ||
| 83 | /** | |
| 84 | * Handle a {@code ClassCastException}, which can occur when attempting to dynamically cast | |
| 85 | * an object to the generic type of a class object. | |
| 86 | * @see ReflectUtil#cast | |
| 87 | */ | |
| 88 | 4 | public T forClassCast(ClassCastException e) { return defaultCase(e); } |
| 89 | ||
| 90 | /** | |
| 91 | * Handle an {@code InvocationTargetException}, which occurs whenever a {@link Throwable} | |
| 92 | * is thrown by a reflectively-executed method or constructor. Note that <em>all</em> throwables | |
| 93 | * are wrapped in this way, and so a good practice would be to re-throw any that are unexpected | |
| 94 | * (such as errors). Because {@code InvocationTargetException} allows construction without | |
| 95 | * a {@code cause} parameter, {@code e.getCause()} may be {@code null}. | |
| 96 | */ | |
| 97 | 2 | public T forInvocationTarget(InvocationTargetException e) { return defaultCase(e); } |
| 98 | ||
| 99 | /** | |
| 100 | * Handle an {@code InstantiationException}, which can occur if an attempt is made to construct | |
| 101 | * a non-constructable class (such as an abstract class). | |
| 102 | */ | |
| 103 | 1 | public T forInstantiation(InstantiationException e) { return defaultCase(e); } |
| 104 | ||
| 105 | /** | |
| 106 | * Handle an {@code IllegalAccessException}, which occurs when an attempt is made to write to a | |
| 107 | * final field or to access an inaccessible class or class member. | |
| 108 | */ | |
| 109 | 2 | public T forIllegalAccess(IllegalAccessException e) { return defaultCase(e); } |
| 110 | ||
| 111 | /** | |
| 112 | * Handle a {@code SecurityException}, which can occur whenever a security manager restricts access | |
| 113 | * to reflection operations. | |
| 114 | */ | |
| 115 | 0 | public T forSecurity(SecurityException e) { return defaultCase(e); } |
| 116 | } |
|
||||||||||