1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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 }