Skip to content

Commit

Permalink
Moving internal cache to paranamer own cache
Browse files Browse the repository at this point in the history
  • Loading branch information
garcia-jj committed Mar 25, 2014
1 parent c90365c commit 2bea0f2
Show file tree
Hide file tree
Showing 17 changed files with 44 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,15 @@
package br.com.caelum.vraptor.http;

import java.lang.reflect.AccessibleObject;
import java.util.concurrent.Callable;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import br.com.caelum.vraptor.cache.CacheStore;
import br.com.caelum.vraptor.cache.LRU;

import com.thoughtworks.paranamer.AnnotationParanamer;
import com.thoughtworks.paranamer.BytecodeReadingParanamer;
import com.thoughtworks.paranamer.CachingParanamer;
import com.thoughtworks.paranamer.ParameterNamesNotFoundException;
import com.thoughtworks.paranamer.Paranamer;

Expand All @@ -43,42 +39,25 @@
public class ParanamerNameProvider implements ParameterNameProvider {
private static final Logger logger = LoggerFactory.getLogger(ParanamerNameProvider.class);

@LRU(capacity = 50)
private final CacheStore<AccessibleObject, Parameter[]> cache;
private final Paranamer info = new AnnotationParanamer(new BytecodeReadingParanamer());

protected ParanamerNameProvider() {
this(null);
}

@Inject
public ParanamerNameProvider(CacheStore<AccessibleObject, Parameter[]> cache) {
this.cache = cache;
}
private final Paranamer info = new CachingParanamer(new AnnotationParanamer(new BytecodeReadingParanamer()));

@Override
public Parameter[] parametersFor(final AccessibleObject executable) {
Parameter[] params = cache.fetch(executable, new Callable<Parameter[]>() {
@Override
public Parameter[] call() throws Exception {
try {
String[] names = info.lookupParameterNames(executable);
Parameter[] params = new Parameter[names.length];
logger.debug("Found parameter names with paranamer for {} as {}", executable, (Object) names);

for (int i = 0; i < names.length; i++) {
params[i] = new Parameter(i, names[i], executable);
}

return params;
} catch (ParameterNamesNotFoundException e) {
throw new IllegalStateException("Paranamer were not able to find your parameter names for " + executable
+ "You must compile your code with debug information (javac -g), or using @Named on "
+ "each method parameter.", e);
}
try {
String[] names = info.lookupParameterNames(executable);
Parameter[] params = new Parameter[names.length];
logger.debug("Found parameter names with paranamer for {} as {}", executable, (Object) names);

for (int i = 0; i < names.length; i++) {
params[i] = new Parameter(i, names[i], executable);
}
});
return defensiveCopy(params);

return defensiveCopy(params);
} catch (ParameterNamesNotFoundException e) {
throw new IllegalStateException("Paranamer were not able to find your parameter names for " + executable
+ "You must compile your code with debug information (javac -g), or using @Named on "
+ "each method parameter.", e);
}
}

private Parameter[] defensiveCopy(Parameter[] src) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import br.com.caelum.vraptor.converter.PrimitiveLongConverter;
import br.com.caelum.vraptor.converter.StringConverter;
import br.com.caelum.vraptor.core.Converters;
import br.com.caelum.vraptor.factory.Factories;
import br.com.caelum.vraptor.ioc.Container;
import br.com.caelum.vraptor.validator.DefaultValidationException;
import br.com.caelum.vraptor.validator.Message;
Expand Down Expand Up @@ -72,7 +71,7 @@ public abstract class ParametersProviderTest {
@Before
public void setup() throws Exception {
MockitoAnnotations.initMocks(this);
nameProvider = Factories.createParameterNameProvider();
nameProvider = new ParanamerNameProvider();
this.provider = getProvider();
this.errors = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,17 @@
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;

import java.lang.reflect.AccessibleObject;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Named;

import org.junit.Before;
import org.junit.Test;

import br.com.caelum.vraptor.cache.CacheStore;
import br.com.caelum.vraptor.cache.DefaultCacheStore;

public class ParanamerNameProviderTest {

private ParanamerNameProvider provider;
private ParanamerNameProvider provider = new ParanamerNameProvider();

@Before
public void setup() {
CacheStore<AccessibleObject, Parameter[]> cache = new DefaultCacheStore<>();
provider = new ParanamerNameProvider(cache);
}

private List<String> toNames(Parameter[] parameters) {
List<String> out = new ArrayList<>();
for (Parameter p : parameters)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@

import br.com.caelum.vraptor.TwoWayConverter;
import br.com.caelum.vraptor.core.Converters;
import br.com.caelum.vraptor.factory.Factories;
import br.com.caelum.vraptor.http.EncodingHandler;
import br.com.caelum.vraptor.http.MutableRequest;
import br.com.caelum.vraptor.http.ParameterNameProvider;
import br.com.caelum.vraptor.http.ParanamerNameProvider;

public class DefaultParametersControlTest {

Expand All @@ -55,7 +55,7 @@ public void setup() {
MockitoAnnotations.initMocks(this);
when(encodingHandler.getEncoding()).thenReturn("UTF-8");
evaluator = new JavaEvaluator();
nameProvider = Factories.createParameterNameProvider();
nameProvider = new ParanamerNameProvider();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@
import br.com.caelum.vraptor.controller.DefaultControllerMethod;
import br.com.caelum.vraptor.controller.HttpMethod;
import br.com.caelum.vraptor.core.Converters;
import br.com.caelum.vraptor.factory.Factories;
import br.com.caelum.vraptor.http.EncodingHandler;
import br.com.caelum.vraptor.http.ParameterNameProvider;
import br.com.caelum.vraptor.http.ParanamerNameProvider;
import br.com.caelum.vraptor.http.VRaptorRequest;
import br.com.caelum.vraptor.interceptor.VRaptorMatchers;
import br.com.caelum.vraptor.proxy.JavassistProxifier;
Expand Down Expand Up @@ -79,7 +79,7 @@ public void setup() {
this.method = mock(ControllerMethod.class);
this.converters = mock(Converters.class);
this.encodingHandler = mock(EncodingHandler.class);
this.nameProvider = Factories.createParameterNameProvider();
this.nameProvider = new ParanamerNameProvider();
this.cache = new DefaultCacheStore<>();

router = new DefaultRouter(proxifier, new NoTypeFinder(), converters, nameProvider, new JavaEvaluator(), encodingHandler,cache);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@
import org.junit.Before;
import org.junit.Test;

import br.com.caelum.vraptor.factory.Factories;
import br.com.caelum.vraptor.http.ParameterNameProvider;
import br.com.caelum.vraptor.http.ParanamerNameProvider;

public class DefaultTypeFinderTest {

private ParameterNameProvider provider;

@Before
public void setup() {
provider = Factories.createParameterNameProvider();
provider = new ParanamerNameProvider();
}

public static class AController {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@
import br.com.caelum.vraptor.controller.DefaultBeanClass;
import br.com.caelum.vraptor.controller.HttpMethod;
import br.com.caelum.vraptor.core.Converters;
import br.com.caelum.vraptor.factory.Factories;
import br.com.caelum.vraptor.http.EncodingHandler;
import br.com.caelum.vraptor.http.ParameterNameProvider;
import br.com.caelum.vraptor.http.ParanamerNameProvider;
import br.com.caelum.vraptor.proxy.JavassistProxifier;
import br.com.caelum.vraptor.proxy.Proxifier;

Expand All @@ -73,7 +73,7 @@ public void setup() {

this.proxifier = new JavassistProxifier();
this.typeFinder = new NoTypeFinder();
this.nameProvider = Factories.createParameterNameProvider();
this.nameProvider = new ParanamerNameProvider();

when(router.builderFor(anyString())).thenAnswer(new Answer<DefaultRouteBuilder>() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
import br.com.caelum.vraptor.controller.DefaultBeanClass;
import br.com.caelum.vraptor.controller.DefaultControllerMethod;
import br.com.caelum.vraptor.core.Converters;
import br.com.caelum.vraptor.factory.Factories;
import br.com.caelum.vraptor.http.EncodingHandler;
import br.com.caelum.vraptor.http.ParameterNameProvider;
import br.com.caelum.vraptor.http.ParanamerNameProvider;
import br.com.caelum.vraptor.proxy.JavassistProxifier;
import br.com.caelum.vraptor.proxy.Proxifier;

Expand All @@ -61,7 +61,7 @@ public void method(String abc, Integer def, BigDecimal ghi) {
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);

provider = Factories.createParameterNameProvider();
provider = new ParanamerNameProvider();

method = new DefaultControllerMethod(new DefaultBeanClass(MyResource.class), MyResource.class.getMethod(
"method", String.class, Integer.class, BigDecimal.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import br.com.caelum.vraptor.controller.DefaultControllerMethod;
import br.com.caelum.vraptor.core.MethodInfo;
import br.com.caelum.vraptor.events.InterceptorsReady;
import br.com.caelum.vraptor.factory.Factories;
import br.com.caelum.vraptor.http.ParanamerNameProvider;
import br.com.caelum.vraptor.ioc.Container;
import br.com.caelum.vraptor.serialization.Deserializer;
import br.com.caelum.vraptor.serialization.Deserializers;
Expand All @@ -43,7 +43,7 @@ public class DeserializingObserverTest {
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);

methodInfo = new MethodInfo(Factories.createParameterNameProvider());
methodInfo = new MethodInfo(new ParanamerNameProvider());

observer = new DeserializingObserver(deserializers, container);
consumeXml = new DefaultControllerMethod(null, DummyResource.class.getDeclaredMethod("consumeXml", String.class, String.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,17 @@
import br.com.caelum.vraptor.controller.DefaultControllerMethod;
import br.com.caelum.vraptor.core.MethodInfo;
import br.com.caelum.vraptor.events.InterceptorsReady;
import br.com.caelum.vraptor.factory.Factories;
import br.com.caelum.vraptor.http.MutableRequest;
import br.com.caelum.vraptor.http.ParametersProvider;
import br.com.caelum.vraptor.http.ParanamerNameProvider;
import br.com.caelum.vraptor.validator.Message;
import br.com.caelum.vraptor.validator.SimpleMessage;
import br.com.caelum.vraptor.validator.Validator;
import br.com.caelum.vraptor.view.FlashScope;

public class ParametersInstantiatorTest {

private MethodInfo methodInfo = new MethodInfo(Factories.createParameterNameProvider());
private MethodInfo methodInfo = new MethodInfo(new ParanamerNameProvider());
private @Mock ParametersProvider parametersProvider;
private @Mock Validator validator;
private @Mock ResourceBundle bundle;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import br.com.caelum.vraptor.controller.ControllerMethod;
import br.com.caelum.vraptor.controller.DefaultBeanClass;
import br.com.caelum.vraptor.controller.DefaultControllerMethod;
import br.com.caelum.vraptor.factory.Factories;
import br.com.caelum.vraptor.http.ParameterNameProvider;
import br.com.caelum.vraptor.http.ParanamerNameProvider;
import br.com.caelum.vraptor.util.test.MockInstanceImpl;
import br.com.caelum.vraptor.view.GenericController;

Expand All @@ -57,7 +57,7 @@ public class GsonDeserializerTest {

@Before
public void setUp() throws Exception {
provider = Factories.createParameterNameProvider();
provider = new ParanamerNameProvider();
request = mock(HttpServletRequest.class);

List<JsonDeserializer<?>> jsonDeserializers = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import br.com.caelum.vraptor.controller.ControllerMethod;
import br.com.caelum.vraptor.controller.DefaultBeanClass;
import br.com.caelum.vraptor.controller.DefaultControllerMethod;
import br.com.caelum.vraptor.factory.Factories;
import br.com.caelum.vraptor.http.ParameterNameProvider;
import br.com.caelum.vraptor.http.ParanamerNameProvider;

import com.thoughtworks.xstream.annotations.XStreamAlias;

Expand All @@ -35,7 +35,7 @@ public class XStreamXmlDeserializerTest {

@Before
public void setUp() throws Exception {
provider = Factories.createParameterNameProvider();
provider = new ParanamerNameProvider();

deserializer = new XStreamXMLDeserializer(provider, cleanInstance(new CalendarConverter()));
BeanClass controllerClass = new DefaultBeanClass(DogController.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import br.com.caelum.vraptor.controller.DefaultControllerInstance;
import br.com.caelum.vraptor.core.MethodInfo;
import br.com.caelum.vraptor.events.MethodReady;
import br.com.caelum.vraptor.factory.Factories;
import br.com.caelum.vraptor.http.ParanamerNameProvider;
import br.com.caelum.vraptor.util.test.MockValidator;
import br.com.caelum.vraptor.validator.beanvalidation.MessageInterpolatorFactory;
import br.com.caelum.vraptor.validator.beanvalidation.MethodValidator;
Expand All @@ -45,7 +45,7 @@ public class MethodValidatorTest {
private ControllerMethod withoutConstraint;
private DefaultControllerInstance instance;

private MethodInfo methodInfo = new MethodInfo(Factories.createParameterNameProvider());
private MethodInfo methodInfo = new MethodInfo(new ParanamerNameProvider());

@Before
public void setup() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
import br.com.caelum.vraptor.controller.ControllerMethod;
import br.com.caelum.vraptor.controller.DefaultControllerMethod;
import br.com.caelum.vraptor.core.MethodInfo;
import br.com.caelum.vraptor.factory.Factories;
import br.com.caelum.vraptor.http.ParanamerNameProvider;

public class ReplicatorOutjectorTest {

private @Mock Result result;
private MethodInfo methodInfo = new MethodInfo(Factories.createParameterNameProvider());
private MethodInfo methodInfo = new MethodInfo(new ParanamerNameProvider());
private Outjector outjector;

@Before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@
import br.com.caelum.vraptor.controller.ControllerMethod;
import br.com.caelum.vraptor.controller.DefaultControllerMethod;
import br.com.caelum.vraptor.core.MethodInfo;
import br.com.caelum.vraptor.factory.Factories;
import br.com.caelum.vraptor.http.MutableRequest;
import br.com.caelum.vraptor.http.MutableResponse;
import br.com.caelum.vraptor.http.ParanamerNameProvider;
import br.com.caelum.vraptor.http.route.Router;
import br.com.caelum.vraptor.interceptor.TypeNameExtractor;
import br.com.caelum.vraptor.ioc.Container;
Expand Down Expand Up @@ -112,7 +112,7 @@ public void setup() {
MockitoAnnotations.initMocks(this);

proxifier = new JavassistProxifier();
methodInfo = new MethodInfo(Factories.createParameterNameProvider());
methodInfo = new MethodInfo(new ParanamerNameProvider());
this.logicResult = new DefaultLogicResult(proxifier, router, request, response, container,
resolver, extractor, flash, methodInfo);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
import br.com.caelum.vraptor.controller.ControllerMethod;
import br.com.caelum.vraptor.controller.DefaultControllerMethod;
import br.com.caelum.vraptor.core.MethodInfo;
import br.com.caelum.vraptor.factory.Factories;
import br.com.caelum.vraptor.http.MutableRequest;
import br.com.caelum.vraptor.http.MutableResponse;
import br.com.caelum.vraptor.http.ParanamerNameProvider;
import br.com.caelum.vraptor.interceptor.ApplicationLogicException;
import br.com.caelum.vraptor.proxy.JavassistProxifier;
import br.com.caelum.vraptor.proxy.Proxifier;
Expand All @@ -62,7 +62,7 @@ public void setup() {
MockitoAnnotations.initMocks(this);
method = DefaultControllerMethod.instanceFor(AnyResource.class, AnyResource.class.getDeclaredMethods()[0]);
proxifier = new JavassistProxifier();
methodInfo = new MethodInfo(Factories.createParameterNameProvider());
methodInfo = new MethodInfo(new ParanamerNameProvider());
methodInfo.setControllerMethod(method);
fixedResolver = new PathResolver() {
@Override
Expand Down

0 comments on commit 2bea0f2

Please sign in to comment.