Commit 882867ad96543d97c5398878d299424a59625eb0

Authored by Cleverson Sacramento
1 parent de4bbc5c
Exists in master

Novos métodos utilitários

impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Reflections.java
@@ -36,6 +36,7 @@ @@ -36,6 +36,7 @@
36 */ 36 */
37 package br.gov.frameworkdemoiselle.util; 37 package br.gov.frameworkdemoiselle.util;
38 38
  39 +import java.io.InputStream;
39 import java.lang.reflect.Field; 40 import java.lang.reflect.Field;
40 import java.lang.reflect.Member; 41 import java.lang.reflect.Member;
41 import java.lang.reflect.Method; 42 import java.lang.reflect.Method;
@@ -55,24 +56,24 @@ import java.util.List; @@ -55,24 +56,24 @@ import java.util.List;
55 public class Reflections { 56 public class Reflections {
56 57
57 protected Reflections() { 58 protected Reflections() {
58 - //Impede instanciar subclasses desse tipo. 59 + // Impede instanciar subclasses desse tipo.
59 throw new UnsupportedOperationException(); 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 * public class SpecializedCollection implements Collection<SpecializedType> { 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 @SuppressWarnings("unchecked") 78 @SuppressWarnings("unchecked")
78 public static <T> Class<T> getGenericTypeArgument(final Class<?> clazz, final int idx) { 79 public static <T> Class<T> getGenericTypeArgument(final Class<?> clazz, final int idx) {
@@ -89,32 +90,37 @@ public class Reflections { @@ -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 * public class MyClass{ 101 * public class MyClass{
97 * private Collection&lt;String&gt; myStringCollection; 102 * private Collection&lt;String&gt; 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 @SuppressWarnings("unchecked") 110 @SuppressWarnings("unchecked")
106 public static <T> Class<T> getGenericTypeArgument(final Field field, final int idx) { 111 public static <T> Class<T> getGenericTypeArgument(final Field field, final int idx) {
107 final Type type = field.getGenericType(); 112 final Type type = field.getGenericType();
108 final ParameterizedType paramType = (ParameterizedType) type; 113 final ParameterizedType paramType = (ParameterizedType) type;
109 - 114 +
110 return (Class<T>) paramType.getActualTypeArguments()[idx]; 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 * @see #getGenericTypeArgument(Field field, int idx) 123 * @see #getGenericTypeArgument(Field field, int idx)
117 - *  
118 */ 124 */
119 public static <T> Class<T> getGenericTypeArgument(final Member member, final int idx) { 125 public static <T> Class<T> getGenericTypeArgument(final Member member, final int idx) {
120 Class<T> result = null; 126 Class<T> result = null;
@@ -129,10 +135,11 @@ public class Reflections { @@ -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 * @see #getGenericTypeArgument(Field field, int idx) 142 * @see #getGenericTypeArgument(Field field, int idx)
135 - *  
136 */ 143 */
137 @SuppressWarnings("unchecked") 144 @SuppressWarnings("unchecked")
138 public static <T> Class<T> getGenericTypeArgument(final Method method, final int pos) { 145 public static <T> Class<T> getGenericTypeArgument(final Method method, final int pos) {
@@ -140,12 +147,12 @@ public class Reflections { @@ -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 * @param field 152 * @param field
146 - * field to be extracted the value. 153 + * field to be extracted the value.
147 * @param object 154 * @param object
148 - * object that contains the field. 155 + * object that contains the field.
149 * @return value of the field. 156 * @return value of the field.
150 */ 157 */
151 @SuppressWarnings("unchecked") 158 @SuppressWarnings("unchecked")
@@ -169,11 +176,11 @@ public class Reflections { @@ -169,11 +176,11 @@ public class Reflections {
169 * Sets a value in a field. 176 * Sets a value in a field.
170 * 177 *
171 * @param field 178 * @param field
172 - * field to be setted. 179 + * field to be setted.
173 * @param object 180 * @param object
174 - * object that contains the field. 181 + * object that contains the field.
175 * @param value 182 * @param value
176 - * value to be setted in the field. 183 + * value to be setted in the field.
177 */ 184 */
178 public static void setFieldValue(Field field, Object object, Object value) { 185 public static void setFieldValue(Field field, Object object, Object value) {
179 try { 186 try {
@@ -188,8 +195,8 @@ public class Reflections { @@ -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 public static Field[] getNonStaticDeclaredFields(Class<?> type) { 201 public static Field[] getNonStaticDeclaredFields(Class<?> type) {
195 List<Field> fields = new ArrayList<Field>(); 202 List<Field> fields = new ArrayList<Field>();
@@ -206,14 +213,14 @@ public class Reflections { @@ -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 public static List<Field> getNonStaticFields(Class<?> type) { 218 public static List<Field> getNonStaticFields(Class<?> type) {
212 List<Field> fields = new ArrayList<Field>(); 219 List<Field> fields = new ArrayList<Field>();
213 220
214 if (type != null) { 221 if (type != null) {
215 Class<?> currentType = type; 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 fields.addAll(Arrays.asList(getNonStaticDeclaredFields(currentType))); 224 fields.addAll(Arrays.asList(getNonStaticDeclaredFields(currentType)));
218 currentType = currentType.getSuperclass(); 225 currentType = currentType.getSuperclass();
219 } 226 }
@@ -223,7 +230,7 @@ public class Reflections { @@ -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 public static <T> T instantiate(Class<T> clazz) { 235 public static <T> T instantiate(Class<T> clazz) {
229 T object = null; 236 T object = null;
@@ -238,14 +245,13 @@ public class Reflections { @@ -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 * @param clazz 250 * @param clazz
244 - * class to be checked. 251 + * class to be checked.
245 * @param type 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 public static boolean isOfType(Class<?> clazz, Class<?> type) { 256 public static boolean isOfType(Class<?> clazz, Class<?> type) {
251 return type.isAssignableFrom(clazz) && clazz != type; 257 return type.isAssignableFrom(clazz) && clazz != type;
@@ -255,9 +261,8 @@ public class Reflections { @@ -255,9 +261,8 @@ public class Reflections {
255 * Obtains the {@link ClassLoader} for the given class, from his canonical name. 261 * Obtains the {@link ClassLoader} for the given class, from his canonical name.
256 * 262 *
257 * @param canonicalName 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 public static ClassLoader getClassLoaderForClass(final String canonicalName) { 267 public static ClassLoader getClassLoaderForClass(final String canonicalName) {
263 return Reflections.getClassLoaderForResource(canonicalName.replaceAll("\\.", "/") + ".class"); 268 return Reflections.getClassLoaderForResource(canonicalName.replaceAll("\\.", "/") + ".class");
@@ -267,9 +272,7 @@ public class Reflections { @@ -267,9 +272,7 @@ public class Reflections {
267 * Obtains the {@link ClassLoader} for the given resource. 272 * Obtains the {@link ClassLoader} for the given resource.
268 * 273 *
269 * @param resource 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 public static ClassLoader getClassLoaderForResource(final String resource) { 277 public static ClassLoader getClassLoaderForResource(final String resource) {
275 final String stripped = resource.charAt(0) == '/' ? resource.substring(1) : resource; 278 final String stripped = resource.charAt(0) == '/' ? resource.substring(1) : resource;
@@ -302,6 +305,14 @@ public class Reflections { @@ -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 * Loads a class with the given name using the active classloader for the current thread. 316 * Loads a class with the given name using the active classloader for the current thread.
306 */ 317 */
307 @SuppressWarnings("unchecked") 318 @SuppressWarnings("unchecked")
impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Strings.java
@@ -36,6 +36,10 @@ @@ -36,6 +36,10 @@
36 */ 36 */
37 package br.gov.frameworkdemoiselle.util; 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 import java.lang.reflect.Field; 43 import java.lang.reflect.Field;
40 import java.util.Arrays; 44 import java.util.Arrays;
41 import java.util.regex.Matcher; 45 import java.util.regex.Matcher;
@@ -57,10 +61,8 @@ public final class Strings { @@ -57,10 +61,8 @@ public final class Strings {
57 * Returns if some string matches with the format of a ResourceBundle key or not. 61 * Returns if some string matches with the format of a ResourceBundle key or not.
58 * 62 *
59 * @param key 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 public static boolean isResourceBundleKeyFormat(final String key) { 67 public static boolean isResourceBundleKeyFormat(final String key) {
66 return Pattern.matches("^\\{(.+)\\}$", key == null ? "" : key); 68 return Pattern.matches("^\\{(.+)\\}$", key == null ? "" : key);
@@ -70,13 +72,10 @@ public final class Strings { @@ -70,13 +72,10 @@ public final class Strings {
70 * Removes specific characteres from a given string. 72 * Removes specific characteres from a given string.
71 * 73 *
72 * @param string 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 * @param chars 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 public static String removeChars(String string, char... chars) { 80 public static String removeChars(String string, char... chars) {
82 String result = string; 81 String result = string;
@@ -89,19 +88,34 @@ public final class Strings { @@ -89,19 +88,34 @@ public final class Strings {
89 return result; 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 * @param string 113 * @param string
97 - * string to insert zeros characthers.  
98 - * 114 + * string to insert zeros characthers.
99 * @param howMuchZeros 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 public static String insertZeros(String string, int howMuchZeros) { 120 public static String insertZeros(String string, int howMuchZeros) {
107 StringBuffer result = new StringBuffer((string == null ? "" : string).trim()); 121 StringBuffer result = new StringBuffer((string == null ? "" : string).trim());
@@ -116,19 +130,18 @@ public final class Strings { @@ -116,19 +130,18 @@ public final class Strings {
116 130
117 /** 131 /**
118 * <p> 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 * <p> 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 * @param params 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 public static String getString(final String string, final Object... params) { 146 public static String getString(final String string, final Object... params) {
134 String result = null; 147 String result = null;
@@ -152,23 +165,19 @@ public final class Strings { @@ -152,23 +165,19 @@ public final class Strings {
152 * Verifies if a given string is empty or null. 165 * Verifies if a given string is empty or null.
153 * 166 *
154 * @param string 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 public static boolean isEmpty(String string) { 171 public static boolean isEmpty(String string) {
161 return string == null || string.trim().isEmpty(); 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 * @param object 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 public static String toString(Object object) { 182 public static String toString(Object object) {
174 StringBuffer result = new StringBuffer(); 183 StringBuffer result = new StringBuffer();
@@ -202,14 +211,13 @@ public final class Strings { @@ -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 * @param string 216 * @param string
208 - * string that separeted with camel case. 217 + * string that separeted with camel case.
209 * @param symbol 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 public static String camelCaseToSymbolSeparated(String string, String symbol) { 222 public static String camelCaseToSymbolSeparated(String string, String symbol) {
215 if (symbol == null) { 223 if (symbol == null) {
@@ -220,12 +228,10 @@ public final class Strings { @@ -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 * @param string 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 public static String firstToUpper(String string) { 236 public static String firstToUpper(String string) {
231 String result = string; 237 String result = string;
@@ -238,12 +244,10 @@ public final class Strings { @@ -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 * @param string 249 * @param string
244 - *  
245 - * @return String  
246 - * the given string without braces. 250 + * @return String the given string without braces.
247 */ 251 */
248 public static String removeBraces(String string) { 252 public static String removeBraces(String string) {
249 String result = string; 253 String result = string;
@@ -256,12 +260,10 @@ public final class Strings { @@ -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 * @param string 265 * @param string
262 - *  
263 - * @return String  
264 - * the given string with braces. 266 + * @return String the given string with braces.
265 */ 267 */
266 public static String insertBraces(String string) { 268 public static String insertBraces(String string) {
267 String result = string; 269 String result = string;
@@ -272,4 +274,28 @@ public final class Strings { @@ -272,4 +274,28 @@ public final class Strings {
272 274
273 return result; 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 }
@@ -81,9 +81,9 @@ @@ -81,9 +81,9 @@
81 <module>archetype/jsf-jpa</module> 81 <module>archetype/jsf-jpa</module>
82 <module>archetype/html-rest</module> 82 <module>archetype/html-rest</module>
83 <!-- 83 <!--
  84 + -->
84 <module>documentation/quickstart</module> 85 <module>documentation/quickstart</module>
85 <module>documentation/reference</module> 86 <module>documentation/reference</module>
86 - -->  
87 </modules> 87 </modules>
88 88
89 <build> 89 <build>