You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mb...@apache.org on 2013/08/19 18:25:59 UTC
svn commit: r1515495 - in
/commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub:
BaseTrainer.java StubBuilder.java Trainer.java
Author: mbenson
Date: Mon Aug 19 16:25:58 2013
New Revision: 1515495
URL: http://svn.apache.org/r1515495
Log:
calculate trainee type at instantiation; support subclassing with self-referencing BaseTrainer class; allow a backing invoker to be specified for a stub; move thenStub() to WhenObject because returning a stub doesn't make sense for any of the other fluent constructs; raw type warning; add special WhenClass construct to be properly documented in a future commit
Added:
commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/BaseTrainer.java
- copied, changed from r1515478, commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/Trainer.java
Modified:
commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubBuilder.java
commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/Trainer.java
Copied: commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/BaseTrainer.java (from r1515478, commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/Trainer.java)
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/BaseTrainer.java?p2=commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/BaseTrainer.java&p1=commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/Trainer.java&r1=1515478&r2=1515495&rev=1515495&view=diff
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/Trainer.java (original)
+++ commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/BaseTrainer.java Mon Aug 19 16:25:58 2013
@@ -18,6 +18,7 @@
package org.apache.commons.proxy2.stub;
import org.apache.commons.lang3.ArrayUtils;
+import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.reflect.TypeUtils;
import org.apache.commons.proxy2.ObjectProvider;
import org.apache.commons.proxy2.ProxyUtils;
@@ -25,9 +26,44 @@ import org.apache.commons.proxy2.interce
import org.apache.commons.proxy2.interceptor.matcher.ArgumentMatcher;
import org.apache.commons.proxy2.interceptor.matcher.argument.ArgumentMatcherUtils;
-public abstract class Trainer<T>
+public abstract class BaseTrainer<S extends BaseTrainer<S, T>, T>
{
//----------------------------------------------------------------------------------------------------------------------
+// Fields
+//----------------------------------------------------------------------------------------------------------------------
+ public final Class<T> traineeType;
+
+//----------------------------------------------------------------------------------------------------------------------
+// Constructors
+//----------------------------------------------------------------------------------------------------------------------
+
+ /**
+ * Create a new {@link BaseTrainer} instance. This constructor should only be called
+ * by classes that explicitly assign the T parameter in the class definition.
+ * This should include basically any runtime-usable class.
+ */
+ protected BaseTrainer()
+ {
+ this(null);
+ }
+
+ protected BaseTrainer(Class<T> traineeType)
+ {
+ super();
+ if (traineeType != null)
+ {
+ this.traineeType = traineeType;
+ return;
+ }
+ @SuppressWarnings("unchecked")
+ final Class<T> resolvedVariable =
+ (Class<T>) TypeUtils.getRawType(BaseTrainer.class.getTypeParameters()[1], getClass());
+ Validate.isTrue(resolvedVariable != null, "Trainee type was not specified and could not be calculated for %s",
+ getClass());
+ this.traineeType = resolvedVariable;
+ }
+
+//----------------------------------------------------------------------------------------------------------------------
// Abstract Methods
//----------------------------------------------------------------------------------------------------------------------
@@ -43,7 +79,7 @@ public abstract class Trainer<T>
return null;
}
- private void record(ArgumentMatcher matcher)
+ private void record(ArgumentMatcher<?> matcher)
{
trainingContext().record(matcher);
}
@@ -54,12 +90,6 @@ public abstract class Trainer<T>
return value;
}
- @SuppressWarnings("unchecked")
- public Class<T> getTraineeType()
- {
- return (Class<T>) TypeUtils.getRawType(Trainer.class.getTypeParameters()[0], getClass());
- }
-
protected <R> R isInstance(Class<R> type)
{
record(ArgumentMatcherUtils.isA(type));
@@ -76,7 +106,7 @@ public abstract class Trainer<T>
trainingContext().then(InterceptorUtils.throwing(provider));
}
- private TrainingContext trainingContext()
+ protected TrainingContext trainingContext()
{
return TrainingContext.getCurrent();
}
@@ -86,6 +116,11 @@ public abstract class Trainer<T>
return new WhenObject<R>();
}
+ protected WhenClass when(Class<?> expression)
+ {
+ return new WhenClass();
+ }
+
protected WhenByteArray when(byte[] expression)
{
return new WhenByteArray();
@@ -131,126 +166,141 @@ public abstract class Trainer<T>
return new WhenCharArray();
}
+ @SuppressWarnings("unchecked")
+ protected S self()
+ {
+ return (S) this;
+ }
+
//----------------------------------------------------------------------------------------------------------------------
// Inner Classes
//----------------------------------------------------------------------------------------------------------------------
protected abstract class BaseWhen<R>
{
- protected Trainer<T> thenStub(Trainer<R> trainer)
- {
- R trainee = trainingContext().push(trainer.getTraineeType());
- trainer.train(trainee);
- trainingContext().then(InterceptorUtils.constant(trainingContext().pop()));
- return Trainer.this;
- }
-
- protected Trainer<T> thenThrow(Exception e)
+ protected S thenThrow(Exception e)
{
trainingContext().then(InterceptorUtils.throwing(e));
- return Trainer.this;
+ return self();
}
- protected Trainer<T> thenThrow(ObjectProvider<? extends Exception> provider)
+ protected S thenThrow(ObjectProvider<? extends Exception> provider)
{
trainingContext().then(InterceptorUtils.throwing(provider));
- return Trainer.this;
+ return self();
}
- protected <R> Trainer<T> thenAnswer(ObjectProvider<? extends R> provider)
+ protected S thenAnswer(ObjectProvider<? extends R> provider)
{
trainingContext().then(InterceptorUtils.provider(provider));
- return Trainer.this;
+ return self();
}
}
protected class WhenBooleanArray extends BaseWhen<boolean[]>
{
- protected Trainer<T> thenReturn(boolean... values)
+ protected S thenReturn(boolean... values)
{
trainingContext().then(InterceptorUtils.constant(ArrayUtils.clone(values)));
- return Trainer.this;
+ return self();
}
}
protected class WhenByteArray extends BaseWhen<byte[]>
{
- protected Trainer<T> thenReturn(byte... values)
+ protected S thenReturn(byte... values)
{
trainingContext().then(InterceptorUtils.constant(ArrayUtils.clone(values)));
- return Trainer.this;
+ return self();
}
}
protected class WhenCharArray extends BaseWhen<char[]>
{
- protected Trainer<T> thenReturn(char... values)
+ protected S thenReturn(char... values)
{
trainingContext().then(InterceptorUtils.constant(ArrayUtils.clone(values)));
- return Trainer.this;
+ return self();
}
}
protected class WhenDoubleArray extends BaseWhen<double[]>
{
- protected Trainer<T> thenReturn(double... values)
+ protected S thenReturn(double... values)
{
trainingContext().then(InterceptorUtils.constant(ArrayUtils.clone(values)));
- return Trainer.this;
+ return self();
}
}
protected class WhenFloatArray extends BaseWhen<float[]>
{
- protected Trainer<T> thenReturn(float... values)
+ protected S thenReturn(float... values)
{
trainingContext().then(InterceptorUtils.constant(ArrayUtils.clone(values)));
- return Trainer.this;
+ return self();
}
}
protected class WhenIntArray extends BaseWhen<int[]>
{
- protected Trainer<T> thenReturn(int... values)
+ protected S thenReturn(int... values)
{
trainingContext().then(InterceptorUtils.constant(ArrayUtils.clone(values)));
- return Trainer.this;
+ return self();
}
}
protected class WhenLongArray extends BaseWhen<long[]>
{
- protected Trainer<T> thenReturn(long... values)
+ protected S thenReturn(long... values)
{
trainingContext().then(InterceptorUtils.constant(ArrayUtils.clone(values)));
- return Trainer.this;
+ return self();
+ }
+ }
+
+ protected class WhenObject<R> extends BaseWhen<R>
+ {
+ protected S thenReturn(R value)
+ {
+ trainingContext().then(InterceptorUtils.constant(value));
+ return self();
+ }
+
+ protected S thenStub(BaseTrainer<?, R> trainer)
+ {
+ final R trainee = trainingContext().push(trainer.traineeType);
+ trainer.train(trainee);
+ trainingContext().then(InterceptorUtils.constant(trainingContext().pop()));
+ return self();
}
}
- protected class WhenObject<R> extends BaseWhen
+ protected class WhenClass extends BaseWhen<Class<?>>
{
- protected Trainer<T> thenReturn(R value)
+ protected S thenReturn(Class<?> value)
{
trainingContext().then(InterceptorUtils.constant(value));
- return Trainer.this;
+ return self();
}
}
protected class WhenObjectArray<R> extends BaseWhen<R[]>
{
- protected Trainer<T> thenReturn(R... values)
+ protected S thenReturn(R... values)
{
trainingContext().then(InterceptorUtils.constant(ArrayUtils.clone(values)));
- return Trainer.this;
+ return self();
}
}
protected class WhenShortArray extends BaseWhen<short[]>
{
- protected Trainer<T> thenReturn(short... values)
+ protected S thenReturn(short... values)
{
trainingContext().then(InterceptorUtils.constant(ArrayUtils.clone(values)));
- return Trainer.this;
+ return self();
}
}
}
Modified: commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubBuilder.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubBuilder.java?rev=1515495&r1=1515494&r2=1515495&view=diff
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubBuilder.java (original)
+++ commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/StubBuilder.java Mon Aug 19 16:25:58 2013
@@ -18,6 +18,7 @@
package org.apache.commons.proxy2.stub;
import org.apache.commons.lang3.builder.Builder;
+import org.apache.commons.proxy2.Invoker;
import org.apache.commons.proxy2.ObjectProvider;
import org.apache.commons.proxy2.ProxyFactory;
import org.apache.commons.proxy2.interceptor.SwitchInterceptor;
@@ -30,9 +31,9 @@ public class StubBuilder<T> implements B
// Fields
//----------------------------------------------------------------------------------------------------------------------
+ protected final Class<T> type;
private final ProxyFactory proxyFactory;
private final T target;
- private final Class<T> type;
private final SwitchInterceptor switchInterceptor = new SwitchInterceptor();
//----------------------------------------------------------------------------------------------------------------------
@@ -41,11 +42,16 @@ public class StubBuilder<T> implements B
public StubBuilder(ProxyFactory proxyFactory, Class<T> type)
{
+ this(proxyFactory, type, NullInvoker.INSTANCE);
+ }
+
+ public StubBuilder(ProxyFactory proxyFactory, Class<T> type, Invoker invoker)
+ {
this.proxyFactory = proxyFactory;
this.type = type;
- this.target = proxyFactory.createInvokerProxy(NullInvoker.INSTANCE, type);
+ this.target = proxyFactory.createInvokerProxy(invoker, type);
}
-
+
public StubBuilder(ProxyFactory proxyFactory, Class<T> type, ObjectProvider<? extends T> provider)
{
this.proxyFactory = proxyFactory;
@@ -69,7 +75,7 @@ public class StubBuilder<T> implements B
return proxyFactory.createInterceptorProxy(target, switchInterceptor, type);
}
- public StubBuilder<T> train(Trainer<T> trainer)
+ public StubBuilder<T> train(BaseTrainer<?, T> trainer)
{
try
{
Modified: commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/Trainer.java
URL: http://svn.apache.org/viewvc/commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/Trainer.java?rev=1515495&r1=1515494&r2=1515495&view=diff
==============================================================================
--- commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/Trainer.java (original)
+++ commons/proper/proxy/branches/version-2.0-work/stub/src/main/java/org/apache/commons/proxy2/stub/Trainer.java Mon Aug 19 16:25:58 2013
@@ -17,240 +17,14 @@
package org.apache.commons.proxy2.stub;
-import org.apache.commons.lang3.ArrayUtils;
-import org.apache.commons.lang3.reflect.TypeUtils;
-import org.apache.commons.proxy2.ObjectProvider;
-import org.apache.commons.proxy2.ProxyUtils;
-import org.apache.commons.proxy2.interceptor.InterceptorUtils;
-import org.apache.commons.proxy2.interceptor.matcher.ArgumentMatcher;
-import org.apache.commons.proxy2.interceptor.matcher.argument.ArgumentMatcherUtils;
+public abstract class Trainer<T> extends BaseTrainer<Trainer<T>, T> {
-public abstract class Trainer<T>
-{
-//----------------------------------------------------------------------------------------------------------------------
-// Abstract Methods
-//----------------------------------------------------------------------------------------------------------------------
-
- protected abstract void train(T trainee);
-
-//----------------------------------------------------------------------------------------------------------------------
-// Other Methods
-//----------------------------------------------------------------------------------------------------------------------
-
- protected <R> R any(Class<R> type)
- {
- record(ArgumentMatcherUtils.any());
- return null;
- }
-
- private void record(ArgumentMatcher matcher)
- {
- trainingContext().record(matcher);
- }
-
- protected <R> R eq(R value)
- {
- record(ArgumentMatcherUtils.eq(value));
- return value;
- }
-
- @SuppressWarnings("unchecked")
- public Class<T> getTraineeType()
- {
- return (Class<T>) TypeUtils.getRawType(Trainer.class.getTypeParameters()[0], getClass());
- }
-
- protected <R> R isInstance(Class<R> type)
- {
- record(ArgumentMatcherUtils.isA(type));
- return ProxyUtils.nullValue(type);
- }
-
- protected void thenThrow(Exception e)
- {
- trainingContext().then(InterceptorUtils.throwing(e));
- }
-
- protected void thenThrow(ObjectProvider<? extends Exception> provider)
- {
- trainingContext().then(InterceptorUtils.throwing(provider));
- }
-
- private TrainingContext trainingContext()
- {
- return TrainingContext.getCurrent();
- }
-
- protected <R> WhenObject<R> when(R expression)
- {
- return new WhenObject<R>();
- }
-
- protected WhenByteArray when(byte[] expression)
- {
- return new WhenByteArray();
- }
-
- protected WhenBooleanArray when(boolean[] expression)
- {
- return new WhenBooleanArray();
- }
-
- protected WhenIntArray when(int[] expression)
- {
- return new WhenIntArray();
- }
-
- protected WhenShortArray when(short[] expresssion)
- {
- return new WhenShortArray();
- }
-
- protected WhenLongArray when(long[] expression)
- {
- return new WhenLongArray();
- }
-
- protected WhenFloatArray when(float[] expression)
- {
- return new WhenFloatArray();
- }
-
- protected WhenDoubleArray when(double[] expression)
- {
- return new WhenDoubleArray();
- }
-
- protected <R> WhenObjectArray<R> when(R[] expression)
- {
- return new WhenObjectArray<R>();
- }
-
- protected WhenCharArray when(char[] expression)
- {
- return new WhenCharArray();
- }
-
-//----------------------------------------------------------------------------------------------------------------------
-// Inner Classes
-//----------------------------------------------------------------------------------------------------------------------
-
- protected abstract class BaseWhen<R>
- {
- protected Trainer<T> thenStub(Trainer<R> trainer)
- {
- R trainee = trainingContext().push(trainer.getTraineeType());
- trainer.train(trainee);
- trainingContext().then(InterceptorUtils.constant(trainingContext().pop()));
- return Trainer.this;
- }
-
- protected Trainer<T> thenThrow(Exception e)
- {
- trainingContext().then(InterceptorUtils.throwing(e));
- return Trainer.this;
- }
-
- protected Trainer<T> thenThrow(ObjectProvider<? extends Exception> provider)
- {
- trainingContext().then(InterceptorUtils.throwing(provider));
- return Trainer.this;
- }
-
- protected <R> Trainer<T> thenAnswer(ObjectProvider<? extends R> provider)
- {
- trainingContext().then(InterceptorUtils.provider(provider));
- return Trainer.this;
- }
- }
-
- protected class WhenBooleanArray extends BaseWhen<boolean[]>
- {
- protected Trainer<T> thenReturn(boolean... values)
- {
- trainingContext().then(InterceptorUtils.constant(ArrayUtils.clone(values)));
- return Trainer.this;
- }
+ protected Trainer() {
+ super();
}
- protected class WhenByteArray extends BaseWhen<byte[]>
- {
- protected Trainer<T> thenReturn(byte... values)
- {
- trainingContext().then(InterceptorUtils.constant(ArrayUtils.clone(values)));
- return Trainer.this;
- }
+ protected Trainer(Class<T> traineeType) {
+ super(traineeType);
}
- protected class WhenCharArray extends BaseWhen<char[]>
- {
- protected Trainer<T> thenReturn(char... values)
- {
- trainingContext().then(InterceptorUtils.constant(ArrayUtils.clone(values)));
- return Trainer.this;
- }
- }
-
- protected class WhenDoubleArray extends BaseWhen<double[]>
- {
- protected Trainer<T> thenReturn(double... values)
- {
- trainingContext().then(InterceptorUtils.constant(ArrayUtils.clone(values)));
- return Trainer.this;
- }
- }
-
- protected class WhenFloatArray extends BaseWhen<float[]>
- {
- protected Trainer<T> thenReturn(float... values)
- {
- trainingContext().then(InterceptorUtils.constant(ArrayUtils.clone(values)));
- return Trainer.this;
- }
- }
-
- protected class WhenIntArray extends BaseWhen<int[]>
- {
- protected Trainer<T> thenReturn(int... values)
- {
- trainingContext().then(InterceptorUtils.constant(ArrayUtils.clone(values)));
- return Trainer.this;
- }
- }
-
- protected class WhenLongArray extends BaseWhen<long[]>
- {
- protected Trainer<T> thenReturn(long... values)
- {
- trainingContext().then(InterceptorUtils.constant(ArrayUtils.clone(values)));
- return Trainer.this;
- }
- }
-
- protected class WhenObject<R> extends BaseWhen
- {
- protected Trainer<T> thenReturn(R value)
- {
- trainingContext().then(InterceptorUtils.constant(value));
- return Trainer.this;
- }
- }
-
- protected class WhenObjectArray<R> extends BaseWhen<R[]>
- {
- protected Trainer<T> thenReturn(R... values)
- {
- trainingContext().then(InterceptorUtils.constant(ArrayUtils.clone(values)));
- return Trainer.this;
- }
- }
-
- protected class WhenShortArray extends BaseWhen<short[]>
- {
- protected Trainer<T> thenReturn(short... values)
- {
- trainingContext().then(InterceptorUtils.constant(ArrayUtils.clone(values)));
- return Trainer.this;
- }
- }
}