View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2018 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
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   * Pojo to generate interaction methods.
32   *
33   * @since 1.0.0
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     * <p>Constructor for InteractionMethodPojo.</p>
44     *
45     * @param method method to generate delegate for
46     */
47    public InteractionMethodPojo(Method method) {
48      setMethod(method);
49    }
50  
51    /**
52     * <p>getBody.</p>
53     *
54     * @return method body for generated class
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     * <p>getHead.</p>
80     *
81     * @return method head for generated class
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     * <p>getHeadForInterface.</p>
92     *
93     * @return method head for generated interface
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    * <p>Getter for the field <code>method</code>.</p>
112    *
113    * @return a {@link java.lang.reflect.Method} object.
114    */
115   public Method getMethod() {
116     return method;
117   }
118 
119   /**
120    * <p>Setter for the field <code>method</code>.</p>
121    *
122    * @param method2 a {@link java.lang.reflect.Method} object.
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 }