summaryrefslogtreecommitdiff
path: root/java-plan2/src/org/msgpack/schema/SpecificClassSchema.java
blob: 75c474a79a6dfa6c348c50d788c815a5dfba145f (plain)
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
151
152
153
154
155
156
package org.msgpack.schema;

import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.util.Iterator;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.msgpack.*;

public class SpecificClassSchema extends ClassSchema {
	private List<SpecificFieldSchema> fields;
	private String namespace;
	private List<String> imports;

	private String fqdn;
	private Constructor constructorCache;

	public SpecificClassSchema(String name, List<SpecificFieldSchema> fields, String namespace, List<String> imports)
	{
		super(name, namespace, imports);
		this.fields = fields;
		if(namespace == null) {
			this.fqdn = name;
		} else {
			this.fqdn = namespace+"."+name;
		}
	}

	//@Override
	//public String getFullName()
	//{
	//	if(namespace == null) {
	//		return getName();
	//	} else {
	//		return namespace+"."+getName();
	//	}
	//}

	public List<? extends FieldSchema> getFields()
	{
		return fields;
	}

	@Override
	public String getExpression()
	{
		StringBuffer b = new StringBuffer();
		b.append("(class ");
		b.append(getName());
		if(namespace != null) {
			b.append(" (package "+namespace+")");
		}
		for(SpecificFieldSchema f : fields) {
			b.append(" "+f.getExpression());
		}
		b.append(")");
		return b.toString();
	}

	@Override
	public void pack(Packer pk, Object obj) throws IOException
	{
		if(obj == null) {
			pk.packNil();
			return;
		}

		if(constructorCache == null) {
			cacheConstructor();
		}

		pk.packArray(fields.size());
		for(SpecificFieldSchema f : fields) {
			f.getType().pack(pk, f.getFieldValue(obj));
		}
	}

	@Override
	@SuppressWarnings("unchecked")
	public Object convert(GenericObject obj)
	{
		if(constructorCache == null) {
			cacheConstructor();
		}

		List<GenericObject> d = obj.asArray();

		try {
			Object g = constructorCache.newInstance((Object[])null);

			Iterator<GenericObject> vi = d.iterator();
			Iterator<SpecificFieldSchema> fi = fields.iterator();
			while(fi.hasNext() && vi.hasNext()) {
				SpecificFieldSchema f = fi.next();
				GenericObject v = vi.next();
				f.setFieldValue(g, f.getType().convert(v));
			}
			// leave it as uninitialized
			//while(fi.hasNext()) {
			//	SpecificFieldSchema f = fi.next();
			//	g.put(f.getName(), null);
			//}

			return g;

		} catch (InvocationTargetException e) {
			throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage());
		} catch (InstantiationException e) {
			throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage());
		} catch (IllegalAccessException e) {
			throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage());
		}
	}

	private void cacheConstructor()
	{
		try {
			Class c = Class.forName(fqdn);
			int index = 0;
			for(SpecificFieldSchema f : fields) {
				f.cacheField(c, index++);
			}
			constructorCache = c.getDeclaredConstructor((Class[])null);
			constructorCache.setAccessible(true);
		} catch(ClassNotFoundException e) {
			throw new RuntimeException("class not found: "+fqdn);
		} catch (NoSuchMethodException e) {
			throw new RuntimeException("class not found: "+fqdn+": "+e.getMessage());
		}
	}

	@Override
	public Object newInstance()
	{
		if(constructorCache == null) {
			cacheConstructor();
		}
		try {
			return constructorCache.newInstance((Object[])null);
		} catch (InvocationTargetException e) {
			throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage());
		} catch (InstantiationException e) {
			throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage());
		} catch (IllegalAccessException e) {
			throw new RuntimeException("can't instantiate "+fqdn+": "+e.getMessage());
		}
	}

	public boolean equals(SpecificClassSchema o)
	{
		return (namespace != null ? namespace.equals(o.getNamespace()) : o.getNamespace() == null) &&
			getName().equals(o.getName());
	}
}