01: /*
02: * Copyright (C) 2007 XStream Committers.
03: * All rights reserved.
04: *
05: * The software in this package is published under the terms of the BSD
06: * style license a copy of which has been included with this distribution in
07: * the LICENSE.txt file.
08: *
09: * Created on 26. June 2007 by Joerg Schaible
10: */
11: package com.thoughtworks.xstream.benchmark.reflection.targets;
12:
13: import com.thoughtworks.xstream.tools.benchmark.Target;
14:
15: import org.apache.commons.lang.builder.EqualsBuilder;
16:
17: import java.lang.reflect.Field;
18: import java.util.Arrays;
19:
20: /**
21: * An abstract Target that fills the fields by reflection.
22: *
23: * @author Jörg Schaible
24: * @see com.thoughtworks.xstream.tools.benchmark.Harness
25: * @see Target
26: */
27: public abstract class AbstractReflectionTarget implements Target {
28:
29: private final Object target;
30:
31: public AbstractReflectionTarget(final Object target) {
32: this .target = target;
33: }
34:
35: public abstract String toString();
36:
37: protected void fill(final Object o) {
38: final char[] zero = new char[8];
39: Arrays.fill(zero, '0');
40: for (Class cls = o.getClass(); cls != Object.class; cls = cls
41: .getSuperclass()) {
42: final Field[] fields = cls.getDeclaredFields();
43: for (int i = 0; i < fields.length; i++) {
44: final Field field = fields[i];
45: field.setAccessible(true);
46: if (field.getType() == String.class) {
47: final String hex = Integer.toHexString(i);
48: try {
49: field.set(o, new String(zero, 0, zero.length
50: - hex.length())
51: + hex);
52: } catch (final IllegalAccessException e) {
53: throw new RuntimeException(e);
54: }
55: }
56: }
57: }
58: }
59:
60: public Object target() {
61: return target;
62: }
63:
64: public boolean isEqual(final Object other) {
65: return EqualsBuilder.reflectionEquals(target, other);
66: }
67: }
|