Inspector | Message | Severity | Location |
---|---|---|---|
Java Inspector 048 | Copyrights information should be present in each file. | 1 | |
Java Inspector 049 | Use a Collection instead of arrays Object[] | 2 | 87:38 |
Java Inspector 089 | Constructor is not properly documented | 2 | 33:9 |
Java Inspector 089 | Undocumented method | 2 | 51:9 |
Java Inspector 089 | Undocumented method | 2 | 69:9 |
Java Inspector 089 | Undocumented method | 2 | 73:9 |
Java Inspector 089 | Undocumented method | 2 | 103:9 |
Java Inspector 005 | Classes, interfaces, methods, and variables should be named according to Sun's naming conventions. | 3 | 18:9 |
Java Inspector 025 | Avoid hardwired numeric literals. Allowed literals: [1, -1, 0] | 3 | 92:67 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 64:48 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 64:94 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 64:135 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 104:52 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 104:68 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 104:86 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 104:110 |
Java Inspector 026 | Avoid hardwired string literals. Allowed literals: [] | 3 | 104:140 |
1package biz.hammurapi.convert;
2
3import java.lang.reflect.Constructor;
4import java.lang.reflect.Method;
5import java.util.ArrayList;
6import java.util.Collection;
7import java.util.logging.Level;
8import java.util.logging.Logger;
9
10import biz.hammurapi.config.Context;
11
12/**
13 * Converts one type to another using accessor/constructor combination.
14 * @author Pavel
15 *
16 */
17public class ReflectionConverter implements AtomicConverter {
18 private static final Logger logger = Logger.getLogger(ReflectionConverter.class.getName());
19
20 Method accessor;
21 Constructor constructor;
22 private Class targetType;
23 private Class sourceType;
24 private boolean singleArg;
25
26 /**
27 * @param accessor Source object method to invoke to get target type or
28 * constructor parameter for the target type. If it is null, then source itself
29 * is passed to constructor. One of parameters may be null, but not both.
30 * @param constructor Target class constructor which takes one parameter, either
31 * source type or return type of source type accessor.
32 */
33 public ReflectionConverter(Method accessor, Constructor constructor) {
34 super();
35 this.accessor = accessor;
36 this.constructor = constructor;
37 singleArg = constructor!=null && constructor.getParameterTypes().length==1;
38 if (accessor==null) {
39 sourceType = constructor.getParameterTypes()[0];
40 } else {
41 sourceType = accessor.getDeclaringClass();
42 }
43
44 if (constructor==null) {
45 targetType = accessor.getReturnType();
46 } else {
47 targetType = constructor.getDeclaringClass();
48 }
49 }
50
51 public Object convert(Object source, Converter master, Context context) {
52 try {
53 Object param = accessor==null ? source : accessor.invoke(source, null);
54 if (constructor==null) {
55 return param;
56 }
57
58 if (singleArg) {
59 return constructor.newInstance(new Object[] {param});
60 }
61
62 return constructor.newInstance(new Object[] {param, context});
63 } catch (Exception e) {
64 logger.log(Level.FINE, "Cannot convert "+source.getClass().getName()+" to " + constructor.getDeclaringClass()+": "+e, e);
65 return null;
66 }
67 }
68
69 public Class getSourceType() {
70 return sourceType;
71 }
72
73 public Class getTargetType() {
74 return targetType;
75 }
76
77 /**
78 * Discovers constructor conversions.
79 * @param target Target type
80 * @return Collection of discovered converters.
81 */
82 public static Collection discoverConstructorConverters(Class target) {
83 Collection ret=new ArrayList();
84 if (!target.isInterface()) {
85 for (int i=0, cc=target.getConstructors().length; i<cc; i++) {
86 Constructor constructor = target.getConstructors()[i];
87 Class[] parameterTypes = constructor.getParameterTypes();
88 if (parameterTypes.length==1
89 && !target.isAssignableFrom(parameterTypes[0])) {
90
91 ret.add(new ReflectionConverter(null, constructor));
92 } else if (parameterTypes.length==2
93 && !target.isAssignableFrom(parameterTypes[0])
94 && Context.class.equals(parameterTypes[1])) {
95
96 ret.add(new ReflectionConverter(null, constructor));
97 }
98 }
99 }
100 return ret;
101 }
102
103 public String toString() {
104 return this.getClass().getName() + " ("+sourceType+" -> "+targetType+", accessor: "+accessor+", constructor: "+constructor+")";
105 }
106
107}
108