|
17 | 17 | */ |
18 | 18 | package com.github.jknack.handlebars.context; |
19 | 19 |
|
| 20 | +import com.github.jknack.handlebars.ValueResolver; |
| 21 | + |
20 | 22 | import java.lang.reflect.Method; |
21 | 23 | import java.util.Collection; |
22 | 24 |
|
23 | | -import com.github.jknack.handlebars.ValueResolver; |
24 | | - |
25 | 25 | /** |
26 | 26 | * A JavaBean method value resolver. |
27 | 27 | * |
|
30 | 30 | */ |
31 | 31 | public class JavaBeanValueResolver extends MethodValueResolver { |
32 | 32 |
|
33 | | - /** |
34 | | - * The 'is' prefix. |
35 | | - */ |
36 | | - private static final String IS_PREFIX = "is"; |
| 33 | + /** |
| 34 | + * The 'is' prefix. |
| 35 | + */ |
| 36 | + private static final String IS_PREFIX = "is"; |
37 | 37 |
|
38 | | - /** |
39 | | - * The 'get' prefix. |
40 | | - */ |
41 | | - private static final String GET_PREFIX = "get"; |
| 38 | + /** |
| 39 | + * The 'get' prefix. |
| 40 | + */ |
| 41 | + private static final String GET_PREFIX = "get"; |
42 | 42 |
|
43 | | - /** |
44 | | - * The default value resolver. |
45 | | - */ |
46 | | - public static final ValueResolver INSTANCE = new JavaBeanValueResolver(); |
| 43 | + /** |
| 44 | + * The default value resolver. |
| 45 | + */ |
| 46 | + public static final ValueResolver INSTANCE = new JavaBeanValueResolver(); |
47 | 47 |
|
48 | | - @Override |
49 | | - public boolean matches(final Method method, final String name) { |
50 | | - if (method.getName().equals("size") && name.equals("length")) { |
51 | | - boolean isCollection = isCollectionMethod(method); |
52 | | - if (isCollection) { |
53 | | - return true; |
54 | | - } |
55 | | - } |
| 48 | + @Override |
| 49 | + public boolean matches(final Method method, final String name) { |
| 50 | + if (name.equals("length") && method.getName().equals("size")) { |
| 51 | + boolean isCollection = isCollectionMethod(method); |
| 52 | + if (isCollection) { |
| 53 | + return true; |
| 54 | + } |
| 55 | + } |
56 | 56 |
|
57 | | - boolean isStatic = isStatic(method); |
58 | | - boolean isPublic = isPublic(method); |
59 | | - boolean isGet = method.getName().equals(javaBeanMethod(GET_PREFIX, name)); |
60 | | - boolean isBoolGet = method.getName().equals(javaBeanMethod(IS_PREFIX, name)); |
61 | | - int parameterCount = method.getParameterTypes().length; |
| 57 | + boolean isStatic = isStatic(method); |
| 58 | + boolean isPublic = isPublic(method); |
| 59 | + boolean isGet = method.getName().equals(javaBeanMethod(GET_PREFIX, name)); |
| 60 | + boolean isBoolGet = method.getName().equals(javaBeanMethod(IS_PREFIX, name)); |
| 61 | + int parameterCount = method.getParameterTypes().length; |
62 | 62 |
|
63 | | - return !isStatic && isPublic && parameterCount == 0 && (isGet || isBoolGet); |
64 | | - } |
| 63 | + return !isStatic && isPublic && parameterCount == 0 && (isGet || isBoolGet); |
| 64 | + } |
65 | 65 |
|
66 | | - /** |
67 | | - * Convert the property's name to a JavaBean read method name. |
68 | | - * |
69 | | - * @param prefix The prefix: 'get' or 'is'. |
70 | | - * @param name The unqualified property name. |
71 | | - * @return The javaBean method name. |
72 | | - */ |
73 | | - private static String javaBeanMethod(final String prefix, |
74 | | - final String name) { |
75 | | - StringBuilder buffer = new StringBuilder(prefix); |
76 | | - buffer.append(name); |
77 | | - buffer.setCharAt(prefix.length(), Character.toUpperCase(name.charAt(0))); |
78 | | - return buffer.toString(); |
79 | | - } |
| 66 | + /** |
| 67 | + * Convert the property's name to a JavaBean read method name. |
| 68 | + * |
| 69 | + * @param prefix The prefix: 'get' or 'is'. |
| 70 | + * @param name The unqualified property name. |
| 71 | + * @return The javaBean method name. |
| 72 | + */ |
| 73 | + private static String javaBeanMethod(final String prefix, |
| 74 | + final String name) { |
| 75 | + StringBuilder buffer = new StringBuilder(prefix); |
| 76 | + buffer.append(name); |
| 77 | + buffer.setCharAt(prefix.length(), Character.toUpperCase(name.charAt(0))); |
| 78 | + return buffer.toString(); |
| 79 | + } |
80 | 80 |
|
81 | | - @Override |
82 | | - protected String memberName(final Method member) { |
83 | | - if (member.getName().equals("size")) { |
84 | | - boolean isCollection = isCollectionMethod(member); |
| 81 | + @Override |
| 82 | + protected String memberName(final Method member) { |
| 83 | + if (member.getName().equals("size")) { |
| 84 | + boolean isCollection = isCollectionMethod(member); |
85 | 85 |
|
86 | | - if (isCollection) { |
87 | | - return "length"; |
88 | | - } |
89 | | - } |
| 86 | + if (isCollection) { |
| 87 | + return "length"; |
| 88 | + } |
| 89 | + } |
90 | 90 |
|
91 | | - String name = member.getName(); |
92 | | - if (name.startsWith(GET_PREFIX)) { |
93 | | - name = name.substring(GET_PREFIX.length()); |
94 | | - } else if (name.startsWith(IS_PREFIX)) { |
95 | | - name = name.substring(IS_PREFIX.length()); |
96 | | - } else { |
97 | | - return name; |
98 | | - } |
99 | | - if (name.length() > 0) { |
100 | | - return Character.toLowerCase(name.charAt(0)) + name.substring(1); |
101 | | - } |
102 | | - return member.getName(); |
| 91 | + String name = member.getName(); |
| 92 | + if (name.startsWith(GET_PREFIX)) { |
| 93 | + name = name.substring(GET_PREFIX.length()); |
| 94 | + } else if (name.startsWith(IS_PREFIX)) { |
| 95 | + name = name.substring(IS_PREFIX.length()); |
| 96 | + } else { |
| 97 | + return name; |
| 98 | + } |
| 99 | + if (name.length() > 0) { |
| 100 | + return Character.toLowerCase(name.charAt(0)) + name.substring(1); |
103 | 101 | } |
| 102 | + return member.getName(); |
| 103 | + } |
104 | 104 |
|
105 | | - /** |
106 | | - * Check is method class implements Collection interface. |
107 | | - * |
108 | | - * @param method from class |
109 | | - * @return true/false |
110 | | - */ |
111 | | - private boolean isCollectionMethod(final Method method) { |
112 | | - for (Class clazz : method.getDeclaringClass().getInterfaces()) { |
113 | | - if (Collection.class.equals(clazz)) { |
114 | | - return true; |
115 | | - } |
116 | | - } |
117 | | - return false; |
| 105 | + /** |
| 106 | + * Check is method class implements Collection interface. |
| 107 | + * |
| 108 | + * @param method from class |
| 109 | + * @return true/false |
| 110 | + */ |
| 111 | + private boolean isCollectionMethod(final Method method) { |
| 112 | + for (Class clazz : method.getDeclaringClass().getInterfaces()) { |
| 113 | + if (Collection.class.equals(clazz)) { |
| 114 | + return true; |
| 115 | + } |
118 | 116 | } |
| 117 | + return false; |
| 118 | + } |
119 | 119 | } |
0 commit comments