View Javadoc

1   /**
2    * Copyright (c) 2012, University of Konstanz, Distributed Systems Group
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    * * Redistributions of source code must retain the above copyright
8    * notice, this list of conditions and the following disclaimer.
9    * * Redistributions in binary form must reproduce the above copyright
10   * notice, this list of conditions and the following disclaimer in the
11   * documentation and/or other materials provided with the distribution.
12   * * Neither the name of the University of Konstanz nor the
13   * names of its contributors may be used to endorse or promote products
14   * derived from this software without specific prior written permission.
15   * 
16   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19   * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
20   * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26   */
27  package org.perfidix.exceptions;
28  
29  import java.lang.annotation.Annotation;
30  import java.lang.reflect.Method;
31  
32  /**
33   * This class acts as a super exception for all exception thrown by the Perfidix
34   * framework while invoking methods.
35   * 
36   * @author Sebastian Graf, University of Konstanz
37   */
38  public abstract class AbstractPerfidixMethodException extends Exception {
39      /** Serial because of serializable. */
40      private static final long serialVersionUID = 5251116501564408317L;
41  
42      /**
43       * Encapsulated Exception.
44       */
45      private transient final Throwable exec;
46  
47      /**
48       * Related method.
49       */
50      private transient final Method meth;
51  
52      /** Related annotation to this method. */
53      private transient final Class<? extends Annotation> relatedAnno;
54  
55      /**
56       * Simple Constructor.
57       * 
58       * @param paramExec
59       *            the exception to be hold in this container.
60       * @param paramMeth
61       *            the related method to the exception.
62       * @param paramAnnotation
63       *            the related annotation to this method
64       */
65      public AbstractPerfidixMethodException(final Throwable paramExec, final Method paramMeth,
66          final Class<? extends Annotation> paramAnnotation) {
67          super();
68          this.exec = paramExec;
69          this.meth = paramMeth;
70          this.relatedAnno = paramAnnotation;
71      }
72  
73      /**
74       * Getter for the related {@link Method}.
75       * 
76       * @return the method for this exception.
77       */
78      public final Method getMethod() {
79          return meth;
80      }
81  
82      /**
83       * Getter for the encapsulated {@link Exception}.
84       * 
85       * @return the exec which is hold in this container.
86       */
87      public final Throwable getExec() {
88          return exec;
89      }
90  
91      /**
92       * Getter for annotation.
93       * 
94       * @return the related annotation for this errored method
95       */
96      public final Class<? extends Annotation> getRelatedAnno() {
97          return relatedAnno;
98      }
99  
100     /** {@inheritDoc} */
101     @Override
102     public final int hashCode() {
103         final int prime = 31;
104         int result = 1;
105         if (exec == null) {
106             result = prime * result;
107         } else {
108             result = prime * result + exec.hashCode();
109         }
110         if (meth == null) {
111             result = prime * result;
112         } else {
113             result = prime * result + meth.hashCode();
114         }
115         if (relatedAnno == null) {
116             result = prime * result;
117         } else {
118             result = prime * result + relatedAnno.hashCode();
119         }
120         return result;
121     }
122 
123     /** {@inheritDoc} */
124     @Override
125     public final boolean equals(final Object obj) {
126         boolean returnVal = true;
127         if (this == obj) {
128             returnVal = true;
129         }
130         if (obj == null) {
131             returnVal = false;
132         }
133         if (getClass() != obj.getClass()) {
134             returnVal = false;
135         }
136         final AbstractPerfidixMethodException other = (AbstractPerfidixMethodException)obj;
137         if (exec == null) {
138             if (other.exec != null) {
139                 returnVal = false;
140             }
141         } else {
142             if (!exec.equals(other.exec)) {
143                 returnVal = false;
144             }
145         }
146         if (meth == null) {
147             if (other.meth != null) {
148                 returnVal = false;
149             }
150         } else {
151             if (!meth.equals(other.meth)) {
152                 returnVal = false;
153             }
154         }
155         if (relatedAnno == null) {
156             if (other.relatedAnno != null) {
157                 returnVal = false;
158             }
159         } else {
160             if (!relatedAnno.equals(other.relatedAnno)) {
161                 returnVal = false;
162             }
163         }
164         return returnVal;
165     }
166 
167     /**
168      * {@inheritDoc}
169      */
170     @Override
171     public final String toString() {
172         final StringBuilder builder = new StringBuilder();
173         builder.append(this.getClass().getSimpleName());
174         builder.append(":");
175         builder.append(this.exec.getClass().getSimpleName());
176         builder.append(":");
177         builder.append(this.relatedAnno.getClass().getSimpleName());
178         builder.append(":");
179         builder.append(this.meth.getName());
180         return builder.toString();
181     }
182 
183 }