xuxiuxi
2017-08-01 e09e9f8a34cbc99a33dfa9ef1792b0025575c3a8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//020 8330 0577 bob
package com.awsle.aibatis.xml.engine.objecttree.reflection;
 
import com.awsle.aibatis.xml.engine.objecttree.ObjectAccessException;
import com.awsle.aibatis.xml.engine.objecttree.ObjectTree;
 
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.LinkedList;
import java.util.List;
 
public class ReflectionObjectGraph implements ObjectTree {
 
    private LinkedList fieldStack = new LinkedList();
    private LinkedList instanceStack = new LinkedList();
    private Class rootType;
    private ObjectFactory objectFactory;
 
    public ReflectionObjectGraph(Object root, ObjectFactory objectFactory) {
        this.objectFactory = objectFactory;
        init(root);
    }
 
    public ReflectionObjectGraph(Class rootType, ObjectFactory objectFactory) {
        this.rootType = rootType;
        this.objectFactory = objectFactory;
        init(null);
    }
 
    private static class RootHolder {
        Object value;
    }
 
    private void init(Object root) {
        RootHolder holder = new RootHolder();
        holder.value = root;
        instanceStack.addLast(holder);
        push("value");
    }
 
    public void push(String fieldName) {
        Object top = instanceStack.getLast();
 
        Field field = null;
        Class currentClass = top.getClass();
        try {
 
            while (field == null) {
                try {
                    field = currentClass.getDeclaredField(fieldName);
                } catch (NoSuchFieldException e) {
                    if (Object.class.equals(currentClass)) {
                        throw new ObjectAccessException("Cannot access field " + fieldName, e);
                    }
                    currentClass = currentClass.getSuperclass();
                }
            }
 
        } catch (SecurityException e) {
            throw new ObjectAccessException("Cannot access field " + fieldName, e);
        }
        field.setAccessible(true);
        fieldStack.addLast(field);
 
        try {
            instanceStack.addLast(field.get(top));
        } catch (IllegalArgumentException e) {
            throw new ObjectAccessException("Cannot access field " + fieldName, e);
        } catch (IllegalAccessException e) {
            throw new ObjectAccessException("Cannot access field " + fieldName, e);
        }
 
    }
 
    public void pop() {
        fieldStack.removeLast();
        instanceStack.removeLast();
    }
 
    public Class type() {
        if (fieldStack.size() == 1) {
            return rootType;
        } else {
            Field field = (Field) fieldStack.getLast();
            Class type = field.getType();
            return type;
        }
    }
 
    public Object get() {
        return instanceStack.getLast();
    }
 
    public void set(Object value) {
        try {
            instanceStack.removeLast();
            Field field = (Field) fieldStack.getLast();
            Object top = instanceStack.getLast();
            field.set(top, value);
            instanceStack.addLast(value);
        } catch (IllegalAccessException e) {
            throw new ObjectAccessException("Cannot set field", e);
        }
    }
 
    public void create(Class type) {
        set(objectFactory.create(type));
    }
 
    public String[] fieldNames() {
        List fieldNames = new LinkedList();
        Class theClass = get().getClass();
        Class currentClass = theClass;
 
        while (!Object.class.equals(currentClass)) {
            getFields(fieldNames, currentClass);
            currentClass = currentClass.getSuperclass();
        }
 
        String[] result = new String[fieldNames.size()];
        fieldNames.toArray(result);
        return result;
    }
 
    private void getFields(List fieldNames, Class theClass) {
        Field[] fields = theClass.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            int modifiers = field.getModifiers();
            if (field.getName().startsWith("this$")) {
                continue;
            }
            if (Modifier.isFinal(modifiers) ||
                    Modifier.isStatic(modifiers) ||
                    Modifier.isTransient(modifiers)) {
                continue;
            }
            fieldNames.add(field.getName());
        }
    }
 
    public ObjectTree newStack(Class type) {
        return new ReflectionObjectGraph(type, objectFactory);
    }
 
    public ObjectTree newStack(Object instance) {
        return new ReflectionObjectGraph(instance, objectFactory);
    }
 
}