예제 #1
0
파일: FieldUtils.java 프로젝트: enenuki/phd
 /*  61:    */
 /*  62:    */ public static Field getDeclaredField(
     Class cls, String fieldName, boolean forceAccess)
       /*  63:    */ {
   /*  64:164 */ if (cls == null) {
     /*  65:165 */ throw new IllegalArgumentException("The class must not be null");
     /*  66:    */ }
   /*  67:167 */ if (fieldName == null) {
     /*  68:168 */ throw new IllegalArgumentException("The field name must not be null");
     /*  69:    */ }
   /*  70:    */ try
   /*  71:    */ {
     /*  72:172 */ Field field = cls.getDeclaredField(fieldName);
     /*  73:173 */ if (!MemberUtils.isAccessible(field)) {
       /*  74:174 */ if (forceAccess) {
         /*  75:175 */ field.setAccessible(true);
         /*  76:    */ } else {
         /*  77:177 */ return null;
         /*  78:    */ }
       /*  79:    */ }
     /*  80:180 */ return field;
     /*  81:    */ }
   /*  82:    */ catch (NoSuchFieldException e) {
   }
   /*  83:183 */ return null;
   /*  84:    */ }
예제 #2
0
  /**
   * Return an accessible method (that is, one that can be invoked via reflection) that implements
   * the specified Method. If no such method can be found, return <code>null</code>.
   *
   * @param method The method that we wish to call
   * @return The accessible method
   */
  public static Method getAccessibleMethod(Method method) {
    if (!MemberUtils.isAccessible(method)) {
      return null;
    }
    // If the declaring class is public, we are done
    Class cls = method.getDeclaringClass();
    if (Modifier.isPublic(cls.getModifiers())) {
      return method;
    }
    String methodName = method.getName();
    Class[] parameterTypes = method.getParameterTypes();

    // Check the implemented interfaces and subinterfaces
    method = getAccessibleMethodFromInterfaceNest(cls, methodName, parameterTypes);

    // Check the superclass chain
    if (method == null) {
      method = getAccessibleMethodFromSuperclass(cls, methodName, parameterTypes);
    }
    return method;
  }
예제 #3
0
 /**
  * Gets an accessible <code>Field</code> by name breaking scope if requested. Only the specified
  * class will be considered.
  *
  * @param cls the class to reflect, must not be null
  * @param fieldName the field name to obtain
  * @param forceAccess whether to break scope restrictions using the <code>setAccessible</code>
  *     method. False will only match public fields.
  * @return the Field object
  * @throws IllegalArgumentException if the class or field name is null
  */
 public static Field getDeclaredField(Class cls, String fieldName, boolean forceAccess) {
   if (cls == null) {
     throw new IllegalArgumentException("The class must not be null");
   }
   if (fieldName == null) {
     throw new IllegalArgumentException("The field name must not be null");
   }
   try {
     // only consider the specified class by using getDeclaredField()
     Field field = cls.getDeclaredField(fieldName);
     if (!MemberUtils.isAccessible(field)) {
       if (forceAccess) {
         field.setAccessible(true);
       } else {
         return null;
       }
     }
     return field;
   } catch (NoSuchFieldException e) {
   }
   return null;
 }
예제 #4
0
 /**
  * Returns accessible version of the given constructor.
  *
  * @param ctor prototype constructor object.
  * @return <code>null</code> if accessible constructor can not be found.
  * @see java.lang.SecurityManager
  */
 public static Constructor getAccessibleConstructor(Constructor ctor) {
   return MemberUtils.isAccessible(ctor)
           && Modifier.isPublic(ctor.getDeclaringClass().getModifiers())
       ? ctor
       : null;
 }