当前位置: 首页>>代码示例>>Java>>正文


Java AbstractInvocationHandler类代码示例

本文整理汇总了Java中com.google.common.reflect.AbstractInvocationHandler的典型用法代码示例。如果您正苦于以下问题:Java AbstractInvocationHandler类的具体用法?Java AbstractInvocationHandler怎么用?Java AbstractInvocationHandler使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。


AbstractInvocationHandler类属于com.google.common.reflect包,在下文中一共展示了AbstractInvocationHandler类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getDefaultValue

import com.google.common.reflect.AbstractInvocationHandler; //导入依赖的package包/类
private static Object getDefaultValue(Class<?> returnType) {
  Object defaultValue = ArbitraryInstances.get(returnType);
  if (defaultValue != null) {
    return defaultValue;
  }
  if ("java.util.function.Predicate".equals(returnType.getCanonicalName())
      || ("java.util.function.Consumer".equals(returnType.getCanonicalName()))) {
    // Generally, methods that accept java.util.function.* instances
    // don't like to get null values.  We generate them dynamically
    // using Proxy so that we can have Java 7 compliant code.
    return Reflection.newProxy(returnType, new AbstractInvocationHandler() {
      @Override public Object handleInvocation(Object proxy, Method method, Object[] args) {
        // Crude, but acceptable until we can use Java 8.  Other
        // methods have default implementations, and it is hard to
        // distinguish.
        if ("test".equals(method.getName()) || "accept".equals(method.getName())) {
          return getDefaultValue(method.getReturnType());
        }
        throw new IllegalStateException("Unexpected " + method + " invoked on " + proxy);
      }
    });
  } else {
    return null;
  }
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:26,代码来源:ForwardingMapTest.java

示例2: createInstance

import com.google.common.reflect.AbstractInvocationHandler; //导入依赖的package包/类
@Override
public AutoCloseable createInstance() {
    // The service is provided via blueprint so wait for and return it here for backwards compatibility.
    final WaitingServiceTracker<Timer> tracker = WaitingServiceTracker.create(
            Timer.class, bundleContext, "(type=global-timer)");
    final Timer timer = tracker.waitForService(WaitingServiceTracker.FIVE_MINUTES);

    return Reflection.newProxy(AutoCloseableTimerInterface.class, new AbstractInvocationHandler() {
        @Override
        protected Object handleInvocation(final Object proxy, final Method method, final Object[] args) throws Throwable {
            if (method.getName().equals("close")) {
                tracker.close();
                return null;
            } else {
                return method.invoke(timer, args);
            }
        }
    });
}
 
开发者ID:hashsdn,项目名称:hashsdn-controller,代码行数:20,代码来源:HashedWheelTimerModule.java

示例3: createInstance

import com.google.common.reflect.AbstractInvocationHandler; //导入依赖的package包/类
@Override
public AutoCloseable createInstance() {
    // The service is provided via blueprint so wait for and return it here for backwards compatibility.
    String typeFilter = String.format("(type=%s)", getIdentifier().getInstanceName());
    final WaitingServiceTracker<EventLoopGroup> tracker = WaitingServiceTracker.create(
            EventLoopGroup.class, bundleContext, typeFilter);
    final EventLoopGroup group = tracker.waitForService(WaitingServiceTracker.FIVE_MINUTES);

    return Reflection.newProxy(AutoCloseableEventLoopGroupInterface.class, new AbstractInvocationHandler() {
        @Override
        protected Object handleInvocation(final Object proxy, final Method method, final Object[] args) throws Throwable {
            if (method.getName().equals("close")) {
                tracker.close();
                return null;
            } else {
                return method.invoke(group, args);
            }
        }
    });
}
 
开发者ID:hashsdn,项目名称:hashsdn-controller,代码行数:21,代码来源:NettyThreadgroupModule.java

示例4: createInstance

import com.google.common.reflect.AbstractInvocationHandler; //导入依赖的package包/类
@Override
public AutoCloseable createInstance() {
    final WaitingServiceTracker<EntityOwnershipService> tracker = WaitingServiceTracker.create(
            EntityOwnershipService.class, bundleContext);
    final EntityOwnershipService service = tracker.waitForService(WaitingServiceTracker.FIVE_MINUTES);

    return Reflection.newProxy(AutoCloseableEntityOwnershipService.class, new AbstractInvocationHandler() {
        @Override
        protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable {
            if (method.getName().equals("close")) {
                tracker.close();
                return null;
            } else {
                try {
                    return method.invoke(service, args);
                } catch (InvocationTargetException e) {
                    // https://bugs.opendaylight.org/show_bug.cgi?id=6564
                    // http://stackoverflow.com/a/10719613/421602
                    // https://amitstechblog.wordpress.com/2011/07/24/java-proxies-and-undeclaredthrowableexception/
                    throw e.getCause();
                }
            }
        }
    });
}
 
开发者ID:hashsdn,项目名称:hashsdn-controller,代码行数:26,代码来源:LegacyEntityOwnershipServiceProviderModule.java

示例5: createInstance

import com.google.common.reflect.AbstractInvocationHandler; //导入依赖的package包/类
@Override
public AutoCloseable createInstance() {
    final WaitingServiceTracker<ClusterSingletonServiceProvider> tracker = WaitingServiceTracker
            .create(ClusterSingletonServiceProvider.class, bundleContext);
    final ClusterSingletonServiceProvider service = tracker.waitForService(WaitingServiceTracker.FIVE_MINUTES);

    // Create a proxy to override close to close the ServiceTracker. The actual DOMClusterSingletonServiceProvider
    // instance will be closed via blueprint.
    return Reflection.newProxy(AutoCloseableDOMClusterSingletonServiceProvider.class,
            new AbstractInvocationHandler() {
                @Override
                protected Object handleInvocation(final Object proxy, final Method method, final Object[] args)
                        throws Throwable {
                    if (method.getName().equals("close")) {
                        tracker.close();
                        return null;
                    } else {
                        return method.invoke(service, args);
                    }
                }
            });
}
 
开发者ID:hashsdn,项目名称:hashsdn-controller,代码行数:23,代码来源:ClusterSingletonServiceProviderModule.java

示例6: newPartitionContextTest

import com.google.common.reflect.AbstractInvocationHandler; //导入依赖的package包/类
public static <S> PartitionContextValidator<S> newPartitionContextTest(final Class<S> ifc, final Class<? extends S> impl) {
    final PartitionContextSupplier supplier = new AnnotationPartitionContextSupplier(ifc, impl);
    return new PartitionContextValidator<S>() {
        @Override
        public S expect(final PartitionContext expected) {
            return Reflection.newProxy(ifc, new AbstractInvocationHandler() {
                @Override
                protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable {
                    PartitionContext actual = supplier.forCall(method, args);
                    assertEquals(actual, expected, "Expected=" + expected.asMap() + ", Actual=" + actual.asMap());
                    return Defaults.defaultValue(method.getReturnType());
                }
            });
        }
    };
}
 
开发者ID:bazaarvoice,项目名称:emodb,代码行数:17,代码来源:OstrichAccessors.java

示例7: verifyThrottled

import com.google.common.reflect.AbstractInvocationHandler; //导入依赖的package包/类
private DataStore verifyThrottled(final DataStore dataStore) {
    return (DataStore) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[] { DataStore.class },
            new AbstractInvocationHandler() {
                @Override
                protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable {
                    try {
                        method.invoke(dataStore, args);
                        fail(String.format("Throttled request did not generate a 503 error: %s(%s)",
                                method.getName(), Joiner.on(",").join(args)));
                    } catch (InvocationTargetException e) {
                        assertTrue(e.getCause() instanceof EmoClientException);
                        EmoClientException ce = (EmoClientException) e.getCause();
                        assertEquals(ce.getResponse().getStatus(), HttpStatus.SERVICE_UNAVAILABLE_503);
                    }
                    // This should be unreachable; the caller doesn't care what the result is
                    if (method.getReturnType().isPrimitive()) {
                        return Primitives.defaultValueForPrimitiveOrWrapper(method.getReturnType());
                    }
                    return null;
                }
            });
}
 
开发者ID:bazaarvoice,项目名称:emodb,代码行数:23,代码来源:AdHocThrottleTest.java

示例8: asCloseableDataStore

import com.google.common.reflect.AbstractInvocationHandler; //导入依赖的package包/类
/**
 * Creates a proxy which delegates all DataStore operations to the provided instance and delegates the Closeable's
 * close() method to the provided Runnable if necessary.
 */
private CloseableDataStore asCloseableDataStore(final DataStore dataStore, final Optional<Runnable> onClose) {
    return (CloseableDataStore) Proxy.newProxyInstance(
            DataStore.class.getClassLoader(), new Class[] { CloseableDataStore.class },
            new AbstractInvocationHandler() {
                @Override
                protected Object handleInvocation(Object proxy, Method method, Object[] args)
                        throws Throwable {
                    if ("close".equals(method.getName())) {
                        if (onClose.isPresent()) {
                            onClose.get().run();
                        }
                        return null;
                    } else {
                        return method.invoke(dataStore, args);
                    }
                }
            });
}
 
开发者ID:bazaarvoice,项目名称:emodb,代码行数:23,代码来源:HadoopDataStoreManager.java

示例9: create

import com.google.common.reflect.AbstractInvocationHandler; //导入依赖的package包/类
/**
 * Create new composite listener for a collection of delegates.
 */
@SafeVarargs
public static <T extends ParseTreeListener> T create(Class<T> type, T... delegates) {
    ImmutableList<T> listeners = ImmutableList.copyOf(delegates);
    return Reflection.newProxy(type, new AbstractInvocationHandler() {

        @Override
        @ParametersAreNonnullByDefault
        protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable {
            for (T listener : listeners) {
                method.invoke(listener, args);
            }
            return null;
        }

        @Override
        public String toString() {
            return MoreObjects.toStringHelper("CompositeParseTreeListener")
                    .add("listeners", listeners)
                    .toString();
        }
    });

}
 
开发者ID:protostuff,项目名称:protostuff-compiler,代码行数:27,代码来源:CompositeParseTreeListener.java

示例10: getName

import com.google.common.reflect.AbstractInvocationHandler; //导入依赖的package包/类
/** Makes a fresh type variable that's only equal to itself. */
@SuppressWarnings("unchecked")  // Delegates to the <T> of class Var except getName().
static TypeVariable<Class<?>> freshTypeVariable(final String name) {
  // Use dynamic proxy so we only changes the behavior of getName() and equals/hashCode
  // Everything else delegates to a JDK native type variable.
  return Reflection.newProxy(TypeVariable.class, new AbstractInvocationHandler() {
    @Override protected Object handleInvocation(Object proxy, Method method, Object[] args)
        throws Throwable {
      if (method.getName().equals("getName")) {
        return name;
      }
      try {
        return method.invoke(PROTOTYPE, args);
      } catch (InvocationTargetException e) {
        throw e.getCause();
      }
    }
    @Override public String toString() { return name; }
  });
}
 
开发者ID:jparsec,项目名称:jparsec-g,代码行数:21,代码来源:Types.java

示例11: TestVagrantRuntime

import com.google.common.reflect.AbstractInvocationHandler; //导入依赖的package包/类
public TestVagrantRuntime(String user, VagrantResource.Ensure after) {
    try {
        ServiceManager serviceManager = (ServiceManager) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{ServiceManager.class}, new AbstractInvocationHandler() {
            @Override
            protected Object handleInvocation(Object o, Method method, Object[] objects) throws Throwable {
                SshHost host = (SshHost) objects[1];
                set(host, "hostProvider", new LinuxMetadataProvider());
                return null;
            }
        });
        Resource resource = Mockito.mock(Resource.class);
        vagrantResource = new VagrantResource("ubuntu-precise64", "_vagrant", VagrantResource.Ensure.UP, after,
                LocalHost.createStandalone(), serviceManager, Arrays.asList(new SharedFolder(true, true, "test", "_vagrant/test", "/test")));
        set(vagrantResource, "resource", resource);
        vagrantResource.exec();
        sshHost = vagrantResource.getSshHost();
        set(sshHost, "hostProvider", new LinuxMetadataProvider());
        sshHost.start();
        if (user != null) {
            // TODO
        }
    } catch (KMRuntimeException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
 
开发者ID:Kloudtek,项目名称:kloudmake,代码行数:26,代码来源:TestVagrantRuntime.java

示例12: createCloseableProxy

import com.google.common.reflect.AbstractInvocationHandler; //导入依赖的package包/类
private static AutoCloseableEventExecutor createCloseableProxy(
        final CloseableEventExecutorMixin closeableEventExecutorMixin) {
    return Reflection.newProxy(AutoCloseableEventExecutor.class, new AbstractInvocationHandler() {
        @Override
        protected Object handleInvocation(final Object proxy, final Method method, final Object[] args) throws Throwable {
            if (method.getName().equals("close")) {
                closeableEventExecutorMixin.close();
                return null;
            } else {
                return method.invoke(closeableEventExecutorMixin.eventExecutor, args);
            }
        }
    });
}
 
开发者ID:hashsdn,项目名称:hashsdn-controller,代码行数:15,代码来源:AutoCloseableEventExecutor.java

示例13: createDelegate

import com.google.common.reflect.AbstractInvocationHandler; //导入依赖的package包/类
private S createDelegate(ServiceEndPoint endPoint) {
    final S delegateService = _delegate.create(endPoint);

    S proxiedService = Reflection.newProxy(_serviceClass, new AbstractInvocationHandler() {
        @Override
        protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable {
            try {
                return method.invoke(delegateService, args);
            } catch (InvocationTargetException e) {
                // If the target exception is a declared exception then rethrow as-is
                Throwable targetException = e.getTargetException();
                for (Class<?> declaredException : method.getExceptionTypes()) {
                    // noinspection unchecked
                    Throwables.propagateIfInstanceOf(targetException, (Class<? extends Throwable>) declaredException);
                }
                // If the exception was due to connection issues and not necessarily the target let the caller know.
                // It's possible the connection timed out due to a problem on the target, but from our perspective
                // there's no definitive way of knowing.
                if (targetException instanceof ClientHandlerException) {
                    _errorMeter.mark();
                    throw new PartitionForwardingException("Failed to handle request at endpoint", targetException.getCause());
                }
                throw Throwables.propagate(targetException);
            }
        }
    });

    _proxiedToDelegateServices.put(proxiedService, delegateService);
    return proxiedService;
}
 
开发者ID:bazaarvoice,项目名称:emodb,代码行数:31,代码来源:PartitionAwareServiceFactory.java

示例14: newEchoProxyInstance

import com.google.common.reflect.AbstractInvocationHandler; //导入依赖的package包/类
/**
 * Creates a dynamic proxy implementing the given interface.
 * All of the proxy's one-arg methods will return their argument.
 * All other methods return a List containing their arguments.
 */
protected <T> T newEchoProxyInstance(Class<T> clazz) {
    return clazz.cast(newProxyInstance(getClass().getClassLoader(), new Class[]{clazz}, new AbstractInvocationHandler() {
        @Override
        @ParametersAreNonnullByDefault
        protected Object handleInvocation(Object proxy, Method method, Object[] args) throws Throwable {
            return args.length == 0 ? null : args.length == 1 ? args[0] : Arrays.asList(args);
        }
    }));
}
 
开发者ID:dnault,项目名称:therapi-json-rpc,代码行数:15,代码来源:AbstractMethodRegistryTest.java

示例15: getLocalizable

import com.google.common.reflect.AbstractInvocationHandler; //导入依赖的package包/类
@Override
public <T extends Localizable> T getLocalizable(Class<T> clazz) {
  return Reflection.newProxy(clazz, new AbstractInvocationHandler() {
    @Override
    protected String handleInvocation(Object proxy, Method method, Object[] args) throws Throwable {
      return getMessage(method.getName(), method.getName(), args);
    }
  });
}
 
开发者ID:Wadpam,项目名称:guja,代码行数:10,代码来源:AbstractLocalization.java


注:本文中的com.google.common.reflect.AbstractInvocationHandler类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。