下面列出了怎么用org.joda.time.base.BaseLocal的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public Object create(Object request, SpecimenContext context) {
if (!(request instanceof SpecimenType)) {
return new NoSpecimen();
}
SpecimenType type = (SpecimenType) request;
if (!BaseLocal.class.isAssignableFrom(type.getRawType())) {
return new NoSpecimen();
}
Date date = (Date) context.resolve(Date.class);
long instant = date.getTime();
try {
return type.getRawType().getDeclaredConstructor(long.class).newInstance(instant);
} catch (Exception e) {
e.printStackTrace();
return new NoSpecimen();
}
}
@Override
public boolean canHandle(Object source, TypeToken<?> targetTypeToken) {
return targetTypeToken.isSubtypeOf(BaseLocal.class) && stringToDateConverter.canHandle(source, TypeToken.of(Date.class));
}
@Override
public boolean canHandle(Object source, TypeToken<?> targetTypeToken) {
return targetTypeToken.isSubtypeOf(BaseLocal.class) && (source instanceof Date);
}
@Override
protected StackManipulation convertDateTime(TypeDescriptor<?> type) {
// The setter might be called with a different subclass of ReadableInstant than the one stored
// in this POJO. We must extract the value passed into the setter and copy it into an instance
// that the POJO can accept.
// Generate the following code:
// return new T(value.getMillis());
// Unless T is a sub-class of BaseLocal. Then generate:
// return new T(value.getMillis(), DateTimeZone.UTC);
ForLoadedType loadedType = new ForLoadedType(type.getRawType());
List<StackManipulation> stackManipulations = new ArrayList<>();
// Create a new instance of the target type.
stackManipulations.add(TypeCreation.of(loadedType));
stackManipulations.add(Duplication.SINGLE);
// Load the parameter and cast it to a ReadableInstant.
stackManipulations.add(readValue);
stackManipulations.add(TypeCasting.to(READABLE_INSTANT_TYPE));
// Call ReadableInstant.getMillis to extract the millis since the epoch.
stackManipulations.add(
MethodInvocation.invoke(
READABLE_INSTANT_TYPE
.getDeclaredMethods()
.filter(ElementMatchers.named("getMillis"))
.getOnly()));
if (type.isSubtypeOf(TypeDescriptor.of(BaseLocal.class))) {
// Access DateTimeZone.UTC
stackManipulations.add(
FieldAccess.forField(
DATE_TIME_ZONE_TYPE
.getDeclaredFields()
.filter(ElementMatchers.named("UTC"))
.getOnly())
.read());
// All subclasses of BaseLocal contain a ()(long, DateTimeZone) constructor
// that takes in a millis and time zone argument. Call that constructor of the field to
// initialize it.
stackManipulations.add(
MethodInvocation.invoke(
loadedType
.getDeclaredMethods()
.filter(
ElementMatchers.isConstructor()
.and(
ElementMatchers.takesArguments(
ForLoadedType.of(long.class), DATE_TIME_ZONE_TYPE)))
.getOnly()));
} else {
// All subclasses of ReadableInstant and ReadablePartial contain a ()(long) constructor
// that takes in a millis argument. Call that constructor of the field to initialize it.
stackManipulations.add(
MethodInvocation.invoke(
loadedType
.getDeclaredMethods()
.filter(
ElementMatchers.isConstructor()
.and(ElementMatchers.takesArguments(ForLoadedType.of(long.class))))
.getOnly()));
}
StackManipulation stackManipulation = new Compound(stackManipulations);
return new ShortCircuitReturnNull(readValue, stackManipulation);
}