diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Reflections.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Reflections.java index 658b687..b4ecc27 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Reflections.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Reflections.java @@ -36,6 +36,7 @@ */ package br.gov.frameworkdemoiselle.util; +import java.io.InputStream; import java.lang.reflect.Field; import java.lang.reflect.Member; import java.lang.reflect.Method; @@ -55,24 +56,24 @@ import java.util.List; public class Reflections { protected Reflections() { - //Impede instanciar subclasses desse tipo. + // Impede instanciar subclasses desse tipo. throw new UnsupportedOperationException(); } /** - * Return the parametized type used with a concrete implementation of - * a class that accepts generics. + * Return the parametized type used with a concrete implementation of a class that accepts generics. Ex: If you + * declare * - * Ex: If you declare - *

+	 * 
+	 * 
 	 * public class SpecializedCollection implements Collection {
 	 *   // ...
 	 * }
-	 * 
- * - * then the code getGenericTypeArgument(SpecializedCollection.class , 0); will - * return the type SpecializedType. + *
+ *
* + * then the code getGenericTypeArgument(SpecializedCollection.class , 0); will return the type + * SpecializedType. */ @SuppressWarnings("unchecked") public static Class getGenericTypeArgument(final Class clazz, final int idx) { @@ -89,32 +90,37 @@ public class Reflections { } /** - *

Return the parametized type passed to field types that accepts Generics.

+ *

+ * Return the parametized type passed to field types that accepts Generics. + *

+ *

+ * Ex: If you declare * - *

Ex: If you declare - *


+	 * 
+	 * 
 	 * public class MyClass{
 	 *    private Collection<String> myStringCollection;
 	 * }
-	 * 
- * - * then the code getGenericTypeArgument( MyClass.class.getDeclaredField("myStringCollection") , 0); will - * return the type String. + *
+ *
* + * then the code getGenericTypeArgument( MyClass.class.getDeclaredField("myStringCollection") , 0); + * will return the type String. */ @SuppressWarnings("unchecked") public static Class getGenericTypeArgument(final Field field, final int idx) { final Type type = field.getGenericType(); final ParameterizedType paramType = (ParameterizedType) type; - + return (Class) paramType.getActualTypeArguments()[idx]; } /** - *

Return the parametized type passed to members (fields or methods) that accepts Generics.

+ *

+ * Return the parametized type passed to members (fields or methods) that accepts Generics. + *

* * @see #getGenericTypeArgument(Field field, int idx) - * */ public static Class getGenericTypeArgument(final Member member, final int idx) { Class result = null; @@ -129,10 +135,11 @@ public class Reflections { } /** - *

Return the parametized type passed to methods that accepts Generics.

+ *

+ * Return the parametized type passed to methods that accepts Generics. + *

* * @see #getGenericTypeArgument(Field field, int idx) - * */ @SuppressWarnings("unchecked") public static Class getGenericTypeArgument(final Method method, final int pos) { @@ -140,12 +147,12 @@ public class Reflections { } /** - * Returns the value contained in the given field. + * Returns the value contained in the given field. * * @param field - * field to be extracted the value. + * field to be extracted the value. * @param object - * object that contains the field. + * object that contains the field. * @return value of the field. */ @SuppressWarnings("unchecked") @@ -169,11 +176,11 @@ public class Reflections { * Sets a value in a field. * * @param field - * field to be setted. + * field to be setted. * @param object - * object that contains the field. + * object that contains the field. * @param value - * value to be setted in the field. + * value to be setted in the field. */ public static void setFieldValue(Field field, Object object, Object value) { try { @@ -188,8 +195,8 @@ public class Reflections { } /** - * @return All non static fields from a certain type. Inherited fields are not returned, so if you - * need to get inherited fields you must iterate over this type's hierarchy. + * @return All non static fields from a certain type. Inherited fields are not returned, so if you need to get + * inherited fields you must iterate over this type's hierarchy. */ public static Field[] getNonStaticDeclaredFields(Class type) { List fields = new ArrayList(); @@ -206,14 +213,14 @@ public class Reflections { } /** - * @return All non static fields from a certain type, including fields declared in superclasses of this type. + * @return All non static fields from a certain type, including fields declared in superclasses of this type. */ public static List getNonStaticFields(Class type) { List fields = new ArrayList(); if (type != null) { Class currentType = type; - while(currentType!=null && !"java.lang.Object".equals(currentType.getCanonicalName())){ + while (currentType != null && !"java.lang.Object".equals(currentType.getCanonicalName())) { fields.addAll(Arrays.asList(getNonStaticDeclaredFields(currentType))); currentType = currentType.getSuperclass(); } @@ -223,7 +230,7 @@ public class Reflections { } /** - * Instantiate an object of the given type. The default constructor with no parameters is used. + * Instantiate an object of the given type. The default constructor with no parameters is used. */ public static T instantiate(Class clazz) { T object = null; @@ -238,14 +245,13 @@ public class Reflections { } /** - * Verifies if a given class could be converted to a given type. + * Verifies if a given class could be converted to a given type. * * @param clazz - * class to be checked. + * class to be checked. * @param type - * type to be checked. - * @return {@link Boolean} - * true if the given class can be converted to a given type, and false otherwise. + * type to be checked. + * @return {@link Boolean} true if the given class can be converted to a given type, and false otherwise. */ public static boolean isOfType(Class clazz, Class type) { return type.isAssignableFrom(clazz) && clazz != type; @@ -255,9 +261,8 @@ public class Reflections { * Obtains the {@link ClassLoader} for the given class, from his canonical name. * * @param canonicalName - * canonical name of the the given class. - * @return {@link ClassLoader} - * ClassLoader for the given class. + * canonical name of the the given class. + * @return {@link ClassLoader} ClassLoader for the given class. */ public static ClassLoader getClassLoaderForClass(final String canonicalName) { return Reflections.getClassLoaderForResource(canonicalName.replaceAll("\\.", "/") + ".class"); @@ -267,9 +272,7 @@ public class Reflections { * Obtains the {@link ClassLoader} for the given resource. * * @param resource - * - * @return {@link ClassLoader} - * ClassLoader for the given resource. + * @return {@link ClassLoader} ClassLoader for the given resource. */ public static ClassLoader getClassLoaderForResource(final String resource) { final String stripped = resource.charAt(0) == '/' ? resource.substring(1) : resource; @@ -302,6 +305,14 @@ public class Reflections { } /** + * Return an InputStream to access a resource available to the active classloader for the calling thread. + */ + public static InputStream getResourceAsStream(final String resource) { + ClassLoader classLoader = getClassLoaderForResource(resource); + return classLoader != null ? classLoader.getResourceAsStream(resource) : null; + } + + /** * Loads a class with the given name using the active classloader for the current thread. */ @SuppressWarnings("unchecked") diff --git a/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Strings.java b/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Strings.java index d9d8df1..547dc8c 100644 --- a/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Strings.java +++ b/impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Strings.java @@ -36,6 +36,10 @@ */ package br.gov.frameworkdemoiselle.util; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.lang.reflect.Field; import java.util.Arrays; import java.util.regex.Matcher; @@ -57,10 +61,8 @@ public final class Strings { * Returns if some string matches with the format of a ResourceBundle key or not. * * @param key - * string to check if matches with key format of ResourceBundle. - * - * @return boolean - * true if matches and false otherwise. + * string to check if matches with key format of ResourceBundle. + * @return boolean true if matches and false otherwise. */ public static boolean isResourceBundleKeyFormat(final String key) { return Pattern.matches("^\\{(.+)\\}$", key == null ? "" : key); @@ -70,13 +72,10 @@ public final class Strings { * Removes specific characteres from a given string. * * @param string - * string to be changed, by the removing of some characters. - * + * string to be changed, by the removing of some characters. * @param chars - * characters to be removed from string. - * - * @return String - * returns the given string without the given characters. + * characters to be removed from string. + * @return String returns the given string without the given characters. */ public static String removeChars(String string, char... chars) { String result = string; @@ -89,19 +88,34 @@ public final class Strings { return result; } + public static String join(String separator, String... strings) { + StringBuffer result = new StringBuffer(); + + if (strings != null) { + for (int i = 0; i < strings.length; i++) { + if (i != 0 && separator != null) { + result.append(separator); + } + + if (strings[i] != null) { + result.append(strings[i]); + } + } + } + + return result.length() > 0 ? result.toString() : null; + } + /** - * Inserts the character "0" in the begin of a given string. The quantity of zeros that will be placed - * depends on the difference between the length of the given string and the value of howMuchZeros. + * Inserts the character "0" in the begin of a given string. The quantity of zeros that will be placed depends on + * the difference between the length of the given string and the value of howMuchZeros. * * @param string - * string to insert zeros characthers. - * + * string to insert zeros characthers. * @param howMuchZeros - * its controls how much zeros will be insert. - * - * @return String - * Retuns the string, added with appropriate number of zeros. - * For exemplo, if string = "yes" and howMuchZeros = 5, the returned string will be "00yes". + * its controls how much zeros will be insert. + * @return String Retuns the string, added with appropriate number of zeros. For exemplo, if string = "yes" and + * howMuchZeros = 5, the returned string will be "00yes". */ public static String insertZeros(String string, int howMuchZeros) { StringBuffer result = new StringBuffer((string == null ? "" : string).trim()); @@ -116,19 +130,18 @@ public final class Strings { /** *

- * Replaces the numbers between braces in the given string with the given parameters. - * The process will replace a number between braces for the parameter for which its order - * in the set of parameters matches with the number of the given string. + * Replaces the numbers between braces in the given string with the given parameters. The process will replace a + * number between braces for the parameter for which its order in the set of parameters matches with the number of + * the given string. *

- * For exemple, if is received the following string "Treats an {0} exception" and the set of parameters - * {"DemoiselleException"}, the return will be the following string: "Treats an DemoiselleException exception". + * For exemple, if is received the following string "Treats an {0} exception" and the set of parameters + * {"DemoiselleException"}, the return will be the following string: "Treats an DemoiselleException exception". * - * @param string - * with the numbers with braces to be replaced with the parameters. + * @param string + * with the numbers with braces to be replaced with the parameters. * @param params - * parameters that will replace the number with braces in the given string. - * @return String - * string with numbers replaced with the matching parameter. + * parameters that will replace the number with braces in the given string. + * @return String string with numbers replaced with the matching parameter. */ public static String getString(final String string, final Object... params) { String result = null; @@ -152,23 +165,19 @@ public final class Strings { * Verifies if a given string is empty or null. * * @param string - * string to be verified. - * - * @return boolean - * returns true if the given string is empty or null and returns false otherwise. + * string to be verified. + * @return boolean returns true if the given string is empty or null and returns false otherwise. */ public static boolean isEmpty(String string) { return string == null || string.trim().isEmpty(); } /** - * Converts any object to string. + * Converts any object to string. * * @param object - * object to be converted. - * - * @return String - * the given object converted to string. + * object to be converted. + * @return String the given object converted to string. */ public static String toString(Object object) { StringBuffer result = new StringBuffer(); @@ -202,14 +211,13 @@ public final class Strings { } /** - * Replace the camel case string for a lowercase string separated for a given symbol. + * Replace the camel case string for a lowercase string separated for a given symbol. * * @param string - * string that separeted with camel case. + * string that separeted with camel case. * @param symbol - * simbol to be the new separator for the given string. - * @return String - * the given string separated with the given symbol. + * simbol to be the new separator for the given string. + * @return String the given string separated with the given symbol. */ public static String camelCaseToSymbolSeparated(String string, String symbol) { if (symbol == null) { @@ -220,12 +228,10 @@ public final class Strings { } /** - * Sets the first character of a given string to upper case. + * Sets the first character of a given string to upper case. * * @param string - * - * @return String - * the given string with the first character setted to upper case. + * @return String the given string with the first character setted to upper case. */ public static String firstToUpper(String string) { String result = string; @@ -238,12 +244,10 @@ public final class Strings { } /** - * Removes braces from a given string. + * Removes braces from a given string. * * @param string - * - * @return String - * the given string without braces. + * @return String the given string without braces. */ public static String removeBraces(String string) { String result = string; @@ -256,12 +260,10 @@ public final class Strings { } /** - * Inserts braces in a given string. + * Inserts braces in a given string. * * @param string - * - * @return String - * the given string with braces. + * @return String the given string with braces. */ public static String insertBraces(String string) { String result = string; @@ -272,4 +274,28 @@ public final class Strings { return result; } + + public static String parse(InputStream inputStream) throws IOException { + StringBuilder result = new StringBuilder(); + + if (inputStream != null) { + BufferedReader reader = null; + + try { + reader = new BufferedReader(new InputStreamReader(inputStream)); + String line; + + while ((line = reader.readLine()) != null) { + result.append(line); + } + + } finally { + if (reader != null) { + reader.close(); + } + } + } + + return result.length() > 0 ? result.toString() : null; + } } diff --git a/pom.xml b/pom.xml index f80f309..5c46d1f 100755 --- a/pom.xml +++ b/pom.xml @@ -81,9 +81,9 @@ archetype/jsf-jpa archetype/html-rest documentation/quickstart documentation/reference - --> -- libgit2 0.21.2