Commit 882867ad96543d97c5398878d299424a59625eb0
1 parent
de4bbc5c
Exists in
master
Novos métodos utilitários
Showing
3 changed files
with
136 additions
and
99 deletions
Show diff stats
impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Reflections.java
... | ... | @@ -36,6 +36,7 @@ |
36 | 36 | */ |
37 | 37 | package br.gov.frameworkdemoiselle.util; |
38 | 38 | |
39 | +import java.io.InputStream; | |
39 | 40 | import java.lang.reflect.Field; |
40 | 41 | import java.lang.reflect.Member; |
41 | 42 | import java.lang.reflect.Method; |
... | ... | @@ -55,24 +56,24 @@ import java.util.List; |
55 | 56 | public class Reflections { |
56 | 57 | |
57 | 58 | protected Reflections() { |
58 | - //Impede instanciar subclasses desse tipo. | |
59 | + // Impede instanciar subclasses desse tipo. | |
59 | 60 | throw new UnsupportedOperationException(); |
60 | 61 | } |
61 | 62 | |
62 | 63 | /** |
63 | - * Return the parametized type used with a concrete implementation of | |
64 | - * a class that accepts generics. | |
64 | + * Return the parametized type used with a concrete implementation of a class that accepts generics. Ex: If you | |
65 | + * declare | |
65 | 66 | * |
66 | - * Ex: If you declare | |
67 | - * <pre><code> | |
67 | + * <pre> | |
68 | + * <code> | |
68 | 69 | * public class SpecializedCollection implements Collection<SpecializedType> { |
69 | 70 | * // ... |
70 | 71 | * } |
71 | - * </code></pre> | |
72 | - * | |
73 | - * then the code <code>getGenericTypeArgument(SpecializedCollection.class , 0);</code> will | |
74 | - * return the type <code>SpecializedType</code>. | |
72 | + * </code> | |
73 | + * </pre> | |
75 | 74 | * |
75 | + * then the code <code>getGenericTypeArgument(SpecializedCollection.class , 0);</code> will return the type | |
76 | + * <code>SpecializedType</code>. | |
76 | 77 | */ |
77 | 78 | @SuppressWarnings("unchecked") |
78 | 79 | public static <T> Class<T> getGenericTypeArgument(final Class<?> clazz, final int idx) { |
... | ... | @@ -89,32 +90,37 @@ public class Reflections { |
89 | 90 | } |
90 | 91 | |
91 | 92 | /** |
92 | - * <p>Return the parametized type passed to field types that accepts Generics.</p> | |
93 | + * <p> | |
94 | + * Return the parametized type passed to field types that accepts Generics. | |
95 | + * </p> | |
96 | + * <p> | |
97 | + * Ex: If you declare | |
93 | 98 | * |
94 | - * <p>Ex: If you declare | |
95 | - * <pre><code> | |
99 | + * <pre> | |
100 | + * <code> | |
96 | 101 | * public class MyClass{ |
97 | 102 | * private Collection<String> myStringCollection; |
98 | 103 | * } |
99 | - * </code></pre> | |
100 | - * | |
101 | - * then the code <code>getGenericTypeArgument( MyClass.class.getDeclaredField("myStringCollection") , 0);</code> will | |
102 | - * return the type <code>String</code>. | |
104 | + * </code> | |
105 | + * </pre> | |
103 | 106 | * |
107 | + * then the code <code>getGenericTypeArgument( MyClass.class.getDeclaredField("myStringCollection") , 0);</code> | |
108 | + * will return the type <code>String</code>. | |
104 | 109 | */ |
105 | 110 | @SuppressWarnings("unchecked") |
106 | 111 | public static <T> Class<T> getGenericTypeArgument(final Field field, final int idx) { |
107 | 112 | final Type type = field.getGenericType(); |
108 | 113 | final ParameterizedType paramType = (ParameterizedType) type; |
109 | - | |
114 | + | |
110 | 115 | return (Class<T>) paramType.getActualTypeArguments()[idx]; |
111 | 116 | } |
112 | 117 | |
113 | 118 | /** |
114 | - * <p>Return the parametized type passed to members (fields or methods) that accepts Generics.</p> | |
119 | + * <p> | |
120 | + * Return the parametized type passed to members (fields or methods) that accepts Generics. | |
121 | + * </p> | |
115 | 122 | * |
116 | 123 | * @see #getGenericTypeArgument(Field field, int idx) |
117 | - * | |
118 | 124 | */ |
119 | 125 | public static <T> Class<T> getGenericTypeArgument(final Member member, final int idx) { |
120 | 126 | Class<T> result = null; |
... | ... | @@ -129,10 +135,11 @@ public class Reflections { |
129 | 135 | } |
130 | 136 | |
131 | 137 | /** |
132 | - * <p>Return the parametized type passed to methods that accepts Generics.</p> | |
138 | + * <p> | |
139 | + * Return the parametized type passed to methods that accepts Generics. | |
140 | + * </p> | |
133 | 141 | * |
134 | 142 | * @see #getGenericTypeArgument(Field field, int idx) |
135 | - * | |
136 | 143 | */ |
137 | 144 | @SuppressWarnings("unchecked") |
138 | 145 | public static <T> Class<T> getGenericTypeArgument(final Method method, final int pos) { |
... | ... | @@ -140,12 +147,12 @@ public class Reflections { |
140 | 147 | } |
141 | 148 | |
142 | 149 | /** |
143 | - * Returns the value contained in the given field. | |
150 | + * Returns the value contained in the given field. | |
144 | 151 | * |
145 | 152 | * @param field |
146 | - * field to be extracted the value. | |
153 | + * field to be extracted the value. | |
147 | 154 | * @param object |
148 | - * object that contains the field. | |
155 | + * object that contains the field. | |
149 | 156 | * @return value of the field. |
150 | 157 | */ |
151 | 158 | @SuppressWarnings("unchecked") |
... | ... | @@ -169,11 +176,11 @@ public class Reflections { |
169 | 176 | * Sets a value in a field. |
170 | 177 | * |
171 | 178 | * @param field |
172 | - * field to be setted. | |
179 | + * field to be setted. | |
173 | 180 | * @param object |
174 | - * object that contains the field. | |
181 | + * object that contains the field. | |
175 | 182 | * @param value |
176 | - * value to be setted in the field. | |
183 | + * value to be setted in the field. | |
177 | 184 | */ |
178 | 185 | public static void setFieldValue(Field field, Object object, Object value) { |
179 | 186 | try { |
... | ... | @@ -188,8 +195,8 @@ public class Reflections { |
188 | 195 | } |
189 | 196 | |
190 | 197 | /** |
191 | - * @return All non static fields from a certain type. Inherited fields are not returned, so if you | |
192 | - * need to get inherited fields you must iterate over this type's hierarchy. | |
198 | + * @return All non static fields from a certain type. Inherited fields are not returned, so if you need to get | |
199 | + * inherited fields you must iterate over this type's hierarchy. | |
193 | 200 | */ |
194 | 201 | public static Field[] getNonStaticDeclaredFields(Class<?> type) { |
195 | 202 | List<Field> fields = new ArrayList<Field>(); |
... | ... | @@ -206,14 +213,14 @@ public class Reflections { |
206 | 213 | } |
207 | 214 | |
208 | 215 | /** |
209 | - * @return All non static fields from a certain type, including fields declared in superclasses of this type. | |
216 | + * @return All non static fields from a certain type, including fields declared in superclasses of this type. | |
210 | 217 | */ |
211 | 218 | public static List<Field> getNonStaticFields(Class<?> type) { |
212 | 219 | List<Field> fields = new ArrayList<Field>(); |
213 | 220 | |
214 | 221 | if (type != null) { |
215 | 222 | Class<?> currentType = type; |
216 | - while(currentType!=null && !"java.lang.Object".equals(currentType.getCanonicalName())){ | |
223 | + while (currentType != null && !"java.lang.Object".equals(currentType.getCanonicalName())) { | |
217 | 224 | fields.addAll(Arrays.asList(getNonStaticDeclaredFields(currentType))); |
218 | 225 | currentType = currentType.getSuperclass(); |
219 | 226 | } |
... | ... | @@ -223,7 +230,7 @@ public class Reflections { |
223 | 230 | } |
224 | 231 | |
225 | 232 | /** |
226 | - * Instantiate an object of the given type. The default constructor with no parameters is used. | |
233 | + * Instantiate an object of the given type. The default constructor with no parameters is used. | |
227 | 234 | */ |
228 | 235 | public static <T> T instantiate(Class<T> clazz) { |
229 | 236 | T object = null; |
... | ... | @@ -238,14 +245,13 @@ public class Reflections { |
238 | 245 | } |
239 | 246 | |
240 | 247 | /** |
241 | - * Verifies if a given class could be converted to a given type. | |
248 | + * Verifies if a given class could be converted to a given type. | |
242 | 249 | * |
243 | 250 | * @param clazz |
244 | - * class to be checked. | |
251 | + * class to be checked. | |
245 | 252 | * @param type |
246 | - * type to be checked. | |
247 | - * @return {@link Boolean} | |
248 | - * true if the given class can be converted to a given type, and false otherwise. | |
253 | + * type to be checked. | |
254 | + * @return {@link Boolean} true if the given class can be converted to a given type, and false otherwise. | |
249 | 255 | */ |
250 | 256 | public static boolean isOfType(Class<?> clazz, Class<?> type) { |
251 | 257 | return type.isAssignableFrom(clazz) && clazz != type; |
... | ... | @@ -255,9 +261,8 @@ public class Reflections { |
255 | 261 | * Obtains the {@link ClassLoader} for the given class, from his canonical name. |
256 | 262 | * |
257 | 263 | * @param canonicalName |
258 | - * canonical name of the the given class. | |
259 | - * @return {@link ClassLoader} | |
260 | - * ClassLoader for the given class. | |
264 | + * canonical name of the the given class. | |
265 | + * @return {@link ClassLoader} ClassLoader for the given class. | |
261 | 266 | */ |
262 | 267 | public static ClassLoader getClassLoaderForClass(final String canonicalName) { |
263 | 268 | return Reflections.getClassLoaderForResource(canonicalName.replaceAll("\\.", "/") + ".class"); |
... | ... | @@ -267,9 +272,7 @@ public class Reflections { |
267 | 272 | * Obtains the {@link ClassLoader} for the given resource. |
268 | 273 | * |
269 | 274 | * @param resource |
270 | - * | |
271 | - * @return {@link ClassLoader} | |
272 | - * ClassLoader for the given resource. | |
275 | + * @return {@link ClassLoader} ClassLoader for the given resource. | |
273 | 276 | */ |
274 | 277 | public static ClassLoader getClassLoaderForResource(final String resource) { |
275 | 278 | final String stripped = resource.charAt(0) == '/' ? resource.substring(1) : resource; |
... | ... | @@ -302,6 +305,14 @@ public class Reflections { |
302 | 305 | } |
303 | 306 | |
304 | 307 | /** |
308 | + * Return an InputStream to access a resource available to the active classloader for the calling thread. | |
309 | + */ | |
310 | + public static InputStream getResourceAsStream(final String resource) { | |
311 | + ClassLoader classLoader = getClassLoaderForResource(resource); | |
312 | + return classLoader != null ? classLoader.getResourceAsStream(resource) : null; | |
313 | + } | |
314 | + | |
315 | + /** | |
305 | 316 | * Loads a class with the given name using the active classloader for the current thread. |
306 | 317 | */ |
307 | 318 | @SuppressWarnings("unchecked") | ... | ... |
impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Strings.java
... | ... | @@ -36,6 +36,10 @@ |
36 | 36 | */ |
37 | 37 | package br.gov.frameworkdemoiselle.util; |
38 | 38 | |
39 | +import java.io.BufferedReader; | |
40 | +import java.io.IOException; | |
41 | +import java.io.InputStream; | |
42 | +import java.io.InputStreamReader; | |
39 | 43 | import java.lang.reflect.Field; |
40 | 44 | import java.util.Arrays; |
41 | 45 | import java.util.regex.Matcher; |
... | ... | @@ -57,10 +61,8 @@ public final class Strings { |
57 | 61 | * Returns if some string matches with the format of a ResourceBundle key or not. |
58 | 62 | * |
59 | 63 | * @param key |
60 | - * string to check if matches with key format of ResourceBundle. | |
61 | - * | |
62 | - * @return boolean | |
63 | - * true if matches and false otherwise. | |
64 | + * string to check if matches with key format of ResourceBundle. | |
65 | + * @return boolean true if matches and false otherwise. | |
64 | 66 | */ |
65 | 67 | public static boolean isResourceBundleKeyFormat(final String key) { |
66 | 68 | return Pattern.matches("^\\{(.+)\\}$", key == null ? "" : key); |
... | ... | @@ -70,13 +72,10 @@ public final class Strings { |
70 | 72 | * Removes specific characteres from a given string. |
71 | 73 | * |
72 | 74 | * @param string |
73 | - * string to be changed, by the removing of some characters. | |
74 | - * | |
75 | + * string to be changed, by the removing of some characters. | |
75 | 76 | * @param chars |
76 | - * characters to be removed from string. | |
77 | - * | |
78 | - * @return String | |
79 | - * returns the given string without the given characters. | |
77 | + * characters to be removed from string. | |
78 | + * @return String returns the given string without the given characters. | |
80 | 79 | */ |
81 | 80 | public static String removeChars(String string, char... chars) { |
82 | 81 | String result = string; |
... | ... | @@ -89,19 +88,34 @@ public final class Strings { |
89 | 88 | return result; |
90 | 89 | } |
91 | 90 | |
91 | + public static String join(String separator, String... strings) { | |
92 | + StringBuffer result = new StringBuffer(); | |
93 | + | |
94 | + if (strings != null) { | |
95 | + for (int i = 0; i < strings.length; i++) { | |
96 | + if (i != 0 && separator != null) { | |
97 | + result.append(separator); | |
98 | + } | |
99 | + | |
100 | + if (strings[i] != null) { | |
101 | + result.append(strings[i]); | |
102 | + } | |
103 | + } | |
104 | + } | |
105 | + | |
106 | + return result.length() > 0 ? result.toString() : null; | |
107 | + } | |
108 | + | |
92 | 109 | /** |
93 | - * Inserts the character "0" in the begin of a given string. The quantity of zeros that will be placed | |
94 | - * depends on the difference between the length of the given string and the value of howMuchZeros. | |
110 | + * Inserts the character "0" in the begin of a given string. The quantity of zeros that will be placed depends on | |
111 | + * the difference between the length of the given string and the value of howMuchZeros. | |
95 | 112 | * |
96 | 113 | * @param string |
97 | - * string to insert zeros characthers. | |
98 | - * | |
114 | + * string to insert zeros characthers. | |
99 | 115 | * @param howMuchZeros |
100 | - * its controls how much zeros will be insert. | |
101 | - * | |
102 | - * @return String | |
103 | - * Retuns the string, added with appropriate number of zeros. | |
104 | - * For exemplo, if string = "yes" and howMuchZeros = 5, the returned string will be "00yes". | |
116 | + * its controls how much zeros will be insert. | |
117 | + * @return String Retuns the string, added with appropriate number of zeros. For exemplo, if string = "yes" and | |
118 | + * howMuchZeros = 5, the returned string will be "00yes". | |
105 | 119 | */ |
106 | 120 | public static String insertZeros(String string, int howMuchZeros) { |
107 | 121 | StringBuffer result = new StringBuffer((string == null ? "" : string).trim()); |
... | ... | @@ -116,19 +130,18 @@ public final class Strings { |
116 | 130 | |
117 | 131 | /** |
118 | 132 | * <p> |
119 | - * Replaces the numbers between braces in the given string with the given parameters. | |
120 | - * The process will replace a number between braces for the parameter for which its order | |
121 | - * in the set of parameters matches with the number of the given string. | |
133 | + * Replaces the numbers between braces in the given string with the given parameters. The process will replace a | |
134 | + * number between braces for the parameter for which its order in the set of parameters matches with the number of | |
135 | + * the given string. | |
122 | 136 | * <p> |
123 | - * For exemple, if is received the following string "Treats an {0} exception" and the set of parameters | |
124 | - * {"DemoiselleException"}, the return will be the following string: "Treats an DemoiselleException exception". | |
137 | + * For exemple, if is received the following string "Treats an {0} exception" and the set of parameters | |
138 | + * {"DemoiselleException"}, the return will be the following string: "Treats an DemoiselleException exception". | |
125 | 139 | * |
126 | - * @param string | |
127 | - * with the numbers with braces to be replaced with the parameters. | |
140 | + * @param string | |
141 | + * with the numbers with braces to be replaced with the parameters. | |
128 | 142 | * @param params |
129 | - * parameters that will replace the number with braces in the given string. | |
130 | - * @return String | |
131 | - * string with numbers replaced with the matching parameter. | |
143 | + * parameters that will replace the number with braces in the given string. | |
144 | + * @return String string with numbers replaced with the matching parameter. | |
132 | 145 | */ |
133 | 146 | public static String getString(final String string, final Object... params) { |
134 | 147 | String result = null; |
... | ... | @@ -152,23 +165,19 @@ public final class Strings { |
152 | 165 | * Verifies if a given string is empty or null. |
153 | 166 | * |
154 | 167 | * @param string |
155 | - * string to be verified. | |
156 | - * | |
157 | - * @return boolean | |
158 | - * returns true if the given string is empty or null and returns false otherwise. | |
168 | + * string to be verified. | |
169 | + * @return boolean returns true if the given string is empty or null and returns false otherwise. | |
159 | 170 | */ |
160 | 171 | public static boolean isEmpty(String string) { |
161 | 172 | return string == null || string.trim().isEmpty(); |
162 | 173 | } |
163 | 174 | |
164 | 175 | /** |
165 | - * Converts any object to string. | |
176 | + * Converts any object to string. | |
166 | 177 | * |
167 | 178 | * @param object |
168 | - * object to be converted. | |
169 | - * | |
170 | - * @return String | |
171 | - * the given object converted to string. | |
179 | + * object to be converted. | |
180 | + * @return String the given object converted to string. | |
172 | 181 | */ |
173 | 182 | public static String toString(Object object) { |
174 | 183 | StringBuffer result = new StringBuffer(); |
... | ... | @@ -202,14 +211,13 @@ public final class Strings { |
202 | 211 | } |
203 | 212 | |
204 | 213 | /** |
205 | - * Replace the camel case string for a lowercase string separated for a given symbol. | |
214 | + * Replace the camel case string for a lowercase string separated for a given symbol. | |
206 | 215 | * |
207 | 216 | * @param string |
208 | - * string that separeted with camel case. | |
217 | + * string that separeted with camel case. | |
209 | 218 | * @param symbol |
210 | - * simbol to be the new separator for the given string. | |
211 | - * @return String | |
212 | - * the given string separated with the given symbol. | |
219 | + * simbol to be the new separator for the given string. | |
220 | + * @return String the given string separated with the given symbol. | |
213 | 221 | */ |
214 | 222 | public static String camelCaseToSymbolSeparated(String string, String symbol) { |
215 | 223 | if (symbol == null) { |
... | ... | @@ -220,12 +228,10 @@ public final class Strings { |
220 | 228 | } |
221 | 229 | |
222 | 230 | /** |
223 | - * Sets the first character of a given string to upper case. | |
231 | + * Sets the first character of a given string to upper case. | |
224 | 232 | * |
225 | 233 | * @param string |
226 | - * | |
227 | - * @return String | |
228 | - * the given string with the first character setted to upper case. | |
234 | + * @return String the given string with the first character setted to upper case. | |
229 | 235 | */ |
230 | 236 | public static String firstToUpper(String string) { |
231 | 237 | String result = string; |
... | ... | @@ -238,12 +244,10 @@ public final class Strings { |
238 | 244 | } |
239 | 245 | |
240 | 246 | /** |
241 | - * Removes braces from a given string. | |
247 | + * Removes braces from a given string. | |
242 | 248 | * |
243 | 249 | * @param string |
244 | - * | |
245 | - * @return String | |
246 | - * the given string without braces. | |
250 | + * @return String the given string without braces. | |
247 | 251 | */ |
248 | 252 | public static String removeBraces(String string) { |
249 | 253 | String result = string; |
... | ... | @@ -256,12 +260,10 @@ public final class Strings { |
256 | 260 | } |
257 | 261 | |
258 | 262 | /** |
259 | - * Inserts braces in a given string. | |
263 | + * Inserts braces in a given string. | |
260 | 264 | * |
261 | 265 | * @param string |
262 | - * | |
263 | - * @return String | |
264 | - * the given string with braces. | |
266 | + * @return String the given string with braces. | |
265 | 267 | */ |
266 | 268 | public static String insertBraces(String string) { |
267 | 269 | String result = string; |
... | ... | @@ -272,4 +274,28 @@ public final class Strings { |
272 | 274 | |
273 | 275 | return result; |
274 | 276 | } |
277 | + | |
278 | + public static String parse(InputStream inputStream) throws IOException { | |
279 | + StringBuilder result = new StringBuilder(); | |
280 | + | |
281 | + if (inputStream != null) { | |
282 | + BufferedReader reader = null; | |
283 | + | |
284 | + try { | |
285 | + reader = new BufferedReader(new InputStreamReader(inputStream)); | |
286 | + String line; | |
287 | + | |
288 | + while ((line = reader.readLine()) != null) { | |
289 | + result.append(line); | |
290 | + } | |
291 | + | |
292 | + } finally { | |
293 | + if (reader != null) { | |
294 | + reader.close(); | |
295 | + } | |
296 | + } | |
297 | + } | |
298 | + | |
299 | + return result.length() > 0 ? result.toString() : null; | |
300 | + } | |
275 | 301 | } | ... | ... |
pom.xml