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.Collections;
19  import java.util.List;
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 class JInnerClass extends JClass {
32  
33      private ClassFile enclosingClass;
34  
35      protected JInnerClass(ClassFile enclosingClass, ClassFile classFile, ClasspathResolver resolver) throws ClasspathAccessException {
36          super(classFile, resolver);
37          // Get the inner class definition
38          this.enclosingClass = enclosingClass;
39      }
40  
41      public static JInnerClass getJInnerClass(ClassFile enclosingClass, ClassFile classFile, ClasspathResolver resolver) throws ClasspathAccessException {
42          return new JInnerClass(enclosingClass, classFile, resolver);
43      }
44  
45      public JClass getEnclosingClass() {
46          return JClass.getJClass(enclosingClass, getResolver());
47      }
48  
49      @Override
50      public JClass getEnclosingElement() {
51          return getEnclosingClass();
52      }
53       
54      @Override
55      public void acceptVisitor(IntrospectionVisitor visitor) throws ClasspathAccessException {
56          visitor.visit(this);
57  
58          for (JInterface next : getImplementedInterfaces()) {
59              next.acceptVisitor(visitor);
60          }
61          for (JInnerClass next : getEnclosedClasses()) {
62              next.acceptVisitor(visitor);
63          }
64          for (JField next : getFields()) {
65              next.acceptVisitor(visitor);
66          }
67          for (JConstructor next : getConstructors()) {
68              next.acceptVisitor(visitor);
69          }
70          for (JMethod next : getMethods()) {
71              next.acceptVisitor(visitor);
72          }
73          for (JStaticInitializer next : getStaticInitializers()) {
74              next.acceptVisitor(visitor);
75          }
76          for (JAnnotation<?> next : getAnnotations()) {
77              next.acceptVisitor(visitor);
78          }
79      }
80      
81      @Override
82      public String toString() {
83      	
84      	ToStringBuilder builder = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
85      	builder.append("name", getName());
86      	builder.append("enclosingClass", getEnclosingClass());
87      	
88      	return builder.toString();
89      }
90      
91      @Override
92      public List<JInnerClass> getEnclosedClasses() {
93      	return Collections.emptyList();
94      }
95      
96      @Override
97  	public boolean equals(Object obj) {
98  		if (obj == null) {
99  			return false;
100 		}
101 		if (obj == this) {
102 			return true;
103 		}
104 		if (obj.getClass() != getClass()) {
105 			return false;
106 		}
107 		JInnerClass rhs = (JInnerClass) obj;
108 		return new EqualsBuilder()
109 			 	.appendSuper(super.equals(obj))
110 				.append(enclosingClass, rhs.enclosingClass).isEquals();
111 	}
112 
113     @Override
114 	public int hashCode() {
115 		return new HashCodeBuilder(11, 47).append(super.hashCode())
116 				.append(enclosingClass).toHashCode();
117 	}
118 }