Modified:
/trunk/dependency-shot-core/src/main/java/cx/ath/mancel01/dependencyshot/injection/handlers/CircularConstructorHandler.java
/trunk/dependency-shot-core/src/main/java/cx/ath/mancel01/dependencyshot/injection/handlers/ConstructorHandler.java
/trunk/dependency-shot-core/src/main/java/cx/ath/mancel01/dependencyshot/injection/handlers/FieldsHandler.java
/trunk/dependency-shot-core/src/main/java/cx/ath/mancel01/dependencyshot/injection/handlers/MethodHandler.java
/trunk/dependency-shot-core/src/main/java/cx/ath/mancel01/dependencyshot/scope/RequestScope.java
/trunk/dependency-shot-core/src/main/java/cx/ath/mancel01/dependencyshot/util/ReflectionUtil.java
=======================================
---
/trunk/dependency-shot-core/src/main/java/cx/ath/mancel01/dependencyshot/injection/handlers/CircularConstructorHandler.java
Thu Mar 17 12:21:53 2011
+++
/trunk/dependency-shot-core/src/main/java/cx/ath/mancel01/dependencyshot/injection/handlers/CircularConstructorHandler.java
Thu Mar 17 12:22:20 2011
@@ -16,8 +16,8 @@
*/
package cx.ath.mancel01.dependencyshot.injection.handlers;
-import cx.ath.mancel01.dependencyshot.exceptions.DSException;
import cx.ath.mancel01.dependencyshot.exceptions.ExceptionManager;
+import cx.ath.mancel01.dependencyshot.util.ReflectionUtil;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
@@ -74,20 +74,7 @@
for (int j = 0; j < parameterTypes.length; j++) {
arguments[j] = possibleInstances.get((Class<?>)
parameterTypes[j]);
}
- boolean accessible = constructor.isAccessible();
- // if the constructor is private, then put it public for
newinstance creation
- if (!accessible) {
- constructor.setAccessible(true);
- }
- // create new instance with the constructor
- try {
- return c.cast(constructor.newInstance(arguments));
- } finally {
- // if constructor was private, then put it private back
- if (!accessible) {
- constructor.setAccessible(accessible);
- }
- }
+ return ReflectionUtil.invokeConstructor(constructor, c,
arguments);
}
}
ExceptionManager
=======================================
---
/trunk/dependency-shot-core/src/main/java/cx/ath/mancel01/dependencyshot/injection/handlers/ConstructorHandler.java
Thu Mar 17 12:21:53 2011
+++
/trunk/dependency-shot-core/src/main/java/cx/ath/mancel01/dependencyshot/injection/handlers/ConstructorHandler.java
Thu Mar 17 12:22:20 2011
@@ -16,9 +16,9 @@
*/
package cx.ath.mancel01.dependencyshot.injection.handlers;
-import cx.ath.mancel01.dependencyshot.exceptions.DSException;
import cx.ath.mancel01.dependencyshot.exceptions.ExceptionManager;
import cx.ath.mancel01.dependencyshot.injection.InjectorImpl;
+import cx.ath.mancel01.dependencyshot.util.ReflectionUtil;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
@@ -75,20 +75,7 @@
arguments[j] =
injector.getProviderOrInstance((Class<?>) parameterTypes[j],
genericParameterTypes[j],
parameterAnnotations[j], constructor);
}
- boolean accessible = constructor.isAccessible();
- // if the constructor is private, then put it public for
newinstance creation
- if (!accessible) {
- constructor.setAccessible(true);
- }
- // create new instance with the constructor
- try {
- return c.cast(constructor.newInstance(arguments));
- } finally {
- // if constructor was private, then put it private back
- if (!accessible) {
- constructor.setAccessible(accessible);
- }
- }
+ return ReflectionUtil.invokeConstructor(constructor, c,
arguments);
}
}
ExceptionManager
=======================================
---
/trunk/dependency-shot-core/src/main/java/cx/ath/mancel01/dependencyshot/injection/handlers/FieldsHandler.java
Thu Mar 17 12:21:53 2011
+++
/trunk/dependency-shot-core/src/main/java/cx/ath/mancel01/dependencyshot/injection/handlers/FieldsHandler.java
Thu Mar 17 12:22:20 2011
@@ -19,6 +19,7 @@
import cx.ath.mancel01.dependencyshot.exceptions.DSIllegalStateException;
import cx.ath.mancel01.dependencyshot.exceptions.ExceptionManager;
import cx.ath.mancel01.dependencyshot.injection.InjectorImpl;
+import cx.ath.mancel01.dependencyshot.util.ReflectionUtil;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
@@ -73,19 +74,7 @@
// get an instance of the field (simple instance or
provided one
Object injectedObject =
injector.getProviderOrInstance(type, genericType,
field.getAnnotations(), field);
- boolean accessible = field.isAccessible();
- // if field is private then put it private for
injectedObject setting
- if (!accessible) {
- field.setAccessible(true);
- }
- try {
- field.set(instance, injectedObject);
- } finally {
- // if the field was private, then put it private back
- if (!accessible) {
- field.setAccessible(accessible);
- }
- }
+ ReflectionUtil.setField(field, instance, injectedObject);
}
}
}
=======================================
---
/trunk/dependency-shot-core/src/main/java/cx/ath/mancel01/dependencyshot/injection/handlers/MethodHandler.java
Thu Mar 17 12:21:00 2011
+++
/trunk/dependency-shot-core/src/main/java/cx/ath/mancel01/dependencyshot/injection/handlers/MethodHandler.java
Thu Mar 17 12:22:20 2011
@@ -17,6 +17,7 @@
package cx.ath.mancel01.dependencyshot.injection.handlers;
import cx.ath.mancel01.dependencyshot.injection.InjectorImpl;
+import cx.ath.mancel01.dependencyshot.util.ReflectionUtil;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@@ -66,7 +67,7 @@
Inject annotation = method.getAnnotation(Inject.class);
// check if method is injectable and if you can inject static
methods and if method is overriden
if (annotation != null && (staticInjection ==
Modifier.isStatic(method.getModifiers()))
- && !isOverridden(method, maybeOverrides)) {
+ && !ReflectionUtil.isOverridden(method,
maybeOverrides)) {
Class<?>[] parameterTypes = method.getParameterTypes();
Type[] genericParameterTypes =
method.getGenericParameterTypes();
Annotation[][] parameterAnnotations =
method.getParameterAnnotations();
@@ -78,87 +79,8 @@
parameterAnnotations[j],
method);
}
- boolean accessible = method.isAccessible();
- // set a private method as public method to invoke it
- if (!accessible) {
- method.setAccessible(true);
- }
- // invocation of the method with rights parameters
- try {
- method.invoke(instance, parameters);
- } finally {
- // if method was private, then put it private back
- if (!accessible) {
- method.setAccessible(accessible);
- }
- }
+ ReflectionUtil.invokeMethod(method, instance, parameters);
}
}
}
-
- /**
- * Check if a method is overridden by another one contained in a list.
- *
- * @param method method to check.
- * @param maybeOverrides methods that can overrides method.
- * @return if the method is overridden.
- */
- private static boolean isOverridden(Method method, List<Method>
maybeOverrides) {
- for (Method candidate : maybeOverrides) {
- if (isOverridden(method, candidate)) {
- return true;
- }
- }
- return false;
- }
-
- /**
- * Check if a method is overriden by another one.
- *
- * @param method method to check.
- * @param candidate method to check against.
- * @return if the method is overridden.
- */
- private static boolean isOverridden(Method method, Method candidate) {
- // check f names are the same
- if (!method.getName().equals(candidate.getName())) {
- return false;
- }
- int modifiers = candidate.getModifiers();
- // check if candidate is private
- boolean isPrivate = Modifier.isPrivate(modifiers);
- if (isPrivate) {
- return false;
- }
- // check if candidate is static
- boolean isStatic = Modifier.isStatic(modifiers);
- if (isStatic) {
- return false;
- }
- boolean isDefault = !isPrivate
- && !Modifier.isPublic(modifiers)
- && !Modifier.isProtected(modifiers);
- boolean samePackage =
- method.getDeclaringClass().getPackage()
- == candidate.getDeclaringClass().getPackage();
- if (isDefault && !samePackage) {
- return false;
- }
- // check if parameters are the same
- Class<?>[] methodParameters = method.getParameterTypes();
- Class<?>[] candidateParameters = candidate.getParameterTypes();
- // check numbers of parameters
- if (methodParameters.length != candidateParameters.length) {
- return false;
- }
- // check types of parameters
- for (int i = 0; i < methodParameters.length; i++) {
- Class<?> class1 = methodParameters[i];
- Class<?> class2 = candidateParameters[i];
- if (!class1.equals(class2)) {
- return false;
- }
- }
- return true;
- }
-}
+}
=======================================
---
/trunk/dependency-shot-core/src/main/java/cx/ath/mancel01/dependencyshot/scope/RequestScope.java
Thu Mar 17 12:21:25 2011
+++
/trunk/dependency-shot-core/src/main/java/cx/ath/mancel01/dependencyshot/scope/RequestScope.java
Thu Mar 17 12:22:20 2011
@@ -22,6 +22,7 @@
import cx.ath.mancel01.dependencyshot.spi.CustomScopeHandler;
import cx.ath.mancel01.dependencyshot.util.ReflectionUtil;
import java.lang.annotation.Annotation;
+import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
@@ -88,21 +89,10 @@
if (method.isAnnotationPresent(PreDestroy.class)) {
Class<?>[] parameterTypes = method.getParameterTypes();
Object[] parameters = new Object[parameterTypes.length];
- boolean accessible = method.isAccessible();
- // set a private method as public method to invoke it
- if (!accessible) {
- method.setAccessible(true);
- }
- // invocation of the method with rights parameters
try {
- method.invoke(o, parameters);
+ ReflectionUtil.invokeMethod(method, o, parameters);
} catch (Exception ex) {
logger.log(Level.SEVERE, null, ex);
- } finally {
- // if method was private, then put it private back
- if (!accessible) {
- method.setAccessible(accessible);
- }
}
}
}
=======================================
---
/trunk/dependency-shot-core/src/main/java/cx/ath/mancel01/dependencyshot/util/ReflectionUtil.java
Thu Mar 17 12:20:07 2011
+++
/trunk/dependency-shot-core/src/main/java/cx/ath/mancel01/dependencyshot/util/ReflectionUtil.java
Thu Mar 17 12:22:20 2011
@@ -17,8 +17,14 @@
package cx.ath.mancel01.dependencyshot.util;
import java.lang.annotation.Annotation;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
+import java.util.List;
import java.util.Set;
import javax.inject.Named;
import javax.inject.Qualifier;
@@ -74,4 +80,125 @@
ReflectionUtil.class.getClassLoader(),
interfaces, handler);
}
-}
+
+ public static void setField(Field field, Object instance, Object value)
+ throws IllegalArgumentException, IllegalAccessException {
+ boolean accessible = field.isAccessible();
+ // if field is private then put it private for injectedObject
setting
+ if (!accessible) {
+ field.setAccessible(true);
+ }
+ try {
+ field.set(instance, value);
+ } finally {
+ // if the field was private, then put it private back
+ if (!accessible) {
+ field.setAccessible(accessible);
+ }
+ }
+ }
+
+ public static Object invokeMethod(Method method, Object instance,
Object... parameters)
+ throws IllegalAccessException,
+ IllegalArgumentException, InvocationTargetException {
+ boolean accessible = method.isAccessible();
+ // set a private method as public method to invoke it
+ if (!accessible) {
+ method.setAccessible(true);
+ }
+ // invocation of the method with rights parameters
+ try {
+ return method.invoke(instance, parameters);
+ } finally {
+ // if method was private, then put it private back
+ if (!accessible) {
+ method.setAccessible(accessible);
+ }
+ }
+ }
+
+ public static <T> T invokeConstructor(Constructor constructor,
Class<T> type, Object... arguments)
+ throws InstantiationException, IllegalAccessException,
+ IllegalArgumentException, InvocationTargetException {
+ boolean accessible = constructor.isAccessible();
+ // if the constructor is private, then put it public for
newinstance creation
+ if (!accessible) {
+ constructor.setAccessible(true);
+ }
+ // create new instance with the constructor
+ try {
+ return type.cast(constructor.newInstance(arguments));
+ } finally {
+ // if constructor was private, then put it private back
+ if (!accessible) {
+ constructor.setAccessible(accessible);
+ }
+ }
+ }
+
+ /**
+ * Check if a method is overridden by another one contained in a list.
+ *
+ * @param method method to check.
+ * @param maybeOverrides methods that can overrides method.
+ * @return if the method is overridden.
+ */
+ public static boolean isOverridden(Method method, List<Method>
maybeOverrides) {
+ for (Method candidate : maybeOverrides) {
+ if (isOverridden(method, candidate)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Check if a method is overriden by another one.
+ *
+ * @param method method to check.
+ * @param candidate method to check against.
+ * @return if the method is overridden.
+ */
+ public static boolean isOverridden(Method method, Method candidate) {
+ // check f names are the same
+ if (!method.getName().equals(candidate.getName())) {
+ return false;
+ }
+ int modifiers = candidate.getModifiers();
+ // check if candidate is private
+ boolean isPrivate = Modifier.isPrivate(modifiers);
+ if (isPrivate) {
+ return false;
+ }
+ // check if candidate is static
+ boolean isStatic = Modifier.isStatic(modifiers);
+ if (isStatic) {
+ return false;
+ }
+ boolean isDefault = !isPrivate
+ && !Modifier.isPublic(modifiers)
+ && !Modifier.isProtected(modifiers);
+ boolean samePackage =
+ method.getDeclaringClass().getPackage()
+ == candidate.getDeclaringClass().getPackage();
+ if (isDefault && !samePackage) {
+ return false;
+ }
+ // check if parameters are the same
+ Class<?>[] methodParameters = method.getParameterTypes();
+ Class<?>[] candidateParameters = candidate.getParameterTypes();
+ // check numbers of parameters
+ if (methodParameters.length != candidateParameters.length) {
+ return false;
+ }
+ // check types of parameters
+ for (int i = 0; i < methodParameters.length; i++) {
+ Class<?> class1 = methodParameters[i];
+ Class<?> class2 = candidateParameters[i];
+ if (!class1.equals(class2)) {
+ return false;
+ }
+ }
+ return true;
+ }
+}