Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2012-2014 the original author or authors.
   *
   * 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 org.springframework.hateoas.core;
 
 
Utility methods to capture dummy method invocations.

Author(s):
Oliver Gierke
 
 public class DummyInvocationUtils {
 
 	private static ObjenesisStd OBJENESIS = new ObjenesisStd();
 
 	public interface LastInvocationAware {
 
 
 	}

Method interceptor that records the last method invocation and creates a proxy for the return value that exposes the method invocation.

Author(s):
Oliver Gierke
 
 
 		private static final Method GET_INVOCATIONS;
 		private static final Method GET_OBJECT_PARAMETERS;
 
 		private final Class<?> targetType;
 		private final Object[] objectParameters;
 
 		static {
 			 = ReflectionUtils.findMethod(LastInvocationAware.class"getLastInvocation");
 			 = ReflectionUtils.findMethod(LastInvocationAware.class"getObjectParameters");
 		}

Creates a new DummyInvocationUtils.InvocationRecordingMethodInterceptor carrying the given parameters forward that might be needed to populate the class level mapping.

Parameters:
parameters
 
 		public InvocationRecordingMethodInterceptor(Class<?> targetTypeObject... parameters) {
 
 			this. = targetType;
 			this. = parameters.clone();
 		}
 
 		/*
 		 * (non-Javadoc)
 		 * @see org.springframework.cglib.proxy.MethodInterceptor#intercept(java.lang.Object, java.lang.reflect.Method, java.lang.Object[], org.springframework.cglib.proxy.MethodProxy)
 		 */
 		public Object intercept(Object objMethod methodObject[] argsMethodProxy proxy) {
 
 			if (.equals(method)) {
 				return getLastInvocation();
 			} else if (.equals(method)) {
 				return getObjectParameters();
 			} else if (Object.class.equals(method.getDeclaringClass())) {
 				return ReflectionUtils.invokeMethod(methodobjargs);
 			}
 
 			this. = new SimpleMethodInvocation(methodargs);
 
 			Class<?> returnType = method.getReturnType();
 			return returnType.cast(getProxyWithInterceptor(returnTypethis));
		}
		/* 
		 * (non-Javadoc)
		 * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
		 */
			return intercept(invocation.getThis(), invocation.getMethod(), invocation.getArguments(), null);
		}
		/*
		 * (non-Javadoc)
		 * @see org.springframework.hateoas.core.DummyInvocationUtils.LastInvocationAware#getLastInvocation()
		 */
			return ;
		}
		/* 
		 * (non-Javadoc)
		 * @see org.springframework.hateoas.core.DummyInvocationUtils.LastInvocationAware#getObjectParameters()
		 */
			return Arrays.asList().iterator();
		}
	}

Returns a proxy of the given type, backed by an org.springframework.aop.target.EmptyTargetSource to simply drop method invocations but equips it with an DummyInvocationUtils.InvocationRecordingMethodInterceptor. The interceptor records the last invocation and returns a proxy of the return type that also implements DummyInvocationUtils.LastInvocationAware so that the last method invocation can be inspected. Parameters passed to the subsequent method invocation are generally neglected except the ones that might be mapped into the URI translation eventually, e.g. PathVariable in the case of Spring MVC. Note, that the return types of the methods have to be capable to be proxied.

Parameters:
type must not be null.
parameters parameters to extend template variables in the type level mapping.
Returns:
	public static <T> T methodOn(Class<T> typeObject... parameters) {
		Assert.notNull(type"Given type must not be null!");
		return getProxyWithInterceptor(typeinterceptor);
	}
	@SuppressWarnings("unchecked")
	private static <T> T getProxyWithInterceptor(Class<?> typeInvocationRecordingMethodInterceptor interceptor) {
		if (type.isInterface()) {
			factory.addInterface(type);
			factory.addAdvice(interceptor);
			return (T) factory.getProxy();
		}
		Enhancer enhancer = new Enhancer();
		enhancer.setSuperclass(type);
		enhancer.setInterfaces(new Class<?>[] { LastInvocationAware.class });
		Factory factory = (Factory.newInstance(enhancer.createClass());
		factory.setCallbacks(new Callback[] { interceptor });
		return (T) factory;
	}
	public interface MethodInvocation {
	}
	static class SimpleMethodInvocation implements MethodInvocation {
		private final Class<?> targetType;
		private final Method method;
		private final Object[] arguments;

Creates a new DummyInvocationUtils.SimpleMethodInvocation for the given java.lang.reflect.Method and arguments.

Parameters:
method
arguments
		private SimpleMethodInvocation(Class<?> targetTypeMethod methodObject[] arguments) {
			this. = targetType;
			this. = arguments;
			this. = method;
		}
		/* 
		 * (non-Javadoc)
		 * @see org.springframework.hateoas.core.DummyInvocationUtils.MethodInvocation#getTargetType()
		 */
		public Class<?> getTargetType() {
			return ;
		}
		/* 
		 * (non-Javadoc)
		 * @see org.springframework.hateoas.core.DummyInvocationUtils.MethodInvocation#getArguments()
		 */
		public Object[] getArguments() {
			return ;
		}
		/* 
		 * (non-Javadoc)
		 * @see org.springframework.hateoas.core.DummyInvocationUtils.MethodInvocation#getMethod()
		 */
		public Method getMethod() {
			return ;
		}
	}
New to GrepCode? Check out our FAQ X