Пример #1
0
  /**
   * Gets an accessible <code>Field</code> by name breaking scope if requested.
   * Superclasses/interfaces 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. <code>False</code> will only match public fields.
   * @return the Field object
   * @throws IllegalArgumentException if the class or field name is null
   */
  public static Field getField(final 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");
    }
    // Sun Java 1.3 has a bugged implementation of getField hence we write the
    // code ourselves

    // getField() will return the Field object with the declaring class
    // set correctly to the class that declares the field. Thus requesting the
    // field on a subclass will return the field from the superclass.
    //
    // priority order for lookup:
    // searchclass private/protected/package/public
    // superclass protected/package/public
    //  private/different package blocks access to further superclasses
    // implementedinterface public

    // check up the superclass hierarchy
    for (Class<?> acls = cls; acls != null; acls = acls.getSuperclass()) {
      try {
        Field field = acls.getDeclaredField(fieldName);
        // getDeclaredField checks for non-public scopes as well
        // and it returns accurate results
        if (!Modifier.isPublic(field.getModifiers())) {
          if (forceAccess) {
            field.setAccessible(true);
          } else {
            continue;
          }
        }
        return field;
      } catch (NoSuchFieldException ex) { // NOPMD
        // ignore
      }
    }
    // check the public interface case. This must be manually searched for
    // incase there is a public supersuperclass field hidden by a private/package
    // superclass field.
    Field match = null;
    for (Class<?> class1 : ClassUtils.getAllInterfaces(cls)) {
      try {
        Field test = ((Class<?>) class1).getField(fieldName);
        if (match != null) {
          throw new IllegalArgumentException(
              "Reference to field "
                  + fieldName
                  + " is ambiguous relative to "
                  + cls
                  + "; a matching field exists on two or more implemented interfaces.");
        }
        match = test;
      } catch (NoSuchFieldException ex) { // NOPMD
        // ignore
      }
    }
    return match;
  }
Пример #2
0
  /**
   * Gets a <code>List</code> of all interfaces implemented by the given class and its superclasses.
   *
   * <p>The order is determined by looking through each interface in turn as declared in the source
   * file and following its hierarchy up. Then each superclass is considered in the same way. Later
   * duplicates are ignored, so the order is maintained.
   *
   * @param cls the class to look up, may be <code>null</code>
   * @return the <code>List</code> of interfaces in order, <code>null</code> if null input
   */
  public static List<Class<?>> getAllInterfaces(Class<?> cls) {
    if (cls == null) {
      return null;
    }

    LinkedHashSet<Class<?>> interfacesFound = new LinkedHashSet<Class<?>>();
    getAllInterfaces(cls, interfacesFound);

    return new ArrayList<Class<?>>(interfacesFound);
  }
  @Override
  public <F> F createCompositeFilter(ClassLoader loader, Object... filters) {
    Set<Class<?>> interfaces = new HashSet<Class<?>>();
    for (Object filter : filters) {
      interfaces.addAll(ClassUtils.getAllInterfaces(filter.getClass()));
    }

    return (F)
        Proxy.newProxyInstance(
            loader, interfaces.toArray(CLASS_ARRAY), new CompositeFilter(this, filters));
  }
Пример #4
0
  /**
   * Get the interfaces for the specified class.
   *
   * @param cls the class to look up, may be <code>null</code>
   * @param interfacesFound the <code>Set</code> of interfaces for the class
   */
  private static void getAllInterfaces(Class<?> cls, HashSet<Class<?>> interfacesFound) {
    while (cls != null) {
      Class<?>[] interfaces = cls.getInterfaces();

      for (Class<?> i : interfaces) {
        if (interfacesFound.add(i)) {
          getAllInterfaces(i, interfacesFound);
        }
      }

      cls = cls.getSuperclass();
    }
  }
 protected final String getShortClassName(Class<?> paramAnonymousClass) {
   Iterator localIterator = ClassUtils.getAllInterfaces(paramAnonymousClass).iterator();
   do {
     if (!localIterator.hasNext()) {
       break;
     }
     paramAnonymousClass = (Class) localIterator.next();
   } while (!Annotation.class.isAssignableFrom(paramAnonymousClass));
   for (; ; ) {
     if (paramAnonymousClass == null) {}
     for (paramAnonymousClass = ""; ; paramAnonymousClass = paramAnonymousClass.getName()) {
       return new StringBuilder(paramAnonymousClass).insert(0, '@').toString();
     }
     paramAnonymousClass = null;
   }
 }
 @Override
 public boolean needToProxy(Class<?> clazz) {
   return ClassUtils.getAllInterfaces(clazz).contains(Session.class);
 }