/
BindDynamicMethod.java
115 lines (101 loc) · 4.34 KB
/
BindDynamicMethod.java
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
/* Copyright 2004-2005 the original author or authors.
*
* Licensed 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.groovy.grails.web.metaclass;
import groovy.lang.MissingMethodException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import org.codehaus.groovy.grails.commons.metaclass.AbstractDynamicMethodInvocation;
import org.codehaus.groovy.grails.web.binding.DataBindingUtils;
/**
* A dynamic method present in controllers allowing data binding from a map to a target instance. Example:
*
* <code>
* def a = new Account()
* bindData( a, this.params )
* </code>
*
* @author Graeme Rocher
*/
public class BindDynamicMethod extends AbstractDynamicMethodInvocation {
public static final String METHOD_SIGNATURE = "bindData";
public static final Pattern METHOD_PATTERN = Pattern.compile('^'+METHOD_SIGNATURE+'$');
private static final String INCLUDE_MAP_KEY = "include";
private static final String EXCLUDE_MAP_KEY = "exclude";
public BindDynamicMethod() {
super(METHOD_PATTERN);
}
@Override
@SuppressWarnings("rawtypes")
public Object invoke(Object target, String methodName, Object[] arguments) {
if (arguments.length < 2 || arguments.length > 4) {
throw new MissingMethodException(METHOD_SIGNATURE, target.getClass(), arguments);
}
if (arguments[0] == null) {
throw new IllegalArgumentException("Argument [target] is required by method [bindData] on class [" +
target.getClass().getName() + "]");
}
Object targetObject = arguments[0];
Object bindParams = arguments[1];
Map includeExclude = new HashMap();
List include = null;
List exclude = null;
String filter = null;
switch(arguments.length) {
case 3:
if (arguments[2] instanceof String) {
filter = (String) arguments[2];
}
else if (!(arguments[2] instanceof Map)) {
throw new IllegalArgumentException("The 3rd Argument for method bindData must represent included and exlucded properties " +
"and implement the interface java.util.Map or be a String and represent a prefix to filter parameters with");
}
else {
includeExclude = (Map) arguments[2];
}
break;
case 4:
if (!(arguments[2] instanceof Map)) {
throw new IllegalArgumentException("The 3rd Argument for method bindData must represent included and exlucded properties " +
"and implement the interface java.util.Map or be a String and represent a prefix to filter parameters with");
}
includeExclude = (Map) arguments[2];
if (!(arguments[3] instanceof String)) {
throw new IllegalArgumentException("Argument [prefix] for method [bindData] must be a String");
}
filter = (String) arguments[3];
break;
}
if (includeExclude.containsKey(INCLUDE_MAP_KEY)) {
include = convertToListIfString(includeExclude.get(INCLUDE_MAP_KEY));
}
if (includeExclude.containsKey(EXCLUDE_MAP_KEY)) {
exclude = convertToListIfString(includeExclude.get(EXCLUDE_MAP_KEY));
}
DataBindingUtils.bindObjectToInstance(targetObject, bindParams, include, exclude, filter);
return targetObject;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private List convertToListIfString(Object o) {
if (o instanceof String) {
List list = new ArrayList();
list.add(o);
o = list;
}
return (List) o;
}
}