You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwebbeans.apache.org by yi...@apache.org on 2010/04/21 20:47:44 UTC
svn commit: r936435 - in
/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util:
AnnotationUtil.java ClassUtil.java SecurityUtil.java WebBeansUtil.java
Author: yingwang
Date: Wed Apr 21 18:47:44 2010
New Revision: 936435
URL: http://svn.apache.org/viewvc?rev=936435&view=rev
Log:
[OWB-312] Part 1 in util package
Added:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/SecurityUtil.java
Modified:
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/AnnotationUtil.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/ClassUtil.java
openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/AnnotationUtil.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/AnnotationUtil.java?rev=936435&r1=936434&r2=936435&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/AnnotationUtil.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/AnnotationUtil.java Wed Apr 21 18:47:44 2010
@@ -544,7 +544,7 @@ public final class AnnotationUtil
return false;
}
- Method[] methods = clazz.getDeclaredMethods();
+ Method[] methods = SecurityUtil.doPrivilegedGetDeclaredMethods(clazz);
List<String> list = new ArrayList<String>();
@@ -678,7 +678,7 @@ public final class AnnotationUtil
Asserts.nullCheckForClass(clazz);
Asserts.assertNotNull(annotation, "Annotation argument can not be null");
- Method[] methods = clazz.getDeclaredMethods();
+ Method[] methods = SecurityUtil.doPrivilegedGetDeclaredMethods(clazz);
List<Method> list = new ArrayList<Method>();
Method[] rMethod = null;
@@ -708,7 +708,7 @@ public final class AnnotationUtil
Asserts.nullCheckForClass(clazz);
Asserts.assertNotNull(annotation, "Annotation argument can not be null");
- Method[] methods = clazz.getDeclaredMethods();
+ Method[] methods = SecurityUtil.doPrivilegedGetDeclaredMethods(clazz);
List<Method> list = new ArrayList<Method>();
Method[] rMethod = null;
@@ -822,7 +822,7 @@ public final class AnnotationUtil
public static Field[] getClazzFieldsWithGivenAnnotation(Class<?> clazz, Class<? extends Annotation> annotation)
{
- Field[] fields = clazz.getDeclaredFields();
+ Field[] fields = SecurityUtil.doPrivilegedGetDeclaredFields(clazz);
List<Field> list = new ArrayList<Field>();
if (fields.length != 0)
@@ -869,7 +869,7 @@ public final class AnnotationUtil
}
private static void checkQualifierConditions(Annotation ann) {
- Method[] methods = ann.annotationType().getDeclaredMethods();
+ Method[] methods = SecurityUtil.doPrivilegedGetDeclaredMethods(ann.annotationType());
for (Method method : methods)
{
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/ClassUtil.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/ClassUtil.java?rev=936435&r1=936434&r2=936435&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/ClassUtil.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/ClassUtil.java Wed Apr 21 18:47:44 2010
@@ -267,7 +267,7 @@ public final class ClassUtil
{
Asserts.nullCheckForClass(clazz);
- Method[] methods = clazz.getDeclaredMethods();
+ Method[] methods = SecurityUtil.doPrivilegedGetDeclaredMethods(clazz);
for (Method m : methods)
{
if (isFinal(m.getModifiers()))
@@ -407,7 +407,7 @@ public final class ClassUtil
public static Method[] getDeclaredMethods(Class<?> clazz)
{
Asserts.nullCheckForClass(clazz);
- return clazz.getDeclaredMethods();
+ return SecurityUtil.doPrivilegedGetDeclaredMethods(clazz);
}
/**
@@ -548,7 +548,7 @@ public final class ClassUtil
List<String> list = new ArrayList<String>();
Class<?> clazz = Object.class;
- Method[] methods = clazz.getDeclaredMethods();
+ Method[] methods = SecurityUtil.doPrivilegedGetDeclaredMethods(clazz);
for (Method method : methods)
{
list.add(method.getName());
@@ -568,7 +568,7 @@ public final class ClassUtil
Asserts.assertNotNull(methodName, "methodName parameter can not be null");
Asserts.nullCheckForClass(clazz);
- Method[] methods = clazz.getDeclaredMethods();
+ Method[] methods = SecurityUtil.doPrivilegedGetDeclaredMethods(clazz);
int i = 0;
for (Method m : methods)
{
@@ -592,7 +592,7 @@ public final class ClassUtil
Asserts.nullCheckForClass(clazz);
try
{
- return clazz.getDeclaredConstructor(new Class<?>[] {});
+ return SecurityUtil.doPrivilegedGetDeclaredConstructor(clazz, new Class<?>[] {});
}
catch (Exception e)
@@ -755,7 +755,7 @@ public final class ClassUtil
{
Asserts.nullCheckForClass(clazz);
- return (Constructor<T>[]) clazz.getDeclaredConstructors();
+ return (Constructor<T>[])SecurityUtil.doPrivilegedGetDeclaredConstructors(clazz);
}
/**
@@ -771,8 +771,7 @@ public final class ClassUtil
try
{
- clazz.getDeclaredConstructor(new Class<?>[] {});
-
+ SecurityUtil.doPrivilegedGetDeclaredConstructor(clazz, new Class<?>[] {});
}
catch (SecurityException e)
{
@@ -1193,7 +1192,7 @@ public final class ClassUtil
try
{
- clazz.getDeclaredField(fieldName);
+ SecurityUtil.doPrivilegedGetDeclaredField(clazz, fieldName);
}
catch (SecurityException e)
@@ -1214,7 +1213,7 @@ public final class ClassUtil
Asserts.nullCheckForClass(clazz);
Asserts.assertNotNull(fieldName, "fieldName parameter can not be null");
- Field[] fields = clazz.getDeclaredFields();
+ Field[] fields = SecurityUtil.doPrivilegedGetDeclaredFields(clazz);
boolean ok = false;
for (Field field : fields)
{
@@ -1241,7 +1240,7 @@ public final class ClassUtil
try
{
- return clazz.getDeclaredField(fieldName);
+ return SecurityUtil.doPrivilegedGetDeclaredField(clazz,fieldName);
}
catch (SecurityException e)
@@ -1271,7 +1270,7 @@ public final class ClassUtil
List<Method> methodList = new ArrayList<Method>();
- Method[] methods = clazz.getDeclaredMethods();
+ Method[] methods = SecurityUtil.doPrivilegedGetDeclaredMethods(clazz);
int j = 0;
for (Method method : methods)
@@ -1326,7 +1325,7 @@ public final class ClassUtil
Asserts.assertNotNull(methodName, "methodName parameter can not be null");
Asserts.assertNotNull(parameterTypes, "parameterTypes parameter can not be null");
- Method[] methods = clazz.getDeclaredMethods();
+ Method[] methods = SecurityUtil.doPrivilegedGetDeclaredMethods(clazz);
int j = 0;
for (Method method : methods)
@@ -1376,7 +1375,7 @@ public final class ClassUtil
Asserts.nullCheckForClass(clazz);
Asserts.assertNotNull(methodName, "methodName parameter can not be null");
- Method[] methods = clazz.getDeclaredMethods();
+ Method[] methods = SecurityUtil.doPrivilegedGetDeclaredMethods(clazz);
for (Method method : methods)
{
@@ -1811,7 +1810,7 @@ public final class ClassUtil
Asserts.nullCheckForClass(clazz);
Asserts.assertNotNull(annotation, "annotation parameter can not be null");
- Field[] fields = clazz.getDeclaredFields();
+ Field[] fields = SecurityUtil.doPrivilegedGetDeclaredFields(clazz);
for (Field field : fields)
{
if (AnnotationUtil.hasAnnotation(field.getAnnotations(), annotation))
@@ -1831,7 +1830,7 @@ public final class ClassUtil
Asserts.assertNotNull(type, "type parameter can not be null");
List<Field> fieldsWithType = new ArrayList<Field>();
- Field[] fields = clazz.getDeclaredFields();
+ Field[] fields = SecurityUtil.doPrivilegedGetDeclaredFields(clazz);
for (Field field : fields)
{
if(field.getType().equals(type))
@@ -1910,7 +1909,7 @@ public final class ClassUtil
if(!field.isAccessible())
{
- field.setAccessible(true);
+ SecurityUtil.doPrivilegedSetAccessible(field, true);
}
try
@@ -1996,7 +1995,7 @@ public final class ClassUtil
{
try
{
- return clazz.getDeclaredMethod(methodName, parameters);
+ return SecurityUtil.doPrivilegedGetDeclaredMethod(clazz,methodName, parameters);
}catch(NoSuchMethodException e)
{
Added: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/SecurityUtil.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/SecurityUtil.java?rev=936435&view=auto
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/SecurityUtil.java (added)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/SecurityUtil.java Wed Apr 21 18:47:44 2010
@@ -0,0 +1,218 @@
+package org.apache.webbeans.util;
+
+import java.util.Set;
+import java.lang.reflect.AccessibleObject;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.Method;
+import java.security.AccessControlContext;
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import javassist.util.proxy.ProxyFactory;
+
+public class SecurityUtil
+{
+
+ private final static int METHOD_CLASS_GETDECLAREDCONSTRUCTOR = 0x01;
+
+ private final static int METHOD_CLASS_GETDECLAREDCONSTRUCTORS = 0x02;
+
+ private final static int METHOD_CLASS_GETDECLAREDMETHOD = 0x03;
+
+ private final static int METHOD_CLASS_GETDECLAREDMETHODS = 0x04;
+
+ private final static int METHOD_CLASS_GETDECLAREDFIELD = 0x05;
+
+ private final static int METHOD_CLASS_GETDECLAREDFIELDS = 0x06;
+
+ @SuppressWarnings("unchecked")
+ public static <T> Constructor<T> doPrivilegedGetDeclaredConstructor(Class<T> clazz, Class<?>... parameterTypes) throws NoSuchMethodException
+ {
+ ClassLoader ld = SecurityUtil.class.getClassLoader();
+ AccessControlContext acc = AccessController.getContext();
+ Object obj = AccessController.doPrivileged(
+ new PrivilegedActionForClass(clazz, parameterTypes, METHOD_CLASS_GETDECLAREDCONSTRUCTOR));
+ if (obj instanceof NoSuchMethodException) throw (NoSuchMethodException)obj;
+ return (Constructor<T>)obj;
+ }
+
+ @SuppressWarnings("unchecked")
+ public static <T> Constructor<T>[] doPrivilegedGetDeclaredConstructors(Class<T> clazz)
+ {
+ Object obj = AccessController.doPrivileged(
+ new PrivilegedActionForClass(clazz, null, METHOD_CLASS_GETDECLAREDCONSTRUCTORS));
+ return (Constructor<T>[])obj;
+ }
+
+ public static <T> Method doPrivilegedGetDeclaredMethod(Class<T> clazz, String name, Class<?>... parameterTypes) throws NoSuchMethodException
+ {
+ Object obj = AccessController.doPrivileged(
+ new PrivilegedActionForClass(clazz, new Object[] {name, parameterTypes}, METHOD_CLASS_GETDECLAREDMETHOD));
+ if (obj instanceof NoSuchMethodException) throw (NoSuchMethodException)obj;
+ return (Method)obj;
+ }
+
+ public static <T> Method[] doPrivilegedGetDeclaredMethods(Class<T> clazz)
+ {
+ Object obj = AccessController.doPrivileged(
+ new PrivilegedActionForClass(clazz, null, METHOD_CLASS_GETDECLAREDMETHODS));
+ return (Method[])obj;
+ }
+
+ public static <T> Field doPrivilegedGetDeclaredField(Class<T> clazz, String name) throws NoSuchFieldException
+ {
+ Object obj = AccessController.doPrivileged(
+ new PrivilegedActionForClass(clazz, name, METHOD_CLASS_GETDECLAREDFIELD));
+ if (obj instanceof NoSuchFieldException) throw (NoSuchFieldException)obj;
+ return (Field)obj;
+ }
+
+ public static <T> Field[] doPrivilegedGetDeclaredFields(Class<T> clazz)
+ {
+ Object obj = AccessController.doPrivileged(
+ new PrivilegedActionForClass(clazz, null, METHOD_CLASS_GETDECLAREDFIELDS));
+ return (Field[])obj;
+ }
+
+ protected static class PrivilegedActionForClass implements PrivilegedAction<Object>
+ {
+ Class<?> clazz;
+
+ Object parameters;
+
+ int method;
+
+ protected PrivilegedActionForClass(Class<?> clazz, Object parameters, int method)
+ {
+ this.clazz = clazz;
+ this.parameters = parameters;
+ this.method = method;
+ }
+
+ public Object run()
+ {
+ try
+ {
+ switch (method)
+ {
+ case METHOD_CLASS_GETDECLAREDCONSTRUCTOR:
+ return clazz.getDeclaredConstructor((Class<?>[])parameters);
+ case METHOD_CLASS_GETDECLAREDCONSTRUCTORS:
+ return clazz.getDeclaredConstructors();
+ case METHOD_CLASS_GETDECLAREDMETHOD:
+ String name = (String)((Object[])parameters)[0];
+ Class<?>[] realParameters = (Class<?>[])((Object[])parameters)[1];
+ return clazz.getDeclaredMethod(name, realParameters);
+ case METHOD_CLASS_GETDECLAREDMETHODS:
+ return clazz.getDeclaredMethods();
+ case METHOD_CLASS_GETDECLAREDFIELD:
+ return clazz.getDeclaredField((String)parameters);
+ case METHOD_CLASS_GETDECLAREDFIELDS:
+ return clazz.getDeclaredFields();
+ }
+ }
+ catch (Exception exception)
+ {
+ return exception;
+ }
+ return null;
+ }
+
+ }
+
+ public static Object doPrivilegedSetAccessible(AccessibleObject obj, boolean flag)
+ {
+ AccessController.doPrivileged(new PrivilegedActionForAccessibleObject(obj, flag));
+ return null;
+ };
+
+ protected static class PrivilegedActionForAccessibleObject implements PrivilegedAction<Object>
+ {
+
+ AccessibleObject object;
+
+ boolean flag;
+
+ protected PrivilegedActionForAccessibleObject(AccessibleObject object, boolean flag)
+ {
+ this.object = object;
+ this.flag = flag;
+ }
+
+ public Object run()
+ {
+ object.setAccessible(flag);
+ return null;
+ }
+ }
+
+
+ public static Class<?> doPrivilegedCreateClass(ProxyFactory factory)
+ {
+ Class<?> ret = (Class<?>)AccessController.doPrivileged(new PrivilegedActionForProxyFactory(factory));
+ return ret;
+ }
+
+ protected static class PrivilegedActionForProxyFactory implements PrivilegedAction<Object>
+ {
+ ProxyFactory factory;
+
+ protected PrivilegedActionForProxyFactory(ProxyFactory factory)
+ {
+ this.factory = factory;
+ }
+
+ public Object run()
+ {
+ return factory.createClass();
+ }
+ }
+
+/*
+ public static <T> Set<T> doPrivilegedSetAdd(Set<T> set, T item)
+ {
+ AccessController.doPrivileged(new PrivilegedActionForSet<T>(set, item));
+ return null;
+ }
+
+ protected static class PrivilegedActionForSet<T> implements PrivilegedAction<T>
+ {
+ Set<T> set;
+
+ T item;
+
+ protected PrivilegedActionForSet(Set<T> set, T item)
+ {
+ this.set = set;
+ this.item = item;
+ }
+
+ public T run()
+ {
+ set.add(item);
+ return null;
+ }
+ }
+
+ public static String doPrivilegedToString(java.lang.annotation.Annotation a)
+ {
+ return (String)AccessController.doPrivileged(new PrivilegedActionForAnnotation(a));
+ }
+
+ protected static class PrivilegedActionForAnnotation implements PrivilegedAction<Object>
+ {
+ java.lang.annotation.Annotation annotation;
+
+ protected PrivilegedActionForAnnotation(java.lang.annotation.Annotation annotation)
+ {
+ this.annotation = annotation;
+ }
+
+ public Object run()
+ {
+ return annotation.toString();
+ }
+ }
+*/
+
+}
Modified: openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java
URL: http://svn.apache.org/viewvc/openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java?rev=936435&r1=936434&r2=936435&view=diff
==============================================================================
--- openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java (original)
+++ openwebbeans/trunk/webbeans-impl/src/main/java/org/apache/webbeans/util/WebBeansUtil.java Wed Apr 21 18:47:44 2010
@@ -1233,7 +1233,7 @@ public final class WebBeansUtil
if (!ct.isAccessible())
{
- ct.setAccessible(true);
+ SecurityUtil.doPrivilegedSetAccessible(ct, true);
}
try