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 org.apache.commons.lang3.builder.EqualsBuilder;
21  import org.apache.commons.lang3.builder.HashCodeBuilder;
22  import org.apache.commons.lang3.builder.ToStringBuilder;
23  import org.apache.commons.lang3.builder.ToStringStyle;
24  import org.jadira.scanner.classpath.ClasspathResolver;
25  import org.jadira.scanner.core.exception.ClasspathAccessException;
26  
27  public abstract class JVariable extends JElement {
28  
29      protected JVariable(String name, ClasspathResolver resolver) {
30          super(name, resolver);
31      }
32  
33      public abstract JType getEnclosingType() throws ClasspathAccessException;
34  
35      public abstract JType getType() throws ClasspathAccessException;
36  
37      @Override
38      public abstract Set<JAnnotation<?>> getAnnotations() throws ClasspathAccessException;
39  
40      @Override
41      public <A extends java.lang.annotation.Annotation> JAnnotation<A> getAnnotation(Class<A> annotation) throws ClasspathAccessException {
42  
43          Set<JAnnotation<?>> inspAnnotations = getAnnotations();
44          for (JAnnotation<?> next : inspAnnotations) {
45              if (next.getName().equals(annotation.getName())
46                      && (next.getActualAnnotation().getClass().equals(annotation.getClass()))) {
47                  @SuppressWarnings("unchecked") JAnnotation<A> retVal = (JAnnotation<A>)next;
48                  return retVal;
49              }
50          }
51          return null;
52      }
53      
54      @Override
55      public String toString() {
56      	
57      	ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
58      	builder.append("name", this.getName());
59      	builder.append("type", this.getType());
60      	builder.append("enclosingType", this.getEnclosingType());
61      	
62      	return builder.toString();
63      }
64      
65      @Override
66  	public boolean equals(Object obj) {
67  		if (obj == null) {
68  			return false;
69  		}
70  		if (obj == this) {
71  			return true;
72  		}
73  		if (obj.getClass() != getClass()) {
74  			return false;
75  		}
76  		// JVariable rhs = (JVariable) obj;
77  		return new EqualsBuilder()
78  			 	.appendSuper(super.equals(obj))
79  				.isEquals();
80  	}
81  
82      @Override
83  	public int hashCode() {
84  		return new HashCodeBuilder(11, 47).append(super.hashCode())
85  				.toHashCode();
86  	}
87  }