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.annotation.Annotation;
19  import java.util.Set;
20  
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.classpath.visitor.IntrospectionVisitor;
29  import org.jadira.scanner.core.exception.ClasspathAccessException;
30  
31  public abstract class JElement {
32  	
33      private final String name;
34  	private final ClasspathResolver resolver;
35  
36      protected JElement(String name, ClasspathResolver resolver) {
37          this.name = name;
38          this.resolver = resolver;
39      }
40  
41      public String getName() {
42          return name;
43      }
44  
45      public abstract Set<JAnnotation<?>> getAnnotations() throws ClasspathAccessException;
46  
47      public abstract <A extends Annotation> JAnnotation<A> getAnnotation(Class<A> annotation) throws ClasspathAccessException;
48  
49      public abstract void acceptVisitor(IntrospectionVisitor visitor) throws ClasspathAccessException;
50  
51      public abstract JElement getEnclosingElement();
52      
53      protected ClasspathResolver getResolver() {
54      		return resolver;
55  	}
56      
57  	protected static ClassFile findClassFile(final String name, ClasspathResolver resolver) throws ClasspathAccessException {
58  
59  		final ClassFile retVal;
60  		if ("boolean".equals(name)) {
61  			retVal = JClass.findClassFile("java.lang.Boolean", resolver);
62  		} else if ("byte".equals(name)) {
63  			retVal = JClass.findClassFile("java.lang.Byte", resolver);
64  		} else if ("char".equals(name)) {
65  			retVal = JClass.findClassFile("java.lang.Character", resolver);
66  		} else if ("short".equals(name)) {
67  			retVal = JClass.findClassFile("java.lang.Short", resolver);
68  		} else if ("int".equals(name)) {
69  			retVal = JClass.findClassFile("java.lang.Integer", resolver);
70  		} else if ("long".equals(name)) {
71  			retVal = JClass.findClassFile("java.lang.Long", resolver);
72  		} else if ("float".equals(name)) {
73  			retVal = JClass.findClassFile("java.lang.Float", resolver);
74  		} else if ("double".equals(name)) {
75  			retVal = JClass.findClassFile("java.lang.Double", resolver);
76  		} else {
77  			retVal = resolver.getClassFileResolver().resolveClassFile(name);
78  		}
79  		return retVal;
80  	}
81  	
82      @Override
83      public String toString() {
84      	
85      	ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
86      	builder.append(this.name);
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 		JElement rhs = (JElement) obj;
103 		return new EqualsBuilder()
104 			.append(name, rhs.name).isEquals();
105 	}
106 
107     @Override
108 	public int hashCode() {
109 		return new HashCodeBuilder(11, 47).append(name).toHashCode();
110 	}
111 }