1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.qa.glnm.maven.freemarker.pojo;
21
22 import java.lang.reflect.Method;
23 import java.lang.reflect.Parameter;
24 import java.util.ArrayList;
25
26 import org.apache.commons.lang3.StringUtils;
27
28 import io.wcm.qa.glnm.maven.freemarker.util.ReflectionUtil;
29
30
31
32
33
34
35 public class InteractionMethodPojo {
36
37 private static final String CODE_PART_PUBLIC = "public ";
38 private static final String CODE_PART_VOID = "void";
39 private static final String CODE_PART_RETURN = "return ";
40 private Method method;
41
42
43
44
45
46
47 public InteractionMethodPojo(Method method) {
48 setMethod(method);
49 }
50
51
52
53
54
55
56 public String getBody() {
57 StringBuilder body = new StringBuilder();
58 if (!StringUtils.equals(CODE_PART_VOID, method.getReturnType().getCanonicalName())) {
59 body.append(CODE_PART_RETURN);
60 }
61 body.append(getSimpleClassName())
62 .append(".")
63 .append(getMethodName())
64 .append("(")
65 .append(getParametersForCall())
66 .append(");");
67 return body.toString();
68 }
69
70 private String getMethodName() {
71 return getMethod().getName();
72 }
73
74 private String getSimpleClassName() {
75 return getMethod().getDeclaringClass().getSimpleName();
76 }
77
78
79
80
81
82
83 public String getHead() {
84 StringBuilder head = new StringBuilder();
85 head.append(CODE_PART_PUBLIC);
86 head.append(getHeadForInterface());
87 return head.toString();
88 }
89
90
91
92
93
94
95 public String getHeadForInterface() {
96 StringBuilder head = new StringBuilder();
97 head.append(getMethodReturnType());
98 head.append(" ");
99 head.append(getMethodName());
100 head.append("(");
101 head.append(getParametersForDeclaration());
102 head.append(")");
103 return head.toString();
104 }
105
106 private String getMethodReturnType() {
107 return method.getGenericReturnType().getTypeName();
108 }
109
110
111
112
113
114
115 public Method getMethod() {
116 return method;
117 }
118
119
120
121
122
123
124 public void setMethod(Method method2) {
125 this.method = method2;
126 }
127
128 private String getParametersForCall() {
129 ArrayList<String> paramStrings = new ArrayList<String>();
130 for (Parameter parameter : method.getParameters()) {
131 if (ReflectionUtil.isSelector(parameter.getType())) {
132 paramStrings.add("this");
133 }
134 else {
135 paramStrings.add(parameter.getName());
136 }
137 }
138 return StringUtils.join(paramStrings, ", ");
139 }
140
141 private String getParametersForDeclaration() {
142 ArrayList<String> paramStrings = new ArrayList<String>();
143 for (Parameter parameter : method.getParameters()) {
144 if (ReflectionUtil.isSelector(parameter.getType())) {
145 continue;
146 }
147 paramStrings.add(getParameterString(parameter));
148 }
149 return StringUtils.join(paramStrings, ", ");
150 }
151
152 private String getParameterString(Parameter parameter) {
153 StringBuilder paramString = new StringBuilder();
154 paramString.append(parameter.getType().getCanonicalName());
155 paramString.append(" ");
156 paramString.append(parameter.getName());
157 return paramString.toString();
158 }
159 }