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 javassist.bytecode.ClassFile;
19  
20  import org.apache.commons.lang3.builder.EqualsBuilder;
21  import org.apache.commons.lang3.builder.HashCodeBuilder;
22  import org.jadira.scanner.classpath.ClasspathResolver;
23  import org.jadira.scanner.core.exception.ClasspathAccessException;
24  
25  public class JPrimitiveClass extends JClass {
26  
27      protected JPrimitiveClass(String name, ClasspathResolver resolver) throws ClasspathAccessException {
28      	super(findClassFile(name, resolver), resolver);
29      }
30  
31      protected JPrimitiveClass(Class<?> clazz, ClasspathResolver resolver) throws ClasspathAccessException {
32      	super(findClassFile(clazz.getName(), resolver), resolver);
33      }
34  
35      protected JPrimitiveClass(ClassFile classFile, ClasspathResolver resolver) {
36          super(classFile, resolver);
37      }
38  
39      public static JPrimitiveClass getJClass(String name, ClasspathResolver resolver) throws ClasspathAccessException {
40          return new JPrimitiveClass(name, resolver);
41      }
42  
43      public static JPrimitiveClass getJClass(ClassFile classFile, ClasspathResolver resolver) {
44          return new JPrimitiveClass(classFile, resolver);
45      }
46  
47      public static JPrimitiveClass getJClass(Class<?> clazz, ClasspathResolver resolver) throws ClasspathAccessException {
48          return new JPrimitiveClass(clazz, resolver);
49      }
50  
51  	protected static final ClassFile findClassFile(final String name, ClasspathResolver resolver) throws ClasspathAccessException {
52  
53  		if ("boolean".equals(name)) {
54  			return JClass.findClassFile("java.lang.Boolean", resolver);
55  		} else if ("byte".equals(name)) {
56  			return JClass.findClassFile("java.lang.Byte", resolver);
57  		} else if ("char".equals(name)) {
58  			return JClass.findClassFile("java.lang.Character", resolver);
59  		} else if ("short".equals(name)) {
60  			return JClass.findClassFile("java.lang.Short", resolver);
61  		} else if ("int".equals(name)) {
62  			return JClass.findClassFile("java.lang.Integer", resolver);
63  		} else if ("long".equals(name)) {
64  			return JClass.findClassFile("java.lang.Long", resolver);
65  		} else if ("float".equals(name)) {
66  			return JClass.findClassFile("java.lang.Float", resolver);
67  		} else if ("double".equals(name)) {
68  			return JClass.findClassFile("java.lang.Double", resolver);
69  		} else {
70  			throw new ClasspathAccessException("Not a valid primitive: " + name);
71  		}
72  	}
73  	
74  	@Override
75  	public boolean isPrimitive() {
76  		return true;
77  	}
78  	
79  	@Override
80  	public boolean equals(Object obj) {
81  		if (obj == null) {
82  			return false;
83  		}
84  		if (obj == this) {
85  			return true;
86  		}
87  		if (obj.getClass() != getClass()) {
88  			return false;
89  		}
90  		// JPrimitiveClass rhs = (JPrimitiveClass) obj;
91  		return new EqualsBuilder()
92  			 	.appendSuper(super.equals(obj))
93  				.isEquals();
94  	}
95  
96  	@Override
97  	public int hashCode() {
98  		return new HashCodeBuilder(11, 47).append(super.hashCode())
99  				.toHashCode();
100 	}
101 }