File: MaterializingDeserializerFactory.java

package info (click to toggle)
libjackson-json-java 1.9.13-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 9,412 kB
  • sloc: java: 88,999; xml: 1,382; sh: 37; makefile: 26
file content (108 lines) | stat: -rw-r--r-- 3,580 bytes parent folder | download | duplicates (2)
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
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.codehaus.jackson.map.deser;

import java.lang.reflect.Method;

import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.DeserializerProvider;
import org.codehaus.jackson.map.JsonDeserializer;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.type.TypeFactory;
import org.codehaus.jackson.type.JavaType;

/**
 * A very basic Jackson deserializer factory that extends the BeanSerializer
 * factory to allow deserialization using interfaces.
 */
public class MaterializingDeserializerFactory extends BeanDeserializerFactory {
	public MaterializingDeserializerFactory() {
		super();
	}

	@Override
	public JsonDeserializer<Object> createBeanDeserializer(
			DeserializationConfig config, JavaType type, DeserializerProvider p)
			throws JsonMappingException {
		if (type.isInterface()) {
			String className = "$org.codehaus.jackson.generated$"
					+ type.getRawClass().getName();
			BeanHelper.BeanBuilder builder = new BeanHelper.BeanBuilder(
					className);
			builder.implement(type.getRawClass());

			return new BeanDeserializerProxyImpl(TypeFactory.fromClass(builder
					.load()));
		}

		return super.createBeanDeserializer(config, type, p);
	}

	protected static class BeanDeserializerProxyImpl extends BeanDeserializer {
		public BeanDeserializerProxyImpl(JavaType type) {
			super(type);

			try {
				this.setDefaultConstructor(type.getRawClass().getConstructor());
			} catch (Exception e) {
				throw new RuntimeException(e);
			}

			try {
				populateSetters(type);
			} catch (NoSuchMethodException e) {
				throw new RuntimeException(e);
			}
		}

		private void populateSetters(JavaType type)
				throws NoSuchMethodException {
			Class clazz = type.getRawClass();
			for (Method m : clazz.getMethods()) {
				String methodName = m.getName();
				if (methodName.startsWith("set")
						&& m.getParameterTypes() != null
						&& m.getParameterTypes().length == 1) {
					Class fieldClass = m.getParameterTypes()[0];
					JavaType fieldType = TypeFactory.fromClass(fieldClass);

					Method setterMethod = getSetterMethod(clazz, methodName,
							fieldClass);

					this.addProperty(new SettableBeanProperty.MethodProperty(
									getFieldName(methodName), fieldType,
									setterMethod));
				}
			}
		}

		private Method getSetterMethod(Class clazz, String methodName,
				Class returnType) throws NoSuchMethodException {
			return clazz.getMethod("set" + methodName.substring(3), returnType);
		}
	}

	private static String getFieldName(String getterMethodName) {
		char[] name = getterMethodName.substring(3).toCharArray();
		name[0] = Character.toLowerCase(name[0]);
		final String propName = new String(name);

		System.out.println(propName);
		return propName;
	}
}