Skip to content

Instantly share code, notes, and snippets.

Created July 5, 2011 02:45
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save lucascs/1064176 to your computer and use it in GitHub Desktop.
Anatomia de uma solução - VRaptor linkTo para jsp
public class LinkToHandler implements StereotypeHandler {
private Map<String, Map<String, Linker>> linkTo = Maps.newHashMap();
private final ServletContext context;
private final Router router;
public LinkToHandler(ServletContext context, Router router) {
this.context = context;
this.router = router;
context.setAttribute("linkTo", linkTo); //para ficar disponível para todos os jsps
public void handle(Class<?> type) {
String controller = type.getSimpleName();
context.setAttribute(controller, controller); //para poder usar linkTo[ProdutoController]
Map<String, Linker> methodMap = Maps.newHashMap();
List<Method> methods = new Mirror().on(type).reflectAll().methods();
for (Method method : methods) {
if (!method.getDeclaringClass().equals(Object.class)) {
methodMap.put(method.getName(), new Linker(type, method));
linkTo.put(controller, methodMap);
public Class<? extends Annotation> stereotype() {
return Resource.class;
class Linker extends ForwardingMap<Object, Linker> {
private final Method method;
private final Class<?> type;
private final Object[] args;
private final int index;
public Linker(Class<?> type, Method method) {
this(type, method, new Object[method.getParameterTypes().length], 0);
public Linker(Class<?> type, Method method, Object[] args, int index) {
this.type = type;
this.method = method;
this.args = args;
this.index = index;
protected Map<Object, Linker> delegate() {
return Maps.newHashMap();
public Linker get(Object key) {
Object[] newArgs = args.clone(); //evitando cache indesejável de args
newArgs[index] = key;
return new Linker(type, method, newArgs, index + 1);
public String toString() {
return router.urlFor(type, method, args);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment