001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.proxy2.invoker.recorder; 019 020import java.lang.reflect.Method; 021import java.lang.reflect.Type; 022import java.util.LinkedList; 023import java.util.List; 024 025import org.apache.commons.lang3.reflect.TypeUtils; 026import org.apache.commons.proxy2.Invoker; 027import org.apache.commons.proxy2.ProxyFactory; 028import org.apache.commons.proxy2.ProxyUtils; 029import org.apache.commons.proxy2.invoker.RecordedInvocation; 030 031/** 032 * An {@link InvocationRecorder} records method invocations against its generated proxies. 033 * 034 */ 035public class InvocationRecorder 036{ 037 private final ProxyFactory proxyFactory; 038 private final List<RecordedInvocation> recordedInvocations = new LinkedList<RecordedInvocation>(); 039 040 /** 041 * Create a new InvocationRecorder instance. 042 * 043 * @param proxyFactory 044 */ 045 public InvocationRecorder(ProxyFactory proxyFactory) 046 { 047 this.proxyFactory = proxyFactory; 048 } 049 050 /** 051 * Get the invocations that have been recorded up to this point. The list is "live" and should not be modified. 052 * 053 * @return {@link List} of {@link RecordedInvocation} 054 */ 055 public List<RecordedInvocation> getRecordedInvocations() 056 { 057 return recordedInvocations; 058 } 059 060 /** 061 * Generate a recording proxy for the specified class. 062 * 063 * @param <T> 064 * @param type 065 * @return the generated proxy 066 */ 067 public <T> T proxy(Class<T> type) 068 { 069 return proxy(type, type); 070 } 071 072 /** 073 * Generate a recording proxy for the specified class, qualified as <code>genericType</code>. 074 * 075 * @param <T> 076 * @param genericType 077 * @param type 078 * @return the generated proxy 079 */ 080 public <T> T proxy(Type genericType, Class<T> type) 081 { 082 if (proxyFactory.canProxy(type)) 083 { 084 return proxyFactory.<T> createInvokerProxy(new InvocationRecorderInvoker(genericType), type); 085 } 086 return ProxyUtils.nullValue(type); 087 } 088 089 private final class InvocationRecorderInvoker implements Invoker 090 { 091 /** Serialization version */ 092 private static final long serialVersionUID = 1L; 093 094 private final Type targetType; 095 096 private InvocationRecorderInvoker(Type targetType) 097 { 098 this.targetType = targetType; 099 } 100 101 /** 102 * {@inheritDoc} 103 */ 104 @Override 105 public Object invoke(Object o, Method method, Object[] args) throws Throwable 106 { 107 recordedInvocations.add(new RecordedInvocation(method, args)); 108 final Class<?> returnType = TypeUtils.getRawType(method.getGenericReturnType(), targetType); 109 //what to do if returnType is null? 110 return proxy(method.getGenericReturnType(), returnType); 111 } 112 } 113 114 /** 115 * Reset this {@link InvocationRecorder}. 116 */ 117 public void reset() 118 { 119 recordedInvocations.clear(); 120 } 121 122}