View Javadoc
1   /*
2    *  Copyright 2012 Chris Pheby
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 org.jadira.scanner.classpath.types;
17  
18  import java.lang.reflect.Method;
19  import java.util.List;
20  
21  import javassist.bytecode.AccessFlag;
22  import javassist.bytecode.MethodInfo;
23  
24  import org.apache.commons.lang3.builder.EqualsBuilder;
25  import org.apache.commons.lang3.builder.HashCodeBuilder;
26  import org.jadira.scanner.classpath.ClasspathResolver;
27  import org.jadira.scanner.classpath.visitor.IntrospectionVisitor;
28  import org.jadira.scanner.core.exception.ClasspathAccessException;
29  
30  public class JMethod extends JOperation {
31  
32      protected JMethod(MethodInfo methodInfo, JType enclosingType, ClasspathResolver resolver) {
33          super(methodInfo, enclosingType, resolver);
34      }
35  
36      public static JMethod getJMethod(MethodInfo methodInfo, JType enclosingType, ClasspathResolver resolver) {
37          return new JMethod(methodInfo, enclosingType, resolver);
38      }
39      
40      @Override
41      public Method getActualMethod() throws ClasspathAccessException {
42  
43          List<JParameter> params = getParameters();
44          Class<?>[] paramClasses = new Class<?>[params.size()];
45          for (int i = 0; i < params.size(); i++) {
46              paramClasses[i] = params.get(i).getType().getActualClass();
47          }
48  
49          try {
50              return getEnclosingType().getActualClass().getDeclaredMethod(getName(), paramClasses);
51          } catch (SecurityException e) {
52              throw new ClasspathAccessException("Could not access class: " + e.getMessage(), e);
53          } catch (NoSuchMethodException e) {
54              throw new ClasspathAccessException("Could not find method: " + e.getMessage(), e);
55          }
56      }
57  
58      public String getModifier() {
59          return isPrivate() ? "private" :
60                 isProtected() ? "protected" :
61                 isPublic() ? "public" : "";
62      }
63      
64      public boolean isPublic() {
65          return AccessFlag.isPublic(getMethodInfo().getAccessFlags());
66      }
67      
68      public boolean isProtected() {
69          return AccessFlag.isProtected(getMethodInfo().getAccessFlags());
70      }
71       
72      public boolean isPrivate() {
73          return AccessFlag.isPrivate(getMethodInfo().getAccessFlags());
74      }
75      
76      @Override
77      public void acceptVisitor(IntrospectionVisitor visitor) throws ClasspathAccessException {
78          visitor.visit(this);
79  
80          for (JParameter next : getParameters()) {
81              next.acceptVisitor(visitor);
82          }
83          for (JAnnotation<?> next : getAnnotations()) {
84              next.acceptVisitor(visitor);
85          }
86      }
87      
88      @Override
89  	public boolean equals(Object obj) {
90  		if (obj == null) {
91  			return false;
92  		}
93  		if (obj == this) {
94  			return true;
95  		}
96  		if (obj.getClass() != getClass()) {
97  			return false;
98  		}
99  		// JMethod rhs = (JMethod) obj;
100 		return new EqualsBuilder()
101 			 	.appendSuper(super.equals(obj))
102 				.isEquals();
103 	}
104 
105     @Override
106 	public int hashCode() {
107 		return new HashCodeBuilder(11, 47).append(super.hashCode())
108 				.toHashCode();
109 	}
110 }