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.util.Set;
19  
20  import javassist.bytecode.AccessFlag;
21  import javassist.bytecode.ClassFile;
22  
23  import org.apache.commons.lang3.builder.EqualsBuilder;
24  import org.apache.commons.lang3.builder.HashCodeBuilder;
25  import org.apache.commons.lang3.builder.ToStringBuilder;
26  import org.apache.commons.lang3.builder.ToStringStyle;
27  import org.jadira.scanner.classpath.ClasspathResolver;
28  import org.jadira.scanner.core.exception.ClasspathAccessException;
29  
30  public abstract class JType extends JElement {
31  
32      private final ClassFile classFile;
33  
34      protected JType(ClassFile classFile, ClasspathResolver resolver) {
35          super(classFile.getName(), resolver);
36          this.classFile = classFile;
37      }
38  
39      public abstract JPackage getPackage() throws ClasspathAccessException;
40  
41      // public abstract List<JType> getTypeParams()
42  
43      public abstract Class<?> getActualClass() throws ClasspathAccessException;
44  
45      
46      @Override
47      public <A extends java.lang.annotation.Annotation>JAnnotation<A> getAnnotation(Class<A> annotation) throws ClasspathAccessException {
48  
49          Set<JAnnotation<?>> inspAnnotations = getAnnotations();
50          for (JAnnotation<?> next : inspAnnotations) {
51              if (next.getName().equals(annotation.getName())
52                      && (next.getActualAnnotation().annotationType().getClass().equals(annotation.getClass()))) {
53                  @SuppressWarnings("unchecked") JAnnotation<A> retVal = (JAnnotation<A>)next;
54                  return retVal;
55              }
56          }
57          return null;
58      }
59  
60      public String getModifier() {
61          return isPrivate() ? "private" :
62                 isProtected() ? "protected" :
63                 isPublic() ? "public" : "";
64      }
65      
66      public boolean isPublic() {
67          return AccessFlag.isPublic(classFile.getAccessFlags());
68      }
69      
70      public boolean isProtected() {
71          return AccessFlag.isProtected(classFile.getAccessFlags());
72      }
73      
74      public boolean isPrivate() {
75          return AccessFlag.isPrivate(classFile.getAccessFlags());
76      }
77      
78      public ClassFile getClassFile() {
79          return classFile;
80      }
81      
82      @Override
83      public String toString() {
84      	
85      	ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
86      	builder.append("name", getName());
87      	
88      	return builder.toString();
89      }
90      
91      @Override
92  	public boolean equals(Object obj) {
93  		if (obj == null) {
94  			return false;
95  		}
96  		if (obj == this) {
97  			return true;
98  		}
99  		if (obj.getClass() != getClass()) {
100 			return false;
101 		}
102 		// JType rhs = (JType) obj;
103 		return new EqualsBuilder()
104 			 	.appendSuper(super.equals(obj))
105 				.isEquals();
106 	}
107 
108     @Override
109 	public int hashCode() {
110 		return new HashCodeBuilder(11, 47).append(super.hashCode())
111 				.toHashCode();
112 	}
113 }