Commit 3cd30a8f945fa0028bc97fbec9336dbc3250b93e

Authored by Emerson Oliveira
1 parent d41c1132
Exists in master

Adição de javadoc à classe br.gov.frameworkdemoiselle.util.Reflections.

Ainda não está completo.
impl/core/src/main/java/br/gov/frameworkdemoiselle/util/Reflections.java
@@ -47,11 +47,19 @@ import java.util.ArrayList; @@ -47,11 +47,19 @@ import java.util.ArrayList;
47 import java.util.Arrays; 47 import java.util.Arrays;
48 import java.util.List; 48 import java.util.List;
49 49
  50 +/**
  51 + * Provides some features to do some operations relating to java reflection.
  52 + *
  53 + * @author SERPRO
  54 + */
50 public final class Reflections { 55 public final class Reflections {
51 56
52 private Reflections() { 57 private Reflections() {
53 } 58 }
54 59
  60 + /**
  61 + * TODO
  62 + */
55 @SuppressWarnings("unchecked") 63 @SuppressWarnings("unchecked")
56 public static <T> Class<T> getGenericTypeArgument(final Class<?> clazz, final int idx) { 64 public static <T> Class<T> getGenericTypeArgument(final Class<?> clazz, final int idx) {
57 final Type type = clazz.getGenericSuperclass(); 65 final Type type = clazz.getGenericSuperclass();
@@ -66,6 +74,9 @@ public final class Reflections { @@ -66,6 +74,9 @@ public final class Reflections {
66 return (Class<T>) paramType.getActualTypeArguments()[idx]; 74 return (Class<T>) paramType.getActualTypeArguments()[idx];
67 } 75 }
68 76
  77 + /**
  78 + * TODO
  79 + */
69 @SuppressWarnings("unchecked") 80 @SuppressWarnings("unchecked")
70 public static <T> Class<T> getGenericTypeArgument(final Field field, final int idx) { 81 public static <T> Class<T> getGenericTypeArgument(final Field field, final int idx) {
71 final Type type = field.getGenericType(); 82 final Type type = field.getGenericType();
@@ -74,6 +85,9 @@ public final class Reflections { @@ -74,6 +85,9 @@ public final class Reflections {
74 return (Class<T>) paramType.getActualTypeArguments()[idx]; 85 return (Class<T>) paramType.getActualTypeArguments()[idx];
75 } 86 }
76 87
  88 + /**
  89 + * TODO
  90 + */
77 public static <T> Class<T> getGenericTypeArgument(final Member member, final int idx) { 91 public static <T> Class<T> getGenericTypeArgument(final Member member, final int idx) {
78 Class<T> result = null; 92 Class<T> result = null;
79 93
@@ -86,11 +100,23 @@ public final class Reflections { @@ -86,11 +100,23 @@ public final class Reflections {
86 return result; 100 return result;
87 } 101 }
88 102
  103 + /**
  104 + * TODO
  105 + */
89 @SuppressWarnings("unchecked") 106 @SuppressWarnings("unchecked")
90 public static <T> Class<T> getGenericTypeArgument(final Method method, final int pos) { 107 public static <T> Class<T> getGenericTypeArgument(final Method method, final int pos) {
91 return (Class<T>) method.getGenericParameterTypes()[pos]; 108 return (Class<T>) method.getGenericParameterTypes()[pos];
92 } 109 }
93 110
  111 + /**
  112 + * Returns the value contained in the given field.
  113 + *
  114 + * @param field
  115 + * field to be extracted the value.
  116 + * @param object
  117 + * object that contains the field.
  118 + * @return value of the field.
  119 + */
94 @SuppressWarnings("unchecked") 120 @SuppressWarnings("unchecked")
95 public static <T> T getFieldValue(Field field, Object object) { 121 public static <T> T getFieldValue(Field field, Object object) {
96 T result = null; 122 T result = null;
@@ -108,6 +134,16 @@ public final class Reflections { @@ -108,6 +134,16 @@ public final class Reflections {
108 return result; 134 return result;
109 } 135 }
110 136
  137 + /**
  138 + * Sets a value in a field.
  139 + *
  140 + * @param field
  141 + * field to be setted.
  142 + * @param object
  143 + * object that contains the field.
  144 + * @param value
  145 + * value to be setted in the field.
  146 + */
111 public static void setFieldValue(Field field, Object object, Object value) { 147 public static void setFieldValue(Field field, Object object, Object value) {
112 try { 148 try {
113 boolean acessible = field.isAccessible(); 149 boolean acessible = field.isAccessible();
@@ -120,6 +156,9 @@ public final class Reflections { @@ -120,6 +156,9 @@ public final class Reflections {
120 } 156 }
121 } 157 }
122 158
  159 + /**
  160 + * TODO
  161 + */
123 public static Field[] getNonStaticDeclaredFields(Class<?> type) { 162 public static Field[] getNonStaticDeclaredFields(Class<?> type) {
124 List<Field> fields = new ArrayList<Field>(); 163 List<Field> fields = new ArrayList<Field>();
125 164
@@ -134,6 +173,9 @@ public final class Reflections { @@ -134,6 +173,9 @@ public final class Reflections {
134 return fields.toArray(new Field[0]); 173 return fields.toArray(new Field[0]);
135 } 174 }
136 175
  176 + /**
  177 + * TODO
  178 + */
137 public static List<Field> getNonStaticFields(Class<?> type) { 179 public static List<Field> getNonStaticFields(Class<?> type) {
138 List<Field> fields = new ArrayList<Field>(); 180 List<Field> fields = new ArrayList<Field>();
139 181
@@ -145,10 +187,13 @@ public final class Reflections { @@ -145,10 +187,13 @@ public final class Reflections {
145 return fields; 187 return fields;
146 } 188 }
147 189
148 - public static <T> T instantiate(Class<T> clasz) { 190 + /**
  191 + * TODO
  192 + */
  193 + public static <T> T instantiate(Class<T> clazz) {
149 T object = null; 194 T object = null;
150 try { 195 try {
151 - object = clasz.newInstance(); 196 + object = clazz.newInstance();
152 } catch (InstantiationException e) { 197 } catch (InstantiationException e) {
153 Exceptions.handleToRuntimeException(e); 198 Exceptions.handleToRuntimeException(e);
154 } catch (IllegalAccessException e) { 199 } catch (IllegalAccessException e) {
@@ -157,14 +202,40 @@ public final class Reflections { @@ -157,14 +202,40 @@ public final class Reflections {
157 return object; 202 return object;
158 } 203 }
159 204
  205 + /**
  206 + * Verifies if a given class could be converted to a given type.
  207 + *
  208 + * @param clazz
  209 + * class to be checked.
  210 + * @param type
  211 + * type to be checked.
  212 + * @return {@link Boolean}
  213 + * true if the given class can be converted to a given type, and false otherwise.
  214 + */
160 public static boolean isOfType(Class<?> clazz, Class<?> type) { 215 public static boolean isOfType(Class<?> clazz, Class<?> type) {
161 return type.isAssignableFrom(clazz) && clazz != type; 216 return type.isAssignableFrom(clazz) && clazz != type;
162 } 217 }
163 218
  219 + /**
  220 + * Obtains the {@link ClassLoader} for the given class, from his canonical name.
  221 + *
  222 + * @param canonicalName
  223 + * canonical name of the the given class.
  224 + * @return {@link ClassLoader}
  225 + * ClassLoader for the given class.
  226 + */
164 public static ClassLoader getClassLoaderForClass(final String canonicalName) { 227 public static ClassLoader getClassLoaderForClass(final String canonicalName) {
165 return Reflections.getClassLoaderForResource(canonicalName.replaceAll("\\.", "/") + ".class"); 228 return Reflections.getClassLoaderForResource(canonicalName.replaceAll("\\.", "/") + ".class");
166 } 229 }
167 230
  231 + /**
  232 + * Obtains the {@link ClassLoader} for the given resource.
  233 + *
  234 + * @param resource
  235 + *
  236 + * @return {@link ClassLoader}
  237 + * ClassLoader for the given resource.
  238 + */
168 public static ClassLoader getClassLoaderForResource(final String resource) { 239 public static ClassLoader getClassLoaderForResource(final String resource) {
169 final String stripped = resource.charAt(0) == '/' ? resource.substring(1) : resource; 240 final String stripped = resource.charAt(0) == '/' ? resource.substring(1) : resource;
170 241
@@ -187,11 +258,17 @@ public final class Reflections { @@ -187,11 +258,17 @@ public final class Reflections {
187 return result; 258 return result;
188 } 259 }
189 260
  261 + /**
  262 + * TODO
  263 + */
190 public static URL getResourceAsURL(final String resource) { 264 public static URL getResourceAsURL(final String resource) {
191 ClassLoader classLoader = getClassLoaderForResource(resource); 265 ClassLoader classLoader = getClassLoaderForResource(resource);
192 return classLoader != null ? classLoader.getResource(resource) : null; 266 return classLoader != null ? classLoader.getResource(resource) : null;
193 } 267 }
194 268
  269 + /**
  270 + * TODO
  271 + */
195 @SuppressWarnings("unchecked") 272 @SuppressWarnings("unchecked")
196 public static <T> Class<T> forName(final String className) throws ClassNotFoundException { 273 public static <T> Class<T> forName(final String className) throws ClassNotFoundException {
197 ClassLoader classLoader = getClassLoaderForClass(className); 274 ClassLoader classLoader = getClassLoaderForClass(className);