Java example source code file (MethodInterceptionTest.java)
This example Java source code file (MethodInterceptionTest.java) is included in the alvinalexander.com
"Java Source Code
Warehouse" project. The intent of this project is to help you "Learn
Java by Example" TM.
The MethodInterceptionTest.java Java example source code
/**
* Copyright (C) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.inject;
import static com.google.inject.matcher.Matchers.only;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.inject.matcher.AbstractMatcher;
import com.google.inject.matcher.Matchers;
import com.google.inject.spi.ConstructorBinding;
import junit.framework.TestCase;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
/**
* @author jessewilson@google.com (Jesse Wilson)
*/
public class MethodInterceptionTest extends TestCase {
private AtomicInteger count = new AtomicInteger();
private final class CountingInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
count.incrementAndGet();
return methodInvocation.proceed();
}
}
private final class ReturnNullInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
return null;
}
}
private final class NoOpInterceptor implements MethodInterceptor {
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
return methodInvocation.proceed();
}
}
public void testSharedProxyClasses() {
Injector injector = Guice.createInjector(new AbstractModule() {
protected void configure() {
bindInterceptor(Matchers.any(), Matchers.returns(only(Foo.class)),
new ReturnNullInterceptor());
}
});
Injector childOne = injector.createChildInjector(new AbstractModule() {
protected void configure() {
bind(Interceptable.class);
}
});
Interceptable nullFoosOne = childOne.getInstance(Interceptable.class);
assertNotNull(nullFoosOne.bar());
assertNull(nullFoosOne.foo()); // confirm it's being intercepted
Injector childTwo = injector.createChildInjector(new AbstractModule() {
protected void configure() {
bind(Interceptable.class);
}
});
Interceptable nullFoosTwo = childTwo.getInstance(Interceptable.class);
assertNull(nullFoosTwo.foo()); // confirm it's being intercepted
assertSame("Child injectors should share proxy classes, otherwise memory leaks!",
nullFoosOne.getClass(), nullFoosTwo.getClass());
Injector injector2 = Guice.createInjector(new AbstractModule() {
protected void configure() {
bindInterceptor(Matchers.any(), Matchers.returns(only(Foo.class)),
new ReturnNullInterceptor());
}
});
Interceptable separateNullFoos = injector2.getInstance(Interceptable.class);
assertNull(separateNullFoos.foo()); // confirm it's being intercepted
assertSame("different injectors should share proxy classes, otherwise memory leaks!",
nullFoosOne.getClass(), separateNullFoos.getClass());
}
public void testGetThis() {
final AtomicReference<Object> lastTarget = new AtomicReference
Other Java examples (source code examples)
Here is a short list of links related to this Java MethodInterceptionTest.java source code file: