mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Gaetan Boismal
13.09.2014 b97463d792458aa6cba0db7d236e117768ac6e78
OPENDJ-1551 checkpoint : apply patch to ensure compatibility with sdk new Function API and prevents circular dependency problem
1 files modified
57 ■■■■■ changed files
opendj3-server-dev/src/server/org/opends/server/types/AttributeParser.java 57 ●●●●● patch | view | raw | blame | history
opendj3-server-dev/src/server/org/opends/server/types/AttributeParser.java
@@ -25,6 +25,8 @@
 */
package org.opends.server.types;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
import org.forgerock.opendj.ldap.ByteString;
@@ -118,14 +120,67 @@
     *            The default value to return if the attribute is empty.
     * @return The first value decoded as a {@code T}.
     */
    @SuppressWarnings("unchecked")
    public <T> T as(final Function<ByteString, ? extends T, Void> f, final T defaultValue) {
        if (!isEmpty(attribute)) {
            return f.apply(attribute.iterator().next(), null);
            ByteString value = attribute.iterator().next();
            try
            {
              // use existing already committed code
              return f.apply(value, null);
            }
            catch (NoSuchMethodError e1)
            {
              try
              {
                // use the new code that is not yet committed and compiled (with Void parameter)
                final Method method = f.getClass().getMethod("apply", Object.class, Void.class);
                return (T) method.invoke(f, value, null);
              }
              catch (NoSuchMethodException e2)
              {
                return invokeForgerockUtilFunctionApply(f, value);
              }
              catch (SecurityException e2)
              {
                return invokeForgerockUtilFunctionApply(f, value);
              }
              catch (IllegalAccessException e)
              {
                return invokeForgerockUtilFunctionApply(f, value);
              }
              catch (InvocationTargetException e)
              {
                return invokeForgerockUtilFunctionApply(f, value);
              }
              catch (RuntimeException e2)
              {
                throw e2;
              }
            }
        } else {
            return defaultValue;
        }
    }
    private <T> T invokeForgerockUtilFunctionApply(final Function<ByteString, ? extends T, Void> f, ByteString value)
    {
      try
      {
        // use forgerock util Function
        final Method method = f.getClass().getMethod("apply", Object.class);
        return (T) method.invoke(f, value);
      }
      catch (RuntimeException e3)
      {
        throw e3;
      }
      catch (Exception e3)
      {
        throw new RuntimeException(e3);
      }
    }
    /**
     * Returns the first value decoded as a boolean, or {@code null} if the
     * attribute does not contain any values.